Skip to content

Commit 0a1a1bb

Browse files
committed
Fixed exists_table and bumped to 0.1.11.0
1 parent a00d1d5 commit 0a1a1bb

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

Changelog.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.1.11.0
4+
* Improved documentation
5+
* Fixed exists_table
6+
37
## 0.1.10.1
48
* Fixed hackage warnings
59

postgresql-simple-migration.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: postgresql-simple-migration
2-
version: 0.1.10.1
2+
version: 0.1.11.0
33
synopsis: PostgreSQL Schema Migrations
44
homepage: https://github.com/ameingast/postgresql-simple-migration
55
Bug-reports: https://github.com/ameingast/postgresql-simple-migration/issues

src/Database/PostgreSQL/Simple/Migration.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import qualified Crypto.Hash.MD5 as MD5 (hash)
4747
import qualified Data.ByteString as BS (ByteString, readFile)
4848
import qualified Data.ByteString.Base64 as B64 (encode)
4949
import Data.Foldable (Foldable)
50-
import Data.Traversable (Traversable)
5150
import Data.List (isPrefixOf, sort)
51+
import Data.Traversable (Traversable)
5252
#if __GLASGOW_HASKELL__ < 710
5353
import Data.Monoid (Monoid (..))
5454
#endif

src/Database/PostgreSQL/Simple/Util.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ import Database.PostgreSQL.Simple (Connection, Only (..), begin,
2525
-- | Checks if the table with the given name exists in the database.
2626
existsTable :: Connection -> String -> IO Bool
2727
existsTable con table =
28-
fmap (not . null) (query con q (Only table) :: IO [[Int]])
28+
fmap checkRowCount (query con q (Only table) :: IO [[Int]])
2929
where
3030
q = "select count(relname) from pg_class where relname = ?"
3131

32+
checkRowCount :: [[Int]] -> Bool
33+
checkRowCount ((1:_):_) = True
34+
checkRowCount _ = False
35+
3236
-- | Executes the given IO monad inside a transaction and performs a roll-back
3337
-- afterwards (even if exceptions occur).
3438
withTransactionRolledBack :: Connection -> IO a -> IO a

test/Database/PostgreSQL/Simple/MigrationTest.hs

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@ migrationSpec con = describe "Migrations" $ do
3333
let migrationDir = MigrationDirectory "share/test/scripts"
3434
let migrationFile = MigrationFile "s.sql" "share/test/script.sql"
3535

36+
it "asserts that the schema_migrations table does not exist" $ do
37+
r <- existsTable con "schema_migrations"
38+
r `shouldBe` False
39+
40+
it "validates an initialization on an empty database" $ do
41+
r <- runMigration $ MigrationContext
42+
(MigrationValidation MigrationInitialization) False con
43+
r `shouldBe` MigrationError "No such table: schema_migrations"
44+
3645
it "initializes a database" $ do
3746
r <- runMigration $ MigrationContext MigrationInitialization False con
3847
r `shouldBe` MigrationSuccess
3948

4049
it "creates the schema_migrations table" $ do
41-
r <- existsTable con "schema_migration"
50+
r <- existsTable con "schema_migrations"
4251
r `shouldBe` True
4352

4453
it "executes a migration script" $ do

0 commit comments

Comments
 (0)