mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-28 06:13:12 +00:00
Add migration, status and pending checks to database
This commit is contained in:
parent
b2933a41c1
commit
66fb03fa0d
9 changed files with 146 additions and 42 deletions
|
@ -2,7 +2,8 @@ package database
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
@ -16,19 +17,22 @@ func Open() {
|
|||
|
||||
Conn, err = sql.Open("sqlite3", "tastybites.db?_journal_mode=WAL")
|
||||
if err != nil {
|
||||
log.Fatal("Error opening connection: ", err)
|
||||
fmt.Println("Error opening connection:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
//Set the connection to use WAL mode
|
||||
_, err = Conn.Exec("PRAGMA journal_mode=WAL;")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func Close() {
|
||||
err := Conn.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
11
database/migrations/20240506163617-init_tables.sql
Normal file
11
database/migrations/20240506163617-init_tables.sql
Normal file
|
@ -0,0 +1,11 @@
|
|||
|
||||
-- +migrate Up
|
||||
CREATE TABLE IF NOT EXISTS Item (
|
||||
uuid TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
price INTEGER NOT NULL,
|
||||
description TEXT
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
DROP TABLE Item;
|
64
database/migrator.go
Normal file
64
database/migrator.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
||||
//go:embed migrations/*
|
||||
var dbMigrations embed.FS
|
||||
var migrations = migrate.EmbedFileSystemMigrationSource{
|
||||
FileSystem: dbMigrations,
|
||||
Root: "migrations",
|
||||
}
|
||||
|
||||
func Migrate(Up bool) error {
|
||||
var direction migrate.MigrationDirection
|
||||
if Up {
|
||||
direction = migrate.Up
|
||||
} else {
|
||||
direction = migrate.Down
|
||||
}
|
||||
|
||||
n, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Applied %d migrations!\n", n)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Status() error {
|
||||
// Get the list of applied migrations
|
||||
applied, err := migrate.GetMigrationRecords(Conn, "sqlite3")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Get the list of migrations that are yet to be applied
|
||||
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Migration list:")
|
||||
for _, migration := range applied {
|
||||
fmt.Printf("DONE: %s\n", migration.Id)
|
||||
}
|
||||
for _, migration := range planned {
|
||||
fmt.Printf("TODO: %s\n", migration.Id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Pending() (bool, error) {
|
||||
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return len(planned) > 0, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue