Skip to content

Commit

Permalink
Support adding foreign key constraints in add_column
Browse files Browse the repository at this point in the history
This patch reverts some of the logic previously introduced for SQLite. It adds new functionality to `add_column`, allowing to define a foreign key when creating a column. This works for all databases and adds support for introducing foreign keys to SQLite schemas after a table has been created (not possible previously).

This patch also upgrade PostgreSQL and CockroachDB and MySQL to supported versions as the versions used previously were no longer maintained.
  • Loading branch information
aeneasr committed Dec 3, 2021
1 parent 3ca9f55 commit f90d9b2
Show file tree
Hide file tree
Showing 145 changed files with 2,325 additions and 1,078 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:

services:
postgres:
image: postgres:14
image: postgres:10.19
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
Expand Down Expand Up @@ -132,9 +132,9 @@ jobs:
run: |
mkdir -p crdb
pushd crdb
wget -qO- https://binaries.cockroachdb.com/cockroach-v2.1.0.linux-amd64.tgz | tar -xz
sudo cp -i cockroach-v2.1.0.linux-amd64/cockroach /usr/local/bin/
cockroach start --insecure --background
wget -qO- https://binaries.cockroachdb.com/cockroach-v21.2.2.linux-amd64.tgz | tar -xz
sudo cp -i cockroach-v21.2.2.linux-amd64/cockroach /usr/local/bin/
cockroach start-single-node --insecure --background
popd
- name: Install and run soda
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ For example for PostgreSQL you could pass `jsonb`and it will be supported, howev
#### Supported Options:

* `size` - The size of the column. For example if you wanted a `varchar(50)` in Postgres you would do: `t.Column("column_name", "string", {"size": 50})`
* `null` - By default columns are not allowed to be `null`.
* `null` - If set to `true` will mark the field as `NULL`able. Otherwise, the column is `NOT NULL` (e.g. if not set or `false`).
* `default` - The default value you want for this column. By default this is `null`.
* `default_raw` - The default value defined as a database function.
* `after` - (MySQL Only) Add a column after another column in the table. `example: {"after":"created_at"}`
Expand Down Expand Up @@ -95,6 +95,20 @@ add_column("table_name", "column_name", "string", {})

