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
7c72b2dc
Commit
7c72b2dc
authored
Aug 25, 2013
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
orm fix syncdb
parent
4c061fed
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
30 deletions
+42
-30
cmd.go
orm/cmd.go
+23
-12
cmd_utils.go
orm/cmd_utils.go
+6
-4
models.go
orm/models.go
+2
-2
models_info_f.go
orm/models_info_f.go
+0
-7
orm_test.go
orm/orm_test.go
+11
-5
No files found.
orm/cmd.go
View file @
7c72b2dc
...
...
@@ -99,18 +99,23 @@ func (d *commandSyncDb) Run() {
}
}
tabl
es
:=
getDbCreateSql
(
d
.
al
)
sqls
,
index
es
:=
getDbCreateSql
(
d
.
al
)
for
i
,
mi
:=
range
modelCache
.
allOrdered
()
{
query
:=
tables
[
i
]
_
,
err
:=
db
.
Exec
(
query
)
fmt
.
Printf
(
"create table `%s`
\n
"
,
mi
.
table
)
if
d
.
verbose
{
query
=
" "
+
strings
.
Join
(
strings
.
Split
(
query
,
"
\n
"
),
"
\n
"
)
fmt
.
Println
(
query
)
}
if
err
!=
nil
{
fmt
.
Printf
(
" %s
\n
"
,
err
.
Error
())
queries
:=
[]
string
{
sqls
[
i
]}
queries
=
append
(
queries
,
indexes
[
mi
.
table
]
...
)
for
_
,
query
:=
range
queries
{
_
,
err
:=
db
.
Exec
(
query
)
if
d
.
verbose
{
query
=
" "
+
strings
.
Join
(
strings
.
Split
(
query
,
"
\n
"
),
"
\n
"
)
fmt
.
Println
(
query
)
}
if
err
!=
nil
{
fmt
.
Printf
(
" %s
\n
"
,
err
.
Error
())
}
}
if
d
.
verbose
{
fmt
.
Println
(
""
)
...
...
@@ -133,9 +138,15 @@ func (d *commandSqlAll) Parse(args []string) {
}
func
(
d
*
commandSqlAll
)
Run
()
{
sqls
:=
getDbCreateSql
(
d
.
al
)
sql
:=
strings
.
Join
(
sqls
,
"
\n\n
"
)
fmt
.
Println
(
sql
)
sqls
,
indexes
:=
getDbCreateSql
(
d
.
al
)
var
all
[]
string
for
i
,
mi
:=
range
modelCache
.
allOrdered
()
{
queries
:=
[]
string
{
sqls
[
i
]}
queries
=
append
(
queries
,
indexes
[
mi
.
table
]
...
)
sql
:=
strings
.
Join
(
queries
,
"
\n
"
)
all
=
append
(
all
,
sql
)
}
fmt
.
Println
(
strings
.
Join
(
all
,
"
\n\n
"
))
}
func
init
()
{
...
...
orm/cmd_utils.go
View file @
7c72b2dc
...
...
@@ -31,7 +31,7 @@ func getDbDropSql(al *alias) (sqls []string) {
return
sqls
}
func
getDbCreateSql
(
al
*
alias
)
(
sqls
[]
string
)
{
func
getDbCreateSql
(
al
*
alias
)
(
sqls
[]
string
,
tableIndexes
map
[
string
][]
string
)
{
if
len
(
modelCache
.
cache
)
==
0
{
fmt
.
Println
(
"no Model found, need register your model"
)
os
.
Exit
(
2
)
...
...
@@ -41,6 +41,8 @@ func getDbCreateSql(al *alias) (sqls []string) {
T
:=
al
.
DbBaser
.
DbTypes
()
sep
:=
fmt
.
Sprintf
(
"%s, %s"
,
Q
,
Q
)
tableIndexes
=
make
(
map
[
string
][]
string
)
for
_
,
mi
:=
range
modelCache
.
allOrdered
()
{
sql
:=
fmt
.
Sprintf
(
"-- %s
\n
"
,
strings
.
Repeat
(
"-"
,
50
))
sql
+=
fmt
.
Sprintf
(
"-- Table Structure for `%s`
\n
"
,
mi
.
fullName
)
...
...
@@ -125,7 +127,7 @@ func getDbCreateSql(al *alias) (sqls []string) {
}
if
fi
.
index
{
sqlIndexes
=
append
(
sqlIndexes
,
[]
string
{
column
})
sqlIndexes
=
append
(
sqlIndexes
,
[]
string
{
fi
.
column
})
}
}
...
...
@@ -179,10 +181,10 @@ func getDbCreateSql(al *alias) (sqls []string) {
name
:=
strings
.
Join
(
names
,
"_"
)
cols
:=
strings
.
Join
(
names
,
sep
)
sql
:=
fmt
.
Sprintf
(
"CREATE INDEX %s%s%s ON %s%s%s (%s%s%s);"
,
Q
,
name
,
Q
,
Q
,
mi
.
table
,
Q
,
Q
,
cols
,
Q
)
sqls
=
append
(
sqls
,
sql
)
tableIndexes
[
mi
.
table
]
=
append
(
tableIndexes
[
mi
.
table
]
,
sql
)
}
}
return
sqls
return
}
orm/models.go
View file @
7c72b2dc
...
...
@@ -58,8 +58,8 @@ func (mc *_modelCache) all() map[string]*modelInfo {
func
(
mc
*
_modelCache
)
allOrdered
()
[]
*
modelInfo
{
m
:=
make
([]
*
modelInfo
,
0
,
len
(
mc
.
orders
))
for
_
,
v
:=
range
mc
.
cache
{
m
=
append
(
m
,
v
)
for
_
,
table
:=
range
mc
.
orders
{
m
=
append
(
m
,
mc
.
cache
[
table
]
)
}
return
m
}
...
...
orm/models_info_f.go
View file @
7c72b2dc
...
...
@@ -344,13 +344,6 @@ checkType:
err
=
fmt
.
Errorf
(
"non-integer type cannot set auto"
)
goto
end
}
if
fi
.
pk
||
fi
.
index
||
fi
.
unique
{
if
fieldType
!=
TypeCharField
&&
fieldType
!=
RelOneToOne
{
err
=
fmt
.
Errorf
(
"cannot set pk/index/unique"
)
goto
end
}
}
}
if
fi
.
auto
||
fi
.
pk
{
...
...
orm/orm_test.go
View file @
7c72b2dc
...
...
@@ -206,13 +206,19 @@ func TestSyncDb(t *testing.T) {
drops
:=
getDbDropSql
(
al
)
for
_
,
query
:=
range
drops
{
_
,
err
:=
db
.
Exec
(
query
)
throwFail
Now
(
t
,
err
,
query
)
throwFail
(
t
,
err
,
query
)
}
tables
:=
getDbCreateSql
(
al
)
for
_
,
query
:=
range
tables
{
_
,
err
:=
db
.
Exec
(
query
)
throwFailNow
(
t
,
err
,
query
)
sqls
,
indexes
:=
getDbCreateSql
(
al
)
for
i
,
mi
:=
range
modelCache
.
allOrdered
()
{
queries
:=
[]
string
{
sqls
[
i
]}
queries
=
append
(
queries
,
indexes
[
mi
.
table
]
...
)
for
_
,
query
:=
range
queries
{
_
,
err
:=
db
.
Exec
(
query
)
throwFail
(
t
,
err
,
query
)
}
}
modelCache
.
clean
()
...
...
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