move migrations
This commit is contained in:
parent
626379b804
commit
708893e566
|
@ -2,8 +2,13 @@ package mast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"embed"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed migrations/0001.sql
|
||||||
|
var migration0001 embed.FS
|
||||||
|
|
||||||
func RunMigrations(db *sql.DB) error {
|
func RunMigrations(db *sql.DB) error {
|
||||||
// Load the cr-sqlite extension
|
// Load the cr-sqlite extension
|
||||||
// _, err := db.Exec("SELECT load_extension('./crsqlite.so', 'sqlite3_crsqlite_init')")
|
// _, err := db.Exec("SELECT load_extension('./crsqlite.so', 'sqlite3_crsqlite_init')")
|
||||||
|
@ -11,11 +16,15 @@ func RunMigrations(db *sql.DB) error {
|
||||||
// return fmt.Errorf("failed to load cr-sqlite extension: %w", err)
|
// return fmt.Errorf("failed to load cr-sqlite extension: %w", err)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// Read the embedded SQL file
|
||||||
|
sqlData, err := migration0001.ReadFile("migrations/0001.sql")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to read embedded SQL file: %v", err)
|
||||||
|
}
|
||||||
|
sqlQuery := string(sqlData)
|
||||||
|
|
||||||
// Create the todos table and initialize it as a CRDT
|
// Create the todos table and initialize it as a CRDT
|
||||||
_, err := db.Exec(`
|
_, err = db.Exec(sqlQuery)
|
||||||
CREATE TABLE IF NOT EXISTS todos (id blob PRIMARY KEY NOT NULL, content TEXT, completed INTEGER NOT NULL DEFAULT 0);
|
|
||||||
SELECT crsql_as_crr('todos');
|
|
||||||
`)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
13
db/migrations/0001.sql
Normal file
13
db/migrations/0001.sql
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS todos
|
||||||
|
(
|
||||||
|
id blob PRIMARY KEY NOT NULL,
|
||||||
|
description text,
|
||||||
|
project text,
|
||||||
|
tags text,
|
||||||
|
due text,
|
||||||
|
wait text,
|
||||||
|
priority text,
|
||||||
|
urgency real,
|
||||||
|
completed integer NOT NULL DEFAULT 0
|
||||||
|
);
|
||||||
|
SELECT crsql_as_crr('todos');
|
Loading…
Reference in a new issue