Skip to content

Commit

Permalink
fix(migrations) makes migrations more reentrant
Browse files Browse the repository at this point in the history
Improves reentrancy of migrations, by making sure each
operation has its own exception. In practice this means
avoiding constructs such as:

```
DO $$
BEGIN
  ALTER TABLE IF EXISTS ONLY "routes" ADD "name" TEXT UNIQUE;
  ALTER TABLE IF EXISTS ONLY "routes" ADD "snis" TEXT UNIQUE;
EXCEPTION WHEN DUPLICATE_COLUMN THEN
   -- Do nothing, accept existing state
END;
$$;
```

in which "name" was declared in one release candidate
and "snis" in the next. By sharing the same exception, running
this migration on the latter rc would throw on the first `ALTER
TABLE` and never run the second one.

It also ensures that `run_on` is added to the Cassandra migration
during the `up` phase.
  • Loading branch information
hishamhm committed Dec 10, 2018
1 parent 91731d4 commit 7bbae2e
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions kong/db/migrations/core/001_14_to_15.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@ return {
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "routes" ADD "name" TEXT UNIQUE;
ALTER TABLE IF EXISTS ONLY "routes" ADD "snis" TEXT[];
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS "routes" ADD "snis" TEXT[];
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "routes" ADD "sources" JSONB[];
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "routes" ADD "destinations" JSONB[];
EXCEPTION WHEN DUPLICATE_COLUMN THEN
-- Do nothing, accept existing state
Expand All @@ -21,8 +42,24 @@ return {
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "plugins"
DROP CONSTRAINT IF EXISTS "plugins_pkey",
DROP CONSTRAINT IF EXISTS "plugins_id_key",
DROP CONSTRAINT IF EXISTS "plugins_pkey";
EXCEPTION WHEN DUPLICATE_TABLE OR INVALID_TABLE_DEFINITION THEN
-- Do nothing, accept existing state
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "plugins"
DROP CONSTRAINT IF EXISTS "plugins_id_key";
EXCEPTION WHEN DUPLICATE_TABLE OR INVALID_TABLE_DEFINITION THEN
-- Do nothing, accept existing state
END;
$$;
DO $$
BEGIN
ALTER TABLE IF EXISTS ONLY "plugins"
ADD PRIMARY KEY ("id");
EXCEPTION WHEN DUPLICATE_TABLE OR INVALID_TABLE_DEFINITION THEN
-- Do nothing, accept existing state
Expand Down Expand Up @@ -196,6 +233,7 @@ return {
ALTER TABLE routes ADD snis set<text>;
ALTER TABLE routes ADD sources set<text>;
ALTER TABLE routes ADD destinations set<text>;
ALTER TABLE plugins ADD run_on text;
CREATE INDEX IF NOT EXISTS routes_name_idx ON routes(name);
Expand Down

0 comments on commit 7bbae2e

Please sign in to comment.