Feature: Add --no-clobber switch to dbml2sql
- Dominant language
- JavaScript
- Stars
- 3.7k
- Forks
- 233
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 4
Description
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
```sql
CREATE SCHEMA "mySchema"
```
`--no-clobber` would emit
```sql
CREATE SCHEMA IF NOT EXISTS "mySchema"
```
Some object types require a more comprehensive template than `iF NOT EXISTS`:
```sql
DO $$
BEGIN
CREATE TYPE "mySchema"."email_type" AS ENUM (
'PERSONAL',
'WORK',
'OTHER'
);
EXCEPTION
WHEN duplicate_object THEN NULL;
END $$;
```
```sql
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._
Contributor guide
Assessment
This issue has not been assessed yet.