MCPcopy Create free account
hub / github.com/cloudwan/gohan / actionMigrateCreateInitialMigration

Function actionMigrateCreateInitialMigration

cli/migrate.go:162–226  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

160}
161
162func 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")

Callers 1

Calls 12

GenTableDefMethod · 0.95
ReadConfigMethod · 0.80
GetStringListMethod · 0.80
FatalMethod · 0.80
LoadSchemasFromFilesMethod · 0.80
FormatMethod · 0.80
JoinMethod · 0.80
OrderedSchemasMethod · 0.80
IsAbstractMethod · 0.80
GetDbTableNameMethod · 0.80
DefaultMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected