diff --git a/README.md b/README.md index 0aff0c61..5d8df241 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,37 @@ $ sql-migrate status | 2_record.sql | no | +---------------+-----------------------------------------+ ``` +#### Migration Prefix + +The `new` command will prefix the generated `.sql` files. This prefix is created using the +the [Format](https://golang.org/pkg/time/#Time.Format) function of the golang `time` package. + +You can change the format globally or per environment using the `migration-prefix-timeformat`. + +If an environment does not define a format, he will the the global one. +If no format is defined globally, the default one is used. +The default format is `20060201150405`. + +```yml +# If specified, it will override +# the global format. +migration-prefix-timeformat: "20060201150405" + +# Development will use its +# own format. +development: + dialect: sqlite3 + datasource: test.db + dir: migrations + migration-prefix-timeformat: "2006" + +# production will use the global format. +production: + dialect: postgres + datasource: dbname=myapp sslmode=disable + dir: migrations/postgres + table: migrations +``` ### As a library Import sql-migrate into your application: diff --git a/sql-migrate/command_new.go b/sql-migrate/command_new.go index 97972f43..87ffc4aa 100644 --- a/sql-migrate/command_new.go +++ b/sql-migrate/command_new.go @@ -76,7 +76,7 @@ func CreateMigration(name string) error { return err } - fileName := fmt.Sprintf("%s-%s.sql", time.Now().Format("20060201150405"), strings.TrimSpace(name)) + fileName := fmt.Sprintf("%s-%s.sql", time.Now().Format(env.TimeFormat), strings.TrimSpace(name)) f, err := os.Create(path.Join(env.Dir, fileName)) if err != nil { diff --git a/sql-migrate/config.go b/sql-migrate/config.go index 67906065..9516391a 100644 --- a/sql-migrate/config.go +++ b/sql-migrate/config.go @@ -25,30 +25,45 @@ var dialects = map[string]gorp.Dialect{ var ConfigFile string var ConfigEnvironment string +var TimeFormatKey = "migration-prefix-timeformat" +var DefaultTimeFormat = "20060201150405" func ConfigFlags(f *flag.FlagSet) { f.StringVar(&ConfigFile, "config", "dbconfig.yml", "Configuration file to use.") f.StringVar(&ConfigEnvironment, "env", "development", "Environment to use.") } +type ConfigurationFile struct { + TimeFormat string `yaml:"migration-prefix-timeformat,omitempty"` + Envs map[string]*Environment `yaml:",inline"` +} + +func (c ConfigurationFile) GetTimeFormat() string { + if c.TimeFormat != "" { + return c.TimeFormat + } + return DefaultTimeFormat +} + type Environment struct { Dialect string `yaml:"dialect"` DataSource string `yaml:"datasource"` Dir string `yaml:"dir"` TableName string `yaml:"table"` SchemaName string `yaml:"schema"` + TimeFormat string `yaml:"migration-prefix-timeformat,omitempty"` } -func ReadConfig() (map[string]*Environment, error) { +func ReadConfig() (ConfigurationFile, error) { file, err := ioutil.ReadFile(ConfigFile) if err != nil { - return nil, err + return ConfigurationFile{}, err } - config := make(map[string]*Environment) - err = yaml.Unmarshal(file, config) + config := ConfigurationFile{} + err = yaml.Unmarshal(file, &config) if err != nil { - return nil, err + return ConfigurationFile{}, err } return config, nil @@ -59,8 +74,7 @@ func GetEnvironment() (*Environment, error) { if err != nil { return nil, err } - - env := config[ConfigEnvironment] + env := config.Envs[ConfigEnvironment] if env == nil { return nil, errors.New("No environment: " + ConfigEnvironment) } @@ -86,6 +100,9 @@ func GetEnvironment() (*Environment, error) { migrate.SetSchema(env.SchemaName) } + if env.TimeFormat == "" { + env.TimeFormat = config.GetTimeFormat() + } return env, nil }