{-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE RankNTypes #-} {- | Copyright : Flipstone Technology Partners 2023 License : MIT Stability : Stable Facilities for performing some database migrations automatically. See 'autoMigrateSchema' as a primary, high-level entry point. @since 1.0.0.0 -} module Orville.PostgreSQL.AutoMigration ( MigrationOptions (runSchemaChanges, runConcurrentIndexCreations, migrationLockId) , defaultOptions , autoMigrateSchema , SchemaItem (..) , schemaItemSummary , MigrationPlan , generateMigrationPlan , migrationPlanSteps , executeMigrationPlan , MigrationStep , MigrationDataError , MigrationLock.MigrationLockId , MigrationLock.defaultLockId , MigrationLock.nextLockId , MigrationLock.withMigrationLock , MigrationLock.MigrationLockError ) where import Control.Exception.Safe (Exception, throwIO) import Control.Monad (guard, when) import Control.Monad.IO.Class (liftIO) import Data.Foldable (traverse_) import qualified Data.List as List import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty) import qualified Data.Map.Strict as Map import qualified Data.Maybe as Maybe import qualified Data.Set as Set import qualified Data.String as String import qualified Data.Text.Encoding as Enc import qualified Database.PostgreSQL.LibPQ as LibPQ import qualified Orville.PostgreSQL as Orville import qualified Orville.PostgreSQL.Expr as Expr import qualified Orville.PostgreSQL.Internal.IndexDefinition as IndexDefinition import qualified Orville.PostgreSQL.Internal.MigrationLock as MigrationLock import qualified Orville.PostgreSQL.PgCatalog as PgCatalog import qualified Orville.PostgreSQL.Raw.RawSql as RawSql import qualified Orville.PostgreSQL.Schema as Schema {- | A 'SchemaItem' represents a single item in a database schema such as a table, index or constraint. The constructor functions below can be used to create items from other types (such as 'Orville.TableDefinition') to put them into a list to be used with 'autoMigrateSchema'. @since 1.0.0.0 -} data SchemaItem where -- | -- Constructs a 'SchemaItem' from a 'Orville.TableDefinition'. -- @since 1.0.0.0 SchemaTable :: Orville.TableDefinition key writeEntity readEntity -> SchemaItem -- | -- Constructs a 'SchemaItem' that will drop the specified table if it is -- found in the database. -- @since 1.0.0.0 SchemaDropTable :: Orville.TableIdentifier -> SchemaItem -- | -- Constructs a 'SchemaItem' from a 'Orville.SequenceDefinition'. -- @since 1.0.0.0 SchemaSequence :: Orville.SequenceDefinition -> SchemaItem -- | -- Constructs a 'SchemaItem' that will drop the specified table if it is -- found in the database. -- @since 1.0.0.0 SchemaDropSequence :: Orville.SequenceIdentifier -> SchemaItem {- | Returns a one-line string describing the 'SchemaItem', suitable for a human to identify it in a list of output. For example, a 'SchemaItem' constructed via 'SchemaTable' gives @Table