-- | Generators of registries, applied sets, and targets for property tests.module Database.DagMigrations.Gen ( TestMigration, testMigration, RegistrySpec, toRegistry, genRegistry, genDownSet, validated, isDownSet, dependencyMap, )whereimport Data.Functor.Identity (Identity (..))import Data.Map.Strict (Map)import Data.Map.Strict qualified as Mapimport Data.Set (Set)import Data.Set qualified as Setimport Data.Text (Text)import Data.Text qualified as Textimport Database.DagMigrations.Graph (MigrationGraph, downSet, linearOrder, lookupMigration, validate)import Database.DagMigrations.Migration (Migration (..))import Hedgehog (Gen, MonadTest, annotateShow, failure)import Hedgehog.Gen qualified as Genimport Hedgehog.Range qualified as Range-- | A migration whose actions do nothing.type TestMigration = Migration Text Identity-- | A reversible migration with the given name and dependencies.testMigration :: Text -> [Text] -> TestMigrationtestMigration name dependencies = Migration { migrationId = name, migrationDependencies = Set.fromList dependencies, migrationDescription = "", migrationFingerprint = Nothing, migrationUp = Identity (), migrationDown = Just (Identity ()) }-- | The name and the dependencies of each migration of a registry.-- Migrations hold actions and cannot be shown, so generators produce-- this, and 'toRegistry' builds the migrations.type RegistrySpec = [(Text, [Text])]-- | The migrations named in the spec, with their dependencies.toRegistry :: RegistrySpec -> [TestMigration]toRegistry = map (uncurry testMigration)-- | An acyclic registry of up to 12 migrations. The k-th migration depends-- on a random subset of the ones before it. The names are a random-- permutation of @m10@, @m11@, ..., so the order of the names differs from-- the order of construction.genRegistry :: Gen RegistrySpecgenRegistry = do size <- Gen.int (Range.linear 0 12) names <- Gen.shuffle [Text.pack ('m' : show k) | k <- [10 .. 9 + size]] dependencies <- traverse (\k -> Gen.subsequence (take k names)) [0 .. size - 1] pure (zip names dependencies)-- | A down-set of the graph: the union of the down-sets of a random subset.genDownSet :: MigrationGraph Text m -> Gen (Set Text)genDownSet graph = do chosen <- Gen.subsequence (linearOrder graph) pure (Set.unions (map (downSet graph) chosen))-- | The validated graph, or a test failure that shows the errors.validated :: (MonadTest t, Ord i, Show i) => [Migration i m] -> t (MigrationGraph i m)validated migrations = either (\errors -> annotateShow errors >> failure) pure (validate migrations)-- | Whether every dependency of every member of the set is in the set.isDownSet :: (Ord i) => MigrationGraph i m -> Set i -> BoolisDownSet graph ids = and [ migrationDependencies m `Set.isSubsetOf` ids | i <- Set.toList ids, Just m <- [lookupMigration i graph] ]-- | The direct dependencies of each migration of the spec.dependencyMap :: RegistrySpec -> Map Text (Set Text)dependencyMap specs = Map.fromList [(name, Set.fromList dependencies) | (name, dependencies) <- specs]