Skip to content

Commit

Permalink
Respect foreign keys on CockroachDB change_column (#97)
Browse files Browse the repository at this point in the history
* Add failing test cases for CockroachDB

* Do not fail fixture mismatches when REFRESH_FIXTURES is set

* Respect foreign keys on CockroachDB change_column
  • Loading branch information
aeneasr authored Sep 1, 2020
1 parent 0226681 commit eec5de9
Show file tree
Hide file tree
Showing 75 changed files with 1,586 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ make test
```

If you made changes to the end-to-end tests and want to update the fixtures,
run:
run the following command a couple of times until tests pass:

```
REFRESH_FIXTURES=true make test
Expand Down
5 changes: 2 additions & 3 deletions internal/e2e/fixtures/cockroach/down/10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ CREATE TABLE e2e_users (

CREATE TABLE e2e_user_posts (
id UUID NOT NULL,
user_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, user_id, content, slug)
FAMILY "primary" (id, content, user_id, slug)
);

CREATE TABLE schema_migration (
Expand Down
29 changes: 29 additions & 0 deletions internal/e2e/fixtures/cockroach/down/11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TABLE 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 (
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_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;
29 changes: 29 additions & 0 deletions internal/e2e/fixtures/cockroach/down/12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TABLE 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 (
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_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;
29 changes: 29 additions & 0 deletions internal/e2e/fixtures/cockroach/down/13.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TABLE 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 (
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_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;
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CREATE TABLE e2e_user_notes (
notes VARCHAR(255) NULL,
title VARCHAR(64) NOT NULL DEFAULT '':::STRING,
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),
INDEX e2e_user_notes_title_idx (title ASC),
FAMILY "primary" (id, user_id, notes, title)
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ CREATE TABLE e2e_user_notes (
user_id UUID NOT NULL,
notes VARCHAR(255) 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),
FAMILY "primary" (id, user_id, notes)
);
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CREATE TABLE e2e_user_notes (
slug VARCHAR(64) NOT NULL,
notes VARCHAR(255) 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, user_id, slug, notes)
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CREATE TABLE e2e_user_notes (
slug VARCHAR(64) NOT NULL,
notes VARCHAR(255) 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, user_id, slug, notes)
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/6.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CREATE TABLE e2e_user_notes (
slug VARCHAR(64) NOT NULL,
notes VARCHAR(255) 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, user_id, slug, notes)
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/fixtures/cockroach/down/7.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ CREATE TABLE e2e_user_posts (
slug VARCHAR(64) NOT NULL,
notes VARCHAR(255) 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, user_id, slug, notes)
Expand Down
5 changes: 2 additions & 3 deletions internal/e2e/fixtures/cockroach/down/8.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ CREATE TABLE e2e_users (

CREATE TABLE e2e_user_posts (
id UUID NOT NULL,
user_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, user_id, content, slug)
FAMILY "primary" (id, content, user_id, slug)
);

CREATE TABLE schema_migration (
Expand Down
5 changes: 2 additions & 3 deletions internal/e2e/fixtures/cockroach/down/9.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ CREATE TABLE e2e_users (

CREATE TABLE e2e_user_posts (
id UUID NOT NULL,
user_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, user_id, content, slug)
FAMILY "primary" (id, content, user_id, slug)
);

CREATE TABLE schema_migration (
Expand Down
30 changes: 30 additions & 0 deletions internal/e2e/fixtures/cockroach/up/11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE 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 (
id UUID NOT NULL,
user_id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) 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, user_id, content, 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_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;
30 changes: 30 additions & 0 deletions internal/e2e/fixtures/cockroach/up/12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE 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 (
id UUID NOT NULL,
author_id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id 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),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
FAMILY "primary" (id, author_id, content, 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 (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;
30 changes: 30 additions & 0 deletions internal/e2e/fixtures/cockroach/up/13.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE 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 (
id UUID NOT NULL,
content VARCHAR(255) NOT NULL DEFAULT '':::STRING,
slug VARCHAR(32) NOT NULL,
published BOOL NOT NULL DEFAULT false,
author_id UUID NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC),
UNIQUE INDEX e2e_user_notes_slug_idx (slug ASC),
INDEX e2e_user_notes_user_id_idx (author_id ASC),
FAMILY "primary" (id, content, slug, published, 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;
4 changes: 2 additions & 2 deletions internal/e2e/fixtures/mysql/down/0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
--
-- Host: 127.0.0.1 Database: pop_test
-- ------------------------------------------------------
-- Server version 5.7.30
-- Server version 5.7.31

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
Expand Down Expand Up @@ -37,4 +37,4 @@ CREATE TABLE `schema_migration` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-06-20 14:38:23
-- Dump completed on 2020-08-30 23:11:40
2 changes: 1 addition & 1 deletion internal/e2e/fixtures/mysql/down/1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ CREATE TABLE `schema_migration` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-08-27 23:39:25
-- Dump completed on 2020-08-30 23:11:40
2 changes: 1 addition & 1 deletion internal/e2e/fixtures/mysql/down/10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ CREATE TABLE `schema_migration` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-08-27 23:39:23
-- Dump completed on 2020-08-30 23:11:36
74 changes: 74 additions & 0 deletions internal/e2e/fixtures/mysql/down/11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- MySQL dump 10.13 Distrib 5.7.29, for macos10.14 (x86_64)
--
-- Host: 127.0.0.1 Database: pop_test
-- ------------------------------------------------------
-- Server version 5.7.31

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `e2e_user_posts`
--

DROP TABLE IF EXISTS `e2e_user_posts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `e2e_user_posts` (
`id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`slug` varchar(32) NOT NULL,
`content` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `e2e_user_notes_slug_idx` (`slug`),
KEY `e2e_user_notes_user_id_idx` (`user_id`),
CONSTRAINT `e2e_user_posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `e2e_users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `e2e_users`
--

DROP TABLE IF EXISTS `e2e_users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `e2e_users` (
`id` char(36) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Table structure for table `schema_migration`
--

DROP TABLE IF EXISTS `schema_migration`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `schema_migration` (
`version` varchar(14) NOT NULL,
UNIQUE KEY `schema_migration_version_idx` (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-08-30 23:11:36
Loading

0 comments on commit eec5de9

Please sign in to comment.