db: make migrations a struct instead of a plain string
This commit is contained in:
@@ -20,11 +20,11 @@ type DBConfig struct {
|
||||
sql_schema *string
|
||||
|
||||
// Database starts at version 0. First migration brings us to version 1
|
||||
migrations *[]string
|
||||
migrations *[]Migration
|
||||
version_number uint
|
||||
}
|
||||
|
||||
func Init(schema *string, migrationsList *[]string) DBConfig {
|
||||
func Init(schema *string, migrationsList *[]Migration) DBConfig {
|
||||
return DBConfig{
|
||||
sql_schema: schema,
|
||||
migrations: migrationsList,
|
||||
@@ -99,7 +99,7 @@ func (c DBConfig) CheckAndUpdateVersion(db *sqlx.DB) error {
|
||||
func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
|
||||
for i := x; i < y; i++ {
|
||||
fmt.Print(textutils.ColorCyan)
|
||||
fmt.Println((*c.migrations)[i])
|
||||
fmt.Println((*c.migrations)[i].SQL)
|
||||
fmt.Print(textutils.ColorReset)
|
||||
|
||||
// Execute the migration in a transaction
|
||||
@@ -107,7 +107,7 @@ func (c DBConfig) UpgradeFromXToY(db *sqlx.DB, x uint, y uint) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx.MustExec((*c.migrations)[i])
|
||||
tx.MustExec((*c.migrations)[i].SQL)
|
||||
tx.MustExec("update db_version set version = ?", i+1)
|
||||
if err := tx.Commit(); err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestCreateAndConnectToDB(t *testing.T) {
|
||||
_schema_sql, err := os.ReadFile("../../sample_data/test_schemas/food.sql")
|
||||
require.NoError(t, err)
|
||||
schema_sql := string(_schema_sql)
|
||||
migrations := []string{}
|
||||
migrations := []db.Migration{}
|
||||
|
||||
config := db.Init(&schema_sql, &migrations)
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
create table db_version (version integer primary key) strict, without rowid;
|
||||
insert into db_version values(0);
|
||||
`
|
||||
migrations := []string{}
|
||||
migrations := []db.Migration{}
|
||||
config := db.Init(&initial_schema, &migrations)
|
||||
|
||||
connection, err := config.Create(":memory:")
|
||||
@@ -61,7 +61,7 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
require.Equal(0, get_version(connection))
|
||||
|
||||
// Create a migration to add a new Item
|
||||
migrations = append(migrations, "insert into items (rowid) values (1)")
|
||||
migrations = append(migrations, db.Migration{ID: 1, SQL: "insert into items (rowid) values (1)"})
|
||||
db.Init(&initial_schema, &migrations) // Reinitialize with the new migration
|
||||
config.UpgradeFromXToY(connection, uint(len(migrations)-1), uint(len(migrations)))
|
||||
|
||||
@@ -71,7 +71,7 @@ func TestVersionUpgrade(t *testing.T) {
|
||||
require.Equal(1, get_version(connection))
|
||||
|
||||
// Create a migration to add a new Item
|
||||
migrations = append(migrations, `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.UpgradeFromXToY(connection, uint(len(migrations)-1), uint(len(migrations)))
|
||||
|
||||
|
||||
12
pkg/db/migration.go
Normal file
12
pkg/db/migration.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package db
|
||||
|
||||
type Migration struct {
|
||||
// ID is the version number that the schema will be at, after applying the migration.
|
||||
ID int
|
||||
|
||||
// Title is an optional name for the migration.
|
||||
Title string
|
||||
|
||||
// SQL is the body of the migration that will be executed.
|
||||
SQL string
|
||||
}
|
||||
Reference in New Issue
Block a user