summaryrefslogtreecommitdiff
path: root/haskell
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-24 00:01:49 +0200
committerJulien Dessaux2023-07-24 00:01:49 +0200
commit565afa5c7b7b2ff7a1fbb7c098aa852a9c2e8dcc (patch)
tree1c25bd37b22dedb0b76cb3dfbcd7a2ec8da332d7 /haskell
parent[haskell] Prevent que networking code from triggering the API rate limiting (diff)
downloadspacetraders-565afa5c7b7b2ff7a1fbb7c098aa852a9c2e8dcc.tar.gz
spacetraders-565afa5c7b7b2ff7a1fbb7c098aa852a9c2e8dcc.tar.bz2
spacetraders-565afa5c7b7b2ff7a1fbb7c098aa852a9c2e8dcc.zip
[haskell] embed sql migration scripts from external files
Diffstat (limited to 'haskell')
-rw-r--r--haskell/package.yaml2
-rw-r--r--haskell/src/SpaceTraders/Database.hs37
-rw-r--r--haskell/src/SpaceTraders/Database/000_init.sql25
3 files changed, 34 insertions, 30 deletions
diff --git a/haskell/package.yaml b/haskell/package.yaml
index ed81dbd..e97a523 100644
--- a/haskell/package.yaml
+++ b/haskell/package.yaml
@@ -20,10 +20,10 @@ dependencies:
- base >= 4.7 && < 5
- bytestring
- directory
+- file-embed
- http-conduit
- http-types
- mtl
-- raw-strings-qq
- sqlite-simple
- text
- time
diff --git a/haskell/src/SpaceTraders/Database.hs b/haskell/src/SpaceTraders/Database.hs
index 4fffb07..66ff893 100644
--- a/haskell/src/SpaceTraders/Database.hs
+++ b/haskell/src/SpaceTraders/Database.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
+{-# LANGUAGE TemplateHaskell #-}
module SpaceTraders.Database
( close
@@ -7,36 +8,14 @@ module SpaceTraders.Database
) where
import Control.Exception
+import qualified Data.ByteString as B
+import Data.FileEmbed
import qualified Database.SQLite.Simple as S
-import Text.RawString.QQ
+import qualified Data.Text as T
+import qualified Data.Text.Encoding as T
-migrations :: [S.Query]
-migrations = [
- [r|CREATE TABLE schema_version (
- version INTEGER NOT NULL
- );|],
- [r|CREATE TABLE tokens (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
- );|],
- [r|CREATE TABLE agents (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
- );|],
- [r|CREATE TABLE contracts (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
- );|],
- [r|CREATE TABLE ships (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
- );|],
- [r|CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));|],
- [r|CREATE TABLE systems (
- id INTEGER PRIMARY KEY,
- data TEXT NOT NULL
- );|],
- [r|CREATE UNIQUE INDEX systems_data_symbol ON systems (json_extract(data, '$.symbol'));|]]
+migrations :: [B.ByteString]
+migrations = [ $(embedFile "src/SpaceTraders/Database/000_init.sql") ]
close :: S.Connection -> IO ()
close conn = S.close conn
@@ -48,7 +27,7 @@ open = do
S.execute_ conn "PRAGMA journal_mode = WAL;"
S.withTransaction conn $ do
version <- getSchemaVersion conn `catch` defaultVersion
- mapM_ (S.execute_ conn) $ drop version migrations
+ mapM_ (S.execute_ conn) $ S.Query <$> (filter (/= "\n") . concat . map ((T.splitOn ";") . T.decodeUtf8) $ drop version migrations)
S.execute_ conn "DELETE FROM schema_version;"
S.execute conn "INSERT INTO schema_version (version) VALUES (?);" (S.Only $ length migrations)
return conn
diff --git a/haskell/src/SpaceTraders/Database/000_init.sql b/haskell/src/SpaceTraders/Database/000_init.sql
new file mode 100644
index 0000000..78a62cd
--- /dev/null
+++ b/haskell/src/SpaceTraders/Database/000_init.sql
@@ -0,0 +1,25 @@
+CREATE TABLE schema_version (
+ version INTEGER NOT NULL
+);
+CREATE TABLE tokens (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
+CREATE TABLE agents (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
+CREATE TABLE contracts (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
+CREATE TABLE ships (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
+CREATE UNIQUE INDEX ships_data_symbol ON ships (json_extract(data, '$.symbol'));
+CREATE TABLE systems (
+ id INTEGER PRIMARY KEY,
+ data TEXT NOT NULL
+);
+CREATE UNIQUE INDEX systems_data_symbol ON systems (json_extract(data, '$.symbol'));