7c72b2dc by slene

orm fix syncdb

1 parent 4c061fed
...@@ -99,18 +99,23 @@ func (d *commandSyncDb) Run() { ...@@ -99,18 +99,23 @@ func (d *commandSyncDb) Run() {
99 } 99 }
100 } 100 }
101 101
102 tables := getDbCreateSql(d.al) 102 sqls, indexes := getDbCreateSql(d.al)
103 103
104 for i, mi := range modelCache.allOrdered() { 104 for i, mi := range modelCache.allOrdered() {
105 query := tables[i]
106 _, err := db.Exec(query)
107 fmt.Printf("create table `%s` \n", mi.table) 105 fmt.Printf("create table `%s` \n", mi.table)
108 if d.verbose { 106
109 query = " " + strings.Join(strings.Split(query, "\n"), "\n ") 107 queries := []string{sqls[i]}
110 fmt.Println(query) 108 queries = append(queries, indexes[mi.table]...)
111 } 109
112 if err != nil { 110 for _, query := range queries {
113 fmt.Printf(" %s\n", err.Error()) 111 _, err := db.Exec(query)
112 if d.verbose {
113 query = " " + strings.Join(strings.Split(query, "\n"), "\n ")
114 fmt.Println(query)
115 }
116 if err != nil {
117 fmt.Printf(" %s\n", err.Error())
118 }
114 } 119 }
115 if d.verbose { 120 if d.verbose {
116 fmt.Println("") 121 fmt.Println("")
...@@ -133,9 +138,15 @@ func (d *commandSqlAll) Parse(args []string) { ...@@ -133,9 +138,15 @@ func (d *commandSqlAll) Parse(args []string) {
133 } 138 }
134 139
135 func (d *commandSqlAll) Run() { 140 func (d *commandSqlAll) Run() {
136 sqls := getDbCreateSql(d.al) 141 sqls, indexes := getDbCreateSql(d.al)
137 sql := strings.Join(sqls, "\n\n") 142 var all []string
138 fmt.Println(sql) 143 for i, mi := range modelCache.allOrdered() {
144 queries := []string{sqls[i]}
145 queries = append(queries, indexes[mi.table]...)
146 sql := strings.Join(queries, "\n")
147 all = append(all, sql)
148 }
149 fmt.Println(strings.Join(all, "\n\n"))
139 } 150 }
140 151
141 func init() { 152 func init() {
......
...@@ -31,7 +31,7 @@ func getDbDropSql(al *alias) (sqls []string) { ...@@ -31,7 +31,7 @@ func getDbDropSql(al *alias) (sqls []string) {
31 return sqls 31 return sqls
32 } 32 }
33 33
34 func getDbCreateSql(al *alias) (sqls []string) { 34 func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]string) {
35 if len(modelCache.cache) == 0 { 35 if len(modelCache.cache) == 0 {
36 fmt.Println("no Model found, need register your model") 36 fmt.Println("no Model found, need register your model")
37 os.Exit(2) 37 os.Exit(2)
...@@ -41,6 +41,8 @@ func getDbCreateSql(al *alias) (sqls []string) { ...@@ -41,6 +41,8 @@ func getDbCreateSql(al *alias) (sqls []string) {
41 T := al.DbBaser.DbTypes() 41 T := al.DbBaser.DbTypes()
42 sep := fmt.Sprintf("%s, %s", Q, Q) 42 sep := fmt.Sprintf("%s, %s", Q, Q)
43 43
44 tableIndexes = make(map[string][]string)
45
44 for _, mi := range modelCache.allOrdered() { 46 for _, mi := range modelCache.allOrdered() {
45 sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50)) 47 sql := fmt.Sprintf("-- %s\n", strings.Repeat("-", 50))
46 sql += fmt.Sprintf("-- Table Structure for `%s`\n", mi.fullName) 48 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) {
125 } 127 }
126 128
127 if fi.index { 129 if fi.index {
128 sqlIndexes = append(sqlIndexes, []string{column}) 130 sqlIndexes = append(sqlIndexes, []string{fi.column})
129 } 131 }
130 } 132 }
131 133
...@@ -179,10 +181,10 @@ func getDbCreateSql(al *alias) (sqls []string) { ...@@ -179,10 +181,10 @@ func getDbCreateSql(al *alias) (sqls []string) {
179 name := strings.Join(names, "_") 181 name := strings.Join(names, "_")
180 cols := strings.Join(names, sep) 182 cols := strings.Join(names, sep)
181 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) 183 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)
182 sqls = append(sqls, sql) 184 tableIndexes[mi.table] = append(tableIndexes[mi.table], sql)
183 } 185 }
184 186
185 } 187 }
186 188
187 return sqls 189 return
188 } 190 }
......
...@@ -58,8 +58,8 @@ func (mc *_modelCache) all() map[string]*modelInfo { ...@@ -58,8 +58,8 @@ func (mc *_modelCache) all() map[string]*modelInfo {
58 58
59 func (mc *_modelCache) allOrdered() []*modelInfo { 59 func (mc *_modelCache) allOrdered() []*modelInfo {
60 m := make([]*modelInfo, 0, len(mc.orders)) 60 m := make([]*modelInfo, 0, len(mc.orders))
61 for _, v := range mc.cache { 61 for _, table := range mc.orders {
62 m = append(m, v) 62 m = append(m, mc.cache[table])
63 } 63 }
64 return m 64 return m
65 } 65 }
......
...@@ -344,13 +344,6 @@ checkType: ...@@ -344,13 +344,6 @@ checkType:
344 err = fmt.Errorf("non-integer type cannot set auto") 344 err = fmt.Errorf("non-integer type cannot set auto")
345 goto end 345 goto end
346 } 346 }
347
348 if fi.pk || fi.index || fi.unique {
349 if fieldType != TypeCharField && fieldType != RelOneToOne {
350 err = fmt.Errorf("cannot set pk/index/unique")
351 goto end
352 }
353 }
354 } 347 }
355 348
356 if fi.auto || fi.pk { 349 if fi.auto || fi.pk {
......
...@@ -206,13 +206,19 @@ func TestSyncDb(t *testing.T) { ...@@ -206,13 +206,19 @@ func TestSyncDb(t *testing.T) {
206 drops := getDbDropSql(al) 206 drops := getDbDropSql(al)
207 for _, query := range drops { 207 for _, query := range drops {
208 _, err := db.Exec(query) 208 _, err := db.Exec(query)
209 throwFailNow(t, err, query) 209 throwFail(t, err, query)
210 } 210 }
211 211
212 tables := getDbCreateSql(al) 212 sqls, indexes := getDbCreateSql(al)
213 for _, query := range tables { 213
214 _, err := db.Exec(query) 214 for i, mi := range modelCache.allOrdered() {
215 throwFailNow(t, err, query) 215 queries := []string{sqls[i]}
216 queries = append(queries, indexes[mi.table]...)
217
218 for _, query := range queries {
219 _, err := db.Exec(query)
220 throwFail(t, err, query)
221 }
216 } 222 }
217 223
218 modelCache.clean() 224 modelCache.clean()
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!