forked from holistics/dbml
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add referential actions support to postgres parser
- Loading branch information
Showing
8 changed files
with
953 additions
and
513 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
packages/dbml-core/__tests__/importer/postgres-importer/input/referential_actions.in.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE "orders" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"user_id" int NOT NULL, | ||
"created_at" datetime DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "order_items" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"order_id" int NOT NULL, | ||
"product_id" int DEFAULT null, | ||
"quantity" int DEFAULT 1 | ||
); | ||
|
||
CREATE TABLE "products" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"name" varchar, | ||
"price" decimal(10,4), | ||
"created_at" datetime DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "users" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"name" varchar, | ||
"email" varchar UNIQUE, | ||
"date_of_birth" datetime, | ||
"created_at" datetime DEFAULT (now()), | ||
"country_code" int NOT NULL | ||
); | ||
|
||
CREATE TABLE "countries" ( | ||
"code" int PRIMARY KEY, | ||
"name" varchar, | ||
"continent_name" varchar | ||
); | ||
|
||
ALTER TABLE "orders" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT; | ||
|
||
ALTER TABLE "order_items" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE; | ||
|
||
ALTER TABLE "order_items" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE SET NULL; | ||
|
||
ALTER TABLE "users" ADD FOREIGN KEY ("country_code") REFERENCES "countries" ("code") ON DELETE NO ACTION; |
42 changes: 42 additions & 0 deletions
42
packages/dbml-core/__tests__/importer/postgres-importer/output/referential_actions.out.dbml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Table "orders" { | ||
"id" int [pk, increment] | ||
"user_id" int [not null] | ||
"created_at" datetime [default: `now()`] | ||
} | ||
|
||
Table "order_items" { | ||
"id" int [pk, increment] | ||
"order_id" int [not null] | ||
"product_id" int [default: null] | ||
"quantity" int [default: 1] | ||
} | ||
|
||
Table "products" { | ||
"id" int [pk, increment] | ||
"name" varchar | ||
"price" "decimal(10, 4)" | ||
"created_at" datetime [default: `now()`] | ||
} | ||
|
||
Table "users" { | ||
"id" int [pk, increment] | ||
"name" varchar | ||
"email" varchar [unique] | ||
"date_of_birth" datetime | ||
"created_at" datetime [default: `now()`] | ||
"country_code" int [not null] | ||
} | ||
|
||
Table "countries" { | ||
"code" int [pk] | ||
"name" varchar | ||
"continent_name" varchar | ||
} | ||
|
||
Ref:"users"."id" < "orders"."user_id" [delete: restrict] | ||
|
||
Ref:"orders"."id" < "order_items"."order_id" [delete: cascade] | ||
|
||
Ref:"products"."id" < "order_items"."product_id" [delete: set null] | ||
|
||
Ref:"countries"."code" < "users"."country_code" [delete: no action] |
42 changes: 42 additions & 0 deletions
42
packages/dbml-core/__tests__/parser/postgres-parse/input/referential_actions.in.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE "orders" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"user_id" int NOT NULL, | ||
"created_at" datetime DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "order_items" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"order_id" int NOT NULL, | ||
"product_id" int DEFAULT null, | ||
"quantity" int DEFAULT 1 | ||
); | ||
|
||
CREATE TABLE "products" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"name" varchar, | ||
"price" decimal(10,4), | ||
"created_at" datetime DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "users" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"name" varchar, | ||
"email" varchar UNIQUE, | ||
"date_of_birth" datetime, | ||
"created_at" datetime DEFAULT (now()), | ||
"country_code" int NOT NULL | ||
); | ||
|
||
CREATE TABLE "countries" ( | ||
"code" int PRIMARY KEY, | ||
"name" varchar, | ||
"continent_name" varchar | ||
); | ||
|
||
ALTER TABLE "orders" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT; | ||
|
||
ALTER TABLE "order_items" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE CASCADE; | ||
|
||
ALTER TABLE "order_items" ADD FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE SET NULL; | ||
|
||
ALTER TABLE "users" ADD FOREIGN KEY ("country_code") REFERENCES "countries" ("code") ON DELETE NO ACTION; |
Oops, something went wrong.