mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 19:10:54 +00:00
orm fix syncdb
This commit is contained in:
parent
4c061feddf
commit
7c72b2dca7
23
orm/cmd.go
23
orm/cmd.go
@ -99,12 +99,16 @@ func (d *commandSyncDb) Run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := getDbCreateSql(d.al)
|
sqls, indexes := getDbCreateSql(d.al)
|
||||||
|
|
||||||
for i, mi := range modelCache.allOrdered() {
|
for i, mi := range modelCache.allOrdered() {
|
||||||
query := tables[i]
|
|
||||||
_, err := db.Exec(query)
|
|
||||||
fmt.Printf("create table `%s` \n", mi.table)
|
fmt.Printf("create table `%s` \n", mi.table)
|
||||||
|
|
||||||
|
queries := []string{sqls[i]}
|
||||||
|
queries = append(queries, indexes[mi.table]...)
|
||||||
|
|
||||||
|
for _, query := range queries {
|
||||||
|
_, err := db.Exec(query)
|
||||||
if d.verbose {
|
if d.verbose {
|
||||||
query = " " + strings.Join(strings.Split(query, "\n"), "\n ")
|
query = " " + strings.Join(strings.Split(query, "\n"), "\n ")
|
||||||
fmt.Println(query)
|
fmt.Println(query)
|
||||||
@ -112,6 +116,7 @@ func (d *commandSyncDb) Run() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf(" %s\n", err.Error())
|
fmt.Printf(" %s\n", err.Error())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if d.verbose {
|
if d.verbose {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
@ -133,9 +138,15 @@ func (d *commandSqlAll) Parse(args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *commandSqlAll) Run() {
|
func (d *commandSqlAll) Run() {
|
||||||
sqls := getDbCreateSql(d.al)
|
sqls, indexes := getDbCreateSql(d.al)
|
||||||
sql := strings.Join(sqls, "\n\n")
|
var all []string
|
||||||
fmt.Println(sql)
|
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() {
|
func init() {
|
||||||
|
@ -31,7 +31,7 @@ func getDbDropSql(al *alias) (sqls []string) {
|
|||||||
return sqls
|
return sqls
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDbCreateSql(al *alias) (sqls []string) {
|
func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]string) {
|
||||||
if len(modelCache.cache) == 0 {
|
if len(modelCache.cache) == 0 {
|
||||||
fmt.Println("no Model found, need register your model")
|
fmt.Println("no Model found, need register your model")
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
@ -41,6 +41,8 @@ func getDbCreateSql(al *alias) (sqls []string) {
|
|||||||
T := al.DbBaser.DbTypes()
|
T := al.DbBaser.DbTypes()
|
||||||
sep := fmt.Sprintf("%s, %s", Q, Q)
|
sep := fmt.Sprintf("%s, %s", Q, Q)
|
||||||
|
|
||||||
|
tableIndexes = make(map[string][]string)
|
||||||
|
|
||||||
for _, mi := range modelCache.allOrdered() {
|
for _, mi := range modelCache.allOrdered() {
|
||||||
sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
|
||||||
sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName)
|
sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName)
|
||||||
@ -125,7 +127,7 @@ func getDbCreateSql(al *alias) (sqls []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fi.index {
|
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, "_")
|
name := strings.Join(names, "_")
|
||||||
cols := strings.Join(names, sep)
|
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)
|
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
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,8 @@ func (mc *_modelCache) all() map[string]*modelInfo {
|
|||||||
|
|
||||||
func (mc *_modelCache) allOrdered() []*modelInfo {
|
func (mc *_modelCache) allOrdered() []*modelInfo {
|
||||||
m := make([]*modelInfo, 0, len(mc.orders))
|
m := make([]*modelInfo, 0, len(mc.orders))
|
||||||
for _, v := range mc.cache {
|
for _, table := range mc.orders {
|
||||||
m = append(m, v)
|
m = append(m, mc.cache[table])
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -344,13 +344,6 @@ checkType:
|
|||||||
err = fmt.Errorf("non-integer type cannot set auto")
|
err = fmt.Errorf("non-integer type cannot set auto")
|
||||||
goto end
|
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 {
|
if fi.auto || fi.pk {
|
||||||
|
@ -206,13 +206,19 @@ func TestSyncDb(t *testing.T) {
|
|||||||
drops := getDbDropSql(al)
|
drops := getDbDropSql(al)
|
||||||
for _, query := range drops {
|
for _, query := range drops {
|
||||||
_, err := db.Exec(query)
|
_, err := db.Exec(query)
|
||||||
throwFailNow(t, err, query)
|
throwFail(t, err, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
tables := getDbCreateSql(al)
|
sqls, indexes := getDbCreateSql(al)
|
||||||
for _, query := range tables {
|
|
||||||
|
for i, mi := range modelCache.allOrdered() {
|
||||||
|
queries := []string{sqls[i]}
|
||||||
|
queries = append(queries, indexes[mi.table]...)
|
||||||
|
|
||||||
|
for _, query := range queries {
|
||||||
_, err := db.Exec(query)
|
_, err := db.Exec(query)
|
||||||
throwFailNow(t, err, query)
|
throwFail(t, err, query)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
modelCache.clean()
|
modelCache.clean()
|
||||||
|
Loading…
Reference in New Issue
Block a user