2021-02-22 16:55:06 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2022-03-21 14:48:03 +00:00
|
|
|
//go:build ignore
|
|
|
|
// +build ignore
|
2021-02-22 16:55:06 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-02-23 08:22:19 +00:00
|
|
|
"strings"
|
2021-02-22 16:55:06 +00:00
|
|
|
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/dbschema"
|
2021-02-22 16:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// find all postgres sql files
|
|
|
|
matches, err := filepath.Glob("testdata/postgres.*")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(matches, func(i, k int) bool {
|
2021-02-23 08:22:19 +00:00
|
|
|
return parseTestdataVersion(matches[i]) < parseTestdataVersion(matches[k])
|
2021-02-22 16:55:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
lastScriptFile := matches[len(matches)-1]
|
2021-02-23 08:22:19 +00:00
|
|
|
version := parseTestdataVersion(lastScriptFile)
|
|
|
|
if version < 0 {
|
|
|
|
panic("invalid version " + lastScriptFile)
|
2021-02-22 16:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scriptData, err := ioutil.ReadFile(lastScriptFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sections := dbschema.NewSections(string(scriptData))
|
|
|
|
data := sections.LookupSection(dbschema.Main)
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
fmt.Fprintf(&buffer, testMigrationFormat, version, data)
|
|
|
|
|
|
|
|
formatted, err := format.Source(buffer.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", formatted)
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile("migratez.go", formatted, 0755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:22:19 +00:00
|
|
|
func parseTestdataVersion(path string) int {
|
|
|
|
path = filepath.ToSlash(strings.ToLower(path))
|
|
|
|
path = strings.TrimPrefix(path, "testdata/postgres.v")
|
|
|
|
path = strings.TrimSuffix(path, ".sql")
|
|
|
|
|
|
|
|
v, err := strconv.Atoi(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "invalid testdata path %q\n", path)
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:55:06 +00:00
|
|
|
var testMigrationFormat = `// AUTOGENERATED BY migrategen.go
|
|
|
|
// DO NOT EDIT.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import "storj.io/storj/private/migrate"
|
|
|
|
|
|
|
|
// testMigration returns migration that can be used for testing.
|
|
|
|
func (db *satelliteDB) testMigration() *migrate.Migration {
|
|
|
|
return &migrate.Migration{
|
|
|
|
Table: "versions",
|
|
|
|
Steps: []*migrate.Step{
|
|
|
|
{
|
|
|
|
DB: &db.migrationDB,
|
|
|
|
Description: "Testing setup",
|
|
|
|
Version: %d,
|
|
|
|
Action: migrate.SQL{` + "`%s`" + `},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|