See [above](#column-info) for more details on column types and options.

You can also add a column with a foreign key constraint. This is particularly
useful for SQLite which does not support foreign keys constraint updates without
recreating the table:

```
add_column("table_name", "column_name", "string", {"null": true, "size": 50, "foreign_key": {
"table": "target_table_name",
"columns": ["target_column", "target_column_2", "target_column_3"],
"name": "the_index_name",
"on_delete": "RESTRICT",
"on_update": "NO ACTION"
}})
```

## Alter a column

``` javascript
Expand Down Expand Up @@ -165,7 +179,6 @@ add_foreign_key("table_name", "field", {"ref_table_name": ["ref_column"]}, {
"on_delete": "action",
"on_update": "action",
})

```

#### Supported Options
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
ports:
- "3306:3306"
postgres:
image: postgres:9.6
image: postgres:10.19
environment:
- POSTGRES_DB=pop_test
- POSTGRES_PASSWORD=postgres
Expand All @@ -22,10 +22,10 @@ services:
volumes:
- ./sqldumps:/docker-entrypoint-initdb.d
cockroach:
image: cockroachdb/cockroach:v19.2.10
image: cockroachdb/cockroach:v21.1.2
ports:
- "26257:26257"
- "8080:8080"
volumes:
- "./cockroach-data/roach1:/cockroach/cockroach-data"
command: start --insecure
command: start-single-node --insecure
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ go 1.16

replace github.com/mattn/go-sqlite3 => github.com/mattn/go-sqlite3 v1.14.9

replace github.com/gobuffalo/pop/v6 => github.com/gobuffalo/pop/v6 v6.0.0-20211203104334-793d5869e4b6

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/cockroachdb/cockroach-go v2.0.1+incompatible // indirect
github.com/go-sql-driver/mysql v1.6.0
github.com/gobuffalo/genny v0.6.0 // indirect
github.com/gobuffalo/packr/v2 v2.8.3 // indirect
github.com/gobuffalo/plush/v4 v4.1.9
github.com/gobuffalo/pop v4.13.1+incompatible // indirect
github.com/gobuffalo/pop/v6 v6.0.0
github.com/gobuffalo/validate v2.0.4+incompatible // indirect
github.com/jackc/pgx/v4 v4.14.0
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/stretchr/testify v1.7.0
Expand Down
86 changes: 2 additions & 84 deletions go.sum

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion internal/e2e/fixtures/cockroach/down/0.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
CREATE TABLE schema_migration (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # 1 row
17 changes: 7 additions & 10 deletions internal/e2e/fixtures/cockroach/down/1.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
CREATE TABLE e2e_users (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
username VARCHAR(255) NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at, username)
);

CREATE TABLE schema_migration (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # 1 row
41 changes: 26 additions & 15 deletions internal/e2e/fixtures/cockroach/down/10.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
CREATE TABLE e2e_users (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # row 2
-- ## 208
CREATE TABLE public.e2e_users (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at)
);

CREATE TABLE e2e_user_posts (
-- # row 3
-- ## 352
CREATE TABLE public.e2e_user_posts (
id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
user_id UUID NOT NULL,
slug VARCHAR(64) NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
INDEX e2e_user_notes_auto_index_e2e_user_notes_e2e_users_id_fk (user_id ASC),
INDEX e2e_user_notes_user_id_idx (user_id ASC),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
FAMILY "primary" (id, content, user_id, slug)
);

CREATE TABLE schema_migration (
version VARCHAR(14) NOT NULL,
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);

ALTER TABLE e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES e2e_users(id) ON DELETE CASCADE;

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # row 4
-- ## 152
ALTER TABLE public.e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES public.e2e_users(id) ON DELETE CASCADE;
-- # row 5
-- ## 115
-- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
-- # row 6
-- ## 85
ALTER TABLE public.e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # 6 rows
41 changes: 26 additions & 15 deletions internal/e2e/fixtures/cockroach/down/11.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
CREATE TABLE e2e_users (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # row 2
-- ## 208
CREATE TABLE public.e2e_users (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at)
);

CREATE TABLE e2e_user_posts (
-- # row 3
-- ## 352
CREATE TABLE public.e2e_user_posts (
id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) NOT NULL,
user_id UUID NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
INDEX e2e_user_notes_auto_index_e2e_user_notes_e2e_users_id_fk (user_id ASC),
INDEX e2e_user_notes_user_id_idx (user_id ASC),
FAMILY "primary" (id, content, slug, user_id)
);

CREATE TABLE schema_migration (
version VARCHAR(14) NOT NULL,
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);

ALTER TABLE e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES e2e_users(id) ON DELETE CASCADE;

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # row 4
-- ## 152
ALTER TABLE public.e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES public.e2e_users(id) ON DELETE CASCADE;
-- # row 5
-- ## 115
-- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
-- # row 6
-- ## 85
ALTER TABLE public.e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # 6 rows
41 changes: 26 additions & 15 deletions internal/e2e/fixtures/cockroach/down/12.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
CREATE TABLE e2e_authors (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # row 2
-- ## 210
CREATE TABLE public.e2e_authors (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at)
);

CREATE TABLE e2e_user_posts (
-- # row 3
-- ## 352
CREATE TABLE public.e2e_user_posts (
id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) NOT NULL,
user_id UUID NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
INDEX e2e_user_notes_auto_index_e2e_user_notes_e2e_users_id_fk (user_id ASC),
INDEX e2e_user_notes_user_id_idx (user_id ASC),
FAMILY "primary" (id, content, slug, user_id)
);

CREATE TABLE schema_migration (
version VARCHAR(14) NOT NULL,
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);

ALTER TABLE e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES e2e_authors(id) ON DELETE CASCADE;

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # row 4
-- ## 154
ALTER TABLE public.e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (user_id) REFERENCES public.e2e_authors(id) ON DELETE CASCADE;
-- # row 5
-- ## 115
-- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
-- # row 6
-- ## 85
ALTER TABLE public.e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # 6 rows
41 changes: 26 additions & 15 deletions internal/e2e/fixtures/cockroach/down/13.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
CREATE TABLE e2e_authors (
-- # 1 column
-- # row 1
-- ## 269
CREATE TABLE public.schema_migration (
version VARCHAR(14) NOT NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);
-- # row 2
-- ## 210
CREATE TABLE public.e2e_authors (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at)
);

CREATE TABLE e2e_user_posts (
-- # row 3
-- ## 358
CREATE TABLE public.e2e_user_posts (
id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) NOT NULL,
author_id UUID NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
INDEX e2e_user_notes_auto_index_e2e_user_notes_e2e_users_id_fk (author_id ASC),
INDEX e2e_user_notes_user_id_idx (author_id ASC),
FAMILY "primary" (id, content, slug, author_id)
);

CREATE TABLE schema_migration (
version VARCHAR(14) NOT NULL,
UNIQUE INDEX schema_migration_version_idx (version ASC),
FAMILY "primary" (version, rowid)
);

ALTER TABLE e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (author_id) REFERENCES e2e_authors(id) ON DELETE CASCADE;

-- Validate foreign key constraints. These can fail if there was unvalidated data during the dump.
ALTER TABLE e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # row 4
-- ## 156
ALTER TABLE public.e2e_user_posts ADD CONSTRAINT e2e_user_notes_e2e_users_id_fk FOREIGN KEY (author_id) REFERENCES public.e2e_authors(id) ON DELETE CASCADE;
-- # row 5
-- ## 115
-- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
-- # row 6
-- ## 85
ALTER TABLE public.e2e_user_posts VALIDATE CONSTRAINT e2e_user_notes_e2e_users_id_fk;
-- # 6 rows
Loading

0 comments on commit f90d9b2

Please sign in to comment.