Commit 7c72b2dc authored by slene's avatar slene

orm fix syncdb

parent 4c061fed
...@@ -99,18 +99,23 @@ func (d *commandSyncDb) Run() { ...@@ -99,18 +99,23 @@ 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)
if d.verbose {
query = " " + strings.Join(strings.Split(query, "\n"), "\n ") queries := []string{sqls[i]}
fmt.Println(query) queries = append(queries, indexes[mi.table]...)
}
if err != nil { for _, query := range queries {
fmt.Printf(" %s\n", err.Error()) _, 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 { if d.verbose {
fmt.Println("") fmt.Println("")
...@@ -133,9 +138,15 @@ func (d *commandSqlAll) Parse(args []string) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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 { ...@@ -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: ...@@ -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) { ...@@ -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 {
_, err := db.Exec(query) for i, mi := range modelCache.allOrdered() {
throwFailNow(t, err, query) queries := []string{sqls[i]}
queries = append(queries, indexes[mi.table]...)
for _, query := range queries {
_, err := db.Exec(query)
throwFail(t, err, query)
}
} }
modelCache.clean() modelCache.clean()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment