The SQL generated from DBML assumes it is going into an empty database. It might be useful to include a --no-clobber flag in the command to modify the output to add protective clauses to the SQL.
For example, instead of
--no-clobber would emit
CREATE SCHEMA IF NOT EXISTS "mySchema"
Some object types require a more comprehensive template than iF NOT EXISTS:
DO $$
BEGIN
CREATE TYPE "mySchema"."email_type" AS ENUM (
'PERSONAL',
'WORK',
'OTHER'
);
EXCEPTION
WHEN duplicate_object THEN NULL;
END $$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conrelid = 'mySchema.email'::regclass
AND contype = 'f'
AND conname IN ('fk_email_contact', 'vip_contact_id_fkey')
) THEN
ALTER TABLE "identity"."email"
ADD CONSTRAINT "fk_email_contact"
FOREIGN KEY ("contact_id") REFERENCES "identity"."customer" ("id") DEFERRABLE INITIALLY IMMEDIATE;
END IF;
END $$;
NOTE: In order to protect constraints in this way, DBML would need to name them deterministically so the names could be used in the protective query shown above.
The SQL generated from DBML assumes it is going into an empty database. It might be useful to include a
--no-clobberflag in the command to modify the output to add protective clauses to the SQL.For example, instead of
--no-clobberwould emitSome object types require a more comprehensive template than
iF NOT EXISTS:NOTE: In order to protect constraints in this way, DBML would need to name them deterministically so the names could be used in the protective query shown above.