Skip to content

Commit daa09c4

Browse files
pbrisbinnuttycom
authored andcommitted
Remove MySQL, add PostgreSQL
1 parent 067c1a7 commit daa09c4

File tree

12 files changed

+247
-66
lines changed

12 files changed

+247
-66
lines changed

dbmigrations.cabal

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ source-repository head
4646
type: git
4747
location: https://github.com/haskell-github-trust/dbmigrations
4848

49-
flag mysql
50-
description: Build the mysql executable (and tests) application
51-
manual: False
52-
default: False
53-
5449
flag postgresql
5550
description: Build the postgresql executable (and tests) application
5651
manual: False
@@ -129,6 +124,49 @@ library
129124
if impl(ghc >= 8.10)
130125
ghc-options: -Wno-missing-safe-haskell-mode
131126

127+
executable dbm-postgresql
128+
main-is: Main.hs
129+
other-modules:
130+
Paths_dbmigrations
131+
hs-source-dirs:
132+
postgresql/app
133+
default-extensions:
134+
BangPatterns
135+
DataKinds
136+
DeriveAnyClass
137+
DeriveFoldable
138+
DeriveFunctor
139+
DeriveGeneric
140+
DeriveLift
141+
DeriveTraversable
142+
DerivingStrategies
143+
FlexibleContexts
144+
FlexibleInstances
145+
GADTs
146+
GeneralizedNewtypeDeriving
147+
LambdaCase
148+
MultiParamTypeClasses
149+
NoImplicitPrelude
150+
NoMonomorphismRestriction
151+
OverloadedStrings
152+
RankNTypes
153+
ScopedTypeVariables
154+
StandaloneDeriving
155+
TypeApplications
156+
TypeFamilies
157+
ghc-options: -fwrite-ide-info -Weverything -Wno-all-missed-specialisations -Wno-missed-specialisations -Wno-missing-exported-signatures -Wno-missing-import-lists -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-safe -Wno-unsafe -threaded -rtsopts "-with-rtsopts=-N"
158+
build-depends:
159+
HDBC-postgresql
160+
, base <5
161+
, dbmigrations
162+
default-language: GHC2021
163+
if impl(ghc >= 9.2)
164+
ghc-options: -Wno-missing-kind-signatures
165+
if impl(ghc >= 8.10)
166+
ghc-options: -Wno-missing-safe-haskell-mode
167+
if !(flag(postgresql))
168+
buildable: False
169+
132170
executable dbm-sqlite
133171
main-is: Main.hs
134172
other-modules:
@@ -172,6 +210,52 @@ executable dbm-sqlite
172210
if !(flag(sqlite))
173211
buildable: False
174212

213+
test-suite postgresql-spec
214+
type: exitcode-stdio-1.0
215+
main-is: Main.hs
216+
other-modules:
217+
Paths_dbmigrations
218+
hs-source-dirs:
219+
postgresql/tests
220+
default-extensions:
221+
BangPatterns
222+
DataKinds
223+
DeriveAnyClass
224+
DeriveFoldable
225+
DeriveFunctor
226+
DeriveGeneric
227+
DeriveLift
228+
DeriveTraversable
229+
DerivingStrategies
230+
FlexibleContexts
231+
FlexibleInstances
232+
GADTs
233+
GeneralizedNewtypeDeriving
234+
LambdaCase
235+
MultiParamTypeClasses
236+
NoImplicitPrelude
237+
NoMonomorphismRestriction
238+
OverloadedStrings
239+
RankNTypes
240+
ScopedTypeVariables
241+
StandaloneDeriving
242+
TypeApplications
243+
TypeFamilies
244+
ghc-options: -fwrite-ide-info -Weverything -Wno-all-missed-specialisations -Wno-missed-specialisations -Wno-missing-exported-signatures -Wno-missing-import-lists -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-safe -Wno-unsafe -threaded -rtsopts "-with-rtsopts=-N"
245+
build-depends:
246+
HDBC
247+
, HDBC-postgresql
248+
, base <5
249+
, dbmigrations
250+
, hspec
251+
default-language: GHC2021
252+
if impl(ghc >= 9.2)
253+
ghc-options: -Wno-missing-kind-signatures
254+
if impl(ghc >= 8.10)
255+
ghc-options: -Wno-missing-safe-haskell-mode
256+
if !(flag(postgresql))
257+
buildable: False
258+
175259
test-suite spec
176260
type: exitcode-stdio-1.0
177261
main-is: Spec.hs

