Skip to content

Commit

Permalink
Added createTableWithSchema to create tables under specific schema
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Sep 9, 2024
1 parent 92a4a50 commit bcbbd76
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
24 changes: 20 additions & 4 deletions beam-migrate/Database/Beam/Migrate/SQL/Tables.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Database.Beam.Migrate.SQL.Tables
( -- * Table manipulation

-- ** Creation and deletion
createTable, dropTable
createTable, createTableWithSchema, dropTable
, preserve

-- ** @ALTER TABLE@
Expand Down Expand Up @@ -65,16 +65,32 @@ import Lens.Micro ((^.))
-- The first argument is the name of the table.
--
-- The second argument is a table containing a 'FieldSchema' for each field.
-- See documentation on the 'Field' command for more information.c
-- See documentation on the 'Field' command for more information.
--
-- To create a table in a specific schema, see `createTableWithSchema`
createTable :: ( Beamable table, Table table
, BeamMigrateSqlBackend be )
=> Text -> TableSchema be table
-> Migration be (CheckedDatabaseEntity be db (TableEntity table))
createTable newTblName tblSettings =
createTable = createTableWithSchema Nothing

-- | Add a @CREATE TABLE@ statement to this migration, with an explicit schema
--
-- The first argument is the name of the schema, while the second argument is the name of the table.
--
-- The second argument is a table containing a 'FieldSchema' for each field.
-- See documentation on the 'Field' command for more information.c
createTableWithSchema :: ( Beamable table, Table table
, BeamMigrateSqlBackend be )
=> Maybe Text -- ^ Schema name, if any
-> Text -- ^ Table name
-> TableSchema be table
-> Migration be (CheckedDatabaseEntity be db (TableEntity table))
createTableWithSchema maybeSchemaName newTblName tblSettings =
do let pkFields = allBeamValues (\(Columnar' (TableFieldSchema name _ _)) -> name) (primaryKey tblSettings)
tblConstraints = if null pkFields then [] else [ primaryKeyConstraintSyntax pkFields ]
createTableCommand =
createTableSyntax Nothing (tableName Nothing newTblName)
createTableSyntax Nothing (tableName maybeSchemaName newTblName)
(allBeamValues (\(Columnar' (TableFieldSchema name (FieldSchema schema) _)) -> (name, schema)) tblSettings)
tblConstraints
command = createTableCmd createTableCommand
Expand Down
2 changes: 1 addition & 1 deletion docs/about/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ planned.
| **E171 SQLSTATE support** | N/A | |
| **E182 Host language binding** | N/A | |
| **F031 Basic schema manipulation** | | |
| F031-01 CREATE TABLE for persistent base tables | FULL | Use `createTable_` in `beam-migrate` |
| F031-01 CREATE TABLE for persistent base tables | FULL | Use `createTable` or `createTableWithSchema` in `beam-migrate` |
| F031-02 CREATE VIEW statement | TODO | |
| F031-03 GRANT statement | TODO | |
| F031-04 ALTER TABLE statement: ADD COLUMN clause | TODO | |
Expand Down

0 comments on commit bcbbd76

Please sign in to comment.