This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/db/migrations/gen_assets.go

73 lines
1.3 KiB
Go
Raw Normal View History

// +build ignore
package main
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"text/template"
)
var funcs = template.FuncMap{
"quote": strconv.Quote,
}
var tmpl = template.Must(template.New("assets.go").Delims("{%", "%}").Funcs(funcs).Parse(`package migrations
// THIS FILE WAS GENERATED BY gen.go DO NOT EDIT
import "github.com/rubenv/sql-migrate"
var PostgresMigrations migrate.MigrationSource = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{{% range $i, $m := . %}
{
Id: {% $m.Name | quote %},
Up: []string{
{% $m.Data | quote %},
},
},{% end %}
},
}
`))
// A single postgres migration.
type migration struct {
Name string
Data string
}
func init() {
log.SetPrefix("gen_assets: ")
log.SetFlags(0)
}
func main() {
files, err := filepath.Glob("*.sql")
if err != nil {
log.Fatalf("finding sql files: %v", err)
}
sort.Strings(files)
migrations := make([]migration, len(files))
for i, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
log.Fatalf("reading file: %v", err)
}
migrations[i] = migration{f, string(data)}
}
f, err := os.OpenFile("assets.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatalf("creating file: %v", err)
}
defer f.Close()
if err := tmpl.Execute(f, migrations); err != nil {
log.Fatal(err)
}
}