package.yaml

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,28 @@ executables:
102102
- condition: ! "!(flag(sqlite))"
103103
buildable: false
104104

105+
# dbm-mysql:
106+
# source-dirs: mysql/app
107+
# ghc-options: -threaded -rtsopts "-with-rtsopts=-N"
108+
# main: Main.hs
109+
# dependencies:
110+
# - HDBC-mysql
111+
# - dbmigrations
112+
# when:
113+
# - condition: ! "!(flag(mysql))"
114+
# buildable: false
115+
116+
dbm-postgresql:
117+
source-dirs: postgresql/app
118+
ghc-options: -threaded -rtsopts "-with-rtsopts=-N"
119+
main: Main.hs
120+
dependencies:
121+
- HDBC-postgresql
122+
- dbmigrations
123+
when:
124+
- condition: ! "!(flag(postgresql))"
125+
buildable: false
126+
105127
tests:
106128
spec:
107129
source-dirs: tests
@@ -133,16 +155,42 @@ tests:
133155
- condition: ! "!(flag(sqlite))"
134156
buildable: false
135157

158+
# mysql-spec:
159+
# source-dirs: mysql/tests
160+
# ghc-options: -threaded -rtsopts "-with-rtsopts=-N"
161+
# main: Main.hs
162+
# dependencies:
163+
# - HDBC
164+
# - HDBC-mysql
165+
# - dbmigrations
166+
# - hspec
167+
# when:
168+
# - condition: ! "!(flag(mysql))"
169+
# buildable: false
170+
171+
postgresql-spec:
172+
source-dirs: postgresql/tests
173+
ghc-options: -threaded -rtsopts "-with-rtsopts=-N"
174+
main: Main.hs
175+
dependencies:
176+
- HDBC
177+
- HDBC-postgresql
178+
- dbmigrations
179+
- hspec
180+
when:
181+
- condition: ! "!(flag(postgresql))"
182+
buildable: false
183+
136184
flags:
137185
sqlite:
138186
description: Build the sqlite executable (and tests)
139187
manual: false
140188
default: false
141189

142-
mysql:
143-
description: Build the mysql executable (and tests) application
144-
manual: false
145-
default: false
190+
# mysql:
191+
# description: Build the mysql executable (and tests) application
192+
# manual: false
193+
# default: false
146194

147195
postgresql:
148196
description: Build the postgresql executable (and tests) application

postgresql/app/Main.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Main (main) where
2+
3+
import Prelude
4+
5+
import Database.HDBC.PostgreSQL (connectPostgreSQL)
6+
import Moo.Main
7+
8+
main :: IO ()
9+
main = hdbcMain connectPostgreSQL

