IHP Api Reference
Copyright(c) digitally induced GmbH 2021
Safe HaskellSafe-Inferred

IHP.IDE.CodeGen.MigrationGenerator

Description

 
Synopsis

Documentation

dumpAppDatabaseSchema :: Text -> IO Text Source #

Returns the DDL statements of the locally running dev db

Basically does the same as make dumpdb but returns the output as a string

unqualifyExpression :: Text -> Expression -> Expression Source #

Replaces table.field with just field

>>> unqualifyExpression "servers" (sql "SELECT * FROM servers WHERE servers.is_public")
sql "SELECT * FROM servers WHERE is_public"

normalizePrimaryKeys :: [Statement] -> [Statement] Source #

Removes ALTER TABLE .. ADD CONSTRAINT .._pkey PRIMARY KEY (id); and moves it into the CreateTable field of the CreateTable statement

pg_dump dumps a table like this:

CREATE TABLE a (
    id uuid DEFAULT uuid_generate_v4() NOT NULL
);

ALTER TABLE a ADD CONSTRAINT users_pkey PRIMARY KEY (id);

This function basically removes the ALTER TABLE statements and moves the primary key directly into the CREATE TABLE statement:

CREATE TABLE a (
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
);

removeImplicitDeletions :: [Statement] -> [Statement] -> [Statement] Source #

Removes DROP INDEX .. statements and other that appear after a DROP TABLE statement. The DROP TABLE .. statement itself already removes indexes and foreigns keys on that table. So an DROP INDEX .. would then fail.

Shrinks a sequence like this:

DROP TABLE a;
DROP INDEX some_index_on_table_a;
ALTER TABLE a DROP CONSTRAINT some_constraint_on_table_a;

Into this:

DROP TABLE a;

disableTransactionWhileAddingEnumValues :: [Statement] -> [Statement] Source #

Moves statements that add enum values outside the database transaction

When IHP generates a migration that contains a statement like this:

ALTER TYPE my_enum ADD VALUE 'some_value';

the migration will fail with this error:

Query (89.238182ms): "BEGIN" ()
migrate: SqlError {sqlState = "25001", sqlExecStatus = FatalError, sqlErrorMsg = "ALTER TYPE ... ADD cannot run inside a transaction block", sqlErrorDetail = "", sqlErrorHint = ""}

This function moves the ADD VALUE statement outside the main database transaction:

COMMIT; -- Commit the transaction previously started by IHP
ALTER TYPE my_enum ADD VALUE 'some_value';
BEGIN; -- Restart the connection as IHP will also try to run it's own COMMIT

truncateIdentifier :: Text -> Text Source #

Postgres truncates identifiers longer than 63 characters.

This function truncates a Text to 63 chars max. This way we avoid unnecssary changes in the generated migrations.