Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
b97279a7
Commit
b97279a7
authored
Aug 12, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update the migration
parent
6a78898b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
6 deletions
+28
-6
migration.go
migration/migration.go
+28
-6
No files found.
migration/migration.go
View file @
b97279a7
...
@@ -12,6 +12,7 @@ package migration
...
@@ -12,6 +12,7 @@ package migration
import
(
import
(
"errors"
"errors"
"sort"
"sort"
"strings"
"time"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego"
...
@@ -25,7 +26,7 @@ const M_DATE_FORMAT = "20060102_150405"
...
@@ -25,7 +26,7 @@ const M_DATE_FORMAT = "20060102_150405"
type
Migrationer
interface
{
type
Migrationer
interface
{
Up
()
Up
()
Down
()
Down
()
Exec
()
error
Exec
(
name
,
status
string
)
error
GetCreated
()
int64
GetCreated
()
int64
}
}
...
@@ -57,7 +58,7 @@ func (m *Migration) Sql(sql string) {
...
@@ -57,7 +58,7 @@ func (m *Migration) Sql(sql string) {
}
}
// execute the sql already add in the sql
// execute the sql already add in the sql
func
(
m
*
Migration
)
Exec
()
error
{
func
(
m
*
Migration
)
Exec
(
name
,
status
string
)
error
{
o
:=
orm
.
NewOrm
()
o
:=
orm
.
NewOrm
()
for
_
,
s
:=
range
m
.
sqls
{
for
_
,
s
:=
range
m
.
sqls
{
beego
.
Info
(
"exec sql:"
,
s
)
beego
.
Info
(
"exec sql:"
,
s
)
...
@@ -67,7 +68,28 @@ func (m *Migration) Exec() error {
...
@@ -67,7 +68,28 @@ func (m *Migration) Exec() error {
return
err
return
err
}
}
}
}
return
nil
return
m
.
addOrUpdateRecord
(
name
,
status
)
}
func
(
m
*
Migration
)
addOrUpdateRecord
(
status
,
name
string
)
error
{
o
:=
orm
.
NewOrm
()
if
status
==
"down"
{
status
=
"rollback"
p
,
err
:=
o
.
Raw
(
"update migrations set status = ?,rollback_statements = ? where name = ?"
)
.
Prepare
()
if
err
!=
nil
{
return
nil
}
_
,
err
=
p
.
Exec
(
status
,
strings
.
Join
(
m
.
sqls
,
"; "
),
name
)
return
err
}
else
{
status
=
"update"
p
,
err
:=
o
.
Raw
(
"insert into migrations(`name`,`created_at`,`statements`,`status`) values(?,?,?,?)"
)
.
Prepare
()
if
err
!=
nil
{
return
err
}
_
,
err
=
p
.
Exec
(
name
,
m
.
GetCreated
(),
strings
.
Join
(
m
.
sqls
,
"; "
),
status
)
return
err
}
}
}
// get the unixtime from the Created
// get the unixtime from the Created
...
@@ -96,7 +118,7 @@ func Upgrade(lasttime int64) error {
...
@@ -96,7 +118,7 @@ func Upgrade(lasttime int64) error {
if
v
.
created
>
lasttime
{
if
v
.
created
>
lasttime
{
beego
.
Info
(
"start upgrade"
,
v
.
name
)
beego
.
Info
(
"start upgrade"
,
v
.
name
)
v
.
m
.
Up
()
v
.
m
.
Up
()
err
:=
v
.
m
.
Exec
()
err
:=
v
.
m
.
Exec
(
v
.
name
,
"up"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -113,7 +135,7 @@ func Rollback(name string) error {
...
@@ -113,7 +135,7 @@ func Rollback(name string) error {
if
v
,
ok
:=
migrationMap
[
name
];
ok
{
if
v
,
ok
:=
migrationMap
[
name
];
ok
{
beego
.
Info
(
"start rollback"
)
beego
.
Info
(
"start rollback"
)
v
.
Down
()
v
.
Down
()
err
:=
v
.
Exec
()
err
:=
v
.
Exec
(
name
,
"down"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -131,7 +153,7 @@ func Reset() error {
...
@@ -131,7 +153,7 @@ func Reset() error {
for
k
,
v
:=
range
migrationMap
{
for
k
,
v
:=
range
migrationMap
{
beego
.
Info
(
"start reset:"
,
k
)
beego
.
Info
(
"start reset:"
,
k
)
v
.
Down
()
v
.
Down
()
err
:=
v
.
Exec
()
err
:=
v
.
Exec
(
k
,
"down"
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment