Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Housekeeping, implement better SQLite DDL, resolve pesky bugs #114

Closed
wants to merge 11 commits into from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ cockroach-data/
migrations/schema.sql
vendor/
sqldumps/
sql/
sql/
tsoda
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
14 changes: 3 additions & 11 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,18 +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
mssqlserver:
image: "microsoft/mssql-server-linux"
environment:
- SA_PASSWORD=Tt@12345678
- MSSQLSERVER_PASSWORD=Tt@12345678
- ACCEPT_EULA=Y
ports:
- "1433:1433"
command: start-single-node --insecure
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module github.com/gobuffalo/fizz

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/go-sql-driver/mysql v1.6.0
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/gobuffalo/packd v1.0.1/go.mod h1:PP2POP3p3RXGz7Jh6eYEf93S7vA2za6xM7QT
github.com/gobuffalo/plush/v4 v4.0.0/go.mod h1:ErFS3UxKqEb8fpFJT7lYErfN/Nw6vHGiDMTjxpk5bQ0=
github.com/gobuffalo/plush/v4 v4.1.9 h1:u9rQBuYCeHC0ppKxsZljk5vb1oT8PQa5EMNTAN2337s=
github.com/gobuffalo/plush/v4 v4.1.9/go.mod h1:9OOII9uAM5pZnhWu1OkQnboXJjaWMQ7kcTl3zNcxvTM=
github.com/gobuffalo/pop/v6 v6.0.0 h1:LvHl9wrMoY0zF+SWjUJoDG1oRpYKfMxLzuBT7SCctZU=
github.com/gobuffalo/pop/v6 v6.0.0/go.mod h1:5rd3OnViLhjteR8+0i/mT9Q4CzkTzCoR7tm/9mmAic4=
github.com/gobuffalo/pop/v6 v6.0.0-20211203104334-793d5869e4b6 h1:JxoNaqV4sON19RHPH72ouzgu0zYbz/XivnRCeDG6xeU=
github.com/gobuffalo/pop/v6 v6.0.0-20211203104334-793d5869e4b6/go.mod h1:5rd3OnViLhjteR8+0i/mT9Q4CzkTzCoR7tm/9mmAic4=
github.com/gobuffalo/tags/v3 v3.0.2/go.mod h1:ZQeN6TCTiwAFnS0dNcbDtSgZDwNKSpqajvVtt6mlYpA=
github.com/gobuffalo/tags/v3 v3.1.2 h1:68sHcwFFDstXyfbk5ovbGcQFDsupgVLs+lw1XZinHJw=
github.com/gobuffalo/tags/v3 v3.1.2/go.mod h1:o3ldUfKv50jxWAC8eZHXMm8dnKW3YvyZUMr0xqUcZTI=
Expand Down Expand Up @@ -311,7 +311,6 @@ github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
Expand Down
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
44 changes: 28 additions & 16 deletions internal/e2e/fixtures/cockroach/down/10.sql
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
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
-- ## 239
CREATE TABLE public.e2e_users (
id UUID NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
name VARCHAR(255) NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
FAMILY "primary" (id, created_at, updated_at)
FAMILY "primary" (id, created_at, updated_at, name)
);

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
47 changes: 29 additions & 18 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,
slug VARCHAR(64) 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)
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
FAMILY "primary" (id, content, user_id, slug)
);

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
-- ## 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_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
-- ## 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
47 changes: 29 additions & 18 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
-- ## 352
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,
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 (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)
INDEX e2e_user_notes_user_id_idx (user_id ASC),
FAMILY "primary" (id, content, slug, user_id)
);

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
-- ## 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
Loading