packages feed

dag-migrations-postgresql-simple-0.1.0.0 / test / Database / DagMigrations / PostgreSQL / ExampleSpec.hs

-- | Runs the example project of "Database.DagMigrations.PostgreSQL.Example"-- against the test server, and checks what each of its migrations does.module Database.DagMigrations.PostgreSQL.ExampleSpec (spec) whereimport Data.List.NonEmpty (NonEmpty (..))import Data.Map.Strict qualified as Mapimport Data.Set qualified as Setimport Data.Text (Text)import Database.DagMigrations (Direction (..), Inspection (..), PlanError (..), RunError (..), Target (..))import Database.DagMigrations.PostgreSQL.Example (ProjectError (..), inspectProject, migrateProject)import Database.DagMigrations.PostgreSQL.TestDatabaseimport Database.PostgreSQL.Simple (Connection, Only (..), execute_, query_)import Test.Hspec-- | The cases of the example project.spec :: TestServer -> Specspec server = describe "the example project" $ do  it "migrates an empty database, moves each view's filter to its own column, and normalizes view names" $    onExample server $ \connection -> do      migrateProject connection Up (TargetMigration addViewFilters) `shouldReturn` Right ()      _ <- execute_ connection "INSERT INTO saved_views (name, definition) VALUES (' Open ', 'title,owner|status = open')"      migrateProject connection Up AllMigrations `shouldReturn` Right ()      views connection `shouldReturn` [("open", "title,owner", Just "status = open")]      inspectProject connection        `shouldReturn` Right (Inspection (map (,True) allMigrations) Set.empty [] Map.empty)  it "fails the data migration, names every view that is not of version 1, and changes nothing" $    onExample server $ \connection -> do      migrateProject connection Up (TargetMigration addViewFilters) `shouldReturn` Right ()      _ <- execute_ connection "INSERT INTO saved_views (name, definition) VALUES ('a', 'title|status = open'), ('b', 'title,owner'), ('c', 'owner')"      migrateProject connection Up AllMigrations        `shouldReturn` Left (RunFailed (MigrationFailed splitViewFilters Up (Right "saved views whose definition is not of version 1: \"b\", \"c\"")))      views connection `shouldReturn` [("a", "title|status = open", Nothing), ("b", "title,owner", Nothing), ("c", "owner", Nothing)]      fmap inspectedStatus <$> inspectProject connection        `shouldReturn` Right (zip allMigrations [True, True, False, False])  it "fails the name migration when two names normalize to the same name, and changes nothing" $    onExample server $ \connection -> do      migrateProject connection Up (TargetMigration createViews) `shouldReturn` Right ()      _ <- execute_ connection "INSERT INTO saved_views (name, definition) VALUES (' Open ', 'a'), ('open', 'b'), ('Closed', 'c')"      migrateProject connection Up (TargetMigration normalizeViewNames)        `shouldReturn` Left (RunFailed (MigrationFailed normalizeViewNames Up (Right "saved views whose names normalize to the same name: \" Open \", \"open\"")))      query_ connection "SELECT name FROM saved_views ORDER BY name COLLATE \"C\"" `shouldReturn` map Only [" Open ", "Closed", "open" :: Text]  it "reverts a schema migration" $    onExample server $ \connection -> do      migrateProject connection Up (TargetMigration addViewFilters) `shouldReturn` Right ()      migrateProject connection Down (TargetMigration createViews) `shouldReturn` Right ()      query_ connection "SELECT count(*) FROM information_schema.columns WHERE table_name = 'saved_views' AND column_name = 'filter'"        `shouldReturn` [Only (0 :: Int)]  it "refuses to revert past the migrations that cannot be reverted" $    onExample server $ \connection -> do      migrateProject connection Up AllMigrations `shouldReturn` Right ()      migrateProject connection Down (TargetMigration createViews)        `shouldReturn` Left (RunFailed (PlanFailed (Irreversible normalizeViewNames :| [Irreversible splitViewFilters])))      fmap inspectedStatus <$> inspectProject connection `shouldReturn` Right (map (,True) allMigrations)-- | The identifiers of the example's migrations, in the order in which the-- runner applies them.allMigrations :: [Text]allMigrations = [createViews, addViewFilters, splitViewFilters, normalizeViewNames]createViews, addViewFilters, splitViewFilters, normalizeViewNames :: TextcreateViews = "2022-11-27_22-28-18_create-views"addViewFilters = "2026-03-08_00-00-00_add-view-filters"splitViewFilters = "2026-03-09_00-00-00_split-view-filters"normalizeViewNames = "2026-03-10_00-00-00_normalize-view-names"-- | Opens a connection to a new empty database, and passes the connection-- to the action.onExample :: TestServer -> (Connection -> IO a) -> IO aonExample server use = withTestDatabase server (`withConnection` use)-- | The name, the definition, and the filter of every saved view, by name.views :: Connection -> IO [(Text, Text, Maybe Text)]views connection = query_ connection "SELECT name, definition, filter FROM saved_views ORDER BY name"