Skip to content

Feature: Add --no-clobber switch to dbml2sql #884

@cnagel-wti

Description

@cnagel-wti

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

CREATE SCHEMA "mySchema"

--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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions