Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions testdata/diff/create_table/add_fk/diff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ALTER TABLE books
ADD CONSTRAINT books_author_id_fkey FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE;

ALTER TABLE employees
ADD CONSTRAINT employees_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id);

ALTER TABLE nodes
ADD CONSTRAINT nodes_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES nodes (id);

ALTER TABLE orders
ADD CONSTRAINT orders_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customers (id);

ALTER TABLE orders
ADD CONSTRAINT orders_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES managers (id) ON DELETE SET NULL;

ALTER TABLE orders
ADD CONSTRAINT orders_product_id_fkey FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE;

ALTER TABLE products
ADD CONSTRAINT products_category_code_fkey FOREIGN KEY (category_code) REFERENCES categories (code) ON UPDATE CASCADE;

ALTER TABLE projects
ADD CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id);

ALTER TABLE teams
ADD CONSTRAINT teams_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES managers (id) ON DELETE SET NULL;

ALTER TABLE user_profiles
ADD CONSTRAINT user_profiles_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) DEFERRABLE INITIALLY DEFERRED;
117 changes: 117 additions & 0 deletions testdata/diff/create_table/add_fk/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
-- Simple single-column FK case
CREATE TABLE public.departments (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT departments_pkey PRIMARY KEY (id)
);

CREATE TABLE public.employees (
id integer NOT NULL,
name text NOT NULL,
department_id integer NOT NULL,
CONSTRAINT employees_pkey PRIMARY KEY (id),
CONSTRAINT employees_department_id_fkey FOREIGN KEY (department_id) REFERENCES public.departments(id)
);

-- Composite FK case
CREATE TABLE public.organizations (
tenant_id integer NOT NULL,
org_id integer NOT NULL,
org_name text NOT NULL,
CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id)
);

CREATE TABLE public.projects (
id integer NOT NULL,
project_name text NOT NULL,
tenant_id integer NOT NULL,
org_id integer NOT NULL,
CONSTRAINT projects_pkey PRIMARY KEY (id),
CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES public.organizations(tenant_id, org_id)
);

-- FK with ON DELETE CASCADE case
CREATE TABLE public.authors (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT authors_pkey PRIMARY KEY (id)
);

CREATE TABLE public.books (
id integer NOT NULL,
title text NOT NULL,
author_id integer NOT NULL,
CONSTRAINT books_pkey PRIMARY KEY (id),
CONSTRAINT books_author_id_fkey FOREIGN KEY (author_id) REFERENCES public.authors(id) ON DELETE CASCADE
);

-- FK with ON UPDATE CASCADE case
CREATE TABLE public.categories (
code text NOT NULL,
name text NOT NULL,
CONSTRAINT categories_pkey PRIMARY KEY (code)
);

CREATE TABLE public.products (
id integer NOT NULL,
name text NOT NULL,
category_code text NOT NULL,
CONSTRAINT products_pkey PRIMARY KEY (id),
CONSTRAINT products_category_code_fkey FOREIGN KEY (category_code) REFERENCES public.categories(code) ON UPDATE CASCADE
);

-- FK with ON DELETE SET NULL case
CREATE TABLE public.managers (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT managers_pkey PRIMARY KEY (id)
);

CREATE TABLE public.teams (
id integer NOT NULL,
name text NOT NULL,
manager_id integer,
CONSTRAINT teams_pkey PRIMARY KEY (id),
CONSTRAINT teams_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES public.managers(id) ON DELETE SET NULL
);

-- FK with DEFERRABLE case
CREATE TABLE public.users (
id integer NOT NULL,
username text NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (id)
);

CREATE TABLE public.user_profiles (
user_id integer NOT NULL,
bio text,
CONSTRAINT user_profiles_pkey PRIMARY KEY (user_id),
CONSTRAINT user_profiles_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(id) DEFERRABLE INITIALLY DEFERRED
);

-- Self-referencing FK case
CREATE TABLE public.nodes (
id integer NOT NULL,
name text NOT NULL,
parent_id integer,
CONSTRAINT nodes_pkey PRIMARY KEY (id),
CONSTRAINT nodes_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES public.nodes(id)
);

-- Multiple FKs in a single table case
CREATE TABLE public.customers (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT customers_pkey PRIMARY KEY (id)
);

CREATE TABLE public.orders (
id integer NOT NULL,
customer_id integer NOT NULL,
product_id integer NOT NULL,
manager_id integer,
CONSTRAINT orders_pkey PRIMARY KEY (id),
CONSTRAINT orders_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES public.customers(id),
CONSTRAINT orders_product_id_fkey FOREIGN KEY (product_id) REFERENCES public.products(id) ON DELETE CASCADE,
CONSTRAINT orders_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES public.managers(id) ON DELETE SET NULL
);
107 changes: 107 additions & 0 deletions testdata/diff/create_table/add_fk/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
-- Simple single-column FK case
CREATE TABLE public.departments (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT departments_pkey PRIMARY KEY (id)
);

CREATE TABLE public.employees (
id integer NOT NULL,
name text NOT NULL,
department_id integer NOT NULL,
CONSTRAINT employees_pkey PRIMARY KEY (id)
);

-- Composite FK case
CREATE TABLE public.organizations (
tenant_id integer NOT NULL,
org_id integer NOT NULL,
org_name text NOT NULL,
CONSTRAINT organizations_pkey PRIMARY KEY (tenant_id, org_id)
);

CREATE TABLE public.projects (
id integer NOT NULL,
project_name text NOT NULL,
tenant_id integer NOT NULL,
org_id integer NOT NULL,
CONSTRAINT projects_pkey PRIMARY KEY (id)
);

-- FK with ON DELETE CASCADE case
CREATE TABLE public.authors (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT authors_pkey PRIMARY KEY (id)
);

