lint: convert snake to camel case
This commit is contained in:
@@ -57,7 +57,6 @@ linters:
|
|||||||
checks:
|
checks:
|
||||||
- all
|
- all
|
||||||
- -ST1000 # Re-enable this once we have docstrings
|
- -ST1000 # Re-enable this once we have docstrings
|
||||||
- -ST1003 # I like snake_case
|
|
||||||
- -ST1013 # HTTP status codes are shorter and more readable than names
|
- -ST1013 # HTTP status codes are shorter and more readable than names
|
||||||
wrapcheck:
|
wrapcheck:
|
||||||
report-internal-errors: false
|
report-internal-errors: false
|
||||||
|
|||||||
14
cmd/main.go
14
cmd/main.go
@@ -16,19 +16,19 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
root_cmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "gas",
|
Use: "gas",
|
||||||
Version: version.String(),
|
Version: version.String(),
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
}
|
}
|
||||||
// Print a bare version string ("v0.0.4"), rather than cobra's "gas version v0.0.4".
|
// Print a bare version string ("v0.0.4"), rather than cobra's "gas version v0.0.4".
|
||||||
root_cmd.SetVersionTemplate("{{.Version}}\n")
|
rootCmd.SetVersionTemplate("{{.Version}}\n")
|
||||||
root_cmd.AddCommand(sqlite_lint)
|
rootCmd.AddCommand(sqliteLint)
|
||||||
root_cmd.AddCommand(cmd_init)
|
rootCmd.AddCommand(cmdInit)
|
||||||
root_cmd.AddCommand(generate_model)
|
rootCmd.AddCommand(generateModel)
|
||||||
root_cmd.AddCommand(cmd_version)
|
rootCmd.AddCommand(cmdVersion)
|
||||||
if err := root_cmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Println(RED + err.Error() + RESET)
|
fmt.Println(RED + err.Error() + RESET)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
|
|
||||||
var ErrNoSuchTable = errors.New("no such table")
|
var ErrNoSuchTable = errors.New("no such table")
|
||||||
|
|
||||||
var generate_model = &cobra.Command{
|
var generateModel = &cobra.Command{
|
||||||
Use: "generate <model_name>",
|
Use: "generate <model_name>",
|
||||||
Short: "Generate a model type",
|
Short: "Generate a model type",
|
||||||
|
|
||||||
@@ -119,7 +119,7 @@ var generate_model = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
generate_model.Flags().String("schema", "pkg/db/schema.sql", "Path to SQL schema file")
|
generateModel.Flags().String("schema", "pkg/db/schema.sql", "Path to SQL schema file")
|
||||||
generate_model.Flags().String("modname", "mymodule", "Name of project's Go module (TODO: detect automatically)")
|
generateModel.Flags().String("modname", "mymodule", "Name of project's Go module (TODO: detect automatically)")
|
||||||
generate_model.Flags().Bool("test", false, "Generate test file instead of regular file")
|
generateModel.Flags().Bool("test", false, "Generate test file instead of regular file")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/must"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd_init = &cobra.Command{
|
var cmdInit = &cobra.Command{
|
||||||
Use: "init [path]",
|
Use: "init [path]",
|
||||||
Short: "Initialize a new project",
|
Short: "Initialize a new project",
|
||||||
Long: "Initialize a new Gas Stack project at the given path. If no path is given, defaults to current directory.",
|
Long: "Initialize a new Gas Stack project at the given path. If no path is given, defaults to current directory.",
|
||||||
@@ -31,7 +31,7 @@ var cmd_init = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get all the config values
|
// Get all the config values
|
||||||
get_val := func(prompt string, val *string) {
|
getVal := func(prompt string, val *string) {
|
||||||
fmt.Printf("%s (%q): ", prompt, *val)
|
fmt.Printf("%s (%q): ", prompt, *val)
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
@@ -40,31 +40,31 @@ var cmd_init = &cobra.Command{
|
|||||||
*val = input
|
*val = input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pkg_opts := codegen.PkgOpts{
|
pkgOpts := codegen.PkgOpts{
|
||||||
ModuleName: must.Get(cmd.Flags().GetString("module")),
|
ModuleName: must.Get(cmd.Flags().GetString("module")),
|
||||||
DBFilename: must.Get(cmd.Flags().GetString("db")),
|
DBFilename: must.Get(cmd.Flags().GetString("db")),
|
||||||
BinaryName: must.Get(cmd.Flags().GetString("binary")),
|
BinaryName: must.Get(cmd.Flags().GetString("binary")),
|
||||||
}
|
}
|
||||||
if pkg_opts.ModuleName == "" {
|
if pkgOpts.ModuleName == "" {
|
||||||
pkg_opts.ModuleName = filepath.Base(target)
|
pkgOpts.ModuleName = filepath.Base(target)
|
||||||
get_val("module name", &pkg_opts.ModuleName)
|
getVal("module name", &pkgOpts.ModuleName)
|
||||||
}
|
}
|
||||||
if pkg_opts.DBFilename == "" {
|
if pkgOpts.DBFilename == "" {
|
||||||
pkg_opts.DBFilename = pkg_opts.ModuleName + ".db"
|
pkgOpts.DBFilename = pkgOpts.ModuleName + ".db"
|
||||||
get_val("db name", &pkg_opts.DBFilename)
|
getVal("db name", &pkgOpts.DBFilename)
|
||||||
}
|
}
|
||||||
if pkg_opts.BinaryName == "" {
|
if pkgOpts.BinaryName == "" {
|
||||||
pkg_opts.BinaryName = pkg_opts.ModuleName
|
pkgOpts.BinaryName = pkgOpts.ModuleName
|
||||||
get_val("binary name", &pkg_opts.BinaryName)
|
getVal("binary name", &pkgOpts.BinaryName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run project initialization
|
// Run project initialization
|
||||||
codegen.InitPkg(pkg_opts)
|
codegen.InitPkg(pkgOpts)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmd_init.Flags().String("module", "", "Module name")
|
cmdInit.Flags().String("module", "", "Module name")
|
||||||
cmd_init.Flags().String("db", "", "Database filename")
|
cmdInit.Flags().String("db", "", "Database filename")
|
||||||
cmd_init.Flags().String("binary", "", "Binary name")
|
cmdInit.Flags().String("binary", "", "Binary name")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema/lint"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/schema/lint"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sqlite_lint = &cobra.Command{
|
var sqliteLint = &cobra.Command{
|
||||||
Use: "sqlite_lint <path/to/schema.sql>",
|
Use: "sqlite_lint <path/to/schema.sql>",
|
||||||
Short: "Lint a SQLite schema",
|
Short: "Lint a SQLite schema",
|
||||||
|
|
||||||
@@ -32,18 +32,18 @@ var sqlite_lint = &cobra.Command{
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
is_failure := false
|
isFailure := false
|
||||||
// Execute each check against the database
|
// Execute each check against the database
|
||||||
for _, check := range lint.Checks {
|
for _, check := range lint.Checks {
|
||||||
// Checks can be disabled via Github config / environment variables
|
// Checks can be disabled via Github config / environment variables
|
||||||
if !is_check_enabled(check) {
|
if !isCheckEnabled(check) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
results := check.Execute(schema)
|
results := check.Execute(schema)
|
||||||
|
|
||||||
// If there are results, print them as lint errors
|
// If there are results, print them as lint errors
|
||||||
if len(results) > 0 {
|
if len(results) > 0 {
|
||||||
is_failure = true
|
isFailure = true
|
||||||
fmt.Printf(RED+"Check '%s' failed:\n"+RESET, check.Name)
|
fmt.Printf(RED+"Check '%s' failed:\n"+RESET, check.Name)
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
fmt.Printf(RED+"- %s: %s.%s\n"+RESET, result.ErrorMsg, result.TableName, result.ColumnName)
|
fmt.Printf(RED+"- %s: %s.%s\n"+RESET, result.ErrorMsg, result.TableName, result.ColumnName)
|
||||||
@@ -51,7 +51,7 @@ var sqlite_lint = &cobra.Command{
|
|||||||
fmt.Printf(RED+"Explanation: %s\n\n"+RESET, check.Explanation)
|
fmt.Printf(RED+"Explanation: %s\n\n"+RESET, check.Explanation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if is_failure {
|
if isFailure {
|
||||||
fmt.Println(RED + "Errors found" + RESET)
|
fmt.Println(RED + "Errors found" + RESET)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -61,16 +61,16 @@ var sqlite_lint = &cobra.Command{
|
|||||||
|
|
||||||
// github_actions_input_env_var converts an input name to the corresponding
|
// github_actions_input_env_var converts an input name to the corresponding
|
||||||
// environment variable name used by GitHub Actions.
|
// environment variable name used by GitHub Actions.
|
||||||
func github_actions_input_env_var(name string) string {
|
func githubActionsInputEnvVar(name string) string {
|
||||||
// GitHub normalizes both hyphens and underscores to underscores, then uppercases the name
|
// GitHub normalizes both hyphens and underscores to underscores, then uppercases the name
|
||||||
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(name)
|
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(name)
|
||||||
return "INPUT_" + strings.ToUpper(normalized)
|
return "INPUT_" + strings.ToUpper(normalized)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting the environment variable INPUT_REQUIRE_NOT_NULL="false" disables the "require_not_null" check
|
// Setting the environment variable INPUT_REQUIRE_NOT_NULL="false" disables the "require_not_null" check
|
||||||
func is_check_enabled(c lint.Check) bool {
|
func isCheckEnabled(c lint.Check) bool {
|
||||||
val, is_set := os.LookupEnv(github_actions_input_env_var(c.Name))
|
val, isSet := os.LookupEnv(githubActionsInputEnvVar(c.Name))
|
||||||
if !is_set {
|
if !isSet {
|
||||||
// Enable all checks by default
|
// Enable all checks by default
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"git.offline-twitter.com/offline-labs/gas-stack/pkg/version"
|
"git.offline-twitter.com/offline-labs/gas-stack/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd_version = &cobra.Command{
|
var cmdVersion = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print the version of `gas`",
|
Short: "Print the version of `gas`",
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
|
|||||||
@@ -17,18 +17,18 @@ type DBConfig struct {
|
|||||||
// is_initialized bool
|
// is_initialized bool
|
||||||
|
|
||||||
// The SQL schema of the database under management
|
// The SQL schema of the database under management
|
||||||
sql_schema *string
|
sqlSchema *string
|
||||||
|
|
||||||
// Database starts at version 0. First migration brings us to version 1
|
// Database starts at version 0. First migration brings us to version 1
|
||||||
migrations *[]Migration
|
migrations *[]Migration
|
||||||
version_number uint
|
versionNumber uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init(schema *string, migrationsList *[]Migration) DBConfig {
|
func Init(schema *string, migrationsList *[]Migration) DBConfig {
|
||||||
return DBConfig{
|
return DBConfig{
|
||||||
sql_schema: schema,
|
sqlSchema: schema,
|
||||||
migrations: migrationsList,
|
migrations: migrationsList,
|
||||||
version_number: uint(len(*migrationsList)),
|
versionNumber: uint(len(*migrationsList)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ func (c DBConfig) Create(path string) (*sqlx.DB, error) {
|
|||||||
if _, err = db.Exec("pragma foreign_keys=on; pragma journal_mode=WAL;"); err != nil {
|
if _, err = db.Exec("pragma foreign_keys=on; pragma journal_mode=WAL;"); err != nil {
|
||||||
return nil, fmt.Errorf("running pragma statements: %w", err)
|
return nil, fmt.Errorf("running pragma statements: %w", err)
|
||||||
}
|
}
|
||||||
if _, err = db.Exec(*c.sql_schema); err != nil {
|
if _, err = db.Exec(*c.sqlSchema); err != nil {
|
||||||
return nil, fmt.Errorf("creating schema: %w", err)
|
return nil, fmt.Errorf("creating schema: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,18 +79,18 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
|||||||
return fmt.Errorf("couldn't check database version: %w", err)
|
return fmt.Errorf("couldn't check database version: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if version > c.version_number {
|
if version > c.versionNumber {
|
||||||
return VersionMismatchError{c.version_number, version}
|
return VersionMismatchError{c.versionNumber, version}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.version_number > version {
|
if c.versionNumber > version {
|
||||||
fmt.Print(textutils.ColorYellow)
|
fmt.Print(textutils.ColorYellow)
|
||||||
fmt.Printf("================================================\n")
|
fmt.Printf("================================================\n")
|
||||||
fmt.Printf("Database version is out of date. Upgrading database from version %d to version %d!\n", version,
|
fmt.Printf("Database version is out of date. Upgrading database from version %d to version %d!\n", version,
|
||||||
c.version_number)
|
c.versionNumber)
|
||||||
fmt.Print(textutils.ColorReset)
|
fmt.Print(textutils.ColorReset)
|
||||||
|
|
||||||
for i := version; i < c.version_number; i++ {
|
for i := version; i < c.versionNumber; i++ {
|
||||||
m := (*c.migrations)[i]
|
m := (*c.migrations)[i]
|
||||||
if err := m.Apply(db); err != nil {
|
if err := m.Apply(db); err != nil {
|
||||||
return fmt.Errorf("migration %d: %w", m.ID, err)
|
return fmt.Errorf("migration %d: %w", m.ID, err)
|
||||||
@@ -103,7 +103,7 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
|||||||
|
|
||||||
fmt.Print(textutils.ColorGreen)
|
fmt.Print(textutils.ColorGreen)
|
||||||
fmt.Printf("================================================\n")
|
fmt.Printf("================================================\n")
|
||||||
fmt.Printf("Database version has been upgraded to version %d.\n", c.version_number)
|
fmt.Printf("Database version has been upgraded to version %d.\n", c.versionNumber)
|
||||||
fmt.Print(textutils.ColorReset)
|
fmt.Print(textutils.ColorReset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,17 +15,17 @@ import (
|
|||||||
func TestCreateAndConnectToDB(t *testing.T) {
|
func TestCreateAndConnectToDB(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
_schema_sql, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
_schemaSQL, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
schema_sql := string(_schema_sql)
|
schemaSQL := string(_schemaSQL)
|
||||||
migrations := []db.Migration{}
|
migrations := []db.Migration{}
|
||||||
|
|
||||||
config := db.Init(&schema_sql, &migrations)
|
config := db.Init(&schemaSQL, &migrations)
|
||||||
|
|
||||||
data_dir := "../../sample_data/data"
|
dataDir := "../../sample_data/data"
|
||||||
_ = os.MkdirAll(data_dir, os.FileMode(0o644))
|
_ = os.MkdirAll(dataDir, os.FileMode(0o644))
|
||||||
// Create then connect to a new empty DB
|
// Create then connect to a new empty DB
|
||||||
dbPath := filepath.Join(data_dir, "test.db")
|
dbPath := filepath.Join(dataDir, "test.db")
|
||||||
_ = os.Remove(dbPath) // Delete it if it exists
|
_ = os.Remove(dbPath) // Delete it if it exists
|
||||||
|
|
||||||
_, err = config.Create(dbPath)
|
_, err = config.Create(dbPath)
|
||||||
@@ -38,18 +38,18 @@ func TestVersionUpgrade(t *testing.T) {
|
|||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
|
|
||||||
// Create a little schema
|
// Create a little schema
|
||||||
initial_schema := `
|
initialSchema := `
|
||||||
create table items(rowid integer primary key);
|
create table items(rowid integer primary key);
|
||||||
create table db_version (version integer primary key) strict, without rowid;
|
create table db_version (version integer primary key) strict, without rowid;
|
||||||
insert into db_version values(0);
|
insert into db_version values(0);
|
||||||
`
|
`
|
||||||
migrations := []db.Migration{}
|
migrations := []db.Migration{}
|
||||||
config := db.Init(&initial_schema, &migrations)
|
config := db.Init(&initialSchema, &migrations)
|
||||||
|
|
||||||
connection, err := config.Create(":memory:")
|
connection, err := config.Create(":memory:")
|
||||||
require.NoError(err)
|
require.NoError(err)
|
||||||
|
|
||||||
get_version := func(c *sqlx.DB) (ret int) {
|
getVersion := func(c *sqlx.DB) (ret int) {
|
||||||
// TODO: this should be a function exposed by the `db` package itself
|
// TODO: this should be a function exposed by the `db` package itself
|
||||||
require.NoError(c.Get(&ret, "select version from db_version"))
|
require.NoError(c.Get(&ret, "select version from db_version"))
|
||||||
return
|
return
|
||||||
@@ -58,21 +58,21 @@ func TestVersionUpgrade(t *testing.T) {
|
|||||||
var items []int
|
var items []int
|
||||||
require.NoError(connection.Select(&items, "select * from items"))
|
require.NoError(connection.Select(&items, "select * from items"))
|
||||||
require.Len(items, 0)
|
require.Len(items, 0)
|
||||||
require.Equal(0, get_version(connection))
|
require.Equal(0, getVersion(connection))
|
||||||
|
|
||||||
// Create a migration to add a new Item
|
// Create a migration to add a new Item
|
||||||
migrations = append(migrations, db.Migration{ID: 1, SQL: "insert into items (rowid) values (1)"})
|
migrations = append(migrations, db.Migration{ID: 1, SQL: "insert into items (rowid) values (1)"})
|
||||||
config = db.Init(&initial_schema, &migrations) // Reinitialize with the new migration
|
config = db.Init(&initialSchema, &migrations) // Reinitialize with the new migration
|
||||||
require.NoError(config.CheckAndUpdateVersion(connection))
|
require.NoError(config.CheckAndUpdateVersion(connection))
|
||||||
|
|
||||||
var items2 []int
|
var items2 []int
|
||||||
require.NoError(connection.Select(&items2, "select * from items"))
|
require.NoError(connection.Select(&items2, "select * from items"))
|
||||||
require.Len(items2, 1)
|
require.Len(items2, 1)
|
||||||
require.Equal(1, get_version(connection))
|
require.Equal(1, getVersion(connection))
|
||||||
|
|
||||||
// Create a migration to add a new Item
|
// Create a migration to add a new Item
|
||||||
migrations = append(migrations, db.Migration{ID: 2, SQL: `alter table items add column name string default 'asdf'`})
|
migrations = append(migrations, db.Migration{ID: 2, SQL: `alter table items add column name string default 'asdf'`})
|
||||||
config = db.Init(&initial_schema, &migrations) // Reinitialize with the new migration
|
config = db.Init(&initialSchema, &migrations) // Reinitialize with the new migration
|
||||||
require.NoError(config.CheckAndUpdateVersion(connection))
|
require.NoError(config.CheckAndUpdateVersion(connection))
|
||||||
|
|
||||||
var items3 []struct {
|
var items3 []struct {
|
||||||
@@ -82,5 +82,5 @@ func TestVersionUpgrade(t *testing.T) {
|
|||||||
require.NoError(connection.Select(&items3, "select * from items"))
|
require.NoError(connection.Select(&items3, "select * from items"))
|
||||||
require.Len(items2, 1)
|
require.Len(items2, 1)
|
||||||
assert.Equal(t, "asdf", items3[0].Name)
|
assert.Equal(t, "asdf", items3[0].Name)
|
||||||
require.Equal(2, get_version(connection))
|
require.Equal(2, getVersion(connection))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,8 +114,8 @@ var Checks = []Check{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if target column is a primary key
|
// Check if target column is a primary key
|
||||||
for _, target_col := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
for _, targetCol := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
||||||
if target_col.Name == column.ForeignKeyTargetColumn && target_col.IsPrimaryKey {
|
if targetCol.Name == column.ForeignKeyTargetColumn && targetCol.IsPrimaryKey {
|
||||||
continue fk_loop
|
continue fk_loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,8 +125,8 @@ var Checks = []Check{
|
|||||||
if idx.TableName != column.ForeignKeyTargetTable {
|
if idx.TableName != column.ForeignKeyTargetTable {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, idx_col_name := range idx.Columns {
|
for _, idxColName := range idx.Columns {
|
||||||
if column.ForeignKeyTargetColumn == idx_col_name {
|
if column.ForeignKeyTargetColumn == idxColName {
|
||||||
continue fk_loop
|
continue fk_loop
|
||||||
}
|
}
|
||||||
break // Only look at 1st column
|
break // Only look at 1st column
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFailureCases(t *testing.T) {
|
func TestFailureCases(t *testing.T) {
|
||||||
test_cases := []struct {
|
testCases := []struct {
|
||||||
sqlFile string
|
sqlFile string
|
||||||
expectedFailures []string
|
expectedFailures []string
|
||||||
}{
|
}{
|
||||||
@@ -34,24 +34,24 @@ func TestFailureCases(t *testing.T) {
|
|||||||
}},
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test_case := range test_cases {
|
for _, testCase := range testCases {
|
||||||
t.Run(test_case.sqlFile, func(t *testing.T) {
|
t.Run(testCase.sqlFile, func(t *testing.T) {
|
||||||
schema, err := schema.SchemaFromSQLFile(test_case.sqlFile)
|
schema, err := schema.SchemaFromSQLFile(testCase.sqlFile)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, check := range lint.Checks {
|
for _, check := range lint.Checks {
|
||||||
results := check.Execute(schema)
|
results := check.Execute(schema)
|
||||||
|
|
||||||
is_failure := len(results) > 0
|
isFailure := len(results) > 0
|
||||||
is_failure_expected := slices.Contains(test_case.expectedFailures, check.Name)
|
isFailureExpected := slices.Contains(testCase.expectedFailures, check.Name)
|
||||||
|
|
||||||
if is_failure != is_failure_expected {
|
if isFailure != isFailureExpected {
|
||||||
if is_failure_expected {
|
if isFailureExpected {
|
||||||
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, test_case.sqlFile)
|
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, testCase.sqlFile)
|
||||||
} else {
|
} else {
|
||||||
t.Errorf("Expected check '%s' to pass, but it failed: %s (%q.%q)",
|
t.Errorf("Expected check '%s' to pass, but it failed: %s (%q.%q)",
|
||||||
check.Name,
|
check.Name,
|
||||||
test_case.sqlFile,
|
testCase.sqlFile,
|
||||||
results[0].TableName, results[0].ColumnName,
|
results[0].TableName, results[0].ColumnName,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
//go:embed views.sql
|
//go:embed views.sql
|
||||||
var create_views string
|
var createViews string
|
||||||
|
|
||||||
// InitDB creates an in-memory DB from a given schema string.
|
// InitDB creates an in-memory DB from a given schema string.
|
||||||
func InitDB(sql_schema string) *sqlx.DB {
|
func InitDB(sqlSchema string) *sqlx.DB {
|
||||||
db := sqlx.MustOpen("sqlite3", ":memory:")
|
db := sqlx.MustOpen("sqlite3", ":memory:")
|
||||||
db.MustExec(sql_schema)
|
db.MustExec(sqlSchema)
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ func SchemaFromSQLFile(filepath string) (Schema, error) {
|
|||||||
// SchemaFromDB takes a DB connection, checks its schema metadata tables, and returns a Schema.
|
// SchemaFromDB takes a DB connection, checks its schema metadata tables, and returns a Schema.
|
||||||
func SchemaFromDB(db *sqlx.DB) Schema {
|
func SchemaFromDB(db *sqlx.DB) Schema {
|
||||||
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
|
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
|
||||||
db.MustExec(create_views)
|
db.MustExec(createViews)
|
||||||
|
|
||||||
var tables []Table
|
var tables []Table
|
||||||
must.Do(db.Select(&tables, `select name, table_type, is_strict, is_without_rowid from tables`))
|
must.Do(db.Select(&tables, `select name, table_type, is_strict, is_without_rowid from tables`))
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ import (
|
|||||||
|
|
||||||
func TestParseSchema(t *testing.T) {
|
func TestParseSchema(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
schema_sql, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
schemaSQL, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
db := schema.InitDB(string(schema_sql))
|
db := schema.InitDB(string(schemaSQL))
|
||||||
schema := schema.SchemaFromDB(db)
|
schema := schema.SchemaFromDB(db)
|
||||||
expected_tbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
expectedTbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
||||||
for _, tbl_name := range expected_tbls {
|
for _, tblName := range expectedTbls {
|
||||||
_, is_ok := schema.Tables[tbl_name]
|
_, isOk := schema.Tables[tblName]
|
||||||
assert.True(is_ok)
|
assert.True(isOk)
|
||||||
}
|
}
|
||||||
|
|
||||||
foods := schema.Tables["foods"]
|
foods := schema.Tables["foods"]
|
||||||
@@ -57,23 +57,23 @@ func TestParseSchema(t *testing.T) {
|
|||||||
// -------
|
// -------
|
||||||
|
|
||||||
// Unique index
|
// Unique index
|
||||||
units_name_idx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
unitsNameIdx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
||||||
require.True(t, isOk)
|
require.True(t, isOk)
|
||||||
assert.True(units_name_idx.IsUnique)
|
assert.True(unitsNameIdx.IsUnique)
|
||||||
assert.Equal("units", units_name_idx.TableName)
|
assert.Equal("units", unitsNameIdx.TableName)
|
||||||
assert.Equal([]string{"name"}, units_name_idx.Columns)
|
assert.Equal([]string{"name"}, unitsNameIdx.Columns)
|
||||||
|
|
||||||
// Non-unique, declared index
|
// Non-unique, declared index
|
||||||
foods_protein_idx, isOk := schema.Indexes["foods_protein"]
|
foodsProteinIdx, isOk := schema.Indexes["foods_protein"]
|
||||||
require.True(t, isOk)
|
require.True(t, isOk)
|
||||||
assert.False(foods_protein_idx.IsUnique)
|
assert.False(foodsProteinIdx.IsUnique)
|
||||||
assert.Equal("foods", foods_protein_idx.TableName)
|
assert.Equal("foods", foodsProteinIdx.TableName)
|
||||||
assert.Equal([]string{"protein"}, foods_protein_idx.Columns)
|
assert.Equal([]string{"protein"}, foodsProteinIdx.Columns)
|
||||||
|
|
||||||
// Multi-column index
|
// Multi-column index
|
||||||
ingr_seq_idx, isOk := schema.Indexes["sqlite_autoindex_ingredients_1"]
|
ingrSeqIdx, isOk := schema.Indexes["sqlite_autoindex_ingredients_1"]
|
||||||
require.True(t, isOk)
|
require.True(t, isOk)
|
||||||
assert.True(ingr_seq_idx.IsUnique)
|
assert.True(ingrSeqIdx.IsUnique)
|
||||||
assert.Equal("ingredients", ingr_seq_idx.TableName)
|
assert.Equal("ingredients", ingrSeqIdx.TableName)
|
||||||
assert.Equal([]string{"in_recipe_id", "list_order"}, ingr_seq_idx.Columns)
|
assert.Equal([]string{"in_recipe_id", "list_order"}, ingrSeqIdx.Columns)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user