From a9a691705672a84118beffb5deeb75cecc4f1eeb Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 20 Jan 2022 18:31:09 +0000 Subject: [PATCH] replace file-embed with QQ (#301) --- migrations/README.md | 9 --------- package.yaml | 2 -- simplexmq.cabal | 7 +------ .../Messaging/Agent/Store/SQLite/Migrations.hs | 18 +++++++++--------- .../SQLite/Migrations/M20220101_initial.hs | 11 +++++++++++ 5 files changed, 21 insertions(+), 26 deletions(-) delete mode 100644 migrations/README.md rename migrations/20220101_initial.sql => src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220101_initial.hs (94%) diff --git a/migrations/README.md b/migrations/README.md deleted file mode 100644 index 6bec7772f..000000000 --- a/migrations/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# SQLite database migrations - -These migrations are [embedded](../src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs) into the executable and run when SMP agent starts (as a separate executable or as a part of [simplex-chat](https://github.com/simplex-chat/simplex-chat) app). - -Migration file names must have a format `YYYYMMDD-name.sql` - they will be executed in the order or lexicographic sorting of the names, the files with any other extension than `.sql` are ignored. - -The proposed approach is to minimize the number of migrations and merge them together when possible, to align with the agent releases. - -**Please note**: Adding or editing migrations will NOT update the migrations embedded into the executable, unless the [Migrations](../src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs) module is rebuilt - use `stack build --force-dirty` (in addition to edited files it seems to rebuild the files with TH splices and their dependencies, not all files as with `stack clean`). diff --git a/package.yaml b/package.yaml index 30119976f..0c63d4d7a 100644 --- a/package.yaml +++ b/package.yaml @@ -20,7 +20,6 @@ category: Chat, Network, Web, System, Cryptography extra-source-files: - README.md - CHANGELOG.md - - migrations/*.* dependencies: - aeson == 1.5.* @@ -40,7 +39,6 @@ dependencies: - data-default == 0.7.* - direct-sqlite == 2.3.* - directory == 1.3.* - - file-embed >= 0.0.14.0 && <= 0.0.15.0 - filepath == 1.4.* - http-types == 0.12.* - generic-random >= 1.3 && < 1.5 diff --git a/simplexmq.cabal b/simplexmq.cabal index d089c1216..db1130ed7 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -26,8 +26,6 @@ build-type: Simple extra-source-files: README.md CHANGELOG.md - migrations/20220101_initial.sql - migrations/README.md library exposed-modules: @@ -40,6 +38,7 @@ library Simplex.Messaging.Agent.Store Simplex.Messaging.Agent.Store.SQLite Simplex.Messaging.Agent.Store.SQLite.Migrations + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial Simplex.Messaging.Client Simplex.Messaging.Crypto Simplex.Messaging.Crypto.Ratchet @@ -82,7 +81,6 @@ library , data-default ==0.7.* , direct-sqlite ==2.3.* , directory ==1.3.* - , file-embed >=0.0.14.0 && <=0.0.15.0 , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* @@ -134,7 +132,6 @@ executable smp-agent , data-default ==0.7.* , direct-sqlite ==2.3.* , directory ==1.3.* - , file-embed >=0.0.14.0 && <=0.0.15.0 , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* @@ -187,7 +184,6 @@ executable smp-server , data-default ==0.7.* , direct-sqlite ==2.3.* , directory ==1.3.* - , file-embed >=0.0.14.0 && <=0.0.15.0 , filepath ==1.4.* , generic-random >=1.3 && <1.5 , http-types ==0.12.* @@ -256,7 +252,6 @@ test-suite smp-server-test , data-default ==0.7.* , direct-sqlite ==2.3.* , directory ==1.3.* - , file-embed >=0.0.14.0 && <=0.0.15.0 , filepath ==1.4.* , generic-random >=1.3 && <1.5 , hspec ==2.7.* diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs index 4e6128493..cceac62fc 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations.hs @@ -16,29 +16,29 @@ module Simplex.Messaging.Agent.Store.SQLite.Migrations where import Control.Monad (forM_) -import Data.FileEmbed (embedDir, makeRelativeToProject) import Data.Function (on) import Data.List (intercalate, sortBy) import Data.Text (Text) -import Data.Text.Encoding (decodeUtf8) import Data.Time.Clock (getCurrentTime) -import Database.SQLite.Simple (Connection, Only (..)) +import Database.SQLite.Simple (Connection, Only (..), Query (..)) import qualified Database.SQLite.Simple as DB import Database.SQLite.Simple.QQ (sql) import qualified Database.SQLite3 as SQLite3 -import System.FilePath (takeBaseName, takeExtension) +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial data Migration = Migration {name :: String, up :: Text} deriving (Show) +schemaMigrations :: [(String, Query)] +schemaMigrations = + [ ("20220101_initial", m20220101_initial) + ] + -- | The list of migrations in ascending order by date app :: [Migration] -app = - sortBy (compare `on` name) . map migration . filter sqlFile $ - $(makeRelativeToProject "migrations" >>= embedDir) +app = sortBy (compare `on` name) $ map migration schemaMigrations where - sqlFile (file, _) = takeExtension file == ".sql" - migration (file, qStr) = Migration {name = takeBaseName file, up = decodeUtf8 qStr} + migration (name, query) = Migration {name = name, up = fromQuery query} get :: Connection -> [Migration] -> IO (Either String [Migration]) get conn migrations = diff --git a/migrations/20220101_initial.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220101_initial.hs similarity index 94% rename from migrations/20220101_initial.sql rename to src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220101_initial.hs index 2abd3ce52..93a93a858 100644 --- a/migrations/20220101_initial.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20220101_initial.hs @@ -1,3 +1,13 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20220101_initial where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20220101_initial :: Query +m20220101_initial = + [sql| CREATE TABLE servers ( host TEXT NOT NULL, port TEXT NOT NULL, @@ -135,3 +145,4 @@ CREATE TABLE skipped_messages ( msg_n INTEGER NOT NULL, msg_key BLOB NOT NULL ); +|]