From 424402cd83a9dd2e9605c0224ab4012f435f7110 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sat, 12 Sep 2020 13:53:50 +0300 Subject: [PATCH] feat: migrations structure and example Refs: https://github.com/metarhia/metasql/issues/23 --- test/schema/.database.js | 16 +++++++++++++++ .../.history/2020-09-11-v1/.database.js | 14 +++++++++++++ test/schema/.history/2020-09-11-v1/City.js | 4 ++++ test/schema/.history/2020-09-11-v1/Country.js | 3 +++ .../.history/2020-09-11-v2/.database.js | 14 +++++++++++++ test/schema/.history/2020-09-11-v2/City.js | 4 ++++ test/schema/.history/2020-09-11-v2/Country.js | 4 ++++ .../schema/.history/2020-09-11-v2/District.js | 4 ++++ test/schema/.history/2020-09-11-v2/Planet.js | 3 +++ test/schema/.migrations/2020-09-11-v1-dn.sql | 5 +++++ test/schema/.migrations/2020-09-11-v1-up.sql | 20 +++++++++++++++++++ test/schema/.migrations/2020-09-11-v2-dn.sql | 5 +++++ test/schema/.migrations/2020-09-11-v2-up.sql | 19 ++++++++++++++++++ 13 files changed, 115 insertions(+) create mode 100644 test/schema/.database.js create mode 100644 test/schema/.history/2020-09-11-v1/.database.js create mode 100644 test/schema/.history/2020-09-11-v1/City.js create mode 100644 test/schema/.history/2020-09-11-v1/Country.js create mode 100644 test/schema/.history/2020-09-11-v2/.database.js create mode 100644 test/schema/.history/2020-09-11-v2/City.js create mode 100644 test/schema/.history/2020-09-11-v2/Country.js create mode 100644 test/schema/.history/2020-09-11-v2/District.js create mode 100644 test/schema/.history/2020-09-11-v2/Planet.js create mode 100644 test/schema/.migrations/2020-09-11-v1-dn.sql create mode 100644 test/schema/.migrations/2020-09-11-v1-up.sql create mode 100644 test/schema/.migrations/2020-09-11-v2-dn.sql create mode 100644 test/schema/.migrations/2020-09-11-v2-up.sql diff --git a/test/schema/.database.js b/test/schema/.database.js new file mode 100644 index 0000000..d1da83c --- /dev/null +++ b/test/schema/.database.js @@ -0,0 +1,16 @@ +({ + name: 'example', + description: 'Example database schema', + version: 3, + + authors: [ + { name: 'Timur Shemsedinov', email: 'timur.shemsedinov@gmail.com' }, + ], + + extensions: [ + 'hstore', + 'postgis', + 'postgis_topology', + 'pg_trgm', + ] +}); diff --git a/test/schema/.history/2020-09-11-v1/.database.js b/test/schema/.history/2020-09-11-v1/.database.js new file mode 100644 index 0000000..8650718 --- /dev/null +++ b/test/schema/.history/2020-09-11-v1/.database.js @@ -0,0 +1,14 @@ +({ + name: 'example', + description: 'Example database schema', + version: 1, + + authors: [ + { name: 'Timur Shemsedinov', email: 'timur.shemsedinov@gmail.com' }, + ], + + extensions: [ + 'hstore', + 'pg_trgm', + ] +}); diff --git a/test/schema/.history/2020-09-11-v1/City.js b/test/schema/.history/2020-09-11-v1/City.js new file mode 100644 index 0000000..836c64f --- /dev/null +++ b/test/schema/.history/2020-09-11-v1/City.js @@ -0,0 +1,4 @@ +({ + country: 'Country', + name: { type: 'string', unique: true }, +}); diff --git a/test/schema/.history/2020-09-11-v1/Country.js b/test/schema/.history/2020-09-11-v1/Country.js new file mode 100644 index 0000000..915d8e5 --- /dev/null +++ b/test/schema/.history/2020-09-11-v1/Country.js @@ -0,0 +1,3 @@ +({ + name: { type: 'string', unique: true }, +}); diff --git a/test/schema/.history/2020-09-11-v2/.database.js b/test/schema/.history/2020-09-11-v2/.database.js new file mode 100644 index 0000000..901a475 --- /dev/null +++ b/test/schema/.history/2020-09-11-v2/.database.js @@ -0,0 +1,14 @@ +({ + name: 'example', + description: 'Example database schema', + version: 2, + + authors: [ + { name: 'Timur Shemsedinov', email: 'timur.shemsedinov@gmail.com' }, + ], + + extensions: [ + 'hstore', + 'pg_trgm', + ] +}); diff --git a/test/schema/.history/2020-09-11-v2/City.js b/test/schema/.history/2020-09-11-v2/City.js new file mode 100644 index 0000000..836c64f --- /dev/null +++ b/test/schema/.history/2020-09-11-v2/City.js @@ -0,0 +1,4 @@ +({ + country: 'Country', + name: { type: 'string', unique: true }, +}); diff --git a/test/schema/.history/2020-09-11-v2/Country.js b/test/schema/.history/2020-09-11-v2/Country.js new file mode 100644 index 0000000..bf9dca5 --- /dev/null +++ b/test/schema/.history/2020-09-11-v2/Country.js @@ -0,0 +1,4 @@ +({ + planet: 'Planet', + name: { type: 'string', unique: true }, +}); diff --git a/test/schema/.history/2020-09-11-v2/District.js b/test/schema/.history/2020-09-11-v2/District.js new file mode 100644 index 0000000..98e9895 --- /dev/null +++ b/test/schema/.history/2020-09-11-v2/District.js @@ -0,0 +1,4 @@ +({ + city: 'City', + name: 'string', +}); diff --git a/test/schema/.history/2020-09-11-v2/Planet.js b/test/schema/.history/2020-09-11-v2/Planet.js new file mode 100644 index 0000000..d062d87 --- /dev/null +++ b/test/schema/.history/2020-09-11-v2/Planet.js @@ -0,0 +1,3 @@ +({ + name: 'string', +}); diff --git a/test/schema/.migrations/2020-09-11-v1-dn.sql b/test/schema/.migrations/2020-09-11-v1-dn.sql new file mode 100644 index 0000000..e2f677d --- /dev/null +++ b/test/schema/.migrations/2020-09-11-v1-dn.sql @@ -0,0 +1,5 @@ +DROP TABLE "Country"; +DROP TABLE "City"; + +DROP EXTENSION hstore; +DROP EXTENSION pg_trgm; diff --git a/test/schema/.migrations/2020-09-11-v1-up.sql b/test/schema/.migrations/2020-09-11-v1-up.sql new file mode 100644 index 0000000..e8b5d70 --- /dev/null +++ b/test/schema/.migrations/2020-09-11-v1-up.sql @@ -0,0 +1,20 @@ +CREATE EXTENSION hstore; +CREATE EXTENSION pg_trgm; + +CREATE TABLE "Country" ( + "countryId" bigint generated always as identity, + "name" varchar NOT NULL +); + +ALTER TABLE "Country" ADD CONSTRAINT "pkCountry" PRIMARY KEY ("countryId"); +CREATE UNIQUE INDEX "akCountryName" ON "Country" ("name"); + +CREATE TABLE "City" ( + "cityId" bigint generated always as identity, + "countryId" bigint NOT NULL, + "name" varchar NOT NULL +); + +ALTER TABLE "City" ADD CONSTRAINT "pkCity" PRIMARY KEY ("cityId"); +ALTER TABLE "City" ADD CONSTRAINT "fkCityCountry" FOREIGN KEY ("countryId") REFERENCES "Country" ("countryId") ON DELETE CASCADE; +CREATE UNIQUE INDEX "akCityName" ON "City" ("name"); diff --git a/test/schema/.migrations/2020-09-11-v2-dn.sql b/test/schema/.migrations/2020-09-11-v2-dn.sql new file mode 100644 index 0000000..ac88c1f --- /dev/null +++ b/test/schema/.migrations/2020-09-11-v2-dn.sql @@ -0,0 +1,5 @@ +DROP TABLE "District"; + +ALTER TABLE "Country" DROP COLUMN "planetId"; + +DROP TABLE "Planet"; diff --git a/test/schema/.migrations/2020-09-11-v2-up.sql b/test/schema/.migrations/2020-09-11-v2-up.sql new file mode 100644 index 0000000..e058616 --- /dev/null +++ b/test/schema/.migrations/2020-09-11-v2-up.sql @@ -0,0 +1,19 @@ +CREATE TABLE "Planet" ( + "planetId" bigint generated always as identity, + "name" varchar NOT NULL +); + +ALTER TABLE "Planet" ADD CONSTRAINT "pkPlanet" PRIMARY KEY ("planetId"); + +ALTER TABLE "Country" ADD COLUMN "planetId" bigint NOT NULL ); + +ALTER TABLE "Country" ADD CONSTRAINT "fkCountryPlanet" FOREIGN KEY ("planetId") REFERENCES "Planet" ("planetId") ON DELETE CASCADE; + +CREATE TABLE "District" ( + "districtId" bigint generated always as identity, + "cityId" bigint NOT NULL, + "name" varchar NOT NULL +); + +ALTER TABLE "District" ADD CONSTRAINT "pkDistrict" PRIMARY KEY ("districtId"); +ALTER TABLE "District" ADD CONSTRAINT "fkDistrictCity" FOREIGN KEY ("cityId") REFERENCES "City" ("cityId") ON DELETE CASCADE;