@contember/engine-system-api 1.2.0 → 1.3.0-alpha.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/dist/src/SystemContainer.d.ts +1 -12
- package/dist/src/SystemContainer.d.ts.map +1 -1
- package/dist/src/SystemContainer.js +1 -6
- package/dist/src/SystemContainer.js.map +1 -1
- package/dist/src/bin/runMigrations.d.ts +2 -0
- package/dist/src/bin/runMigrations.d.ts.map +1 -0
- package/dist/src/bin/runMigrations.js +31 -0
- package/dist/src/bin/runMigrations.js.map +1 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/migrations/2018-08-04-102200-init.d.ts +3 -0
- package/dist/src/migrations/2018-08-04-102200-init.d.ts.map +1 -0
- package/dist/src/migrations/2018-08-04-102200-init.js +37 -0
- package/dist/src/migrations/2018-08-04-102200-init.js.map +1 -0
- package/dist/src/migrations/2018-08-05-130501-trigger-event-function.d.ts +3 -0
- package/dist/src/migrations/2018-08-05-130501-trigger-event-function.d.ts.map +1 -0
- package/dist/src/migrations/2018-08-05-130501-trigger-event-function.js +99 -0
- package/dist/src/migrations/2018-08-05-130501-trigger-event-function.js.map +1 -0
- package/dist/src/migrations/2019-01-14-143432-event-trx-id.d.ts +3 -0
- package/dist/src/migrations/2019-01-14-143432-event-trx-id.d.ts.map +1 -0
- package/dist/src/migrations/2019-01-14-143432-event-trx-id.js +17 -0
- package/dist/src/migrations/2019-01-14-143432-event-trx-id.js.map +1 -0
- package/dist/src/migrations/2019-02-04-154500-event-rebase.d.ts +3 -0
- package/dist/src/migrations/2019-02-04-154500-event-rebase.d.ts.map +1 -0
- package/dist/src/migrations/2019-02-04-154500-event-rebase.js +59 -0
- package/dist/src/migrations/2019-02-04-154500-event-rebase.js.map +1 -0
- package/dist/src/migrations/2019-03-18-104147-event-run-migration-struct.d.ts +3 -0
- package/dist/src/migrations/2019-03-18-104147-event-run-migration-struct.d.ts.map +1 -0
- package/dist/src/migrations/2019-03-18-104147-event-run-migration-struct.js +12 -0
- package/dist/src/migrations/2019-03-18-104147-event-run-migration-struct.js.map +1 -0
- package/dist/src/migrations/2019-07-29-103000-trigger-event-function-fix.d.ts +3 -0
- package/dist/src/migrations/2019-07-29-103000-trigger-event-function-fix.d.ts.map +1 -0
- package/dist/src/migrations/2019-07-29-103000-trigger-event-function-fix.js +66 -0
- package/dist/src/migrations/2019-07-29-103000-trigger-event-function-fix.js.map +1 -0
- package/dist/src/migrations/2019-11-28-100000-missing-setting-fallback.d.ts +3 -0
- package/dist/src/migrations/2019-11-28-100000-missing-setting-fallback.d.ts.map +1 -0
- package/dist/src/migrations/2019-11-28-100000-missing-setting-fallback.js +21 -0
- package/dist/src/migrations/2019-11-28-100000-missing-setting-fallback.js.map +1 -0
- package/dist/src/migrations/2021-03-23-110000-timestamp-fix.d.ts +3 -0
- package/dist/src/migrations/2021-03-23-110000-timestamp-fix.d.ts.map +1 -0
- package/dist/src/migrations/2021-03-23-110000-timestamp-fix.js +15 -0
- package/dist/src/migrations/2021-03-23-110000-timestamp-fix.js.map +1 -0
- package/dist/src/migrations/2021-05-07-155800-event-log-rework.js +2 -2
- package/dist/src/migrations/2021-12-21-155500-events-up.d.ts +3 -0
- package/dist/src/migrations/2021-12-21-155500-events-up.d.ts.map +1 -0
- package/dist/src/migrations/2021-12-21-155500-events-up.js +15 -0
- package/dist/src/migrations/2021-12-21-155500-events-up.js.map +1 -0
- package/dist/src/migrations/2022-02-08-132600-fn-search-path.d.ts +3 -0
- package/dist/src/migrations/2022-02-08-132600-fn-search-path.d.ts.map +1 -0
- package/dist/src/migrations/2022-02-08-132600-fn-search-path.js +16 -0
- package/dist/src/migrations/2022-02-08-132600-fn-search-path.js.map +1 -0
- package/dist/src/migrations/2022-02-08-140500-drop-deadcode.d.ts +3 -0
- package/dist/src/migrations/2022-02-08-140500-drop-deadcode.d.ts.map +1 -0
- package/dist/src/migrations/2022-02-08-140500-drop-deadcode.js +11 -0
- package/dist/src/migrations/2022-02-08-140500-drop-deadcode.js.map +1 -0
- package/dist/src/migrations/2022-02-08-144400-dynamic-stage-schema.d.ts +3 -0
- package/dist/src/migrations/2022-02-08-144400-dynamic-stage-schema.d.ts.map +1 -0
- package/dist/src/migrations/2022-02-08-144400-dynamic-stage-schema.js +50 -0
- package/dist/src/migrations/2022-02-08-144400-dynamic-stage-schema.js.map +1 -0
- package/dist/src/migrations/2022-10-03-110000-table-on-delete.d.ts +4 -0
- package/dist/src/migrations/2022-10-03-110000-table-on-delete.d.ts.map +1 -0
- package/dist/src/migrations/2022-10-03-110000-table-on-delete.js +56 -0
- package/dist/src/migrations/2022-10-03-110000-table-on-delete.js.map +1 -0
- package/dist/src/migrations/runner.d.ts +16 -3
- package/dist/src/migrations/runner.d.ts.map +1 -1
- package/dist/src/migrations/runner.js +64 -10
- package/dist/src/migrations/runner.js.map +1 -1
- package/dist/src/migrations/snapshot-factory.d.ts +6 -0
- package/dist/src/migrations/snapshot-factory.d.ts.map +1 -0
- package/dist/src/migrations/snapshot-factory.js +46 -0
- package/dist/src/migrations/snapshot-factory.js.map +1 -0
- package/dist/src/migrations/snapshot.d.ts +3 -0
- package/dist/src/migrations/snapshot.d.ts.map +1 -0
- package/dist/src/migrations/snapshot.js +172 -0
- package/dist/src/migrations/snapshot.js.map +1 -0
- package/dist/src/model/ProjectInitializer.d.ts +6 -10
- package/dist/src/model/ProjectInitializer.d.ts.map +1 -1
- package/dist/src/model/ProjectInitializer.js +11 -29
- package/dist/src/model/ProjectInitializer.js.map +1 -1
- package/dist/src/model/database/DatabaseContext.d.ts +5 -4
- package/dist/src/model/database/DatabaseContext.d.ts.map +1 -1
- package/dist/src/model/database/DatabaseContext.js +7 -7
- package/dist/src/model/database/DatabaseContext.js.map +1 -1
- package/dist/src/model/dtos/ExecutedMigration.d.ts +2 -1
- package/dist/src/model/dtos/ExecutedMigration.d.ts.map +1 -1
- package/dist/src/model/dtos/ExecutedMigration.js +2 -1
- package/dist/src/model/dtos/ExecutedMigration.js.map +1 -1
- package/dist/src/model/events/EventResponseBuilder.d.ts +2 -2
- package/dist/src/model/events/EventResponseBuilder.d.ts.map +1 -1
- package/dist/src/model/events/EventResponseBuilder.js +10 -10
- package/dist/src/model/events/EventResponseBuilder.js.map +1 -1
- package/dist/src/model/events/EventResponseHelper.d.ts +1 -1
- package/dist/src/model/events/EventResponseHelper.d.ts.map +1 -1
- package/dist/src/model/events/index.d.ts +1 -0
- package/dist/src/model/events/index.d.ts.map +1 -1
- package/dist/src/model/events/index.js +1 -0
- package/dist/src/model/events/index.js.map +1 -1
- package/dist/src/model/events/types.d.ts +56 -0
- package/dist/src/model/events/types.d.ts.map +1 -0
- package/dist/src/model/events/types.js +51 -0
- package/dist/src/model/events/types.js.map +1 -0
- package/dist/src/model/helpers/index.d.ts +0 -1
- package/dist/src/model/helpers/index.d.ts.map +1 -1
- package/dist/src/model/helpers/index.js +0 -1
- package/dist/src/model/helpers/index.js.map +1 -1
- package/dist/src/model/migrations/ProjectMigrator.js +1 -1
- package/dist/src/model/migrations/ProjectMigrator.js.map +1 -1
- package/dist/src/model/migrations/SchemaVersionBuilder.d.ts +18 -0
- package/dist/src/model/migrations/SchemaVersionBuilder.d.ts.map +1 -1
- package/dist/src/model/migrations/SchemaVersionBuilder.js +5 -4
- package/dist/src/model/migrations/SchemaVersionBuilder.js.map +1 -1
- package/dist/src/model/queries/events/EventQueryHelpers.d.ts +2 -2
- package/dist/src/model/queries/events/EventQueryHelpers.d.ts.map +1 -1
- package/dist/src/model/queries/events/EventQueryHelpers.js +7 -7
- package/dist/src/model/queries/events/EventQueryHelpers.js.map +1 -1
- package/dist/src/model/queries/events/EventsQuery.d.ts +2 -2
- package/dist/src/model/queries/events/EventsQuery.d.ts.map +1 -1
- package/dist/src/model/queries/events/EventsQuery.js.map +1 -1
- package/dist/src/model/queries/migrations/ExecutedMigrationsQueryHelper.d.ts +1 -0
- package/dist/src/model/queries/migrations/ExecutedMigrationsQueryHelper.d.ts.map +1 -1
- package/dist/src/model/queries/migrations/ExecutedMigrationsQueryHelper.js +2 -1
- package/dist/src/model/queries/migrations/ExecutedMigrationsQueryHelper.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/cases/migrations/2022-10-03-110000-table-on-delete.test.d.ts +2 -0
- package/dist/tests/cases/migrations/2022-10-03-110000-table-on-delete.test.d.ts.map +1 -0
- package/dist/tests/cases/migrations/2022-10-03-110000-table-on-delete.test.js +40 -0
- package/dist/tests/cases/migrations/2022-10-03-110000-table-on-delete.test.js.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +13 -14
- package/src/SystemContainer.ts +0 -15
- package/src/bin/runMigrations.ts +35 -0
- package/src/index.ts +0 -2
- package/src/migrations/{2018-08-04-102200-init.sql → 2018-08-04-102200-init.ts} +8 -0
- package/src/migrations/{2018-08-05-130501-trigger-event-function.sql → 2018-08-05-130501-trigger-event-function.ts} +8 -0
- package/src/migrations/{2019-01-14-143432-event-trx-id.sql → 2019-01-14-143432-event-trx-id.ts} +8 -0
- package/src/migrations/{2019-02-04-154500-event-rebase.sql → 2019-02-04-154500-event-rebase.ts} +9 -0
- package/src/migrations/2019-03-18-104147-event-run-migration-struct.ts +11 -0
- package/src/migrations/{2019-07-29-103000-trigger-event-function-fix.sql → 2019-07-29-103000-trigger-event-function-fix.ts} +8 -0
- package/src/migrations/{2019-11-28-100000-missing-setting-fallback.sql → 2019-11-28-100000-missing-setting-fallback.ts} +8 -0
- package/src/migrations/{2021-03-23-110000-timestamp-fix.sql → 2021-03-23-110000-timestamp-fix.ts} +8 -0
- package/src/migrations/2021-05-07-155800-event-log-rework.ts +2 -2
- package/src/migrations/{2021-12-21-155500-events-up.sql → 2021-12-21-155500-events-up.ts} +8 -0
- package/src/migrations/{2022-02-08-132600-fn-search-path.sql → 2022-02-08-132600-fn-search-path.ts} +8 -0
- package/src/migrations/2022-02-08-140500-drop-deadcode.ts +10 -0
- package/src/migrations/{2022-02-08-144400-dynamic-stage-schema.sql → 2022-02-08-144400-dynamic-stage-schema.ts} +8 -0
- package/src/migrations/2022-10-03-110000-table-on-delete.ts +66 -0
- package/src/migrations/runner.ts +79 -15
- package/src/migrations/snapshot-factory.ts +45 -0
- package/src/migrations/snapshot-template-end.txt +1 -0
- package/src/migrations/snapshot-template-start.txt +2 -0
- package/src/migrations/snapshot.ts +169 -0
- package/src/model/ProjectInitializer.ts +15 -50
- package/src/model/database/DatabaseContext.ts +12 -8
- package/src/model/dtos/ExecutedMigration.ts +1 -0
- package/src/model/events/EventResponseBuilder.ts +3 -3
- package/src/model/events/EventResponseHelper.ts +1 -1
- package/src/model/events/index.ts +1 -0
- package/src/model/events/types.ts +63 -0
- package/src/model/helpers/index.ts +0 -1
- package/src/model/migrations/ProjectMigrator.ts +1 -1
- package/src/model/migrations/SchemaVersionBuilder.ts +3 -2
- package/src/model/queries/events/EventQueryHelpers.ts +2 -2
- package/src/model/queries/events/EventsQuery.ts +2 -2
- package/src/model/queries/migrations/ExecutedMigrationsQueryHelper.ts +3 -1
- package/src/tsconfig.json +10 -33
- package/tests/cases/migrations/2022-10-03-110000-table-on-delete.test.ts +42 -0
- package/tests/tsconfig.json +2 -6
- package/tsconfig.json +2 -6
- package/dist/src/model/helpers/SystemVariablesSetupHelper.d.ts +0 -5
- package/dist/src/model/helpers/SystemVariablesSetupHelper.d.ts.map +0 -1
- package/dist/src/model/helpers/SystemVariablesSetupHelper.js +0 -15
- package/dist/src/model/helpers/SystemVariablesSetupHelper.js.map +0 -1
- package/dist/tests/cases/integration/systemApi/projectInitializer.test.d.ts +0 -2
- package/dist/tests/cases/integration/systemApi/projectInitializer.test.d.ts.map +0 -1
- package/dist/tests/cases/integration/systemApi/projectInitializer.test.js +0 -26
- package/dist/tests/cases/integration/systemApi/projectInitializer.test.js.map +0 -1
- package/src/migrations/2019-03-18-104147-event-run-migration-struct.sql +0 -3
- package/src/migrations/2022-02-08-140500-drop-deadcode.sql +0 -2
- package/src/model/helpers/SystemVariablesSetupHelper.ts +0 -14
- package/tests/cases/integration/systemApi/projectInitializer.test.ts +0 -31
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-system-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-alpha.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -9,23 +9,22 @@
|
|
|
9
9
|
"test": "vitest"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@contember/authorization": "^1.
|
|
13
|
-
"@contember/database": "^1.
|
|
14
|
-
"@contember/database-migrations": "^1.
|
|
15
|
-
"@contember/dic": "^1.
|
|
16
|
-
"@contember/
|
|
17
|
-
"@contember/
|
|
18
|
-
"@contember/
|
|
19
|
-
"@contember/
|
|
20
|
-
"@contember/schema": "^1.
|
|
21
|
-
"@contember/schema-
|
|
22
|
-
"@contember/schema-utils": "^1.2.0",
|
|
12
|
+
"@contember/authorization": "^1.3.0-alpha.2",
|
|
13
|
+
"@contember/database": "^1.3.0-alpha.2",
|
|
14
|
+
"@contember/database-migrations": "^1.3.0-alpha.2",
|
|
15
|
+
"@contember/dic": "^1.3.0-alpha.2",
|
|
16
|
+
"@contember/graphql-utils": "^1.3.0-alpha.2",
|
|
17
|
+
"@contember/logger": "^1.3.0-alpha.2",
|
|
18
|
+
"@contember/queryable": "^1.3.0-alpha.2",
|
|
19
|
+
"@contember/schema": "^1.3.0-alpha.2",
|
|
20
|
+
"@contember/schema-migrations": "^1.3.0-alpha.2",
|
|
21
|
+
"@contember/schema-utils": "^1.3.0-alpha.2",
|
|
23
22
|
"@graphql-tools/utils": "^8.6.13",
|
|
24
23
|
"graphql-tag": "^2.12.5"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"@contember/engine-api-tester": "^1.
|
|
28
|
-
"@contember/schema-definition": "^1.
|
|
26
|
+
"@contember/engine-api-tester": "^1.3.0-alpha.2",
|
|
27
|
+
"@contember/schema-definition": "^1.3.0-alpha.2",
|
|
29
28
|
"@graphql-codegen/cli": "^2.6.2",
|
|
30
29
|
"@graphql-codegen/typescript": "^2.5.1",
|
|
31
30
|
"@graphql-codegen/typescript-operations": "^2.4.2",
|
package/src/SystemContainer.ts
CHANGED
|
@@ -6,19 +6,15 @@ import {
|
|
|
6
6
|
SchemaDiffer,
|
|
7
7
|
SchemaMigrator,
|
|
8
8
|
} from '@contember/schema-migrations'
|
|
9
|
-
import { MigrationsRunner } from '@contember/database-migrations'
|
|
10
|
-
import { Connection } from '@contember/database'
|
|
11
9
|
import {
|
|
12
10
|
EventResponseBuilder,
|
|
13
11
|
ExecutedMigrationsResolver,
|
|
14
12
|
IdentityFetcher,
|
|
15
13
|
MigrationAlterer,
|
|
16
14
|
PermissionsFactory,
|
|
17
|
-
ProjectInitializer,
|
|
18
15
|
ProjectMigrator,
|
|
19
16
|
ProjectTruncateExecutor,
|
|
20
17
|
SchemaVersionBuilder,
|
|
21
|
-
StageCreator,
|
|
22
18
|
} from './model'
|
|
23
19
|
import { UuidProvider } from './utils'
|
|
24
20
|
import {
|
|
@@ -31,7 +27,6 @@ import {
|
|
|
31
27
|
SystemResolverContextFactory,
|
|
32
28
|
TruncateMutationResolver,
|
|
33
29
|
} from './resolvers'
|
|
34
|
-
import { getSystemMigrations, SystemMigrationArgs } from './migrations'
|
|
35
30
|
import { EventOldValuesResolver } from './resolvers/types'
|
|
36
31
|
|
|
37
32
|
export interface SystemContainer {
|
|
@@ -39,11 +34,8 @@ export interface SystemContainer {
|
|
|
39
34
|
authorizator: Authorizator
|
|
40
35
|
resolverContextFactory: SystemResolverContextFactory
|
|
41
36
|
schemaVersionBuilder: SchemaVersionBuilder
|
|
42
|
-
projectInitializer: ProjectInitializer
|
|
43
37
|
}
|
|
44
38
|
|
|
45
|
-
export type SystemDbMigrationsRunnerFactory = (db: Connection.ConnectionLike, schema: string) => MigrationsRunner<SystemMigrationArgs>
|
|
46
|
-
|
|
47
39
|
type Args = {
|
|
48
40
|
identityFetcher: IdentityFetcher
|
|
49
41
|
}
|
|
@@ -63,7 +55,6 @@ export class SystemContainerFactory {
|
|
|
63
55
|
'authorizator',
|
|
64
56
|
'resolverContextFactory',
|
|
65
57
|
'schemaVersionBuilder',
|
|
66
|
-
'projectInitializer',
|
|
67
58
|
)
|
|
68
59
|
}
|
|
69
60
|
public createBuilder({ identityFetcher }: Args) {
|
|
@@ -74,8 +65,6 @@ export class SystemContainerFactory {
|
|
|
74
65
|
this.modificationHandlerFactory)
|
|
75
66
|
.addService('identityFetcher', () =>
|
|
76
67
|
identityFetcher)
|
|
77
|
-
.addService('systemDbMigrationsRunnerFactory', (): SystemDbMigrationsRunnerFactory =>
|
|
78
|
-
(db, schema) => new MigrationsRunner(db, schema, getSystemMigrations))
|
|
79
68
|
.addService('schemaMigrator', ({ modificationHandlerFactory }) =>
|
|
80
69
|
new SchemaMigrator(modificationHandlerFactory))
|
|
81
70
|
.addService('executedMigrationsResolver', ({}) =>
|
|
@@ -92,8 +81,6 @@ export class SystemContainerFactory {
|
|
|
92
81
|
new MigrationDescriber(modificationHandlerFactory))
|
|
93
82
|
.addService('projectMigrator', ({ migrationDescriber, schemaVersionBuilder, executedMigrationsResolver }) =>
|
|
94
83
|
new ProjectMigrator(migrationDescriber, schemaVersionBuilder, executedMigrationsResolver))
|
|
95
|
-
.addService('stageCreator', () =>
|
|
96
|
-
new StageCreator())
|
|
97
84
|
.addService('projectTruncateExecutor', () =>
|
|
98
85
|
new ProjectTruncateExecutor())
|
|
99
86
|
.addService('migrationAlterer', () =>
|
|
@@ -110,8 +97,6 @@ export class SystemContainerFactory {
|
|
|
110
97
|
new TruncateMutationResolver(projectTruncateExecutor))
|
|
111
98
|
.addService('migrationAlterMutationResolver', ({ migrationAlterer }) =>
|
|
112
99
|
new MigrationAlterMutationResolver(migrationAlterer))
|
|
113
|
-
.addService('projectInitializer', ({ projectMigrator, stageCreator, systemDbMigrationsRunnerFactory, schemaVersionBuilder }) =>
|
|
114
|
-
new ProjectInitializer(projectMigrator, stageCreator, systemDbMigrationsRunnerFactory, schemaVersionBuilder))
|
|
115
100
|
.addService('eventsQueryResolver', ({ eventResponseBuilder }) =>
|
|
116
101
|
new EventsQueryResolver(eventResponseBuilder))
|
|
117
102
|
.addService('eventOldValuesResolver', () =>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SystemMigrationsRunner } from '../migrations'
|
|
2
|
+
import { DatabaseContextFactory, emptyVersionedSchema, SchemaVersionBuilder } from '../model'
|
|
3
|
+
import { Connection } from '@contember/database'
|
|
4
|
+
import { createLogger, PrettyPrintLoggerHandler } from '@contember/logger'
|
|
5
|
+
|
|
6
|
+
;(async () => {
|
|
7
|
+
const dbConfig = {
|
|
8
|
+
database: process.env.PGDATABASE as string,
|
|
9
|
+
host: process.env.PGHOST as string,
|
|
10
|
+
password: process.env.PGPASSWORD as string,
|
|
11
|
+
user: process.env.PGUSER as string,
|
|
12
|
+
port: process.env.PGPORT ? Number(process.env.PGPORT) : 5432,
|
|
13
|
+
}
|
|
14
|
+
// eslint-disable-next-line no-console
|
|
15
|
+
const connection = Connection.createSingle(dbConfig, err => console.error(err))
|
|
16
|
+
const migrationsRunner = new SystemMigrationsRunner(
|
|
17
|
+
new DatabaseContextFactory('system', connection, {
|
|
18
|
+
uuid: () => {
|
|
19
|
+
throw new Error()
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
{ db: dbConfig, slug: 'test', stages: [] },
|
|
23
|
+
'system',
|
|
24
|
+
{
|
|
25
|
+
buildSchema: () => Promise.resolve(emptyVersionedSchema),
|
|
26
|
+
} as unknown as SchemaVersionBuilder,
|
|
27
|
+
{},
|
|
28
|
+
)
|
|
29
|
+
// eslint-disable-next-line no-console
|
|
30
|
+
await migrationsRunner.run(createLogger(new PrettyPrintLoggerHandler(process.stderr)))
|
|
31
|
+
})().catch(e => {
|
|
32
|
+
// eslint-disable-next-line no-console
|
|
33
|
+
console.error(e)
|
|
34
|
+
process.exit(1)
|
|
35
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -12,12 +12,10 @@ export {
|
|
|
12
12
|
ProjectInitializer,
|
|
13
13
|
ProjectMigrator,
|
|
14
14
|
SchemaVersionBuilder,
|
|
15
|
-
setupSystemVariables,
|
|
16
15
|
Stage,
|
|
17
16
|
StageBySlugQuery,
|
|
18
17
|
StageCreator,
|
|
19
18
|
StagesQuery,
|
|
20
|
-
unnamedIdentity,
|
|
21
19
|
VersionedSchema,
|
|
22
20
|
} from './model'
|
|
23
21
|
export * from './SystemContainer'
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
CREATE DOMAIN "event_type" AS TEXT CHECK (
|
|
2
5
|
VALUE IN ('init', 'create', 'update', 'delete', 'run_migration')
|
|
3
6
|
);
|
|
@@ -26,3 +29,8 @@ CREATE TABLE "stage" (
|
|
|
26
29
|
"event_id" uuid NOT NULL REFERENCES "event"("id") ON DELETE RESTRICT
|
|
27
30
|
);
|
|
28
31
|
CREATE INDEX "system_stage_event_id" ON "stage"("event_id");
|
|
32
|
+
`
|
|
33
|
+
|
|
34
|
+
export default async function (builder: MigrationBuilder) {
|
|
35
|
+
builder.sql(sql)
|
|
36
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
DO
|
|
2
5
|
$BLOCK$
|
|
3
6
|
BEGIN
|
|
@@ -88,3 +91,8 @@ CREATE FUNCTION "trigger_event"() RETURNS TRIGGER AS $$
|
|
|
88
91
|
END;
|
|
89
92
|
|
|
90
93
|
$$ LANGUAGE plpgsql;
|
|
94
|
+
`
|
|
95
|
+
|
|
96
|
+
export default async function (builder: MigrationBuilder) {
|
|
97
|
+
builder.sql(sql)
|
|
98
|
+
}
|
package/src/migrations/{2019-01-14-143432-event-trx-id.sql → 2019-01-14-143432-event-trx-id.ts}
RENAMED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
ALTER TABLE "event"
|
|
2
5
|
ADD COLUMN transaction_id uuid DEFAULT NULL;
|
|
3
6
|
UPDATE "event"
|
|
@@ -6,3 +9,8 @@ SET transaction_id = uuid_generate_v4();
|
|
|
6
9
|
ALTER TABLE "event"
|
|
7
10
|
ALTER "transaction_id" SET DEFAULT current_setting('system.transaction_id')::uuid,
|
|
8
11
|
ALTER "transaction_id" SET NOT NULL;
|
|
12
|
+
`
|
|
13
|
+
|
|
14
|
+
export default async function (builder: MigrationBuilder) {
|
|
15
|
+
builder.sql(sql)
|
|
16
|
+
}
|
package/src/migrations/{2019-02-04-154500-event-rebase.sql → 2019-02-04-154500-event-rebase.ts}
RENAMED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
4
|
+
|
|
1
5
|
DROP FUNCTION IF EXISTS rebase_events_unsafe(head UUID, oldBase UUID, newBase UUID, appliedEvents UUID[]);
|
|
2
6
|
|
|
3
7
|
CREATE OR REPLACE FUNCTION rebase_events_unsafe(head UUID, oldBase UUID, newBase UUID, appliedEvents UUID[]) RETURNS TABLE (
|
|
@@ -47,3 +51,8 @@ FROM with_ids_by_window
|
|
|
47
51
|
ORDER BY number DESC
|
|
48
52
|
$$
|
|
49
53
|
LANGUAGE SQL;
|
|
54
|
+
`
|
|
55
|
+
|
|
56
|
+
export default async function (builder: MigrationBuilder) {
|
|
57
|
+
builder.sql(sql)
|
|
58
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
4
|
+
UPDATE event
|
|
5
|
+
SET data = jsonb_build_object('version', coalesce(substring(data ->> 'file', 0, 17), data ->> 'version'))
|
|
6
|
+
WHERE type = 'run_migration';
|
|
7
|
+
`
|
|
8
|
+
|
|
9
|
+
export default async function (builder: MigrationBuilder) {
|
|
10
|
+
builder.sql(sql)
|
|
11
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
CREATE OR REPLACE FUNCTION "trigger_event"() RETURNS TRIGGER AS $$
|
|
2
5
|
|
|
3
6
|
DECLARE new_event_id uuid := uuid_generate_v4();
|
|
@@ -55,3 +58,8 @@ CREATE OR REPLACE FUNCTION "trigger_event"() RETURNS TRIGGER AS $$
|
|
|
55
58
|
END;
|
|
56
59
|
|
|
57
60
|
$$ LANGUAGE plpgsql;
|
|
61
|
+
`
|
|
62
|
+
|
|
63
|
+
export default async function (builder: MigrationBuilder) {
|
|
64
|
+
builder.sql(sql)
|
|
65
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
ALTER TABLE "event"
|
|
2
5
|
ALTER COLUMN "identity_id" SET DEFAULT
|
|
3
6
|
coalesce(
|
|
@@ -10,3 +13,8 @@ ALTER TABLE "event"
|
|
|
10
13
|
nullif(current_setting('system.transaction_id', true), '')::uuid,
|
|
11
14
|
set_config('system.transaction_id', uuid_generate_v4()::text, true)::uuid
|
|
12
15
|
);
|
|
16
|
+
`
|
|
17
|
+
|
|
18
|
+
export default async function (builder: MigrationBuilder) {
|
|
19
|
+
builder.sql(sql)
|
|
20
|
+
}
|
package/src/migrations/{2021-03-23-110000-timestamp-fix.sql → 2021-03-23-110000-timestamp-fix.ts}
RENAMED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
ALTER TABLE "schema_migration"
|
|
2
5
|
ALTER "executed_at" TYPE TIMESTAMPTZ,
|
|
3
6
|
ALTER "executed_at" SET DEFAULT now();
|
|
4
7
|
|
|
5
8
|
ALTER TABLE "event"
|
|
6
9
|
ALTER "created_at" TYPE TIMESTAMPTZ;
|
|
10
|
+
`
|
|
11
|
+
|
|
12
|
+
export default async function (builder: MigrationBuilder) {
|
|
13
|
+
builder.sql(sql)
|
|
14
|
+
}
|
|
@@ -42,7 +42,7 @@ export default async function (builder: MigrationBuilder, args: MigrationArgs<Sy
|
|
|
42
42
|
identity_id UUID NOT NULL,
|
|
43
43
|
transaction_id UUID DEFAULT COALESCE(
|
|
44
44
|
(NULLIF(CURRENT_SETTING('system.transaction_id'::TEXT, TRUE), ''::TEXT))::UUID,
|
|
45
|
-
(SET_CONFIG('system.transaction_id'::TEXT, (uuid_generate_v4())::TEXT, TRUE))::UUID) NOT NULL
|
|
45
|
+
(SET_CONFIG('system.transaction_id'::TEXT, ("uuid_generate_v4"())::TEXT, TRUE))::UUID) NOT NULL
|
|
46
46
|
)
|
|
47
47
|
`)
|
|
48
48
|
builder.sql(`
|
|
@@ -210,7 +210,7 @@ export default async function (builder: MigrationBuilder, args: MigrationArgs<Sy
|
|
|
210
210
|
|
|
211
211
|
INSERT INTO "event_data" ("id", "type", "table_name", "row_ids", "values", "created_at", schema_id, identity_id)
|
|
212
212
|
VALUES (
|
|
213
|
-
uuid_generate_v4(),
|
|
213
|
+
"uuid_generate_v4"(),
|
|
214
214
|
new_event_type,
|
|
215
215
|
table_name,
|
|
216
216
|
primary_values,
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
CREATE INDEX event_data_created ON event_data (created_at);
|
|
2
5
|
CREATE INDEX transaction_applied ON stage_transaction (applied_at);
|
|
3
6
|
|
|
4
7
|
ALTER TABLE stage
|
|
5
8
|
DROP event_id;
|
|
6
9
|
DROP TABLE event_bak;
|
|
10
|
+
`
|
|
11
|
+
|
|
12
|
+
export default async function (builder: MigrationBuilder) {
|
|
13
|
+
builder.sql(sql)
|
|
14
|
+
}
|
package/src/migrations/{2022-02-08-132600-fn-search-path.sql → 2022-02-08-132600-fn-search-path.ts}
RENAMED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
DO LANGUAGE plpgsql
|
|
2
5
|
$$
|
|
3
6
|
BEGIN
|
|
@@ -5,3 +8,8 @@ $$
|
|
|
5
8
|
EXECUTE FORMAT('ALTER FUNCTION trigger_event() SET SEARCH_PATH = %s', QUOTE_IDENT(CURRENT_SCHEMA()));
|
|
6
9
|
END
|
|
7
10
|
$$
|
|
11
|
+
`
|
|
12
|
+
|
|
13
|
+
export default async function (builder: MigrationBuilder) {
|
|
14
|
+
builder.sql(sql)
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
4
|
+
DROP FUNCTION rebase_events_unsafe(head UUID, oldbase UUID, newbase UUID, appliedevents UUID[]);
|
|
5
|
+
DROP FUNCTION statement_trigger_event();
|
|
6
|
+
`
|
|
7
|
+
|
|
8
|
+
export default async function (builder: MigrationBuilder) {
|
|
9
|
+
builder.sql(sql)
|
|
10
|
+
}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
|
|
3
|
+
const sql = `
|
|
1
4
|
ALTER TABLE stage
|
|
2
5
|
ADD COLUMN schema TEXT UNIQUE;
|
|
3
6
|
UPDATE stage
|
|
@@ -39,3 +42,8 @@ $$
|
|
|
39
42
|
EXECUTE FORMAT('ALTER FUNCTION trigger_event_commit() SET SEARCH_PATH = %s', QUOTE_IDENT(CURRENT_SCHEMA()));
|
|
40
43
|
END
|
|
41
44
|
$$
|
|
45
|
+
`
|
|
46
|
+
|
|
47
|
+
export default async function (builder: MigrationBuilder) {
|
|
48
|
+
builder.sql(sql)
|
|
49
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { MigrationArgs, MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
import { wrapIdentifier } from '@contember/database'
|
|
3
|
+
import { SystemMigrationArgs } from './types'
|
|
4
|
+
import { acceptFieldVisitor, NamingHelper } from '@contember/schema-utils'
|
|
5
|
+
import { Model } from '@contember/schema'
|
|
6
|
+
|
|
7
|
+
export default async function (builder: MigrationBuilder, args: MigrationArgs<SystemMigrationArgs>) {
|
|
8
|
+
const schema = await args.schemaResolver(args.connection)
|
|
9
|
+
const stages = (await args.connection.query('SELECT schema FROM stage')).rows
|
|
10
|
+
for (const entity of Object.values(schema.model.entities)) {
|
|
11
|
+
for (const field of Object.values(entity.fields)) {
|
|
12
|
+
const result = acceptFieldVisitor<null | {
|
|
13
|
+
relation: Model.OneHasOneOwningRelation | Model.ManyHasOneRelation
|
|
14
|
+
targetEntity: Model.Entity
|
|
15
|
+
}>(schema.model, entity, field, {
|
|
16
|
+
visitManyHasOne: ({ entity, relation, targetEntity }) => {
|
|
17
|
+
return { relation, entity, targetEntity }
|
|
18
|
+
},
|
|
19
|
+
visitOneHasOneOwning: ({ entity, relation, targetEntity }) => {
|
|
20
|
+
return { relation, entity, targetEntity }
|
|
21
|
+
},
|
|
22
|
+
visitOneHasOneInverse: () => null,
|
|
23
|
+
visitManyHasManyInverse: () => null,
|
|
24
|
+
visitManyHasManyOwning: () => null,
|
|
25
|
+
visitOneHasMany: () => null,
|
|
26
|
+
visitColumn: () => null,
|
|
27
|
+
})
|
|
28
|
+
if (!result) {
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
const { targetEntity, relation } = result
|
|
32
|
+
if (entity.view || targetEntity.view) {
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
if (relation.joiningColumn.onDelete === Model.OnDelete.restrict) {
|
|
36
|
+
continue
|
|
37
|
+
}
|
|
38
|
+
for (const stage of stages) {
|
|
39
|
+
const fkName = NamingHelper.createForeignKeyName(
|
|
40
|
+
entity.tableName,
|
|
41
|
+
relation.joiningColumn.columnName,
|
|
42
|
+
targetEntity.tableName,
|
|
43
|
+
targetEntity.primaryColumn,
|
|
44
|
+
)
|
|
45
|
+
const tableName = { name: entity.tableName, schema: stage.schema }
|
|
46
|
+
|
|
47
|
+
builder.dropConstraint(tableName, fkName)
|
|
48
|
+
|
|
49
|
+
const onDelete = ({
|
|
50
|
+
[Model.OnDelete.setNull]: 'SET NULL',
|
|
51
|
+
[Model.OnDelete.cascade]: 'CASCADE',
|
|
52
|
+
} as const)[relation.joiningColumn.onDelete]
|
|
53
|
+
|
|
54
|
+
builder.addConstraint(tableName, fkName, {
|
|
55
|
+
foreignKeys: {
|
|
56
|
+
columns: relation.joiningColumn.columnName,
|
|
57
|
+
references: `${wrapIdentifier(stage.schema)}.${wrapIdentifier(targetEntity.tableName)}(${wrapIdentifier(targetEntity.primaryColumn)})`,
|
|
58
|
+
onDelete: onDelete,
|
|
59
|
+
},
|
|
60
|
+
deferrable: true,
|
|
61
|
+
deferred: false,
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
package/src/migrations/runner.ts
CHANGED
|
@@ -1,17 +1,81 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
1
|
+
import { MigrationGroup, MigrationsRunner as DbMigrationsRunner } from '@contember/database-migrations'
|
|
2
|
+
import _20180804102200init from './2018-08-04-102200-init'
|
|
3
|
+
import _20180805130501triggereventfunction from './2018-08-05-130501-trigger-event-function'
|
|
4
|
+
import _20190114143432eventtrxid from './2019-01-14-143432-event-trx-id'
|
|
5
|
+
import _20190204154500eventrebase from './2019-02-04-154500-event-rebase'
|
|
6
|
+
import _20190318104147eventrunmigrationstruct from './2019-03-18-104147-event-run-migration-struct'
|
|
7
|
+
import _20190729103000triggereventfunctionfix from './2019-07-29-103000-trigger-event-function-fix'
|
|
8
|
+
import _20191128100000missingsettingfallback from './2019-11-28-100000-missing-setting-fallback'
|
|
9
|
+
import _20200327130000projectmigrations from './2020-03-27-130000-project-migrations'
|
|
10
|
+
import _20200506150000composedprimary from './2020-05-06-150000-composed-primary'
|
|
11
|
+
import _20200601103000eventtriggerperf from './2020-06-01-103000-event-trigger-perf'
|
|
12
|
+
import _20210323110000timestampfix from './2021-03-23-110000-timestamp-fix'
|
|
13
|
+
import _20210507155800eventlogrework from './2021-05-07-155800-event-log-rework'
|
|
14
|
+
import _202105191232000eventlogfixconstraint from './2021-05-19-1232000-event-log-fix-constraint'
|
|
15
|
+
import _20211221155500eventsup from './2021-12-21-155500-events-up'
|
|
16
|
+
import _20220208132600fnsearchpath from './2022-02-08-132600-fn-search-path'
|
|
17
|
+
import _20220208140500dropdeadcode from './2022-02-08-140500-drop-deadcode'
|
|
18
|
+
import _20220208144400dynamicstageschema from './2022-02-08-144400-dynamic-stage-schema'
|
|
19
|
+
import _20221003110000tableondelete from './2022-10-03-110000-table-on-delete'
|
|
20
|
+
import snapshot from './snapshot'
|
|
8
21
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
22
|
+
import { Client, Connection, createDatabaseIfNotExists, DatabaseConfig } from '@contember/database'
|
|
23
|
+
import { DatabaseContextFactory, SchemaVersionBuilder } from '../model'
|
|
24
|
+
import { ProjectConfig } from '../types'
|
|
25
|
+
import { Logger } from '@contember/logger'
|
|
26
|
+
import { GroupMigrationsResolver } from '@contember/database-migrations'
|
|
27
|
+
import { SnapshotMigrationResolver } from '@contember/database-migrations'
|
|
28
|
+
|
|
29
|
+
const migrations = {
|
|
30
|
+
'2018-08-04-102200-init': _20180804102200init,
|
|
31
|
+
'2018-08-05-130501-trigger-event-function': _20180805130501triggereventfunction,
|
|
32
|
+
'2019-01-14-143432-event-trx-id': _20190114143432eventtrxid,
|
|
33
|
+
'2019-02-04-154500-event-rebase': _20190204154500eventrebase,
|
|
34
|
+
'2019-03-18-104147-event-run-migration-struct': _20190318104147eventrunmigrationstruct,
|
|
35
|
+
'2019-07-29-103000-trigger-event-function-fix': _20190729103000triggereventfunctionfix,
|
|
36
|
+
'2019-11-28-100000-missing-setting-fallback': _20191128100000missingsettingfallback,
|
|
37
|
+
'2020-03-27-130000-project-migrations': _20200327130000projectmigrations,
|
|
38
|
+
'2020-05-06-150000-composed-primary': _20200506150000composedprimary,
|
|
39
|
+
'2020-06-01-103000-event-trigger-perf': _20200601103000eventtriggerperf,
|
|
40
|
+
'2021-03-23-110000-timestamp-fix': _20210323110000timestampfix,
|
|
41
|
+
'2021-05-07-155800-event-log-rework': _20210507155800eventlogrework,
|
|
42
|
+
'2021-05-19-1232000-event-log-fix-constraint': _202105191232000eventlogfixconstraint,
|
|
43
|
+
'2021-12-21-155500-events-up': _20211221155500eventsup,
|
|
44
|
+
'2022-02-08-132600-fn-search-path': _20220208132600fnsearchpath,
|
|
45
|
+
'2022-02-08-140500-drop-deadcode': _20220208140500dropdeadcode,
|
|
46
|
+
'2022-02-08-144400-dynamic-stage-schema': _20220208144400dynamicstageschema,
|
|
47
|
+
'2022-10-03-110000-table-on-delete': _20221003110000tableondelete,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
export class SystemMigrationsRunner {
|
|
52
|
+
constructor(
|
|
53
|
+
private readonly databaseContextFactory: DatabaseContextFactory,
|
|
54
|
+
private readonly project: ProjectConfig & { db: DatabaseConfig },
|
|
55
|
+
private readonly schema: string,
|
|
56
|
+
private readonly schemaVersionBuilder: SchemaVersionBuilder,
|
|
57
|
+
private readonly migrationGroups: Record<string, MigrationGroup<unknown>>,
|
|
58
|
+
) {
|
|
59
|
+
}
|
|
60
|
+
async run(logger: Logger) {
|
|
61
|
+
await createDatabaseIfNotExists(this.project.db, message => typeof message === 'string' ? logger.warn(message) : logger.error(message))
|
|
62
|
+
const singleConnection = Connection.createSingle(this.project.db, err => logger.error(err))
|
|
63
|
+
await singleConnection.scope(async connection => {
|
|
64
|
+
const schemaResolver = (connection: Connection.ConnectionLike) => {
|
|
65
|
+
const dbContextMigrations = this.databaseContextFactory.create(connection)
|
|
66
|
+
return this.schemaVersionBuilder.buildSchema(dbContextMigrations)
|
|
67
|
+
}
|
|
68
|
+
const migrationResolver = new GroupMigrationsResolver(
|
|
69
|
+
new SnapshotMigrationResolver(snapshot, migrations),
|
|
70
|
+
Object.fromEntries(Object.entries(this.migrationGroups).map(([group, it]) => [group, new SnapshotMigrationResolver(it.snapshot, it.migrations, group.replace(/[^-_\w]+/g, '-'))])),
|
|
71
|
+
)
|
|
72
|
+
const migrationsRunner = new DbMigrationsRunner(connection, this.schema, migrationResolver)
|
|
73
|
+
|
|
74
|
+
await migrationsRunner.migrate(message => logger.warn(message), {
|
|
75
|
+
project: this.project,
|
|
76
|
+
schemaResolver,
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
await singleConnection.end()
|
|
80
|
+
}
|
|
17
81
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { MigrationArgs, MigrationBuilder } from '@contember/database-migrations'
|
|
2
|
+
import { SystemMigrationArgs } from './types'
|
|
3
|
+
|
|
4
|
+
export const snapshotMigration = (sqlFactory: (args: { randomUuidFn: string }) => string) => {
|
|
5
|
+
return async (builder: MigrationBuilder, args: MigrationArgs<SystemMigrationArgs>) => {
|
|
6
|
+
const hasGenRandomUuid = await args.connection.query(`
|
|
7
|
+
SELECT 1
|
|
8
|
+
FROM pg_proc
|
|
9
|
+
JOIN pg_namespace ON pg_proc.pronamespace = pg_namespace.oid
|
|
10
|
+
WHERE pg_namespace.nspname = 'pg_catalog' AND pg_proc.proname = 'gen_random_uuid'
|
|
11
|
+
`)
|
|
12
|
+
let randomUuidFn: string = 'uuid_generate_v4'
|
|
13
|
+
if (hasGenRandomUuid.rowCount > 0) {
|
|
14
|
+
randomUuidFn = 'gen_random_uuid'
|
|
15
|
+
} else {
|
|
16
|
+
const hasUuidGenV4 = await args.connection.query(`
|
|
17
|
+
SELECT 1
|
|
18
|
+
FROM pg_proc
|
|
19
|
+
JOIN pg_namespace ON pg_proc.pronamespace = pg_namespace.oid
|
|
20
|
+
WHERE pg_namespace.nspname = 'pg_catalog' AND pg_proc.proname = 'uuid_generate_v4'
|
|
21
|
+
`)
|
|
22
|
+
if (hasUuidGenV4.rowCount === 0) {
|
|
23
|
+
builder.sql(`
|
|
24
|
+
CREATE FUNCTION "uuid_generate_v4"() RETURNS "uuid"
|
|
25
|
+
LANGUAGE "sql"
|
|
26
|
+
AS $$
|
|
27
|
+
SELECT OVERLAY(OVERLAY(md5(random()::TEXT || ':' || clock_timestamp()::TEXT) PLACING '4' FROM 13) PLACING
|
|
28
|
+
to_hex(floor(random() * (11 - 8 + 1) + 8)::INT)::TEXT FROM 17)::UUID;
|
|
29
|
+
$$;
|
|
30
|
+
`)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const sql = sqlFactory({ randomUuidFn })
|
|
34
|
+
builder.sql(sql)
|
|
35
|
+
|
|
36
|
+
builder.sql(`DO LANGUAGE plpgsql
|
|
37
|
+
$$
|
|
38
|
+
BEGIN
|
|
39
|
+
EXECUTE FORMAT('ALTER FUNCTION trigger_event_commit() SET SEARCH_PATH = %s', QUOTE_IDENT(CURRENT_SCHEMA()));
|
|
40
|
+
EXECUTE FORMAT('ALTER FUNCTION trigger_event() SET SEARCH_PATH = %s', QUOTE_IDENT(CURRENT_SCHEMA()));
|
|
41
|
+
END
|
|
42
|
+
$$`)
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
`)
|