postgresql/tests/Main.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{-# LANGUAGE DerivingVia #-}
2+
{-# OPTIONS_GHC -Wno-orphans #-}
3+
4+
module Main (main) where
5+
6+
import Prelude
7+
8+
import Data.Maybe (fromMaybe)
9+
import Database.HDBC (IConnection (disconnect))
10+
import Database.HDBC.PostgreSQL (Connection, connectPostgreSQL)
11+
import Database.Schema.Migrations.Backend.HDBC
12+
import Database.Schema.Migrations.Test.BackendTest hiding (spec)
13+
import Database.Schema.Migrations.Test.BackendTest qualified as BackendTest
14+
import System.Environment (lookupEnv)
15+
import Test.Hspec
16+
17+
deriving via (HDBCConnection Connection) instance BackendConnection Connection
18+
19+
main :: IO ()
20+
main = hspec $ before setupPostgreSQL $ after disconnect BackendTest.spec
21+
22+
setupPostgreSQL :: IO Connection
23+
setupPostgreSQL = do
24+
url <- fromMaybe defaultDatabaseURL <$> lookupEnv "DATABASE_URL"
25+
conn <- connectPostgreSQL url
26+
conn <$ dropTables conn
27+
28+
defaultDatabaseURL :: String
29+
defaultDatabaseURL = "postgres://postgres:password@localhost:5432"

sqlite/app/Main.hs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
module Main (main) where
22

3-
import Prelude hiding (lookup)
3+
import Prelude
44

55
import Database.HDBC.Sqlite3 (connectSqlite3)
6-
import Database.Schema.Migrations.Backend.HDBC (hdbcBackend)
7-
import Moo.Core
86
import Moo.Main
9-
import System.Environment (getArgs)
10-
import System.Exit
117

128
main :: IO ()
13-
main = do
14-
args <- getArgs
15-
(_, opts, _) <- procArgs args
16-
loadedConf <- loadConfiguration $ _configFilePath opts
17-
case loadedConf of
18-
Left e -> putStrLn e >> exitFailure
19-
Right conf -> do
20-
let connectionString = _connectionString conf
21-
connection <- connectSqlite3 connectionString
22-
let
23-
backend = hdbcBackend connection
24-
parameters = makeParameters conf backend
25-
mainWithParameters args parameters
9+
main = hdbcMain connectSqlite3

sqlite/tests/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Test.Hspec
1515
deriving via (HDBCConnection Connection) instance BackendConnection Connection
1616

1717
main :: IO ()
18-
main =
19-
hspec
20-
. before (connectSqlite3 ":memory:")
21-
$ after disconnect BackendTest.spec
18+
main = hspec $ before setupSQLite3 $ after disconnect BackendTest.spec
19+
20+
setupSQLite3 :: IO Connection
21+
setupSQLite3 = connectSqlite3 ":memory:"

src/Database/Schema/Migrations/Backend.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Database.Schema.Migrations.Backend
22
( Backend (..)
33
, rootMigrationName
4+
, bootstrapIfNecessary
45
)
56
where
67

78
import Prelude
89

10+
import Control.Monad (unless)
911
import Data.Text (Text)
1012
import Database.Schema.Migrations.Migration (Migration (..))
1113

@@ -16,9 +18,9 @@ rootMigrationName :: Text
1618
rootMigrationName = "root"
1719

1820
-- | A Backend represents a database engine backend such as MySQL or
19-
-- SQLite. A Backend supplies relatively low-level functions for
21+
-- SQLite. A Backend supplies relatively low-level functions for
2022
-- inspecting the backend's state, applying migrations, and reverting
21-
-- migrations. A Backend also supplies the migration necessary to
23+
-- migrations. A Backend also supplies the migration necessary to
2224
-- "bootstrap" a backend so that it can track which migrations are
2325
-- installed.
2426
data Backend = Backend
@@ -67,3 +69,11 @@ data Backend = Backend
6769

6870
instance Show Backend where
6971
show _ = "dbmigrations backend"
72+
73+
bootstrapIfNecessary :: Backend -> IO ()
74+
bootstrapIfNecessary backend = do
75+
x <- isBootstrapped backend
76+
77+
unless x $ do
78+
bs <- getBootstrapMigration backend
79+
applyMigration backend bs

src/Database/Schema/Migrations/Backend/HDBC.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ where
66

77
import Prelude
88

9-
import Control.Exception (catch)
109
import Control.Monad (void)
10+
import Data.Foldable (traverse_)
1111
import Data.String.Conversions (cs)
1212
import Data.Text (Text)
1313
import Data.Time.Clock (getCurrentTime)
1414
import Database.HDBC
1515
( IConnection (getTables, run, runRaw)
16-
, SqlError
1716
, commit
1817
, disconnect
1918
, fromSql
@@ -83,9 +82,10 @@ newtype HDBCConnection a = HDBCConnection a
8382

8483
instance IConnection a => BackendTest.BackendConnection (HDBCConnection a) where
8584
supportsTransactionalDDL = const True
86-
makeBackend (HDBCConnection c) = hdbcBackend c
87-
commit (HDBCConnection c) = commit c
8885
withTransaction (HDBCConnection c) transaction =
8986
withTransaction c (transaction . HDBCConnection)
9087
getTables (HDBCConnection c) = map cs <$> getTables c
91-
catchAll (HDBCConnection _) act handler = act `catch` \(_ :: SqlError) -> handler
88+
dropTables (HDBCConnection c) = do
89+
ts <- getTables c
90+
traverse_ (\t -> runRaw c (cs $ "DROP TABLE " <> t)) ts
91+
makeBackend (HDBCConnection c) = hdbcBackend c

0 commit comments

Comments
 (0)