Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-3058] Implement MigrationInterface for SQLite #3977

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/wallet/api/http/Cardano/Wallet/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import Cardano.Wallet.Api.Http.Shelley.Server
)
import Cardano.Wallet.DB.Layer
( PersistAddressBook )
import Cardano.Wallet.DB.Sqlite.Migration
import Cardano.Wallet.DB.Sqlite.MigrationOld
( DefaultFieldValues (..) )
import Cardano.Wallet.Flavor
( CredFromOf, KeyFlavorS (..), KeyOf, WalletFlavor (..) )
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/bench/restore-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ import qualified Cardano.Wallet as W
import qualified Cardano.Wallet.Address.Derivation.Byron as Byron
import qualified Cardano.Wallet.Address.Derivation.Shelley as Shelley
import qualified Cardano.Wallet.Checkpoints.Policy as CP
import qualified Cardano.Wallet.DB.Sqlite.Migration as Sqlite
import qualified Cardano.Wallet.DB.Sqlite.MigrationOld as Sqlite
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.UTxO as UTxO
import qualified Cardano.Wallet.Primitive.Types.UTxOStatistics as UTxOStatistics
Expand Down
3 changes: 2 additions & 1 deletion lib/wallet/cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ library
Cardano.Wallet.DB.Migration
Cardano.Wallet.DB.Pure.Implementation
Cardano.Wallet.DB.Pure.Layer
Cardano.Wallet.DB.Sqlite.Migration
Cardano.Wallet.DB.Sqlite.MigrationNew
Cardano.Wallet.DB.Sqlite.MigrationOld
Cardano.Wallet.DB.Sqlite.Schema
Cardano.Wallet.DB.Sqlite.Types
Cardano.Wallet.DB.Store.Checkpoints.Store
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import Cardano.Wallet.DB
, mkDBLayerFromParts
, transactionsStore
)
import Cardano.Wallet.DB.Sqlite.Migration
import Cardano.Wallet.DB.Sqlite.MigrationOld
( DefaultFieldValues (..), migrateManually )
import Cardano.Wallet.DB.Sqlite.Schema
( CBOR (..)
Expand Down
47 changes: 47 additions & 0 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Cardano.Wallet.DB.Sqlite.MigrationNew (newMigrationInterface) where

import Prelude

import Cardano.DB.Sqlite
( DBLog, withConnectionPool )
import Cardano.Wallet.DB.Migration
( MigrationInterface (..), Version )
import Cardano.Wallet.DB.Sqlite.MigrationOld
( getSchemaVersion, putSchemaVersion )
import Control.Tracer
( Tracer )
import Data.Pool
( withResource )
import System.Directory
( copyFile )

import qualified Cardano.Wallet.DB.Sqlite.MigrationOld as MigrateOld
import qualified Database.Sqlite as Sqlite

newMigrationInterface
:: Tracer IO DBLog
-> MigrationInterface IO Sqlite.Connection
newMigrationInterface tr =
MigrationInterface
{ backupDatabaseFile = \fp v -> do
let backupFile = fp <> ".v" <> show v <> ".bak"
copyFile fp backupFile
, withDatabaseFile = \fp f -> do
withConnectionPool tr fp $ \pool ->
withResource pool $ \(_, conn) -> do
f conn
, getVersion = getVersionNew
, setVersion = setVersionNew
}

oldToNewSchemaVersion :: MigrateOld.SchemaVersion -> Version
oldToNewSchemaVersion (MigrateOld.SchemaVersion v) = v

newToOldSchemaVersion :: Version -> MigrateOld.SchemaVersion
newToOldSchemaVersion = MigrateOld.SchemaVersion

getVersionNew :: Sqlite.Connection -> IO Version
getVersionNew = fmap oldToNewSchemaVersion . getSchemaVersion

setVersionNew :: Sqlite.Connection -> Version -> IO ()
setVersionNew conn = putSchemaVersion conn . newToOldSchemaVersion
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
-- These migrations are soon to be removed in favor of
-- a file format with version number.

module Cardano.Wallet.DB.Sqlite.Migration
module Cardano.Wallet.DB.Sqlite.MigrationOld
( DefaultFieldValues (..)
, migrateManually
, SchemaVersion (..)
, currentSchemaVersion
, InvalidDatabaseSchemaVersion (..)
, putSchemaVersion
, getSchemaVersion
)
where

Expand Down Expand Up @@ -182,23 +184,7 @@ migrateManually tr key defaultFieldValues =
\)"
_ -> pure TableExisted

putSchemaVersion :: Sqlite.Connection -> SchemaVersion -> IO ()
putSchemaVersion conn schemaVersion = void $ runSql conn $ T.unwords
[ "INSERT INTO database_schema_version (name, version)"
, "VALUES ('schema',"
, version
, ") ON CONFLICT (name) DO UPDATE SET version ="
, version
]
where
version = T.pack $ show schemaVersion

getSchemaVersion :: Sqlite.Connection -> IO SchemaVersion
getSchemaVersion conn =
runSql conn "SELECT version FROM database_schema_version" >>= \case
[[PersistInt64 int]] | int >= 0 -> pure $ SchemaVersion
$ fromIntegral int
_ -> throwString "Database metadata table is corrupt"

-- NOTE
-- We originally stored script pool gap inside sequential state in the 'SeqState' table,
Expand Down Expand Up @@ -942,3 +928,21 @@ runSql conn raw = do
collect query (result : acc)
Sqlite.Done -> do
return (reverse acc)

putSchemaVersion :: Sqlite.Connection -> SchemaVersion -> IO ()
putSchemaVersion conn schemaVersion = void $ runSql conn $ T.unwords
[ "INSERT INTO database_schema_version (name, version)"
, "VALUES ('schema',"
, version
, ") ON CONFLICT (name) DO UPDATE SET version ="
, version
]
where
version = T.pack $ show schemaVersion

getSchemaVersion :: Sqlite.Connection -> IO SchemaVersion
getSchemaVersion conn =
runSql conn "SELECT version FROM database_schema_version" >>= \case
[[PersistInt64 int]] | int >= 0 -> pure $ SchemaVersion
$ fromIntegral int
_ -> throwString "Database metadata table is corrupt"
2 changes: 1 addition & 1 deletion lib/wallet/test/unit/Cardano/Wallet/DB/LayerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ import Cardano.Wallet.DB.Layer
)
import Cardano.Wallet.DB.Properties
( properties )
import Cardano.Wallet.DB.Sqlite.Migration
import Cardano.Wallet.DB.Sqlite.MigrationOld
( InvalidDatabaseSchemaVersion (..)
, SchemaVersion (..)
, currentSchemaVersion
Expand Down