()
| 160 | } |
| 161 | |
| 162 | func actionMigrateCreateInitialMigration() func(context *cli.Context) { |
| 163 | return func(c *cli.Context) { |
| 164 | schemaFile := c.String("schema") |
| 165 | cascade := c.Bool("cascade") |
| 166 | manager := schema.GetManager() |
| 167 | configFile := c.String("config-file") |
| 168 | if configFile != "" { |
| 169 | config := util.GetConfig() |
| 170 | config.ReadConfig(configFile) |
| 171 | schemaFiles := config.GetStringList("schemas", nil) |
| 172 | if schemaFiles == nil { |
| 173 | log.Fatal("No schema specified in configuration") |
| 174 | return |
| 175 | } |
| 176 | if err := manager.LoadSchemasFromFiles(schemaFiles...); err != nil { |
| 177 | log.Fatal(err) |
| 178 | return |
| 179 | } |
| 180 | } |
| 181 | if schemaFile != "" { |
| 182 | manager.LoadSchemasFromFiles(schemaFile) |
| 183 | } |
| 184 | name := c.String("name") |
| 185 | now := time.Now() |
| 186 | version := fmt.Sprintf("%s_%s.sql", now.Format("20060102150405"), name) |
| 187 | path := filepath.Join(c.String("path"), version) |
| 188 | var sqlString = bytes.NewBuffer(make([]byte, 0, 100)) |
| 189 | fmt.Printf("Generating goose migration file to %s ...\n", path) |
| 190 | sqlDB := db_sql.NewDB(db_options.Default()) |
| 191 | schemas := manager.OrderedSchemas() |
| 192 | sqlString.WriteString("\n") |
| 193 | sqlString.WriteString("-- +goose Up\n") |
| 194 | sqlString.WriteString("-- SQL in section 'Up' is executed when this migration is applied\n") |
| 195 | for _, s := range schemas { |
| 196 | if s.IsAbstract() { |
| 197 | continue |
| 198 | } |
| 199 | if s.Metadata["type"] == "metaschema" { |
| 200 | continue |
| 201 | } |
| 202 | createSQL, indices := sqlDB.GenTableDef(s, cascade) |
| 203 | sqlString.WriteString(createSQL + "\n") |
| 204 | for _, indexSQL := range indices { |
| 205 | sqlString.WriteString(indexSQL + "\n") |
| 206 | } |
| 207 | } |
| 208 | sqlString.WriteString("\n") |
| 209 | sqlString.WriteString("-- +goose Down\n") |
| 210 | sqlString.WriteString("-- SQL section 'Down' is executed when this migration is rolled back\n") |
| 211 | for _, s := range schemas { |
| 212 | if s.IsAbstract() { |
| 213 | continue |
| 214 | } |
| 215 | if s.Metadata["type"] == "metaschema" { |
| 216 | continue |
| 217 | } |
| 218 | sqlString.WriteString(fmt.Sprintf("drop table %s;", s.GetDbTableName())) |
| 219 | sqlString.WriteString("\n\n") |
no test coverage detected