chore: remove db migration support — fold household_id into users table definition
- Added household_id column directly to users CREATE TABLE - Removed ALTER TABLE migration hack and Control.Exception/ScopedTypeVariables - Renamed runMigrations to createTables (now internal, unexported) - Tables created from scratch if database doesn't exist; no migration logic
This commit is contained in:
+4
-9
@@ -2,7 +2,6 @@
|
|||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
{-# LANGUAGE GADTs #-}
|
{-# LANGUAGE GADTs #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
{-# LANGUAGE ScopedTypeVariables #-}
|
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE TypeOperators #-}
|
{-# LANGUAGE TypeOperators #-}
|
||||||
|
|
||||||
@@ -12,7 +11,6 @@ module Sis.Database (
|
|||||||
DB (..),
|
DB (..),
|
||||||
runDB,
|
runDB,
|
||||||
openDatabase,
|
openDatabase,
|
||||||
runMigrations,
|
|
||||||
|
|
||||||
-- * DB operations (convenience wrappers)
|
-- * DB operations (convenience wrappers)
|
||||||
findUserByEmail,
|
findUserByEmail,
|
||||||
@@ -37,7 +35,6 @@ module Sis.Database (
|
|||||||
seed,
|
seed,
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Exception (IOException, catch)
|
|
||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
import Data.Maybe (fromMaybe, listToMaybe)
|
import Data.Maybe (fromMaybe, listToMaybe)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
@@ -398,12 +395,12 @@ openDatabase path = do
|
|||||||
conn <- SQL.open path
|
conn <- SQL.open path
|
||||||
SQL.execute_ conn "PRAGMA journal_mode=WAL"
|
SQL.execute_ conn "PRAGMA journal_mode=WAL"
|
||||||
SQL.execute_ conn "PRAGMA foreign_keys=ON"
|
SQL.execute_ conn "PRAGMA foreign_keys=ON"
|
||||||
runMigrations conn
|
createTables conn
|
||||||
pure conn
|
pure conn
|
||||||
|
|
||||||
-- | Create all tables if they don't exist.
|
-- | Create all tables if they don't exist.
|
||||||
runMigrations :: SQL.Connection -> IO ()
|
createTables :: SQL.Connection -> IO ()
|
||||||
runMigrations conn' = do
|
createTables conn' = do
|
||||||
mapM_
|
mapM_
|
||||||
(SQL.execute_ conn')
|
(SQL.execute_ conn')
|
||||||
[ "CREATE TABLE IF NOT EXISTS users (\
|
[ "CREATE TABLE IF NOT EXISTS users (\
|
||||||
@@ -411,6 +408,7 @@ runMigrations conn' = do
|
|||||||
\ display_name TEXT NOT NULL,\
|
\ display_name TEXT NOT NULL,\
|
||||||
\ email TEXT NOT NULL UNIQUE,\
|
\ email TEXT NOT NULL UNIQUE,\
|
||||||
\ password_hash TEXT NOT NULL,\
|
\ password_hash TEXT NOT NULL,\
|
||||||
|
\ household_id INTEGER REFERENCES households(id),\
|
||||||
\ created_at TEXT NOT NULL DEFAULT (datetime('now')))"
|
\ created_at TEXT NOT NULL DEFAULT (datetime('now')))"
|
||||||
, "CREATE TABLE IF NOT EXISTS sessions (\
|
, "CREATE TABLE IF NOT EXISTS sessions (\
|
||||||
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
|
\ id INTEGER PRIMARY KEY AUTOINCREMENT,\
|
||||||
@@ -467,9 +465,6 @@ runMigrations conn' = do
|
|||||||
\ expires_at TEXT NOT NULL,\
|
\ expires_at TEXT NOT NULL,\
|
||||||
\ created_at TEXT NOT NULL DEFAULT (datetime('now')))"
|
\ created_at TEXT NOT NULL DEFAULT (datetime('now')))"
|
||||||
]
|
]
|
||||||
-- Add household_id to users (safe to re-run, ignores "duplicate column" error)
|
|
||||||
SQL.execute_ conn' "ALTER TABLE users ADD COLUMN household_id INTEGER REFERENCES households(id)"
|
|
||||||
`catch` (\(_ :: IOException) -> pure ())
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
-- Convenience wrappers (send through the DB effect)
|
-- Convenience wrappers (send through the DB effect)
|
||||||
|
|||||||
Reference in New Issue
Block a user