func NewMigration(cmd *cobra.Command, args []string) { if len(args) != 1 { cmd.Help() os.Exit(1) } name := args[0] migrationsPath := cliOptions.migrationsPath migrations, err := migrate.FindMigrations(migrationsPath) if err != nil { fmt.Fprintf(os.Stderr, "Error loading migrations:\n %v\n", err) os.Exit(1) } newMigrationName := fmt.Sprintf("%03d_%s.sql", len(migrations)+1, name) // Write new migration mPath := filepath.Join(migrationsPath, newMigrationName) mFile, err := os.OpenFile(mPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } defer mFile.Close() _, err = mFile.WriteString(newMigrationText) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }
func (s *MigrateSuite) TestFindMigrationsWithDuplicate(c *C) { _, err := migrate.FindMigrations("testdata/duplicate") c.Assert(err, ErrorMatches, "Duplicate migration 2") }
func (s *MigrateSuite) TestFindMigrationsWithGaps(c *C) { _, err := migrate.FindMigrations("testdata/gap") c.Assert(err, ErrorMatches, "Missing migration 2") }