Dunno, just trying to get this thing going : (
This commit is contained in:
@@ -19,7 +19,6 @@ func verifyArgs(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
func migrate(cmd *cobra.Command, args []string) {
|
func migrate(cmd *cobra.Command, args []string) {
|
||||||
migID, err := strconv.ParseUint(args[0], 10, 0)
|
migID, err := strconv.ParseUint(args[0], 10, 0)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s is an invalid migration ID", args[1])
|
log.Fatalf("%s is an invalid migration ID", args[1])
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-6
@@ -3,8 +3,8 @@ package expenses
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"expenses/config"
|
"expenses/config"
|
||||||
"log"
|
|
||||||
mod "expenses/expenses/models"
|
mod "expenses/expenses/models"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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 {
|
func StartApp() error {
|
||||||
config.SetConfig("./config.yaml")
|
config.SetConfig("./config.yaml")
|
||||||
cfg := config.GetInstance()
|
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 {
|
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(migDB)
|
||||||
|
|
||||||
err = mod.RunMigrations(db)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to apply migrations: %v", err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
-- 0002_change_floats_to_decimal.down.sql
|
-- 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
|
-- expensesShares
|
||||||
CREATE TABLE expensesShares_old (
|
CREATE TABLE expensesShares_old (
|
||||||
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -25,24 +45,3 @@ CREATE TABLE expensesPayments_old (
|
|||||||
INSERT INTO expensesPayments_old SELECT * FROM expensesPayments;
|
INSERT INTO expensesPayments_old SELECT * FROM expensesPayments;
|
||||||
DROP TABLE expensesPayments;
|
DROP TABLE expensesPayments;
|
||||||
ALTER TABLE expensesPayments_old RENAME TO 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;
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,25 @@
|
|||||||
-- 0002_change_floats_to_decimal.up.sql
|
-- 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
|
-- expensesShares
|
||||||
CREATE TABLE expensesShares_new (
|
CREATE TABLE expensesShares_new (
|
||||||
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
"ExpShareID" integer PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -25,23 +45,3 @@ CREATE TABLE expensesPayments_new (
|
|||||||
INSERT INTO expensesPayments_new SELECT * FROM expensesPayments;
|
INSERT INTO expensesPayments_new SELECT * FROM expensesPayments;
|
||||||
DROP TABLE expensesPayments;
|
DROP TABLE expensesPayments;
|
||||||
ALTER TABLE expensesPayments_new RENAME TO 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;
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ version: "2"
|
|||||||
sql:
|
sql:
|
||||||
- engine: "sqlite"
|
- engine: "sqlite"
|
||||||
queries: "queries/"
|
queries: "queries/"
|
||||||
# Convert this to use a migrations/ (migrate)
|
|
||||||
schema: "migrations/"
|
schema: "migrations/"
|
||||||
gen:
|
gen:
|
||||||
go:
|
go:
|
||||||
@@ -12,3 +11,12 @@ sql:
|
|||||||
expenseTypes: "ExpenseTypes"
|
expenseTypes: "ExpenseTypes"
|
||||||
expenseShares: "ExpenseShares"
|
expenseShares: "ExpenseShares"
|
||||||
expensePayments: "ExpensePayments"
|
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"
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package models
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"expenses/config"
|
"expenses/config"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
mig "github.com/golang-migrate/migrate/v4"
|
mig "github.com/golang-migrate/migrate/v4"
|
||||||
@@ -26,7 +25,8 @@ func Goto(db *sql.DB, id uint) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.Force(int(id))
|
err = m.Migrate(id)
|
||||||
|
// err = m.Force(int(id))
|
||||||
if err != nil && err != mig.ErrNoChange {
|
if err != nil && err != mig.ErrNoChange {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -47,17 +47,15 @@ func RunMigrations(db *sql.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if dirty, ok := err.(mig.ErrDirty); ok {
|
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 {
|
forceErr := m.Force(int(dirty.Version - 1))
|
||||||
return fmt.Errorf("failed to force clean state: %w", ferr)
|
if forceErr != nil {
|
||||||
|
log.Fatalln("force reset failed: %w", forceErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if uerr := m.Up(); uerr != nil && uerr != mig.ErrNoChange {
|
migErr := m.Migrate(uint(dirty.Version))
|
||||||
return fmt.Errorf("retry migration failed: %w", uerr)
|
log.Fatalln(migErr)
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user