Skip to content

Commit

Permalink
Merge pull request #13 from haskell-works/newhoggy/support-for-compos…
Browse files Browse the repository at this point in the history
…ite-primary-keys

Support for composite primary keys
  • Loading branch information
newhoggy authored Sep 17, 2024
2 parents b163e1d + d3acd88 commit 16b99f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion db/migration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ plan:
columns:
- name: ulid
type: CHAR(26)
primary_key: true
- name: example_data
type: BYTEA
required: true
- name: example_hash
type: CHAR(64)
unique: true
remarks: SHA-256 hash of the object data
primary_key:
- ulid
- example_hash
constraints:
- name: valid_ulid_constraint
check: ulid ~ '^[0-9A-HJKMNP-TV-Z]{26}'
Expand Down
14 changes: 13 additions & 1 deletion polysemy/Data/RdsData/Polysemy/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,15 @@ migrateUp migrationFp = do
columnClauses <- pure $
createTableStatement ^.. the @"createTable" . the @"columns" . each . to columnToText

primaryKeyClause <- pure $
createTableStatement ^.. the @"createTable" . the @"primaryKey" . _Just . to primaryKeyToText

constraintClauses <- pure $
createTableStatement ^.. the @"createTable" . the @"constraints" . _Just . each . to constraintToText

statement <- pure $ mconcat
[ "CREATE TABLE " <> createTableStatement ^. the @"createTable" . the @"name" <> " ("
, mconcat $ L.intersperse ", " (columnClauses <> constraintClauses)
, mconcat $ L.intersperse ", " (columnClauses <> primaryKeyClause <> constraintClauses)
, ");\n"
]

Expand Down Expand Up @@ -171,6 +174,15 @@ columnToText c =
]
]

primaryKeyToText :: [Text] -> Text
primaryKeyToText cs =
T.intercalate " "
[ "PRIMARY KEY"
, "("
, T.intercalate ", " cs
, ")"
]

constraintToText :: Constraint -> Text
constraintToText c =
T.intercalate " "
Expand Down
1 change: 1 addition & 0 deletions src/Data/RdsData/Migration/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ instance FromJSON CreateIndexStep where
data TableSchema = TableSchema
{ name :: Text
, columns :: [Column]
, primaryKey :: Maybe [Text]
, constraints :: Maybe [Constraint]
} deriving (Eq, Generic, Show)

Expand Down

0 comments on commit 16b99f4

Please sign in to comment.