storj/satellite/satellitedb/migrate_gen.go
Egon Elbre a2e20c93ae private/dbutil: use dbutil and tagsql from storj.io/private
Initially we duplicated the code to avoid large scale changes to
the packages. Now we are past metainfo refactor we can remove the
duplication.

Change-Id: I9d0b2756cc6e2a2f4d576afa408a15273a7e1cef
2021-04-23 14:36:52 +03:00

97 lines
1.9 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
//+build ignore
package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"storj.io/private/dbutil/dbschema"
)
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 {
return parseTestdataVersion(matches[i]) < parseTestdataVersion(matches[k])
})
lastScriptFile := matches[len(matches)-1]
version := parseTestdataVersion(lastScriptFile)
if version < 0 {
panic("invalid version " + lastScriptFile)
}
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)
}
}
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
}
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`" + `},
},
},
}
}
`