File: new-migration

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (38 lines) | stat: -rwxr-xr-x 904 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
require 'erb'

PROGNAME = File.basename(__FILE__)
MIGRATIONS_DIR = 'internal/praefect/datastore/migrations'

def main(migration_name)
  unless /\A[a-z0-9_]+\z/ =~ migration_name
    abort "invalid migration name #{migration_name.inspect} (only a-z0-9_ are allowed)"
  end

  sequence_number = Time.now.utc.strftime('%Y%m%d%H%M%S')
 
  migration_file =  File.join(MIGRATIONS_DIR, "#{sequence_number}_#{migration_name}.go")
  warn "#{PROGNAME}: creating #{migration_file}"
  IO.write(migration_file, ERB.new(DATA.read).result(binding))
end

unless ARGV.count == 1
  abort "usage: #{PROGNAME} MIGRATION_NAME"
end

main(*ARGV)

__END__
package migrations

import migrate "github.com/rubenv/sql-migrate"

func init() {
	m := &migrate.Migration{
		Id:   "<%= sequence_number %>_<%= migration_name %>",
		Up:   []string{},
		Down: []string{},
	}

	allMigrations = append(allMigrations, m)
}