Skip to content

Possibility to configure the date format for the prefix #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion sql-migrate/command_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 24 additions & 7 deletions sql-migrate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -86,6 +100,9 @@ func GetEnvironment() (*Environment, error) {
migrate.SetSchema(env.SchemaName)
}

if env.TimeFormat == "" {
env.TimeFormat = config.GetTimeFormat()
}
return env, nil
}

Expand Down