move migrations

This commit is contained in:
Anish Lakhwara 2024-09-18 18:47:29 -07:00
parent 626379b804
commit 708893e566
2 changed files with 26 additions and 4 deletions

View file

@ -2,8 +2,13 @@ package mast
import (
"database/sql"
"embed"
"log"
)
//go:embed migrations/0001.sql
var migration0001 embed.FS
func RunMigrations(db *sql.DB) error {
// Load the cr-sqlite extension
// _, 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)
// }
// 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
_, err := db.Exec(`
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');
`)
_, err = db.Exec(sqlQuery)
if err != nil {
return err
}

13
db/migrations/0001.sql Normal file
View 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');