Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Fixed exists_table and bumped to 0.1.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ameingast committed Aug 9, 2017
1 parent a00d1d5 commit 0a1a1bb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Changelog.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.11.0
* Improved documentation
* Fixed exists_table

## 0.1.10.1
* Fixed hackage warnings

Expand Down
2 changes: 1 addition & 1 deletion postgresql-simple-migration.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: postgresql-simple-migration
version: 0.1.10.1
version: 0.1.11.0
synopsis: PostgreSQL Schema Migrations
homepage: https://github.com/ameingast/postgresql-simple-migration
Bug-reports: https://github.com/ameingast/postgresql-simple-migration/issues
Expand Down
2 changes: 1 addition & 1 deletion src/Database/PostgreSQL/Simple/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import qualified Crypto.Hash.MD5 as MD5 (hash)
import qualified Data.ByteString as BS (ByteString, readFile)
import qualified Data.ByteString.Base64 as B64 (encode)
import Data.Foldable (Foldable)
import Data.Traversable (Traversable)
import Data.List (isPrefixOf, sort)
import Data.Traversable (Traversable)
#if __GLASGOW_HASKELL__ < 710
import Data.Monoid (Monoid (..))
#endif
Expand Down
6 changes: 5 additions & 1 deletion src/Database/PostgreSQL/Simple/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ import Database.PostgreSQL.Simple (Connection, Only (..), begin,
-- | Checks if the table with the given name exists in the database.
existsTable :: Connection -> String -> IO Bool
existsTable con table =
fmap (not . null) (query con q (Only table) :: IO [[Int]])
fmap checkRowCount (query con q (Only table) :: IO [[Int]])
where
q = "select count(relname) from pg_class where relname = ?"

checkRowCount :: [[Int]] -> Bool
checkRowCount ((1:_):_) = True
checkRowCount _ = False

-- | Executes the given IO monad inside a transaction and performs a roll-back
-- afterwards (even if exceptions occur).
withTransactionRolledBack :: Connection -> IO a -> IO a
Expand Down
11 changes: 10 additions & 1 deletion test/Database/PostgreSQL/Simple/MigrationTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ migrationSpec con = describe "Migrations" $ do
let migrationDir = MigrationDirectory "share/test/scripts"
let migrationFile = MigrationFile "s.sql" "share/test/script.sql"

it "asserts that the schema_migrations table does not exist" $ do
r <- existsTable con "schema_migrations"
r `shouldBe` False

it "validates an initialization on an empty database" $ do
r <- runMigration $ MigrationContext
(MigrationValidation MigrationInitialization) False con
r `shouldBe` MigrationError "No such table: schema_migrations"

it "initializes a database" $ do
r <- runMigration $ MigrationContext MigrationInitialization False con
r `shouldBe` MigrationSuccess

it "creates the schema_migrations table" $ do
r <- existsTable con "schema_migration"
r <- existsTable con "schema_migrations"
r `shouldBe` True

it "executes a migration script" $ do
Expand Down

0 comments on commit 0a1a1bb

Please sign in to comment.