@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"text/template"
)
@ -20,7 +21,7 @@ func generate(dialect string) {
fmt . Printf ( "error opening dir with dialect %s: %v\n" , dialect , err )
os . Exit ( 1 )
}
name s, err := dir . Readdirnames ( 0 )
versio ns, err := dir . Readdirnames ( 0 )
if err != nil {
fmt . Printf ( "error reading dir names with dialect %s: %v\n" , dialect , err )
os . Exit ( 1 )
@ -28,34 +29,85 @@ func generate(dialect string) {
outBuf := bytes . NewBuffer ( nil )
valuesBuf := bytes . NewBuffer ( nil )
sort . Strings ( name s)
sort . Strings ( versio ns)
for _ , name := range names {
if ! strings . HasSuffix ( name , ".sql" ) {
continue
isDev := false
largestVer := 0
for _ , ver := range versions {
var verVal int
switch ver {
case "dev" :
verVal = largestVer + 1
default :
if verVal , err = strconv . Atoi ( ver ) ; err != nil {
fmt . Printf ( "error reading major schema version directory %q. Must be a number or 'dev'\n" , ver )
os . Exit ( 1 )
}
if verVal > largestVer {
largestVer = verVal
}
}
contents , err := ioutil . ReadFile ( fmt . Sprintf ( "%s/%s/%s" , baseDir , dialect , name ) )
dir , err := os . Open ( fmt . Sprintf ( "%s/%s/%s" , baseDir , dialect , ver ) )
if err != nil {
fmt . Printf ( "error opening file %s with dialect %s: %v" , name , dialect , err )
fmt . Printf ( "error opening dir with dialect %s: %v\n" , dialect , err )
os . Exit ( 1 )
}
if err := migrationsValueTemplate . Execute ( valuesBuf , struct {
Name string
Contents string
} {
Name : name ,
Contents : string ( contents ) ,
} ) ; err != nil {
fmt . Printf ( "error executing migrations value template for file %s: %s" , name , err )
names , err := dir . Readdirnames ( 0 )
if err != nil {
fmt . Printf ( "error reading dir names with dialect %s: %v\n" , dialect , err )
os . Exit ( 1 )
}
if ver == "dev" && len ( names ) > 0 {
isDev = true
}
sort . Strings ( names )
for _ , name := range names {
if ! strings . HasSuffix ( name , ".sql" ) {
continue
}
contents , err := ioutil . ReadFile ( fmt . Sprintf ( "%s/%s/%s/%s" , baseDir , dialect , ver , name ) )
if err != nil {
fmt . Printf ( "error opening file %s with dialect %s: %v" , name , dialect , err )
os . Exit ( 1 )
}
vName := name
nameParts := strings . SplitN ( name , "_" , 2 )
if len ( nameParts ) != 2 {
continue
}
nameVer , err := strconv . Atoi ( nameParts [ 0 ] )
if err != nil {
fmt . Printf ( "Unable to get file version from %q\n" , name )
continue
}
vName = fmt . Sprintf ( "%02d_%s" , ( verVal * 1000 ) + nameVer , nameParts [ 1 ] )
if err := migrationsValueTemplate . Execute ( valuesBuf , struct {
Name string
Contents string
} {
Name : vName ,
Contents : string ( contents ) ,
} ) ; err != nil {
fmt . Printf ( "error executing migrations value template for file %s/%s: %s" , ver , name , err )
os . Exit ( 1 )
}
}
}
if err := migrationsTemplate . Execute ( outBuf , struct {
Type string
Values string
Type string
Values string
DevMigration bool
} {
Type : dialect ,
Values : valuesBuf . String ( ) ,
Type : dialect ,
Values : valuesBuf . String ( ) ,
DevMigration : isDev ,
} ) ; err != nil {
fmt . Printf ( "error executing migrations value template for dialect %s: %s" , dialect , err )
os . Exit ( 1 )
@ -76,6 +128,11 @@ import (
"bytes"
)
// DevMigration is true if the database schema that would be applied by
// InitStore would be from files in the /dev directory which indicates it would
// not be safe to run in a non dev environment.
var DevMigration = { { . DevMigration } }
var { { . Type } } Migrations = map [ string ] * fakeFile {
"migrations" : {
name : "migrations" ,