@onereach/idw-init-account-resources 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @onereach/idw-init-account-resources
2
2
 
3
+ ## 0.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - ea5a2ad: use sequelize for sql migrations
8
+
3
9
  ## 0.3.1
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ module.exports = {
40
+ up: function (queryInterface) {
41
+ return __awaiter(this, void 0, void 0, function () {
42
+ return __generator(this, function (_a) {
43
+ switch (_a.label) {
44
+ case 0: return [4, queryInterface.context.sequelize.query("\nCREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\n\n-- Function to automatically set updated_at\nCREATE OR REPLACE FUNCTION update_updated_at()\nRETURNS TRIGGER AS $$\nBEGIN\n\tNEW.updated_at = now();\n\tRETURN NEW;\nEND;\n$$ language 'plpgsql';\n\n-- Table Definition\nCREATE TABLE feed (\n \"id\" uuid NOT NULL DEFAULT uuid_generate_v4(),\n \"image_url\" varchar,\n \"title\" varchar NOT NULL,\n \"short_content\" varchar NOT NULL,\n \"content\" text NOT NULL,\n \"created_at\" timestamptz NOT NULL DEFAULT now(),\n \"updated_at\" timestamptz NOT NULL DEFAULT now(),\n \"created_by\" uuid,\n PRIMARY KEY (\"id\")\n);\n\n-- Column Comment\nCOMMENT ON COLUMN feed.id IS 'primary key';\nCOMMENT ON COLUMN feed.image_url IS 'the URL of the post in the feed. Visible in the list and the full post views';\nCOMMENT ON COLUMN feed.title IS 'title of the post';\nCOMMENT ON COLUMN feed.short_content IS 'partial content to show in the list of posts';\nCOMMENT ON COLUMN feed.content IS 'full content of the post';\nCOMMENT ON COLUMN feed.created_at IS 'timestamp when the post was created';\nCOMMENT ON COLUMN feed.updated_at IS 'timestamp when the post was last updated';\nCOMMENT ON COLUMN feed.created_by IS 'id of the person who created the post';\n\n-- Trigger to update 'updated_at' for feed table\nCREATE TRIGGER \"update_feed_updated_at\"\n BEFORE UPDATE\n ON\n feed\n FOR EACH ROW\nEXECUTE PROCEDURE update_updated_at();\n\n ")];
45
+ case 1:
46
+ _a.sent();
47
+ return [2];
48
+ }
49
+ });
50
+ });
51
+ },
52
+ down: function (queryInterface) {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ return __generator(this, function (_a) {
55
+ switch (_a.label) {
56
+ case 0: return [4, queryInterface.context.sequelize.query("\n-- TODO: Implement undo script\n-- DROP SCHEMA \"idw\" CASCADE;\n ")];
57
+ case 1:
58
+ _a.sent();
59
+ return [2];
60
+ }
61
+ });
62
+ });
63
+ },
64
+ };
65
+ //# sourceMappingURL=20240527090840-create_feed_table.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20240527090840-create_feed_table.js","sourceRoot":"","sources":["../../../src/assets/db_migrations/20240527090840-create_feed_table.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIb,MAAM,CAAC,OAAO,GAAG;IACT,EAAE,YAAC,cAAoE;;;;4BAC3E,WAAM,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,m8CA2C5C,CAAC,EAAA;;wBA3CF,SA2CE,CAAC;;;;;KAOJ;IAEK,IAAI,YAAC,cAAoE;;;;4BAC7E,WAAM,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,yEAG5C,CAAC,EAAA;;wBAHF,SAGE,CAAC;;;;;KAOJ;CACF,CAAC"}
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ module.exports = {
40
+ up: function (queryInterface) {
41
+ return __awaiter(this, void 0, void 0, function () {
42
+ return __generator(this, function (_a) {
43
+ switch (_a.label) {
44
+ case 0: return [4, queryInterface.context.sequelize.query("\nALTER TABLE \"feed\"\nADD COLUMN \"for_user_roles\" jsonb DEFAULT NULL,\nADD COLUMN \"for_user_emails\" jsonb DEFAULT NULL;\n\nCOMMENT ON COLUMN \"feed\".\"for_user_roles\" IS 'record available only for specific user roles';\nCOMMENT ON COLUMN \"feed\".\"for_user_emails\" IS 'record available only for specific users';\n\nCREATE INDEX \"feed_user_roles_gin_idx\" ON \"feed\" USING gin (for_user_roles);\nCREATE INDEX \"feed_user_emails_gin_idx\" ON \"feed\" USING gin (for_user_emails);\n\n-- example query for role includes admin or not set:\n-- SELECT * from \"idw\".\"feed\" WHERE \"for_user_roles\" IS NULL OR \"for_user_roles\" @> '[\"admin\"]';\n\n ")];
45
+ case 1:
46
+ _a.sent();
47
+ return [2];
48
+ }
49
+ });
50
+ });
51
+ },
52
+ down: function (queryInterface) {
53
+ return __awaiter(this, void 0, void 0, function () {
54
+ return __generator(this, function (_a) {
55
+ switch (_a.label) {
56
+ case 0: return [4, queryInterface.context.sequelize.query("\nALTER TABLE \"feed\"\nDROP COLUMN \"for_user_roles\",\nDROP COLUMN \"for_user_emails\";\n ")];
57
+ case 1:
58
+ _a.sent();
59
+ return [2];
60
+ }
61
+ });
62
+ });
63
+ },
64
+ };
65
+ //# sourceMappingURL=20240527090856-add_user_columns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20240527090856-add_user_columns.js","sourceRoot":"","sources":["../../../src/assets/db_migrations/20240527090856-add_user_columns.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIb,MAAM,CAAC,OAAO,GAAG;IACT,EAAE,YAAC,cAAoE;;;;4BAC3E,WAAM,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,upBAc5C,CAAC,EAAA;;wBAdF,SAcE,CAAC;;;;;KAOJ;IAEK,IAAI,YAAC,cAAoE;;;;4BAC7E,WAAM,cAAc,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,iGAI5C,CAAC,EAAA;;wBAJF,SAIE,CAAC;;;;;KAOJ;CACF,CAAC"}
@@ -50,16 +50,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.InitRdbResources = exports.schemaTableName = void 0;
54
- var postgrator_1 = __importDefault(require("postgrator"));
55
- var pg_1 = __importDefault(require("pg"));
56
- var path_1 = require("path");
53
+ exports.InitRdbResources = void 0;
54
+ var sequelize_1 = require("sequelize");
55
+ var umzug_1 = require("umzug");
56
+ var path_1 = __importDefault(require("path"));
57
57
  var pgsql_1 = require("@or-sdk/pgsql");