CREATE TABLE public.books (
id integer NOT NULL,
title text NOT NULL,
author_id integer NOT NULL,
CONSTRAINT books_pkey PRIMARY KEY (id)
);

-- FK with ON UPDATE CASCADE case
CREATE TABLE public.categories (
code text NOT NULL,
name text NOT NULL,
CONSTRAINT categories_pkey PRIMARY KEY (code)
);

CREATE TABLE public.products (
id integer NOT NULL,
name text NOT NULL,
category_code text NOT NULL,
CONSTRAINT products_pkey PRIMARY KEY (id)
);

-- FK with ON DELETE SET NULL case
CREATE TABLE public.managers (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT managers_pkey PRIMARY KEY (id)
);

CREATE TABLE public.teams (
id integer NOT NULL,
name text NOT NULL,
manager_id integer,
CONSTRAINT teams_pkey PRIMARY KEY (id)
);

-- FK with DEFERRABLE case
CREATE TABLE public.users (
id integer NOT NULL,
username text NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (id)
);

CREATE TABLE public.user_profiles (
user_id integer NOT NULL,
bio text,
CONSTRAINT user_profiles_pkey PRIMARY KEY (user_id)
);

-- Self-referencing FK case
CREATE TABLE public.nodes (
id integer NOT NULL,
name text NOT NULL,
parent_id integer,
CONSTRAINT nodes_pkey PRIMARY KEY (id)
);

-- Multiple FKs in a single table case
CREATE TABLE public.customers (
id integer NOT NULL,
name text NOT NULL,
CONSTRAINT customers_pkey PRIMARY KEY (id)
);

CREATE TABLE public.orders (
id integer NOT NULL,
customer_id integer NOT NULL,
product_id integer NOT NULL,
manager_id integer,
CONSTRAINT orders_pkey PRIMARY KEY (id)
);
134 changes: 134 additions & 0 deletions testdata/diff/create_table/add_fk/plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"version": "1.0.0",
"pgschema_version": "1.4.2",
"created_at": "1970-01-01T00:00:00Z",
"source_fingerprint": {
"hash": "a2aaf409600a24c35c0310b73f6ff938ddf34a2eea5f7fbad576df693e6060d9"
},
"groups": [
{
"steps": [
{
"sql": "ALTER TABLE books\nADD CONSTRAINT books_author_id_fkey FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.books.books_author_id_fkey"
},
{
"sql": "ALTER TABLE books VALIDATE CONSTRAINT books_author_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.books.books_author_id_fkey"
},
{
"sql": "ALTER TABLE employees\nADD CONSTRAINT employees_department_id_fkey FOREIGN KEY (department_id) REFERENCES departments (id) NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.employees.employees_department_id_fkey"
},
{
"sql": "ALTER TABLE employees VALIDATE CONSTRAINT employees_department_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.employees.employees_department_id_fkey"
},
{
"sql": "ALTER TABLE nodes\nADD CONSTRAINT nodes_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES nodes (id) NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.nodes.nodes_parent_id_fkey"
},
{
"sql": "ALTER TABLE nodes VALIDATE CONSTRAINT nodes_parent_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.nodes.nodes_parent_id_fkey"
},
{
"sql": "ALTER TABLE orders\nADD CONSTRAINT orders_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_customer_id_fkey"
},
{
"sql": "ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_customer_id_fkey"
},
{
"sql": "ALTER TABLE orders\nADD CONSTRAINT orders_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES managers (id) ON DELETE SET NULL NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_manager_id_fkey"
},
{
"sql": "ALTER TABLE orders VALIDATE CONSTRAINT orders_manager_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_manager_id_fkey"
},
{
"sql": "ALTER TABLE orders\nADD CONSTRAINT orders_product_id_fkey FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_product_id_fkey"
},
{
"sql": "ALTER TABLE orders VALIDATE CONSTRAINT orders_product_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.orders.orders_product_id_fkey"
},
{
"sql": "ALTER TABLE products\nADD CONSTRAINT products_category_code_fkey FOREIGN KEY (category_code) REFERENCES categories (code) ON UPDATE CASCADE NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.products.products_category_code_fkey"
},
{
"sql": "ALTER TABLE products VALIDATE CONSTRAINT products_category_code_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.products.products_category_code_fkey"
},
{
"sql": "ALTER TABLE projects\nADD CONSTRAINT projects_tenant_id_org_id_fkey FOREIGN KEY (tenant_id, org_id) REFERENCES organizations (tenant_id, org_id) NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.projects.projects_tenant_id_org_id_fkey"
},
{
"sql": "ALTER TABLE projects VALIDATE CONSTRAINT projects_tenant_id_org_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.projects.projects_tenant_id_org_id_fkey"
},
{
"sql": "ALTER TABLE teams\nADD CONSTRAINT teams_manager_id_fkey FOREIGN KEY (manager_id) REFERENCES managers (id) ON DELETE SET NULL NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.teams.teams_manager_id_fkey"
},
{
"sql": "ALTER TABLE teams VALIDATE CONSTRAINT teams_manager_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.teams.teams_manager_id_fkey"
},
{
"sql": "ALTER TABLE user_profiles\nADD CONSTRAINT user_profiles_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id) DEFERRABLE INITIALLY DEFERRED NOT VALID;",
"type": "table.constraint",
"operation": "create",
"path": "public.user_profiles.user_profiles_user_id_fkey"
},
{
"sql": "ALTER TABLE user_profiles VALIDATE CONSTRAINT user_profiles_user_id_fkey;",
"type": "table.constraint",
"operation": "create",
"path": "public.user_profiles.user_profiles_user_id_fkey"
}
]
}
]
}
Loading