@loomcore/api 0.1.18 → 0.1.19
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/dist/__tests__/postgres.test-database.js +5 -4
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +2 -2
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +7 -18
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +2 -2
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +9 -9
- package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +1 -1
- package/dist/databases/postgres/migrations/003-create-users-table.migration.js +13 -29
- package/dist/databases/postgres/migrations/004-create-refresh-tokens-table.migration.d.ts +1 -1
- package/dist/databases/postgres/migrations/004-create-refresh-tokens-table.migration.js +12 -28
- package/dist/databases/postgres/migrations/005-create-meta-org.migration.d.ts +2 -2
- package/dist/databases/postgres/migrations/005-create-meta-org.migration.js +8 -8
- package/dist/databases/postgres/migrations/006-create-admin-user.migration.d.ts +1 -1
- package/dist/databases/postgres/migrations/006-create-admin-user.migration.js +5 -7
- package/dist/databases/postgres/migrations/migration.interface.d.ts +2 -2
- package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +1 -1
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +4 -4
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +10 -10
- package/package.json +1 -1
|
@@ -6,7 +6,7 @@ import { setupDatabaseForMultitenant } from '../databases/postgres/migrations/se
|
|
|
6
6
|
import { setupDatabaseForAuth } from '../databases/postgres/migrations/setup-for-auth.migration.js';
|
|
7
7
|
import { runTestMigrations } from './postgres-test-migrations/run-test-migrations.js';
|
|
8
8
|
import { PostgresDatabase } from '../databases/postgres/postgres.database.js';
|
|
9
|
-
import {
|
|
9
|
+
import { getSystemUserContext } from '@loomcore/common/models';
|
|
10
10
|
export class TestPostgresDatabase {
|
|
11
11
|
database = null;
|
|
12
12
|
postgresClient = null;
|
|
@@ -33,11 +33,13 @@ export class TestPostgresDatabase {
|
|
|
33
33
|
if (!success) {
|
|
34
34
|
throw new Error('Failed to setup for multitenant');
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
await initSystemUserContext(this.database);
|
|
37
|
+
success = (await setupDatabaseForAuth(postgresClient, adminUsername || 'admin', adminPassword || 'password')).success;
|
|
37
38
|
if (!success) {
|
|
38
39
|
throw new Error('Failed to setup for auth');
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
+
const systemUserContext = getSystemUserContext();
|
|
42
|
+
success = (await runTestMigrations(postgresClient, systemUserContext._orgId)).success;
|
|
41
43
|
if (!success) {
|
|
42
44
|
throw new Error('Failed to run test migrations');
|
|
43
45
|
}
|
|
@@ -45,7 +47,6 @@ export class TestPostgresDatabase {
|
|
|
45
47
|
await this.createIndexes(postgresClient);
|
|
46
48
|
await testUtils.createMetaOrg();
|
|
47
49
|
}
|
|
48
|
-
await initSystemUserContext(this.database);
|
|
49
50
|
return this.database;
|
|
50
51
|
}
|
|
51
52
|
async createIndexes(client) {
|
|
@@ -4,14 +4,14 @@ export declare class CreateMigrationTableMigration implements IMigration {
|
|
|
4
4
|
private readonly client;
|
|
5
5
|
constructor(client: Client);
|
|
6
6
|
index: number;
|
|
7
|
-
execute(
|
|
7
|
+
execute(): Promise<{
|
|
8
8
|
success: boolean;
|
|
9
9
|
error: Error;
|
|
10
10
|
} | {
|
|
11
11
|
success: boolean;
|
|
12
12
|
error: null;
|
|
13
13
|
}>;
|
|
14
|
-
revert(
|
|
14
|
+
revert(): Promise<{
|
|
15
15
|
success: boolean;
|
|
16
16
|
error: Error;
|
|
17
17
|
} | {
|
|
@@ -5,13 +5,12 @@ export class CreateMigrationTableMigration {
|
|
|
5
5
|
this.client = client;
|
|
6
6
|
}
|
|
7
7
|
index = 1;
|
|
8
|
-
async execute(
|
|
8
|
+
async execute() {
|
|
9
9
|
const _id = randomUUID().toString();
|
|
10
10
|
try {
|
|
11
11
|
await this.client.query(`
|
|
12
12
|
CREATE TABLE "migrations" (
|
|
13
13
|
"_id" VARCHAR(255) PRIMARY KEY,
|
|
14
|
-
"_orgId" VARCHAR(255),
|
|
15
14
|
"index" INTEGER NOT NULL,
|
|
16
15
|
"hasRun" BOOLEAN NOT NULL,
|
|
17
16
|
"reverted" BOOLEAN NOT NULL
|
|
@@ -24,22 +23,12 @@ export class CreateMigrationTableMigration {
|
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
try {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
VALUES ('${_id}', '${_orgId}', ${this.index}, TRUE, FALSE);`);
|
|
31
|
-
if (result.rowCount === 0) {
|
|
32
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
const result = await this.client.query(`
|
|
37
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
38
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
26
|
+
const result = await this.client.query(`
|
|
27
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
28
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
39
29
|
`);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
30
|
+
if (result.rowCount === 0) {
|
|
31
|
+
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
43
32
|
}
|
|
44
33
|
}
|
|
45
34
|
catch (error) {
|
|
@@ -47,7 +36,7 @@ export class CreateMigrationTableMigration {
|
|
|
47
36
|
}
|
|
48
37
|
return { success: true, error: null };
|
|
49
38
|
}
|
|
50
|
-
async revert(
|
|
39
|
+
async revert() {
|
|
51
40
|
try {
|
|
52
41
|
const result = await this.client.query(`
|
|
53
42
|
DROP TABLE "migrations";
|
|
@@ -6,14 +6,14 @@ export declare class CreateOrganizationsTableMigration implements IMigration {
|
|
|
6
6
|
private readonly orgCode;
|
|
7
7
|
constructor(client: Client, orgName: string, orgCode: string);
|
|
8
8
|
index: number;
|
|
9
|
-
execute(
|
|
9
|
+
execute(): Promise<{
|
|
10
10
|
success: boolean;
|
|
11
11
|
error: Error;
|
|
12
12
|
} | {
|
|
13
13
|
success: boolean;
|
|
14
14
|
error: null;
|
|
15
15
|
}>;
|
|
16
|
-
revert(
|
|
16
|
+
revert(): Promise<{
|
|
17
17
|
success: boolean;
|
|
18
18
|
error: Error;
|
|
19
19
|
} | {
|
|
@@ -9,7 +9,7 @@ export class CreateOrganizationsTableMigration {
|
|
|
9
9
|
this.orgCode = orgCode;
|
|
10
10
|
}
|
|
11
11
|
index = 2;
|
|
12
|
-
async execute(
|
|
12
|
+
async execute() {
|
|
13
13
|
const _id = randomUUID().toString();
|
|
14
14
|
try {
|
|
15
15
|
await this.client.query(`
|
|
@@ -40,8 +40,8 @@ export class CreateOrganizationsTableMigration {
|
|
|
40
40
|
}
|
|
41
41
|
try {
|
|
42
42
|
const result = await this.client.query(`
|
|
43
|
-
INSERT INTO "migrations" ("_id", "
|
|
44
|
-
VALUES ('${_id}',
|
|
43
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
44
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
45
45
|
`);
|
|
46
46
|
if (result.rowCount === 0) {
|
|
47
47
|
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
@@ -52,7 +52,7 @@ export class CreateOrganizationsTableMigration {
|
|
|
52
52
|
}
|
|
53
53
|
return { success: true, error: null };
|
|
54
54
|
}
|
|
55
|
-
async revert(
|
|
55
|
+
async revert() {
|
|
56
56
|
try {
|
|
57
57
|
const result = await this.client.query(`
|
|
58
58
|
DROP TABLE "organizations";
|
|
@@ -62,21 +62,21 @@ export class CreateOrganizationsTableMigration {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
catch (error) {
|
|
65
|
-
return { success: false, error: new Error(`Error dropping organizations table
|
|
65
|
+
return { success: false, error: new Error(`Error dropping organizations table: ${error.message}`) };
|
|
66
66
|
}
|
|
67
67
|
try {
|
|
68
68
|
const result = await this.client.query(`
|
|
69
|
-
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}'
|
|
69
|
+
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
70
70
|
`);
|
|
71
71
|
if (result.rowCount === 0) {
|
|
72
72
|
return {
|
|
73
|
-
success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
74
|
-
Migration index: ${this.index}
|
|
73
|
+
success: false, error: new Error(`Error updating migration record for index ${this.index}: Migration record not found.
|
|
74
|
+
Migration index: ${this.index}`)
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
catch (error) {
|
|
79
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
79
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: ${error.message}`) };
|
|
80
80
|
}
|
|
81
81
|
return { success: true, error: null };
|
|
82
82
|
}
|
|
@@ -37,37 +37,21 @@ export class CreateUsersTableMigration {
|
|
|
37
37
|
return { success: false, error: new Error(`Error creating users table: ${error.message}`) };
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
40
|
+
try {
|
|
41
|
+
const result = await this.client.query(`
|
|
42
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
43
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
44
|
+
`);
|
|
45
|
+
if (result.rowCount === 0) {
|
|
46
|
+
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
52
47
|
}
|
|
53
48
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const result = await this.client.query(`
|
|
57
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
58
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
59
|
-
`);
|
|
60
|
-
if (result.rowCount === 0) {
|
|
61
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
catch (error) {
|
|
65
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
66
|
-
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
67
51
|
}
|
|
68
52
|
return { success: true, error: null };
|
|
69
53
|
}
|
|
70
|
-
async revert(
|
|
54
|
+
async revert() {
|
|
71
55
|
try {
|
|
72
56
|
const result = await this.client.query(`
|
|
73
57
|
DROP TABLE "users";
|
|
@@ -81,14 +65,14 @@ export class CreateUsersTableMigration {
|
|
|
81
65
|
}
|
|
82
66
|
try {
|
|
83
67
|
const result = await this.client.query(`
|
|
84
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}'
|
|
68
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
85
69
|
`);
|
|
86
70
|
if (result.rowCount === 0) {
|
|
87
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
71
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
|
|
88
72
|
}
|
|
89
73
|
}
|
|
90
74
|
catch (error) {
|
|
91
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
75
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: ${error.message}`) };
|
|
92
76
|
}
|
|
93
77
|
return { success: true, error: null };
|
|
94
78
|
}
|
|
@@ -29,37 +29,21 @@ export class CreateRefreshTokenTableMigration {
|
|
|
29
29
|
return { success: false, error: new Error(`Error creating refresh token table: ${error.message}`) };
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
32
|
+
try {
|
|
33
|
+
const result = await this.client.query(`
|
|
34
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
35
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
36
|
+
`);
|
|
37
|
+
if (result.rowCount === 0) {
|
|
38
|
+
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
44
39
|
}
|
|
45
40
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const result = await this.client.query(`
|
|
49
|
-
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
50
|
-
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
51
|
-
`);
|
|
52
|
-
if (result.rowCount === 0) {
|
|
53
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
58
|
-
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: ${error.message}`) };
|
|
59
43
|
}
|
|
60
44
|
return { success: true, error: null };
|
|
61
45
|
}
|
|
62
|
-
async revert(
|
|
46
|
+
async revert() {
|
|
63
47
|
try {
|
|
64
48
|
const result = await this.client.query(`
|
|
65
49
|
DROP TABLE "refreshTokens";
|
|
@@ -73,10 +57,10 @@ export class CreateRefreshTokenTableMigration {
|
|
|
73
57
|
}
|
|
74
58
|
try {
|
|
75
59
|
const result = await this.client.query(`
|
|
76
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}'
|
|
60
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
77
61
|
`);
|
|
78
62
|
if (result.rowCount === 0) {
|
|
79
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
63
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
|
|
80
64
|
}
|
|
81
65
|
}
|
|
82
66
|
catch (error) {
|
|
@@ -6,7 +6,7 @@ export declare class CreateMetaOrgMigration implements IMigration {
|
|
|
6
6
|
private readonly orgCode;
|
|
7
7
|
constructor(client: Client, orgName: string, orgCode: string);
|
|
8
8
|
index: number;
|
|
9
|
-
execute(
|
|
9
|
+
execute(): Promise<{
|
|
10
10
|
success: boolean;
|
|
11
11
|
error: Error;
|
|
12
12
|
metaOrgId?: undefined;
|
|
@@ -15,7 +15,7 @@ export declare class CreateMetaOrgMigration implements IMigration {
|
|
|
15
15
|
metaOrgId: string;
|
|
16
16
|
error: null;
|
|
17
17
|
}>;
|
|
18
|
-
revert(
|
|
18
|
+
revert(): Promise<{
|
|
19
19
|
success: boolean;
|
|
20
20
|
error: Error;
|
|
21
21
|
} | {
|
|
@@ -9,7 +9,7 @@ export class CreateMetaOrgMigration {
|
|
|
9
9
|
this.orgCode = orgCode;
|
|
10
10
|
}
|
|
11
11
|
index = 5;
|
|
12
|
-
async execute(
|
|
12
|
+
async execute() {
|
|
13
13
|
const _id = randomUUID().toString();
|
|
14
14
|
try {
|
|
15
15
|
const result = await this.client.query(`
|
|
@@ -24,8 +24,8 @@ export class CreateMetaOrgMigration {
|
|
|
24
24
|
}
|
|
25
25
|
try {
|
|
26
26
|
const result = await this.client.query(`
|
|
27
|
-
INSERT INTO "migrations" ("_id", "
|
|
28
|
-
VALUES ('${_id}',
|
|
27
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
28
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
29
29
|
`);
|
|
30
30
|
if (result.rowCount === 0) {
|
|
31
31
|
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
@@ -36,9 +36,9 @@ export class CreateMetaOrgMigration {
|
|
|
36
36
|
}
|
|
37
37
|
return { success: true, metaOrgId: _id, error: null };
|
|
38
38
|
}
|
|
39
|
-
async revert(
|
|
39
|
+
async revert() {
|
|
40
40
|
try {
|
|
41
|
-
const result = await this.client.query(`DELETE FROM "organizations" WHERE "
|
|
41
|
+
const result = await this.client.query(`DELETE FROM "organizations" WHERE "isMetaOrg" = TRUE;`);
|
|
42
42
|
if (result.rowCount === 0) {
|
|
43
43
|
return { success: false, error: new Error(`Error reverting meta org: No row returned`) };
|
|
44
44
|
}
|
|
@@ -48,14 +48,14 @@ export class CreateMetaOrgMigration {
|
|
|
48
48
|
}
|
|
49
49
|
try {
|
|
50
50
|
const result = await this.client.query(`
|
|
51
|
-
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}'
|
|
51
|
+
UPDATE "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}';
|
|
52
52
|
`);
|
|
53
53
|
if (result.rowCount === 0) {
|
|
54
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
54
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: No row returned`) };
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
catch (error) {
|
|
58
|
-
return { success: false, error: new Error(`Error updating migration record for index ${this.index}
|
|
58
|
+
return { success: false, error: new Error(`Error updating migration record for index ${this.index}: ${error.message}`) };
|
|
59
59
|
}
|
|
60
60
|
return { success: true, error: null };
|
|
61
61
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PostgresDatabase } from "../index.js";
|
|
2
2
|
import { randomUUID } from "crypto";
|
|
3
|
-
import { getSystemUserContext
|
|
3
|
+
import { getSystemUserContext } from "@loomcore/common/models";
|
|
4
4
|
import { AuthService } from "../../../services/auth.service.js";
|
|
5
5
|
export class CreateAdminUserMigration {
|
|
6
6
|
client;
|
|
@@ -17,9 +17,7 @@ export class CreateAdminUserMigration {
|
|
|
17
17
|
try {
|
|
18
18
|
const database = new PostgresDatabase(this.client);
|
|
19
19
|
const authService = new AuthService(database);
|
|
20
|
-
|
|
21
|
-
const systemUserContext = getSystemUserContext();
|
|
22
|
-
const adminUser = await authService.createUser(systemUserContext, {
|
|
20
|
+
const adminUser = await authService.createUser(getSystemUserContext(), {
|
|
23
21
|
_id: _id,
|
|
24
22
|
_orgId: _orgId,
|
|
25
23
|
email: this.adminEmail,
|
|
@@ -35,8 +33,8 @@ export class CreateAdminUserMigration {
|
|
|
35
33
|
}
|
|
36
34
|
try {
|
|
37
35
|
const result = await this.client.query(`
|
|
38
|
-
INSERT INTO "migrations" ("_id", "
|
|
39
|
-
VALUES ('${_id}',
|
|
36
|
+
INSERT INTO "migrations" ("_id", "index", "hasRun", "reverted")
|
|
37
|
+
VALUES ('${_id}', ${this.index}, TRUE, FALSE);
|
|
40
38
|
`);
|
|
41
39
|
if (result.rowCount === 0) {
|
|
42
40
|
return { success: false, error: new Error(`Error inserting migration ${this.index} to migrations table: No row returned`) };
|
|
@@ -47,7 +45,7 @@ export class CreateAdminUserMigration {
|
|
|
47
45
|
}
|
|
48
46
|
return { success: true, error: null };
|
|
49
47
|
}
|
|
50
|
-
async revert(
|
|
48
|
+
async revert() {
|
|
51
49
|
throw new Error('Not implemented');
|
|
52
50
|
}
|
|
53
51
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export interface IMigration {
|
|
2
2
|
index: number;
|
|
3
|
-
execute(
|
|
3
|
+
execute(): Promise<{
|
|
4
4
|
success: boolean;
|
|
5
5
|
error: Error | null;
|
|
6
6
|
}>;
|
|
7
|
-
revert(
|
|
7
|
+
revert(): Promise<{
|
|
8
8
|
success: boolean;
|
|
9
9
|
error: Error | null;
|
|
10
10
|
}>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Client } from "pg";
|
|
2
|
-
export declare function setupDatabaseForAuth(client: Client, adminUsername: string, adminPassword: string
|
|
2
|
+
export declare function setupDatabaseForAuth(client: Client, adminUsername: string, adminPassword: string): Promise<{
|
|
3
3
|
success: boolean;
|
|
4
4
|
error: Error | null;
|
|
5
5
|
}>;
|
|
@@ -3,14 +3,14 @@ import { CreateMigrationTableMigration } from "./001-create-migrations-table.mig
|
|
|
3
3
|
import { CreateUsersTableMigration } from "./003-create-users-table.migration.js";
|
|
4
4
|
import { doesTableExist } from "../utils/does-table-exist.util.js";
|
|
5
5
|
import { CreateAdminUserMigration } from "./006-create-admin-user.migration.js";
|
|
6
|
-
export async function setupDatabaseForAuth(client, adminUsername, adminPassword
|
|
6
|
+
export async function setupDatabaseForAuth(client, adminUsername, adminPassword) {
|
|
7
7
|
let runMigrations = [];
|
|
8
8
|
if (await doesTableExist(client, 'migrations')) {
|
|
9
9
|
const migrations = await client.query(`
|
|
10
10
|
SELECT "_id", "index"
|
|
11
11
|
FROM migrations
|
|
12
|
-
WHERE "hasRun" = TRUE AND "reverted" = FALSE
|
|
13
|
-
|
|
12
|
+
WHERE "hasRun" = TRUE AND "reverted" = FALSE
|
|
13
|
+
`);
|
|
14
14
|
runMigrations = migrations.rows.map((row) => {
|
|
15
15
|
return row.index;
|
|
16
16
|
});
|
|
@@ -25,7 +25,7 @@ export async function setupDatabaseForAuth(client, adminUsername, adminPassword,
|
|
|
25
25
|
if (!runMigrations.includes(6))
|
|
26
26
|
migrationsToRun.push(new CreateAdminUserMigration(client, adminUsername, adminPassword));
|
|
27
27
|
for (const migration of migrationsToRun) {
|
|
28
|
-
await migration.execute(
|
|
28
|
+
await migration.execute();
|
|
29
29
|
}
|
|
30
30
|
return { success: true, error: null };
|
|
31
31
|
}
|
|
@@ -21,12 +21,20 @@ export async function setupDatabaseForMultitenant(client, orgName, orgCode) {
|
|
|
21
21
|
}
|
|
22
22
|
if (!runMigrations.includes(1)) {
|
|
23
23
|
const createMigrationTableMigration = new CreateMigrationTableMigration(client);
|
|
24
|
-
const result = await createMigrationTableMigration.execute(
|
|
24
|
+
const result = await createMigrationTableMigration.execute();
|
|
25
25
|
if (!result.success) {
|
|
26
26
|
console.log('setupDatabaseForMultitenant: error creating migration table', result.error);
|
|
27
27
|
return { success: false, metaOrgId: metaOrgId, error: result.error };
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
+
if (!runMigrations.includes(2)) {
|
|
31
|
+
const createOrganizationTableMigration = new CreateOrganizationsTableMigration(client, orgName, orgCode);
|
|
32
|
+
const result = await createOrganizationTableMigration.execute();
|
|
33
|
+
if (!result.success) {
|
|
34
|
+
console.log('setupDatabaseForMultitenant: error creating organizations table', result.error);
|
|
35
|
+
return { success: false, metaOrgId: metaOrgId, error: result.error };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
30
38
|
else {
|
|
31
39
|
const database = new PostgresDatabase(client);
|
|
32
40
|
const organizationService = new OrganizationService(database);
|
|
@@ -35,17 +43,9 @@ export async function setupDatabaseForMultitenant(client, orgName, orgCode) {
|
|
|
35
43
|
metaOrgId = metaOrg._id;
|
|
36
44
|
}
|
|
37
45
|
}
|
|
38
|
-
if (!runMigrations.includes(2)) {
|
|
39
|
-
const createOrganizationTableMigration = new CreateOrganizationsTableMigration(client, orgName, orgCode);
|
|
40
|
-
const result = await createOrganizationTableMigration.execute(metaOrgId);
|
|
41
|
-
if (!result.success) {
|
|
42
|
-
console.log('setupDatabaseForMultitenant: error creating organizations table', result.error);
|
|
43
|
-
return { success: false, metaOrgId: metaOrgId, error: result.error };
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
46
|
if (!runMigrations.includes(5)) {
|
|
47
47
|
const createMetaOrgMigration = new CreateMetaOrgMigration(client, orgName, orgCode);
|
|
48
|
-
const result = await createMetaOrgMigration.execute(
|
|
48
|
+
const result = await createMetaOrgMigration.execute();
|
|
49
49
|
if (!result.success || !result.metaOrgId) {
|
|
50
50
|
console.log('setupDatabaseForMultitenant: error creating meta org', result.error);
|
|
51
51
|
return { success: false, metaOrgId: metaOrgId, error: result.error };
|