Dunno, just trying to get this thing going : (

This commit is contained in:
2025-08-10 00:16:50 +01:00
parent 65c112abfa
commit 528540a723
6 changed files with 77 additions and 59 deletions
-1
View File
@@ -19,7 +19,6 @@ func verifyArgs(cmd *cobra.Command, args []string) error {
func migrate(cmd *cobra.Command, args []string) {
migID, err := strconv.ParseUint(args[0], 10, 0)
if err != nil {
log.Fatalf("%s is an invalid migration ID", args[1])
}
+20 -6
View File
@@ -3,8 +3,8 @@ package expenses
import (
"database/sql"
"expenses/config"
"log"
mod "expenses/expenses/models"
"log"
)
var (
@@ -21,21 +21,35 @@ func NewExpenseService(db *sql.DB) *Service {
}
}
func openDB(sys string, path string, extra string) (*sql.DB, error) {
db, err := sql.Open(sys, "file:"+path+extra)
if err != nil {
return nil, err
}
return db, nil
}
func StartApp() error {
config.SetConfig("./config.yaml")
cfg := config.GetInstance()
db, err := sql.Open(cfg.DBSys, "file:"+cfg.DBPath+"?_foreign_keys=on")
migDB, err := openDB(cfg.DBSys, cfg.DBPath, "")
if err != nil {
log.Fatalf("Failed to open DB: %v", err)
log.Fatalf("Failed to open migration DB: %v", err)
}
Serv = NewExpenseService(db)
err = mod.RunMigrations(db)
err = mod.RunMigrations(migDB)
if err != nil {
log.Fatalf("Failed to apply migrations: %v", err)
}
migDB.Close()
db, err := openDB(cfg.DBSys, cfg.DBPath, "?_foreign_keys=on")
if err != nil {
log.Fatalf("Failed to open DB: %v", err)
}
Serv = NewExpenseService(db)
return nil
}
@@ -1,5 +1,25 @@
-- 0002_change_floats_to_decimal.down.sql
-- expenses
CREATE TABLE expenses_old (
"ExpID" integer PRIMARY KEY AUTOINCREMENT,
"Description" text NOT NULL,
"Value" float NOT NULL DEFAULT 0,
"StoreID" int NOT NULL DEFAULT 0,
"CategoryID" int NOT NULL DEFAULT 0,
"OwnerUserID" int NOT NULL DEFAULT 0,
"TypeID" int NOT NULL DEFAULT 0,
"ExpDate" int NOT NULL DEFAULT -1,
"CreationDate" int NOT NULL DEFAULT -1,
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
);
INSERT INTO expenses_old SELECT * FROM expenses;
DROP TABLE expenses;
ALTER TABLE expenses_old RENAME TO expenses;
-- expensesShares
CREATE TABLE expensesShares_old (
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
@@ -25,24 +45,3 @@ CREATE TABLE expensesPayments_old (
INSERT INTO expensesPayments_old SELECT * FROM expensesPayments;
DROP TABLE expensesPayments;
ALTER TABLE expensesPayments_old RENAME TO expensesPayments;
-- expenses
CREATE TABLE expenses_old (
"ExpID" integer PRIMARY KEY AUTOINCREMENT,
"Description" text NOT NULL,
"Value" float NOT NULL DEFAULT 0,
"StoreID" int NOT NULL DEFAULT 0,
"CategoryID" int NOT NULL DEFAULT 0,
"OwnerUserID" int NOT NULL DEFAULT 0,
"TypeID" int NOT NULL DEFAULT 0,
"ExpDate" int NOT NULL DEFAULT -1,
"CreationDate" int NOT NULL DEFAULT -1,
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
);
INSERT INTO expenses_old SELECT * FROM expenses;
DROP TABLE expenses;
ALTER TABLE expenses_old RENAME TO expenses;
+20 -20
View File
@@ -1,5 +1,25 @@
-- 0002_change_floats_to_decimal.up.sql
-- expenses
CREATE TABLE expenses_new (
"ExpID" integer PRIMARY KEY AUTOINCREMENT,
"Description" text NOT NULL,
"Value" DECIMAL(10,2) NOT NULL DEFAULT 0,
"StoreID" int NOT NULL DEFAULT 0,
"CategoryID" int NOT NULL DEFAULT 0,
"OwnerUserID" int NOT NULL DEFAULT 0,
"TypeID" int NOT NULL DEFAULT 0,
"ExpDate" int NOT NULL DEFAULT -1,
"CreationDate" int NOT NULL DEFAULT -1,
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
);
INSERT INTO expenses_new SELECT * FROM expenses;
DROP TABLE expenses;
ALTER TABLE expenses_new RENAME TO expenses;
-- expensesShares
CREATE TABLE expensesShares_new (
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
@@ -25,23 +45,3 @@ CREATE TABLE expensesPayments_new (
INSERT INTO expensesPayments_new SELECT * FROM expensesPayments;
DROP TABLE expensesPayments;
ALTER TABLE expensesPayments_new RENAME TO expensesPayments;
-- expenses
CREATE TABLE expenses_new (
"ExpID" integer PRIMARY KEY AUTOINCREMENT,
"Description" text NOT NULL,
"Value" DECIMAL(10,2) NOT NULL DEFAULT 0,
"StoreID" int NOT NULL DEFAULT 0,
"CategoryID" int NOT NULL DEFAULT 0,
"OwnerUserID" int NOT NULL DEFAULT 0,
"TypeID" int NOT NULL DEFAULT 0,
"ExpDate" int NOT NULL DEFAULT -1,
"CreationDate" int NOT NULL DEFAULT -1,
FOREIGN KEY(StoreID) REFERENCES stores(StoreID) ON DELETE RESTRICT,
FOREIGN KEY(CategoryID) REFERENCES categories(CategoryID) ON DELETE RESTRICT,
FOREIGN KEY(OwnerUserID) REFERENCES users(UserID) ON DELETE RESTRICT,
FOREIGN KEY(TypeID) REFERENCES expensetypes(TypeID) ON DELETE RESTRICT
);
INSERT INTO expenses_new SELECT * FROM expenses;
DROP TABLE expenses;
ALTER TABLE expenses_new RENAME TO expenses;
+9 -1
View File
@@ -2,7 +2,6 @@ version: "2"
sql:
- engine: "sqlite"
queries: "queries/"
# Convert this to use a migrations/ (migrate)
schema: "migrations/"
gen:
go:
@@ -12,3 +11,12 @@ sql:
expenseTypes: "ExpenseTypes"
expenseShares: "ExpenseShares"
expensePayments: "ExpensePayments"
overrides:
- db_type: "float"
go_type:
import: "github.com/shopspring/decimal"
type: "DECIMAL"
- db_type: "DECIMAL"
go_type:
import: "github.com/shopspring/decimal"
type: "DECIMAL"
+8 -10
View File
@@ -3,7 +3,6 @@ package models
import (
"database/sql"
"expenses/config"
"fmt"
"log"
mig "github.com/golang-migrate/migrate/v4"
@@ -26,7 +25,8 @@ func Goto(db *sql.DB, id uint) error {
return err
}
err = m.Force(int(id))
err = m.Migrate(id)
// err = m.Force(int(id))
if err != nil && err != mig.ErrNoChange {
return err
}
@@ -47,17 +47,15 @@ func RunMigrations(db *sql.DB) error {
}
if dirty, ok := err.(mig.ErrDirty); ok {
log.Printf("Database is dirty at version %d, cleaning", dirty.Version)
log.Printf("migration %d is dirty:", dirty.Version)
if ferr := m.Force(dirty.Version); ferr != nil {
return fmt.Errorf("failed to force clean state: %w", ferr)
forceErr := m.Force(int(dirty.Version - 1))
if forceErr != nil {
log.Fatalln("force reset failed: %w", forceErr)
}
if uerr := m.Up(); uerr != nil && uerr != mig.ErrNoChange {
return fmt.Errorf("retry migration failed: %w", uerr)
}
return nil
migErr := m.Migrate(uint(dirty.Version))
log.Fatalln(migErr)
}
}