All checks were successful
CI / Test (1.24) (push) Successful in -29m0s
CI / Test (1.25) (push) Successful in -29m10s
Integration Tests / Integration Tests (push) Successful in -29m6s
CI / Build (push) Successful in -30m1s
CI / Lint (push) Successful in -29m43s
Release / Build and Release (push) Successful in -29m56s
220 lines
5.6 KiB
Go
220 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// minimalColumn is used to build test JSON fixtures.
|
|
type minimalColumn struct {
|
|
Name string `json:"name"`
|
|
Table string `json:"table"`
|
|
Schema string `json:"schema"`
|
|
Type string `json:"type"`
|
|
NotNull bool `json:"not_null"`
|
|
IsPrimaryKey bool `json:"is_primary_key"`
|
|
AutoIncrement bool `json:"auto_increment"`
|
|
}
|
|
|
|
type minimalTable struct {
|
|
Name string `json:"name"`
|
|
Schema string `json:"schema"`
|
|
Columns map[string]minimalColumn `json:"columns"`
|
|
}
|
|
|
|
type minimalSchema struct {
|
|
Name string `json:"name"`
|
|
Tables []minimalTable `json:"tables"`
|
|
}
|
|
|
|
type minimalDatabase struct {
|
|
Name string `json:"name"`
|
|
Schemas []minimalSchema `json:"schemas"`
|
|
}
|
|
|
|
// writeTestJSON writes a minimal JSON database file with one schema ("public")
|
|
// containing tables with the given names. Each table has a single "id" PK column.
|
|
func writeTestJSON(t *testing.T, path string, tableNames []string) {
|
|
t.Helper()
|
|
|
|
tables := make([]minimalTable, len(tableNames))
|
|
for i, name := range tableNames {
|
|
tables[i] = minimalTable{
|
|
Name: name,
|
|
Schema: "public",
|
|
Columns: map[string]minimalColumn{
|
|
"id": {
|
|
Name: "id",
|
|
Table: name,
|
|
Schema: "public",
|
|
Type: "bigint",
|
|
NotNull: true,
|
|
IsPrimaryKey: true,
|
|
AutoIncrement: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
db := minimalDatabase{
|
|
Name: "test_db",
|
|
Schemas: []minimalSchema{{Name: "public", Tables: tables}},
|
|
}
|
|
|
|
data, err := json.Marshal(db)
|
|
if err != nil {
|
|
t.Fatalf("failed to marshal test JSON: %v", err)
|
|
}
|
|
if err := os.WriteFile(path, data, 0644); err != nil {
|
|
t.Fatalf("failed to write test file %s: %v", path, err)
|
|
}
|
|
}
|
|
|
|
// convertState captures and restores all convert global vars.
|
|
type convertState struct {
|
|
sourceType string
|
|
sourcePath string
|
|
sourceConn string
|
|
fromList []string
|
|
targetType string
|
|
targetPath string
|
|
packageName string
|
|
schemaFilter string
|
|
flattenSchema bool
|
|
}
|
|
|
|
func saveConvertState() convertState {
|
|
return convertState{
|
|
sourceType: convertSourceType,
|
|
sourcePath: convertSourcePath,
|
|
sourceConn: convertSourceConn,
|
|
fromList: convertFromList,
|
|
targetType: convertTargetType,
|
|
targetPath: convertTargetPath,
|
|
packageName: convertPackageName,
|
|
schemaFilter: convertSchemaFilter,
|
|
flattenSchema: convertFlattenSchema,
|
|
}
|
|
}
|
|
|
|
func restoreConvertState(s convertState) {
|
|
convertSourceType = s.sourceType
|
|
convertSourcePath = s.sourcePath
|
|
convertSourceConn = s.sourceConn
|
|
convertFromList = s.fromList
|
|
convertTargetType = s.targetType
|
|
convertTargetPath = s.targetPath
|
|
convertPackageName = s.packageName
|
|
convertSchemaFilter = s.schemaFilter
|
|
convertFlattenSchema = s.flattenSchema
|
|
}
|
|
|
|
// templState captures and restores all templ global vars.
|
|
type templState struct {
|
|
sourceType string
|
|
sourcePath string
|
|
sourceConn string
|
|
fromList []string
|
|
templatePath string
|
|
outputPath string
|
|
schemaFilter string
|
|
mode string
|
|
filenamePattern string
|
|
}
|
|
|
|
func saveTemplState() templState {
|
|
return templState{
|
|
sourceType: templSourceType,
|
|
sourcePath: templSourcePath,
|
|
sourceConn: templSourceConn,
|
|
fromList: templFromList,
|
|
templatePath: templTemplatePath,
|
|
outputPath: templOutputPath,
|
|
schemaFilter: templSchemaFilter,
|
|
mode: templMode,
|
|
filenamePattern: templFilenamePattern,
|
|
}
|
|
}
|
|
|
|
func restoreTemplState(s templState) {
|
|
templSourceType = s.sourceType
|
|
templSourcePath = s.sourcePath
|
|
templSourceConn = s.sourceConn
|
|
templFromList = s.fromList
|
|
templTemplatePath = s.templatePath
|
|
templOutputPath = s.outputPath
|
|
templSchemaFilter = s.schemaFilter
|
|
templMode = s.mode
|
|
templFilenamePattern = s.filenamePattern
|
|
}
|
|
|
|
// mergeState captures and restores all merge global vars.
|
|
type mergeState struct {
|
|
targetType string
|
|
targetPath string
|
|
targetConn string
|
|
sourceType string
|
|
sourcePath string
|
|
sourceConn string
|
|
fromList []string
|
|
outputType string
|
|
outputPath string
|
|
outputConn string
|
|
skipDomains bool
|
|
skipRelations bool
|
|
skipEnums bool
|
|
skipViews bool
|
|
skipSequences bool
|
|
skipTables string
|
|
verbose bool
|
|
reportPath string
|
|
flattenSchema bool
|
|
}
|
|
|
|
func saveMergeState() mergeState {
|
|
return mergeState{
|
|
targetType: mergeTargetType,
|
|
targetPath: mergeTargetPath,
|
|
targetConn: mergeTargetConn,
|
|
sourceType: mergeSourceType,
|
|
sourcePath: mergeSourcePath,
|
|
sourceConn: mergeSourceConn,
|
|
fromList: mergeFromList,
|
|
outputType: mergeOutputType,
|
|
outputPath: mergeOutputPath,
|
|
outputConn: mergeOutputConn,
|
|
skipDomains: mergeSkipDomains,
|
|
skipRelations: mergeSkipRelations,
|
|
skipEnums: mergeSkipEnums,
|
|
skipViews: mergeSkipViews,
|
|
skipSequences: mergeSkipSequences,
|
|
skipTables: mergeSkipTables,
|
|
verbose: mergeVerbose,
|
|
reportPath: mergeReportPath,
|
|
flattenSchema: mergeFlattenSchema,
|
|
}
|
|
}
|
|
|
|
func restoreMergeState(s mergeState) {
|
|
mergeTargetType = s.targetType
|
|
mergeTargetPath = s.targetPath
|
|
mergeTargetConn = s.targetConn
|
|
mergeSourceType = s.sourceType
|
|
mergeSourcePath = s.sourcePath
|
|
mergeSourceConn = s.sourceConn
|
|
mergeFromList = s.fromList
|
|
mergeOutputType = s.outputType
|
|
mergeOutputPath = s.outputPath
|
|
mergeOutputConn = s.outputConn
|
|
mergeSkipDomains = s.skipDomains
|
|
mergeSkipRelations = s.skipRelations
|
|
mergeSkipEnums = s.skipEnums
|
|
mergeSkipViews = s.skipViews
|
|
mergeSkipSequences = s.skipSequences
|
|
mergeSkipTables = s.skipTables
|
|
mergeVerbose = s.verbose
|
|
mergeReportPath = s.reportPath
|
|
mergeFlattenSchema = s.flattenSchema
|
|
}
|