lint: convert snake to camel case
This commit is contained in:
@@ -17,18 +17,18 @@ type DBConfig struct {
|
||||
// is_initialized bool
|
||||
|
||||
// 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
|
||||
migrations *[]Migration
|
||||
version_number uint
|
||||
migrations *[]Migration
|
||||
versionNumber uint
|
||||
}
|
||||
|
||||
func Init(schema *string, migrationsList *[]Migration) DBConfig {
|
||||
return DBConfig{
|
||||
sql_schema: schema,
|
||||
migrations: migrationsList,
|
||||
version_number: uint(len(*migrationsList)),
|
||||
sqlSchema: schema,
|
||||
migrations: 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 {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -79,18 +79,18 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
||||
return fmt.Errorf("couldn't check database version: %w", err)
|
||||
}
|
||||
|
||||
if version > c.version_number {
|
||||
return VersionMismatchError{c.version_number, version}
|
||||
if version > c.versionNumber {
|
||||
return VersionMismatchError{c.versionNumber, version}
|
||||
}
|
||||
|
||||
if c.version_number > version {
|
||||
if c.versionNumber > version {
|
||||
fmt.Print(textutils.ColorYellow)
|
||||
fmt.Printf("================================================\n")
|
||||
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)
|
||||
|
||||
for i := version; i < c.version_number; i++ {
|
||||
for i := version; i < c.versionNumber; i++ {
|
||||
m := (*c.migrations)[i]
|
||||
if err := m.Apply(db); err != nil {
|
||||
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.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)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,17 +15,17 @@ import (
|
||||
func TestCreateAndConnectToDB(t *testing.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)
|
||||
schema_sql := string(_schema_sql)
|
||||
schemaSQL := string(_schemaSQL)
|
||||
migrations := []db.Migration{}
|
||||
|
||||
config := db.Init(&schema_sql, &migrations)
|
||||
config := db.Init(&schemaSQL, &migrations)
|
||||
|
||||
data_dir := "../../sample_data/data"
|
||||
_ = os.MkdirAll(data_dir, os.FileMode(0o644))
|
||||
dataDir := "../../sample_data/data"
|
||||
_ = os.MkdirAll(dataDir, os.FileMode(0o644))
|
||||
// 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
|
||||
|
||||
_, err = config.Create(dbPath)
|
||||
@@ -38,18 +38,18 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
// Create a little schema
|
||||
initial_schema := `
|
||||
initialSchema := `
|
||||
create table items(rowid integer primary key);
|
||||
create table db_version (version integer primary key) strict, without rowid;
|
||||
insert into db_version values(0);
|
||||
`
|
||||
migrations := []db.Migration{}
|
||||
config := db.Init(&initial_schema, &migrations)
|
||||
config := db.Init(&initialSchema, &migrations)
|
||||
|
||||
connection, err := config.Create(":memory:")
|
||||
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
|
||||
require.NoError(c.Get(&ret, "select version from db_version"))
|
||||
return
|
||||
@@ -58,21 +58,21 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
var items []int
|
||||
require.NoError(connection.Select(&items, "select * from items"))
|
||||
require.Len(items, 0)
|
||||
require.Equal(0, get_version(connection))
|
||||
require.Equal(0, getVersion(connection))
|
||||
|
||||
// Create a migration to add a new Item
|
||||
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))
|
||||
|
||||
var items2 []int
|
||||
require.NoError(connection.Select(&items2, "select * from items"))
|
||||
require.Len(items2, 1)
|
||||
require.Equal(1, get_version(connection))
|
||||
require.Equal(1, getVersion(connection))
|
||||
|
||||
// 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'`})
|
||||
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))
|
||||
|
||||
var items3 []struct {
|
||||
@@ -82,5 +82,5 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
require.NoError(connection.Select(&items3, "select * from items"))
|
||||
require.Len(items2, 1)
|
||||
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
|
||||
for _, target_col := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
||||
if target_col.Name == column.ForeignKeyTargetColumn && target_col.IsPrimaryKey {
|
||||
for _, targetCol := range s.Tables[column.ForeignKeyTargetTable].Columns {
|
||||
if targetCol.Name == column.ForeignKeyTargetColumn && targetCol.IsPrimaryKey {
|
||||
continue fk_loop
|
||||
}
|
||||
}
|
||||
@@ -125,8 +125,8 @@ var Checks = []Check{
|
||||
if idx.TableName != column.ForeignKeyTargetTable {
|
||||
continue
|
||||
}
|
||||
for _, idx_col_name := range idx.Columns {
|
||||
if column.ForeignKeyTargetColumn == idx_col_name {
|
||||
for _, idxColName := range idx.Columns {
|
||||
if column.ForeignKeyTargetColumn == idxColName {
|
||||
continue fk_loop
|
||||
}
|
||||
break // Only look at 1st column
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestFailureCases(t *testing.T) {
|
||||
test_cases := []struct {
|
||||
testCases := []struct {
|
||||
sqlFile string
|
||||
expectedFailures []string
|
||||
}{
|
||||
@@ -34,24 +34,24 @@ func TestFailureCases(t *testing.T) {
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test_case := range test_cases {
|
||||
t.Run(test_case.sqlFile, func(t *testing.T) {
|
||||
schema, err := schema.SchemaFromSQLFile(test_case.sqlFile)
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.sqlFile, func(t *testing.T) {
|
||||
schema, err := schema.SchemaFromSQLFile(testCase.sqlFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, check := range lint.Checks {
|
||||
results := check.Execute(schema)
|
||||
|
||||
is_failure := len(results) > 0
|
||||
is_failure_expected := slices.Contains(test_case.expectedFailures, check.Name)
|
||||
isFailure := len(results) > 0
|
||||
isFailureExpected := slices.Contains(testCase.expectedFailures, check.Name)
|
||||
|
||||
if is_failure != is_failure_expected {
|
||||
if is_failure_expected {
|
||||
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, test_case.sqlFile)
|
||||
if isFailure != isFailureExpected {
|
||||
if isFailureExpected {
|
||||
t.Errorf("Expected check '%s' to fail, but it passed: %s", check.Name, testCase.sqlFile)
|
||||
} else {
|
||||
t.Errorf("Expected check '%s' to pass, but it failed: %s (%q.%q)",
|
||||
check.Name,
|
||||
test_case.sqlFile,
|
||||
testCase.sqlFile,
|
||||
results[0].TableName, results[0].ColumnName,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ import (
|
||||
)
|
||||
|
||||
//go:embed views.sql
|
||||
var create_views string
|
||||
var createViews 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.MustExec(sql_schema)
|
||||
db.MustExec(sqlSchema)
|
||||
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.
|
||||
func SchemaFromDB(db *sqlx.DB) Schema {
|
||||
ret := Schema{Tables: map[string]Table{}, Indexes: map[string]Index{}}
|
||||
db.MustExec(create_views)
|
||||
db.MustExec(createViews)
|
||||
|
||||
var tables []Table
|
||||
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) {
|
||||
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)
|
||||
|
||||
db := schema.InitDB(string(schema_sql))
|
||||
db := schema.InitDB(string(schemaSQL))
|
||||
schema := schema.SchemaFromDB(db)
|
||||
expected_tbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
||||
for _, tbl_name := range expected_tbls {
|
||||
_, is_ok := schema.Tables[tbl_name]
|
||||
assert.True(is_ok)
|
||||
expectedTbls := []string{"food_types", "foods", "units", "ingredients", "recipes", "iterations", "db_version"}
|
||||
for _, tblName := range expectedTbls {
|
||||
_, isOk := schema.Tables[tblName]
|
||||
assert.True(isOk)
|
||||
}
|
||||
|
||||
foods := schema.Tables["foods"]
|
||||
@@ -57,23 +57,23 @@ func TestParseSchema(t *testing.T) {
|
||||
// -------
|
||||
|
||||
// Unique index
|
||||
units_name_idx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
||||
unitsNameIdx, isOk := schema.Indexes["sqlite_autoindex_units_1"]
|
||||
require.True(t, isOk)
|
||||
assert.True(units_name_idx.IsUnique)
|
||||
assert.Equal("units", units_name_idx.TableName)
|
||||
assert.Equal([]string{"name"}, units_name_idx.Columns)
|
||||
assert.True(unitsNameIdx.IsUnique)
|
||||
assert.Equal("units", unitsNameIdx.TableName)
|
||||
assert.Equal([]string{"name"}, unitsNameIdx.Columns)
|
||||
|
||||
// Non-unique, declared index
|
||||
foods_protein_idx, isOk := schema.Indexes["foods_protein"]
|
||||
foodsProteinIdx, isOk := schema.Indexes["foods_protein"]
|
||||
require.True(t, isOk)
|
||||
assert.False(foods_protein_idx.IsUnique)
|
||||
assert.Equal("foods", foods_protein_idx.TableName)
|
||||
assert.Equal([]string{"protein"}, foods_protein_idx.Columns)
|
||||
assert.False(foodsProteinIdx.IsUnique)
|
||||
assert.Equal("foods", foodsProteinIdx.TableName)
|
||||
assert.Equal([]string{"protein"}, foodsProteinIdx.Columns)
|
||||
|
||||
// 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)
|
||||
assert.True(ingr_seq_idx.IsUnique)
|
||||
assert.Equal("ingredients", ingr_seq_idx.TableName)
|
||||
assert.Equal([]string{"in_recipe_id", "list_order"}, ingr_seq_idx.Columns)
|
||||
assert.True(ingrSeqIdx.IsUnique)
|
||||
assert.Equal("ingredients", ingrSeqIdx.TableName)
|
||||
assert.Equal([]string{"in_recipe_id", "list_order"}, ingrSeqIdx.Columns)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user