2021-11-21 14:05:07 +01:00
|
|
|
//go:build ignore
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This generates countries.go by running "go generate"
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-24 12:42:18 +01:00
|
|
|
"bytes"
|
2021-11-21 14:05:07 +01:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2022-10-15 21:54:11 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/migrate"
|
2021-11-21 14:05:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func gen_migrations(name string) {
|
|
|
|
if name == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dialect := strings.ToLower(name)
|
|
|
|
|
|
|
|
type Migration struct {
|
2021-11-24 12:42:18 +01:00
|
|
|
ID string
|
2022-10-15 21:54:11 +02:00
|
|
|
Stage string
|
2021-11-24 12:42:18 +01:00
|
|
|
Dialect string
|
|
|
|
Statements []string
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var migrations []Migration
|
|
|
|
|
|
|
|
// Folder in which migration files are stored.
|
|
|
|
folder := "./" + dialect
|
|
|
|
|
|
|
|
// Returns directory entries sorted by filename.
|
|
|
|
files, _ := os.ReadDir(folder)
|
|
|
|
|
|
|
|
fmt.Printf("generating %s...", dialect)
|
|
|
|
|
2021-11-24 12:42:18 +01:00
|
|
|
strToStmts := func(b []byte) (result []string) {
|
|
|
|
stmts := bytes.Split(b, []byte(";\n"))
|
|
|
|
result = make([]string, 0, len(stmts))
|
|
|
|
|
|
|
|
for i := range stmts {
|
|
|
|
if s := bytes.TrimSpace(stmts[i]); len(s) > 0 {
|
|
|
|
if s[len(s)-1] != ';' {
|
|
|
|
s = append(s, ';')
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, string(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
// Read migrations from files.
|
|
|
|
for _, file := range files {
|
2022-10-15 21:54:11 +02:00
|
|
|
stage := ""
|
2021-11-21 14:05:07 +01:00
|
|
|
filePath := filepath.Join(folder, file.Name())
|
2022-10-15 21:54:11 +02:00
|
|
|
fileName := strings.SplitN(filepath.Base(file.Name()), ".", 3)
|
2021-11-21 14:05:07 +01:00
|
|
|
|
|
|
|
if file.IsDir() {
|
2022-10-15 21:54:11 +02:00
|
|
|
// Skip directory.
|
2021-11-21 14:05:07 +01:00
|
|
|
continue
|
2022-10-15 21:54:11 +02:00
|
|
|
} else if len(fileName) < 2 || fileName[0] == "" {
|
|
|
|
// Invalid filename.
|
2021-11-21 14:05:07 +01:00
|
|
|
fmt.Printf("e")
|
2022-10-15 21:54:11 +02:00
|
|
|
continue
|
|
|
|
} else if fileName[1] != "sql" && fileName[2] != "sql" {
|
|
|
|
// Invalid filename.
|
|
|
|
fmt.Printf("e")
|
|
|
|
continue
|
|
|
|
} else if fileName[1] != "sql" {
|
|
|
|
// Stage, if any.
|
|
|
|
stage = fileName[1]
|
|
|
|
} else {
|
|
|
|
stage = migrate.StageMain
|
|
|
|
}
|
2021-11-24 12:42:18 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
// Migration ID.
|
|
|
|
id := fileName[0]
|
|
|
|
|
|
|
|
// Extract SQL from file.
|
|
|
|
if s, err := os.ReadFile(filePath); err == nil && len(s) > 0 {
|
|
|
|
fmt.Printf(".")
|
|
|
|
migrations = append(migrations, Migration{ID: id, Stage: stage, Dialect: dialect, Statements: strToStmts(s)})
|
2021-11-21 14:05:07 +01:00
|
|
|
} else {
|
|
|
|
fmt.Printf("f")
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(" found %d migrations\n", len(migrations))
|
|
|
|
|
|
|
|
// Create source file from migrations.
|
|
|
|
f, err := os.Create(fmt.Sprintf("dialect_%s.go", dialect))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
// Render source template.
|
|
|
|
migrationsTemplate.Execute(f, struct {
|
|
|
|
Name string
|
|
|
|
Migrations []Migration
|
|
|
|
}{
|
|
|
|
Name: name,
|
|
|
|
Migrations: migrations,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
gen_migrations("MySQL")
|
2021-12-09 07:37:49 +01:00
|
|
|
gen_migrations("SQLite3")
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:53:55 +02:00
|
|
|
var migrationsTemplate = template.Must(template.New("").Parse(`
|
2021-11-21 14:05:07 +01:00
|
|
|
package migrate
|
|
|
|
|
2022-04-25 09:53:55 +02:00
|
|
|
// Generated code, do not edit.
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
var Dialect{{ print .Name }} = Migrations{
|
|
|
|
{{- range .Migrations }}
|
|
|
|
{
|
2021-11-24 12:42:18 +01:00
|
|
|
ID: {{ printf "%q" .ID }},
|
|
|
|
Dialect: {{ printf "%q" .Dialect }},
|
2022-10-15 21:54:11 +02:00
|
|
|
Stage: {{ printf "%q" .Stage }},
|
2021-11-24 12:42:18 +01:00
|
|
|
Statements: []string{ {{ range $index, $s := .Statements}}{{if $index}},{{end}}{{ printf "%q" $s }}{{end}} },
|
2021-11-21 14:05:07 +01:00
|
|
|
},
|
|
|
|
{{- end }}
|
|
|
|
}`))
|