Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
1f6c5599
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1f6c5599
编写于
8月 07, 2014
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
migration: version 1
上级
ea6982fc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
187 addition
and
0 deletion
+187
-0
migration/migration.go
migration/migration.go
+187
-0
未找到文件。
migration/migration.go
0 → 100644
浏览文件 @
1f6c5599
// Beego (http://beego.me/)
//
// @description beego is an open-source, high-performance web framework for the Go programming language.
//
// @link http://github.com/astaxie/beego for the canonical source repository
//
// @license http://github.com/astaxie/beego/blob/master/LICENSE
//
// @authors astaxie
package
migration
import
(
"errors"
"sort"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
// const the data format for the bee generate migration datatype
const
M_DATE_FORMAT
=
"20060102_150405"
// Migrationer is an interface for all Migration struct
type
Migrationer
interface
{
Up
()
Down
()
Exec
()
error
GetCreated
()
int64
}
var
migrationMap
map
[
string
]
Migrationer
func
init
()
{
migrationMap
=
make
(
map
[
string
]
Migrationer
)
}
// the basic type which will implement the basic type
type
Migration
struct
{
sqls
[]
string
Created
string
}
// implement in the Inheritance struct for upgrade
func
(
m
*
Migration
)
Up
()
{
}
// implement in the Inheritance struct for down
func
(
m
*
Migration
)
Down
()
{
}
// add sql want to execute
func
(
m
*
Migration
)
Sql
(
sql
string
)
{
m
.
sqls
=
append
(
m
.
sqls
,
sql
)
}
// execute the sql already add in the sql
func
(
m
*
Migration
)
Exec
()
error
{
o
:=
orm
.
NewOrm
()
for
_
,
s
:=
range
m
.
sqls
{
beego
.
Info
(
"exec sql:"
,
s
)
r
:=
o
.
Raw
(
s
)
_
,
err
:=
r
.
Exec
()
if
err
!=
nil
{
return
err
}
}
return
nil
}
// get the unixtime from the Created
func
(
m
*
Migration
)
GetCreated
()
int64
{
t
,
err
:=
time
.
Parse
(
M_DATE_FORMAT
,
m
.
Created
)
if
err
!=
nil
{
return
0
}
return
t
.
Unix
()
}
// register the Migration in the map
func
Register
(
name
string
,
m
Migrationer
)
error
{
if
_
,
ok
:=
migrationMap
[
name
];
ok
{
return
errors
.
New
(
"already exist name:"
+
name
)
}
migrationMap
[
name
]
=
m
return
nil
}
// upgrate the migration from lasttime
func
Upgrade
(
lasttime
int64
)
error
{
sm
:=
sortMap
(
migrationMap
)
i
:=
0
for
_
,
v
:=
range
sm
{
if
v
.
created
>
lasttime
{
beego
.
Info
(
"start upgrade"
,
v
.
name
)
v
.
m
.
Up
()
err
:=
v
.
m
.
Exec
()
if
err
!=
nil
{
return
err
}
beego
.
Info
(
"end upgrade:"
,
v
.
name
)
i
++
}
}
beego
.
Info
(
"total success upgrade:"
,
i
,
" migration"
)
return
nil
}
//rollback the migration by the name
func
Rollback
(
name
string
)
error
{
if
v
,
ok
:=
migrationMap
[
name
];
ok
{
beego
.
Info
(
"start rollback"
)
v
.
Down
()
err
:=
v
.
Exec
()
if
err
!=
nil
{
return
err
}
beego
.
Info
(
"end rollback"
)
return
nil
}
else
{
return
errors
.
New
(
"not exist the migrationMap name:"
+
name
)
}
}
// reset all migration
// run all migration's down function
func
Reset
()
error
{
i
:=
0
for
k
,
v
:=
range
migrationMap
{
beego
.
Info
(
"start reset:"
,
k
)
v
.
Down
()
err
:=
v
.
Exec
()
if
err
!=
nil
{
return
err
}
beego
.
Info
(
"end reset:"
,
k
)
}
beego
.
Info
(
"total success reset:"
,
i
,
" migration"
)
return
nil
}
// first Reset, then Upgrade
func
Refresh
()
error
{
err
:=
Reset
()
if
err
!=
nil
{
return
err
}
return
Upgrade
(
0
)
}
type
dataSlice
[]
data
type
data
struct
{
created
int64
name
string
m
Migrationer
}
// Len is part of sort.Interface.
func
(
d
dataSlice
)
Len
()
int
{
return
len
(
d
)
}
// Swap is part of sort.Interface.
func
(
d
dataSlice
)
Swap
(
i
,
j
int
)
{
d
[
i
],
d
[
j
]
=
d
[
j
],
d
[
i
]
}
// Less is part of sort.Interface. We use count as the value to sort by
func
(
d
dataSlice
)
Less
(
i
,
j
int
)
bool
{
return
d
[
i
]
.
created
<
d
[
j
]
.
created
}
func
sortMap
(
m
map
[
string
]
Migrationer
)
dataSlice
{
s
:=
make
(
dataSlice
,
0
,
len
(
m
))
for
k
,
v
:=
range
m
{
d
:=
data
{}
d
.
created
=
v
.
GetCreated
()
d
.
name
=
k
d
.
m
=
v
s
=
append
(
s
,
d
)
}
sort
.
Sort
(
s
)
return
s
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录