58
- exports.schemaTableName = '_schema_version';
59
58
  var InitRdbResources = (function () {
60
59
  function InitRdbResources(_a) {
61
60
  var token = _a.token, discoveryUrl = _a.discoveryUrl;
62
- this._migrationsTable = exports.schemaTableName;
63
61
  this.token = token;
64
62
  this.discoveryUrl = discoveryUrl;
65
63
  this.pgSql = new pgsql_1.Pgsql({
@@ -69,61 +67,32 @@ var InitRdbResources = (function () {
69
67
  }
70
68
  InitRdbResources.prototype.init = function () {
71
69
  return __awaiter(this, void 0, void 0, function () {
72
- var connectionParams, client;
70
+ var connectionParams, sequelize, umzug;
73
71
  return __generator(this, function (_a) {
74
72
  switch (_a.label) {
75
73
  case 0: return [4, this._getPgConnectionParams()];
76
74
  case 1:
77
75
  connectionParams = _a.sent();
78
- client = new pg_1.default.Client(connectionParams);
79
- return [4, this._migrate(client, connectionParams.database)];
80
- case 2:
81
- _a.sent();
82
- return [2];
83
- }
84
- });
85
- });
86
- };
87
- InitRdbResources.prototype._migrate = function (client, dbName) {
88
- return __awaiter(this, void 0, void 0, function () {
89
- var postgrator, error_1;
90
- var _this = this;
91
- return __generator(this, function (_a) {
92
- switch (_a.label) {
93
- case 0:
94
- _a.trys.push([0, 3, 4, 6]);
95
- return [4, client.connect()];
96
- case 1:
97
- _a.sent();
98
- postgrator = new postgrator_1.default({
99
- migrationPattern: (0, path_1.resolve)(__dirname, '../assets/db_migrations/*'),
100
- driver: 'pg',
101
- database: dbName !== null && dbName !== void 0 ? dbName : 'default',
102
- schemaTable: this._migrationsTable,
103
- execQuery: function (query) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
104
- switch (_a.label) {
105
- case 0: return [4, client.query(query)];
106
- case 1: return [2, _a.sent()];
107
- }
108
- }); }); },
76
+ sequelize = new sequelize_1.Sequelize({
77
+ database: connectionParams.database,
78
+ username: connectionParams.user,
79
+ password: connectionParams.password,
80
+ host: connectionParams.host,
81
+ dialect: 'postgres',
82
+ logging: false,
109
83
  });
110
- postgrator.on('validation-started', function (migration) { return console.debug('validation-started', migration); });
111
- postgrator.on('validation-finished', function (migration) { return console.debug('validation-finished', migration); });
112
- postgrator.on('migration-started', function (migration) { return console.debug('migration-started', migration); });
113
- postgrator.on('migration-finished', function (migration) { return console.debug('migration-finished', migration); });
114
- return [4, postgrator.migrate()];
84
+ umzug = new umzug_1.Umzug({
85
+ migrations: {
86
+ glob: path_1.default.join(__dirname, '../assets/db_migrations/*.{js,ts}'),
87
+ },
88
+ context: sequelize.getQueryInterface(),
89
+ storage: new umzug_1.SequelizeStorage({ sequelize: sequelize }),
90
+ logger: undefined,
91
+ });
92
+ return [4, umzug.up()];
115
93
  case 2:
116
94
  _a.sent();
117
- return [3, 6];
118
- case 3:
119
- error_1 = _a.sent();
120
- console.error(error_1, error_1.appliedMigrations);
121
- throw error_1;
122
- case 4: return [4, client.end()];
123
- case 5:
124
- _a.sent();
125
- return [7];
126
- case 6: return [2];
95
+ return [2];
127
96
  }
128
97
  });
129
98
  });
@@ -1 +1 @@
1
- {"version":3,"file":"rdb.js","sourceRoot":"","sources":["../../src/initializers/rdb.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0DAAoC;AACpC,0CAAoB;AACpB,6BAA+B;AAC/B,uCAAsC;AAGzB,QAAA,eAAe,GAAG,iBAAiB,CAAC;AAEjD;IAME,0BAAY,EAA+C;YAA7C,KAAK,WAAA,EAAE,YAAY,kBAAA;QAFhB,qBAAgB,GAAG,uBAAe,CAAC;QAGlD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QAEjC,IAAI,CAAC,KAAK,GAAG,IAAI,aAAK,CAAC;YACrB,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,YAAY,EAAE,IAAI,CAAC,YAAY;SAChC,CAAC,CAAC;IACL,CAAC;IAEY,+BAAI,GAAjB;;;;;4BAC2B,WAAM,IAAI,CAAC,sBAAsB,EAAE,EAAA;;wBAAtD,gBAAgB,GAAG,SAAmC;wBACtD,MAAM,GAAG,IAAI,YAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;wBAE/C,WAAM,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC,EAAA;;wBAAtD,SAAsD,CAAC;;;;;KACxD;IAEa,mCAAQ,GAAtB,UAAuB,MAAiB,EAAE,MAAe;;;;;;;;wBAErD,WAAM,MAAM,CAAC,OAAO,EAAE,EAAA;;wBAAtB,SAAsB,CAAC;wBAGjB,UAAU,GAAG,IAAI,oBAAU,CAAC;4BAChC,gBAAgB,EAAE,IAAA,cAAO,EAAC,SAAS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AI,EAAE,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,mCAAmC,CAAC;6BAChE;4BACD,OAAO,EAAE,SAAS,CAAC,iBAAiB,EAAE;4BACtC,OAAO,EAAE,IAAI,wBAAgB,CAAC,EAAE,SAAS,WAAA,EAAE,CAAC;4BAC5C,MAAM,EAAE,SAAS;yBAClB,CAAC,CAAC;wBACH,WAAM,KAAK,CAAC,EAAE,EAAE,EAAA;;wBAAhB,SAAgB,CAAC;;;;;KAClB;IAEa,iDAAsB,GAApC;4DAAqC,MAAsB;;YAAtB,uBAAA,EAAA,cAAsB;;;4BAEtC,WAAM,IAAI,CAAC,KAAK,CAAC,WAAW,CAAqB;4BAClE,KAAK,EAAE,qBAAqB;4BAC5B,MAAM,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE;yBAC7B,CAAC,EAAA;;wBAHM,MAAM,GAAK,CAAA,SAGjB,CAAA,OAHY;wBAKd,iCACK,MAAM,KACT,IAAI,EAAE,MAAM,CAAC,WAAW,KACxB;;;;KACH;IACH,uBAAC;AAAD,CAAC,AAlDD,IAkDC;AAlDY,4CAAgB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onereach/idw-init-account-resources",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Creation of required resources in OneReach account to enable IDW",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Antony Peklo <antony.peklo@onereach.com>",
@@ -12,8 +12,9 @@
12
12
  "@or-sdk/lookup": "1.14.5",
13
13
  "@or-sdk/pgsql": "1.1.8",
14
14
  "pg": "8.11.5",
15
- "postgrator": "7.2.0",
16
15
  "semver": "7.6.2",
16
+ "sequelize": "^6.37.3",
17
+ "umzug": "^3.8.0",
17
18
  "zod": "3.23.8"
18
19
  },
19
20
  "devDependencies": {
@@ -31,6 +32,7 @@
31
32
  "build": "rimraf dist && tsc -b",
32
33
  "build:watch": "rimraf dist && tsc -b --watch",
33
34
  "dev": "pnpm build:watch",
34
- "local": "source ./scripts/or_login.sh && bun src/local.ts"
35
+ "local": "source ./scripts/or_login.sh && bun src/local.ts",
36
+ "migration:create": "npx sequelize-cli --migrations-path src/assets/db_migrations migration:generate --name $1"
35
37
  }
36
38
  }
@@ -1,7 +1,14 @@
1
+ 'use strict';
2
+ import { Sequelize, QueryInterface } from 'sequelize';
3
+
4
+ /** @type {import('sequelize-cli').Migration} */
5
+ module.exports = {
6
+ async up(queryInterface: QueryInterface & {context: {sequelize: Sequelize;};}) {
7
+ await queryInterface.context.sequelize.query(`
1
8
  CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
2
9
 
3
10
  -- Function to automatically set updated_at
4
- CREATE FUNCTION update_updated_at()
11
+ CREATE OR REPLACE FUNCTION update_updated_at()
5
12
  RETURNS TRIGGER AS $$
6
13
  BEGIN
7
14
  NEW.updated_at = now();
@@ -39,3 +46,26 @@ CREATE TRIGGER "update_feed_updated_at"
39
46
  feed
40
47
  FOR EACH ROW
41
48
  EXECUTE PROCEDURE update_updated_at();
49
+
50
+ `);
51
+ /**
52
+ * Add altering commands here.
53
+ *
54
+ * Example:
55
+ * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
56
+ */
57
+ },
58
+
59
+ async down(queryInterface: QueryInterface & {context: {sequelize: Sequelize;};}) {
60
+ await queryInterface.context.sequelize.query(`
61
+ -- TODO: Implement undo script
62
+ -- DROP SCHEMA "idw" CASCADE;
63
+ `);
64
+ /**
65
+ * Add reverting commands here.
66
+ *
67
+ * Example:
68
+ * await queryInterface.dropTable('users');
69
+ */
70
+ },
71
+ };
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+ import { Sequelize, QueryInterface } from 'sequelize';
3
+
4
+ /** @type {import('sequelize-cli').Migration} */
5
+ module.exports = {
6
+ async up(queryInterface: QueryInterface & {context: {sequelize: Sequelize;};}) {
7
+ await queryInterface.context.sequelize.query(`
8
+ ALTER TABLE "feed"
9
+ ADD COLUMN "for_user_roles" jsonb DEFAULT NULL,
10
+ ADD COLUMN "for_user_emails" jsonb DEFAULT NULL;
11
+
12
+ COMMENT ON COLUMN "feed"."for_user_roles" IS 'record available only for specific user roles';
13
+ COMMENT ON COLUMN "feed"."for_user_emails" IS 'record available only for specific users';
14
+
15
+ CREATE INDEX "feed_user_roles_gin_idx" ON "feed" USING gin (for_user_roles);
16
+ CREATE INDEX "feed_user_emails_gin_idx" ON "feed" USING gin (for_user_emails);
17
+
18
+ -- example query for role includes admin or not set:
19
+ -- SELECT * from "idw"."feed" WHERE "for_user_roles" IS NULL OR "for_user_roles" @> '["admin"]';
20
+
21
+ `);
22
+ /**
23
+ * Add altering commands here.
24
+ *
25
+ * Example:
26
+ * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
27
+ */
28
+ },
29
+
30
+ async down(queryInterface: QueryInterface & {context: {sequelize: Sequelize;};}) {
31
+ await queryInterface.context.sequelize.query(`
32
+ ALTER TABLE "feed"
33
+ DROP COLUMN "for_user_roles",
34
+ DROP COLUMN "for_user_emails";
35
+ `);
36
+ /**
37
+ * Add reverting commands here.
38
+ *
39
+ * Example:
40
+ * await queryInterface.dropTable('users');
41
+ */
42
+ },
43
+ };
@@ -1,16 +1,14 @@
1
- import Postgrator from 'postgrator';
1
+ import { Sequelize } from 'sequelize';
2
+ import { Umzug, SequelizeStorage } from 'umzug';
2
3
  import pg from 'pg';
3
- import { resolve } from 'path';
4
+ import path from 'path';
4
5
  import { Pgsql } from '@or-sdk/pgsql';
5
6
  import { DbConnectionParams, InitRdbResourcesConfig } from '../types';
6
7
 
7
- export const schemaTableName = '_schema_version';
8
-
9
8
  export class InitRdbResources {
10
9
  private token: string;
11
10
  private discoveryUrl: string;
12
11
  private pgSql: Pgsql;
13
- private readonly _migrationsTable = schemaTableName;
14
12
 
15
13
  constructor({ token, discoveryUrl }: InitRdbResourcesConfig) {
16
14
  this.token = token;
@@ -24,40 +22,25 @@ export class InitRdbResources {
24
22
 
25
23
  public async init(): Promise<void> {
26
24
  const connectionParams = await this._getPgConnectionParams();
27
- const client = new pg.Client(connectionParams);
28
-
29
- await this._migrate(client, connectionParams.database);
30
- }
31
-
32
- private async _migrate(client: pg.Client, dbName?: string): Promise<void> {
33
- try {
34
- await client.connect();
35
-
36
- // Create postgrator instance
37
- const postgrator = new Postgrator({
38
- migrationPattern: resolve(__dirname, '../assets/db_migrations/*'),
39
- driver: 'pg',
40
- database: dbName ?? 'default',
41
- schemaTable: this._migrationsTable,
42
- execQuery: async (query) => await client.query(query),
25
+ const sequelize = new Sequelize(
26
+ {
27
+ database: connectionParams.database,
28
+ username: connectionParams.user,
29
+ password: connectionParams.password as string,
30
+ host: connectionParams.host,
31
+ dialect: 'postgres',
32
+ logging: false,
43
33
  });
44
34
 
45
- // TODO: improve logging
46
- postgrator.on('validation-started', (migration) => console.debug('validation-started', migration));
47
- postgrator.on('validation-finished', (migration) => console.debug('validation-finished', migration));
48
- postgrator.on('migration-started', (migration) => console.debug('migration-started', migration));
49
- postgrator.on('migration-finished', (migration) => console.debug('migration-finished', migration));
50
-
51
- // migrate to max version (optionally can provide 'max')
52
- await postgrator.migrate();
53
- } catch (error) {
54
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- console.error(error, (error as any).appliedMigrations); // array of migration objects
56
-
57
- throw error;
58
- } finally {
59
- await client.end();
60
- }
35
+ const umzug = new Umzug({
36
+ migrations: {
37
+ glob: path.join(__dirname, '../assets/db_migrations/*.{js,ts}'),
38
+ },
39
+ context: sequelize.getQueryInterface(),
40
+ storage: new SequelizeStorage({ sequelize }),
41
+ logger: undefined,
42
+ });
43
+ await umzug.up();
61
44
  }
62
45
 
63
46
  private async _getPgConnectionParams(dbName: string = 'idw'): Promise<pg.ClientConfig> {
@@ -1,2 +0,0 @@
1
- -- TODO: Implement undo script
2
- -- DROP SCHEMA "idw" CASCADE;
@@ -1,12 +0,0 @@
1
- ALTER TABLE "feed"
2
- ADD COLUMN "for_user_roles" jsonb DEFAULT NULL,
3
- ADD COLUMN "for_user_emails" jsonb DEFAULT NULL;
4
-
5
- COMMENT ON COLUMN "feed"."for_user_roles" IS 'record available only for specific user roles';
6
- COMMENT ON COLUMN "feed"."for_user_emails" IS 'record available only for specific users';
7
-
8
- CREATE INDEX "feed_user_roles_gin_idx" ON "feed" USING gin (for_user_roles);
9
- CREATE INDEX "feed_user_emails_gin_idx" ON "feed" USING gin (for_user_emails);
10
-
11
- -- example query for role includes admin or not set:
12
- -- SELECT * from "idw"."feed" WHERE "for_user_roles" IS NULL OR "for_user_roles" @> '["admin"]';
@@ -1,3 +0,0 @@
1
- ALTER TABLE "feed"
2
- DROP COLUMN "for_user_roles",
3
- DROP COLUMN "for_user_emails";