@loomcore/api 0.1.8 → 0.1.9
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/databases/postgres/migrations/002-create-organizations-table.migration.js +8 -2
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +20 -11
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +18 -5
- package/dist/databases/postgres/utils/does-table-exist.util.d.ts +2 -0
- package/dist/databases/postgres/utils/does-table-exist.util.js +8 -0
- package/package.json +1 -1
|
@@ -46,7 +46,7 @@ export class CreateOrganizationTableMigration {
|
|
|
46
46
|
`);
|
|
47
47
|
}
|
|
48
48
|
catch (error) {
|
|
49
|
-
return { success: false, error: new Error(`Error
|
|
49
|
+
return { success: false, error: new Error(`Error creating meta organization: ${error.message}`) };
|
|
50
50
|
}
|
|
51
51
|
try {
|
|
52
52
|
await this.client.query(`
|
|
@@ -69,9 +69,15 @@ export class CreateOrganizationTableMigration {
|
|
|
69
69
|
return { success: false, error: new Error(`Error dropping organizations table: ${error.message}`) };
|
|
70
70
|
}
|
|
71
71
|
try {
|
|
72
|
-
await this.client.query(`
|
|
72
|
+
const result = await this.client.query(`
|
|
73
73
|
Update "migrations" SET "reverted" = TRUE WHERE "index" = '${this.index}' AND "_orgId" = '${_orgId}';
|
|
74
74
|
`);
|
|
75
|
+
if (result.rowCount === 0) {
|
|
76
|
+
return {
|
|
77
|
+
success: false, error: new Error(`Error updating migration record: Migration record not found.
|
|
78
|
+
Migration index: ${this.index}, _orgId: ${_orgId}`)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
75
81
|
}
|
|
76
82
|
catch (error) {
|
|
77
83
|
return { success: false, error: new Error(`Error updating migration record: ${error.message}`) };
|
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
import { CreateRefreshTokenTableMigration } from "./004-create-refresh-token-table.migration.js";
|
|
2
2
|
import { CreateMigrationTableMigration } from "./001-create-migrations-table.migration.js";
|
|
3
3
|
import { CreateUsersTableMigration } from "./003-create-users-table.migration.js";
|
|
4
|
+
import { doesTableExist } from "../utils/does-table-exist.util.js";
|
|
4
5
|
export async function setupDatabaseForAuth(client, _orgId) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
let runMigrations = [];
|
|
7
|
+
if (await doesTableExist(client, 'migrations')) {
|
|
8
|
+
const migrations = await client.query(`
|
|
9
|
+
SELECT "_id", "index"
|
|
10
|
+
FROM migrations
|
|
11
|
+
WHERE "hasRun" = TRUE AND "reverted" = FALSE AND "_orgId" = $1
|
|
12
|
+
`, [_orgId]);
|
|
13
|
+
runMigrations = migrations.rows.map((row) => {
|
|
14
|
+
return row.index;
|
|
15
|
+
});
|
|
14
16
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
let migrationsToRun = [];
|
|
18
|
+
if (!runMigrations.includes(1))
|
|
19
|
+
migrationsToRun.push(new CreateMigrationTableMigration(client));
|
|
20
|
+
if (!runMigrations.includes(3))
|
|
21
|
+
migrationsToRun.push(new CreateUsersTableMigration(client));
|
|
22
|
+
if (!runMigrations.includes(4))
|
|
23
|
+
migrationsToRun.push(new CreateRefreshTokenTableMigration(client));
|
|
24
|
+
for (const migration of migrationsToRun) {
|
|
25
|
+
await migration.execute(_orgId);
|
|
17
26
|
}
|
|
18
27
|
return { success: true, error: null };
|
|
19
28
|
}
|
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
import { CreateMigrationTableMigration } from "./001-create-migrations-table.migration.js";
|
|
2
2
|
import { CreateOrganizationTableMigration } from "./002-create-organizations-table.migration.js";
|
|
3
|
+
import { doesTableExist } from "../utils/does-table-exist.util.js";
|
|
3
4
|
export async function setupDatabaseForMultitenant(client, orgName, orgCode) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
let runMigrations = [];
|
|
6
|
+
if (await doesTableExist(client, 'migrations')) {
|
|
7
|
+
const migrations = await client.query(`
|
|
8
|
+
SELECT "_id", "index"
|
|
9
|
+
FROM migrations
|
|
10
|
+
WHERE "hasRun" = TRUE AND "reverted" = FALSE AND "_orgId" IS NULL
|
|
11
|
+
`);
|
|
12
|
+
runMigrations = migrations.rows.map((row) => {
|
|
13
|
+
return row.index;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
let migrationsToRun = [];
|
|
17
|
+
if (!runMigrations.includes(1))
|
|
18
|
+
migrationsToRun.push(new CreateMigrationTableMigration(client));
|
|
19
|
+
if (!runMigrations.includes(2))
|
|
20
|
+
migrationsToRun.push(new CreateOrganizationTableMigration(client, orgName, orgCode));
|
|
8
21
|
try {
|
|
9
|
-
for (const migration of
|
|
22
|
+
for (const migration of migrationsToRun) {
|
|
10
23
|
await migration.execute();
|
|
11
24
|
}
|
|
12
25
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export async function doesTableExist(client, tableName) {
|
|
2
|
+
const result = await client.query(`
|
|
3
|
+
SELECT EXISTS (
|
|
4
|
+
SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = $1
|
|
5
|
+
)
|
|
6
|
+
`, [tableName]);
|
|
7
|
+
return result.rows[0].exists;
|
|
8
|
+
}
|