@contember/engine-system-api 2.1.0-alpha.3 → 2.1.0-alpha.30

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.
Files changed (35) hide show
  1. package/dist/development/src/migrations/2025-03-14-110000-fix-missing-index.cjs +3 -0
  2. package/dist/development/src/migrations/2025-03-14-110000-fix-missing-index.cjs.map +1 -1
  3. package/dist/development/src/migrations/2025-03-14-110000-fix-missing-index.js +3 -0
  4. package/dist/development/src/migrations/2025-03-14-110000-fix-missing-index.js.map +1 -1
  5. package/dist/development/src/model/migrations/ProjectMigrator.cjs +15 -5
  6. package/dist/development/src/model/migrations/ProjectMigrator.cjs.map +1 -1
  7. package/dist/development/src/model/migrations/ProjectMigrator.js +15 -5
  8. package/dist/development/src/model/migrations/ProjectMigrator.js.map +1 -1
  9. package/dist/development/src/model/migrations/SchemaProvider.cjs +1 -4
  10. package/dist/development/src/model/migrations/SchemaProvider.cjs.map +1 -1
  11. package/dist/development/src/model/migrations/SchemaProvider.js +1 -4
  12. package/dist/development/src/model/migrations/SchemaProvider.js.map +1 -1
  13. package/dist/production/src/migrations/2025-03-14-110000-fix-missing-index.cjs +3 -0
  14. package/dist/production/src/migrations/2025-03-14-110000-fix-missing-index.cjs.map +1 -1
  15. package/dist/production/src/migrations/2025-03-14-110000-fix-missing-index.js +3 -0
  16. package/dist/production/src/migrations/2025-03-14-110000-fix-missing-index.js.map +1 -1
  17. package/dist/production/src/model/migrations/ProjectMigrator.cjs +15 -5
  18. package/dist/production/src/model/migrations/ProjectMigrator.cjs.map +1 -1
  19. package/dist/production/src/model/migrations/ProjectMigrator.js +15 -5
  20. package/dist/production/src/model/migrations/ProjectMigrator.js.map +1 -1
  21. package/dist/production/src/model/migrations/SchemaProvider.cjs +1 -4
  22. package/dist/production/src/model/migrations/SchemaProvider.cjs.map +1 -1
  23. package/dist/production/src/model/migrations/SchemaProvider.js +1 -4
  24. package/dist/production/src/model/migrations/SchemaProvider.js.map +1 -1
  25. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  26. package/dist/types/migrations/2025-03-14-110000-fix-missing-index.d.ts.map +1 -1
  27. package/dist/types/model/migrations/ProjectMigrator.d.ts +1 -0
  28. package/dist/types/model/migrations/ProjectMigrator.d.ts.map +1 -1
  29. package/dist/types/model/migrations/SchemaProvider.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/graphql.codegen.yml +1 -1
  32. package/package.json +12 -12
  33. package/src/migrations/2025-03-14-110000-fix-missing-index.ts +3 -0
  34. package/src/model/migrations/ProjectMigrator.ts +21 -5
  35. package/src/model/migrations/SchemaProvider.ts +4 -5
@@ -43,7 +43,7 @@ class ProjectMigrator {
43
43
  modification,
44
44
  formatVersion,
45
45
  migration.version,
46
- db.client.schema,
46
+ project,
47
47
  metadataStore
48
48
  );
49
49
  }
@@ -148,7 +148,13 @@ class ProjectMigrator {
148
148
  }
149
149
  return toExecute;
150
150
  }
151
- async applyModification(db, stages, schema, modification, formatVersion, migrationVersion, systemSchema, metadataStore) {
151
+ sqlPreprocessor(text, variables) {
152
+ return Object.entries(variables).reduce(
153
+ (content, [key, value]) => content.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value),
154
+ text
155
+ );
156
+ }
157
+ async applyModification(db, stages, schema, modification, formatVersion, migrationVersion, project, metadataStore) {
152
158
  const {
153
159
  getSql,
154
160
  schema: newSchema
@@ -156,16 +162,20 @@ class ProjectMigrator {
156
162
  for (const stage of stages) {
157
163
  const databaseMetadata = await metadataStore.getMetadata(stage.schema);
158
164
  const sql = getSql({
159
- systemSchema,
165
+ systemSchema: project.systemSchema,
160
166
  databaseMetadata,
161
167
  invalidateDatabaseMetadata: () => metadataStore.invalidate(stage.schema)
162
168
  });
163
- await this.executeOnStage(db, stage, sql, migrationVersion);
169
+ const processedSql = this.sqlPreprocessor(sql, {
170
+ project_slug: project.slug,
171
+ system_schema: project.systemSchema
172
+ });
173
+ await this.executeOnStage(db, stage, processedSql, migrationVersion);
164
174
  }
165
175
  return [newSchema];
166
176
  }
167
177
  async executeOnStage(db, stage, sql, migrationVersion) {
168
- await db.query("SET search_path TO " + wrapIdentifier(stage.schema));
178
+ await db.query("SET LOCAL search_path TO " + wrapIdentifier(stage.schema));
169
179
  try {
170
180
  await db.query(sql);
171
181
  } catch (e) {
@@ -1 +1 @@
1
- {"version":3,"file":"ProjectMigrator.js","sources":["../../../../../../packages/engine-system-api/src/model/migrations/ProjectMigrator.ts"],"sourcesContent":["import { calculateMigrationChecksum, Migration, MigrationDescriber, MigrationVersionHelper } from '@contember/schema-migrations'\nimport { Client, DatabaseMetadata, QueryError, wrapIdentifier } from '@contember/database'\nimport { Schema } from '@contember/schema'\nimport { SaveMigrationCommand } from '../commands'\nimport { Stage } from '../dtos'\nimport { DatabaseContext } from '../database'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { MigrateErrorCode } from '../../schema'\nimport { calculateSchemaChecksum, SchemaValidator, SchemaValidatorSkippedErrors } from '@contember/schema-utils'\nimport { logger } from '@contember/logger'\nimport { MigrationsDatabaseMetadataResolverStoreFactory, SchemaDatabaseMetadataResolverStore } from '../metadata'\nimport { ContentMigrationQuery, MigrationInput } from './MigrationInput'\nimport { ContentQueryExecutor } from '../dependencies'\nimport { SchemaProvider } from './SchemaProvider'\nimport { SaveSchemaCommand } from '../commands/schema/SaveSchemaCommand'\nimport { ImplementationException } from '../../utils'\n\nexport class ProjectMigrator {\n\tconstructor(\n\t\tprivate readonly migrationDescriber: MigrationDescriber,\n\t\tprivate readonly schemaProvider: SchemaProvider,\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly migrationsDatabaseMetadataResolverStoreFactory: MigrationsDatabaseMetadataResolverStoreFactory,\n\t\tprivate readonly contentQueryExecutor: ContentQueryExecutor,\n\t) {}\n\n\tpublic async migrate({ db, project, identity, options: { ignoreOrder = false, skipExecuted = false }, migrationsToExecute, stages }: {\n\t\tdb: DatabaseContext\n\t\tproject: { slug: string; systemSchema: string }\n\t\tidentity: { id: string }\n\t\tstages: Stage[]\n\t\tmigrationsToExecute: readonly MigrationInput[]\n\t\toptions: {\n\t\t\tignoreOrder?: boolean\n\t\t\tskipExecuted?: boolean\n\t\t}\n\t}) {\n\t\tif (migrationsToExecute.length === 0) {\n\t\t\treturn\n\t\t}\n\t\tconst schemaWithMeta = await this.schemaProvider.fetch({ db })\n\t\tlet schema = schemaWithMeta.schema\n\t\tlet id = schemaWithMeta.meta.id\n\n\t\tconst validated = await this.validateMigrations(db, schema, schemaWithMeta.meta.version ?? null, migrationsToExecute, { ignoreOrder, skipExecuted })\n\t\tif (validated.length === 0) {\n\t\t\treturn\n\t\t}\n\n\t\tconst sorted = [...validated].sort((a, b) => a.version.localeCompare(b.version))\n\n\t\tconst metadataStore = this.migrationsDatabaseMetadataResolverStoreFactory.create(db)\n\n\t\tfor (const migration of sorted) {\n\n\t\t\tif (migration.type === 'schema') {\n\t\t\t\tconst formatVersion = migration.formatVersion\n\n\t\t\t\tfor (const modification of migration.modifications) {\n\t\t\t\t\t[schema] = await this.applyModification(\n\t\t\t\t\t\tdb.client,\n\t\t\t\t\t\tstages,\n\t\t\t\t\t\tschema,\n\t\t\t\t\t\tmodification,\n\t\t\t\t\t\tformatVersion,\n\t\t\t\t\t\tmigration.version,\n\t\t\t\t\t\tdb.client.schema,\n\t\t\t\t\t\tmetadataStore,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (migration.type === 'content') {\n\t\t\t\tif (!id) {\n\t\t\t\t\tthrow new FailedContentMigrationError(migration.version, `Cannot execute content migration without schema`)\n\t\t\t\t}\n\t\t\t\tfor (const query of migration.queries) {\n\t\t\t\t\tawait this.executeContentMigration({\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tquery,\n\t\t\t\t\t\tdb,\n\t\t\t\t\t\tschema,\n\t\t\t\t\t\tschemaMeta: { id },\n\t\t\t\t\t\tstages,\n\t\t\t\t\t\tdatabaseMetadata: await metadataStore.getMetadata(db.client.schema),\n\t\t\t\t\t\tidentity,\n\t\t\t\t\t\tproject,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\t// event log uses deferred constraint triggers, we need to fire them before ALTER\n\t\t\t\tawait db.client.query(`SET CONSTRAINTS ALL IMMEDIATE`)\n\t\t\t\tawait db.client.query(`SET CONSTRAINTS ALL DEFERRED`)\n\n\t\t\t}\n\t\t\tid = await db.commandBus.execute(new SaveMigrationCommand(migration))\n\t\t}\n\t\tif (!id) {\n\t\t\tthrow new ImplementationException()\n\t\t}\n\n\t\tawait db.commandBus.execute(new SaveSchemaCommand({\n\t\t\tschema,\n\t\t\tmeta: {\n\t\t\t\tid,\n\t\t\t\tupdatedAt: new Date(),\n\t\t\t\tversion: sorted[sorted.length - 1].version,\n\t\t\t\tchecksum: calculateSchemaChecksum(schema),\n\t\t\t},\n\t\t}))\n\t}\n\n\tprivate async executeContentMigration({ query, stages, version, ...ctx }: {\n\t\tversion: string\n\t\tdb: DatabaseContext\n\t\tquery: ContentMigrationQuery\n\t\tstages: Stage[]\n\t\tschema: Schema\n\t\tschemaMeta: { id: number }\n\t\tproject: { slug: string; systemSchema: string }\n\t\tidentity: { id: string }\n\t\tdatabaseMetadata: DatabaseMetadata\n\t}) {\n\t\tconst queryStages = query.stage ? stages.filter(it => it.slug === query.stage) : stages\n\t\tfor (const stage of queryStages) {\n\t\t\tconst response = await this.contentQueryExecutor.execute({\n\t\t\t\tstage,\n\t\t\t\t...ctx,\n\t\t\t}, query)\n\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow new FailedContentMigrationError(version, response.errors.join(', '))\n\t\t\t}\n\t\t\tif (query.checkMutationResult !== false) {\n\t\t\t\tfor (const [key, value] of Object.entries(response.result.data ?? {})) {\n\t\t\t\t\tif (typeof value === 'object' && value !== null && 'ok' in value && !value.ok) {\n\t\t\t\t\t\tconst errorDetails = 'errorMessage' in value ? value.errorMessage : 'No details available. Please fetch errorMessage field.'\n\t\t\t\t\t\tthrow new NotSuccessfulContentMigrationError(version, `Mutation ${key} failed: ${errorDetails}`)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async validateMigrations(\n\t\tdb: DatabaseContext,\n\t\tschema: Schema,\n\t\tversion: string | null,\n\t\tmigrationsToExecute: readonly MigrationInput[],\n\t\t{ ignoreOrder, skipExecuted }: {\n\t\t\tignoreOrder: boolean\n\t\t\tskipExecuted: boolean\n\t\t},\n\t): Promise<readonly MigrationInput[]> {\n\t\tconst executedMigrations = await this.executedMigrationsResolver.getMigrations(db)\n\t\tconst toExecute: MigrationInput[] = []\n\t\tlet skippedErrors: SchemaValidatorSkippedErrors[] = []\n\t\tfor (const migration of migrationsToExecute) {\n\t\t\tconst executedMigration = executedMigrations.find(it => it.version === migration.version)\n\t\t\tif (executedMigration) {\n\t\t\t\tif (skipExecuted && (migration.type !== 'schema' || executedMigration.checksum === calculateMigrationChecksum(migration))) {\n\t\t\t\t\tcontinue\n\t\t\t\t} else {\n\t\t\t\t\tthrow new AlreadyExecutedMigrationError(migration.version, `Migration is already executed`)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (version && migration.version < version && !ignoreOrder) {\n\t\t\t\tthrow new MustFollowLatestMigrationError(migration.version, `Must follow latest executed migration ${version}`)\n\t\t\t}\n\t\t\tif (migration.type === 'schema') {\n\t\t\t\tconst described = this.migrationDescriber.describeModifications(schema, migration)\n\t\t\t\tif (described.length === 0) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst latestModification = described[described.length - 1]\n\t\t\t\tschema = latestModification.schema\n\t\t\t\tskippedErrors = [\n\t\t\t\t\t...skippedErrors.filter(it => it.skipUntil && MigrationVersionHelper.extractVersion(it.skipUntil) >= migration.version),\n\t\t\t\t\t...migration.skippedErrors ?? [],\n\t\t\t\t]\n\t\t\t\tconst errors = SchemaValidator.validate(schema, skippedErrors)\n\t\t\t\tif (errors.length > 0) {\n\t\t\t\t\tthrow new InvalidSchemaError(\n\t\t\t\t\t\tmigration.version,\n\t\t\t\t\t\t'Migration generates invalid schema: \\n' +\n\t\t\t\t\t\terrors.map(it => `${it.path.join('.')}: [${it.code}] ${it.message}`).join('\\n'),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttoExecute.push(migration)\n\t\t}\n\t\t// intentionally not using skippedErrors here\n\t\tconst errors = SchemaValidator.validate(schema)\n\t\tif (errors.length > 0) {\n\t\t\tthrow new InvalidSchemaError(\n\t\t\t\ttoExecute[toExecute.length - 1].version,\n\t\t\t\t'Migration generates invalid schema: \\n' +\n\t\t\t\terrors.map(it => it.path.join('.') + ': ' + it.message).join('\\n'),\n\t\t\t)\n\t\t}\n\t\treturn toExecute\n\t}\n\n\tprivate async applyModification(\n\t\tdb: Client,\n\t\tstages: Stage[],\n\t\tschema: Schema,\n\t\tmodification: Migration.Modification,\n\t\tformatVersion: number,\n\t\tmigrationVersion: string,\n\t\tsystemSchema: string,\n\t\tmetadataStore: SchemaDatabaseMetadataResolverStore,\n\t): Promise<[Schema]> {\n\t\tconst {\n\t\t\tgetSql,\n\t\t\tschema: newSchema,\n\t\t} = this.migrationDescriber.describeModification(schema, modification, { formatVersion })\n\t\tfor (const stage of stages) {\n\t\t\tconst databaseMetadata = await metadataStore.getMetadata(stage.schema)\n\t\t\tconst sql = getSql({\n\t\t\t\tsystemSchema,\n\t\t\t\tdatabaseMetadata,\n\t\t\t\tinvalidateDatabaseMetadata: () => metadataStore.invalidate(stage.schema),\n\t\t\t})\n\t\t\tawait this.executeOnStage(db, stage, sql, migrationVersion)\n\t\t}\n\t\treturn [newSchema]\n\t}\n\n\tprivate async executeOnStage(db: Client, stage: Stage, sql: string, migrationVersion: string) {\n\t\tawait db.query('SET search_path TO ' + wrapIdentifier(stage.schema))\n\t\ttry {\n\t\t\tawait db.query(sql)\n\t\t} catch (e) {\n\t\t\tif (e instanceof QueryError) {\n\t\t\t\tlogger.error(e, { message: 'Migration failed' })\n\t\t\t\tthrow new MigrationFailedError(migrationVersion, e.message)\n\t\t\t}\n\t\t\tthrow e\n\t\t}\n\t}\n}\n\nexport abstract class MigrationError extends Error {\n\tpublic abstract code: MigrateErrorCode\n\n\tconstructor(public readonly version: string, public readonly migrationError: string) {\n\t\tsuper(`${version}: ${migrationError}`)\n\t}\n}\n\nexport class MustFollowLatestMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.MustFollowLatest\n}\n\nexport class AlreadyExecutedMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.AlreadyExecuted\n}\n\nexport class MigrationFailedError extends MigrationError {\n\tcode = MigrateErrorCode.MigrationFailed\n}\n\nexport class InvalidSchemaError extends MigrationError {\n\tcode = MigrateErrorCode.InvalidSchema\n}\n\nexport class FailedContentMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.ContentMigrationFailed\n}\n\nexport class NotSuccessfulContentMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.ContentMigrationNotSuccessful\n}\n"],"names":["errors"],"mappings":";;;;;;;;;;;;;AAiBO,MAAM,gBAAgB;AAAA,EAC5B,YACkB,oBACA,gBACA,4BACA,gDACA,sBAChB;AALgB,SAAA,qBAAA;AACA,SAAA,iBAAA;AACA,SAAA,6BAAA;AACA,SAAA,iDAAA;AACA,SAAA,uBAAA;AAAA,EAAA;AAAA,EAGlB,MAAa,QAAQ,EAAE,IAAI,SAAS,UAAU,SAAS,EAAE,cAAc,OAAO,eAAe,MAAS,GAAA,qBAAqB,UAUxH;AACE,QAAA,oBAAoB,WAAW,GAAG;AACrC;AAAA,IAAA;AAED,UAAM,iBAAiB,MAAM,KAAK,eAAe,MAAM,EAAE,IAAI;AAC7D,QAAI,SAAS,eAAe;AACxB,QAAA,KAAK,eAAe,KAAK;AAE7B,UAAM,YAAY,MAAM,KAAK,mBAAmB,IAAI,QAAQ,eAAe,KAAK,WAAW,MAAM,qBAAqB,EAAE,aAAa,cAAc;AAC/I,QA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A,EAAA;AACzB;AAEO,MAAM,2BAA2B,eAAe;AAAA,EAAhD,cAAA;AAAA,UAAA,GAAA,SAAA;AACN,kBAAA,MAAA,QAAO,iBAAiB,aAAA;AAAA,EAAA;AACzB;AAEO,MAAM,oCAAoC,eAAe;AAAA,EAAzD,cAAA;AAAA,UAAA,GAAA,SAAA;AACN,kBAAA,MAAA,QAAO,iBAAiB,sBAAA;AAAA,EAAA;AACzB;AAEO,MAAM,2CAA2C,eAAe;AAAA,EAAhE,cAAA;AAAA,UAAA,GAAA,SAAA;AACN,kBAAA,MAAA,QAAO,iBAAiB,6BAAA;AAAA,EAAA;AACzB;"}
1
+ {"version":3,"file":"ProjectMigrator.js","sources":["../../../../../../packages/engine-system-api/src/model/migrations/ProjectMigrator.ts"],"sourcesContent":["import { calculateMigrationChecksum, Migration, MigrationDescriber, MigrationVersionHelper } from '@contember/schema-migrations'\nimport { Client, DatabaseMetadata, QueryError, wrapIdentifier } from '@contember/database'\nimport { Schema } from '@contember/schema'\nimport { SaveMigrationCommand } from '../commands'\nimport { Stage } from '../dtos'\nimport { DatabaseContext } from '../database'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { MigrateErrorCode } from '../../schema'\nimport { calculateSchemaChecksum, SchemaValidator, SchemaValidatorSkippedErrors } from '@contember/schema-utils'\nimport { logger } from '@contember/logger'\nimport { MigrationsDatabaseMetadataResolverStoreFactory, SchemaDatabaseMetadataResolverStore } from '../metadata'\nimport { ContentMigrationQuery, MigrationInput } from './MigrationInput'\nimport { ContentQueryExecutor } from '../dependencies'\nimport { SchemaProvider } from './SchemaProvider'\nimport { SaveSchemaCommand } from '../commands/schema/SaveSchemaCommand'\nimport { ImplementationException } from '../../utils'\n\ntype MigrationVariables = {\n\tsystem_schema: string\n\tproject_slug: string\n}\n\nexport class ProjectMigrator {\n\tconstructor(\n\t\tprivate readonly migrationDescriber: MigrationDescriber,\n\t\tprivate readonly schemaProvider: SchemaProvider,\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly migrationsDatabaseMetadataResolverStoreFactory: MigrationsDatabaseMetadataResolverStoreFactory,\n\t\tprivate readonly contentQueryExecutor: ContentQueryExecutor,\n\t) {}\n\n\tpublic async migrate({ db, project, identity, options: { ignoreOrder = false, skipExecuted = false }, migrationsToExecute, stages }: {\n\t\tdb: DatabaseContext\n\t\tproject: { slug: string; systemSchema: string }\n\t\tidentity: { id: string }\n\t\tstages: Stage[]\n\t\tmigrationsToExecute: readonly MigrationInput[]\n\t\toptions: {\n\t\t\tignoreOrder?: boolean\n\t\t\tskipExecuted?: boolean\n\t\t}\n\t}) {\n\t\tif (migrationsToExecute.length === 0) {\n\t\t\treturn\n\t\t}\n\t\tconst schemaWithMeta = await this.schemaProvider.fetch({ db })\n\t\tlet schema = schemaWithMeta.schema\n\t\tlet id = schemaWithMeta.meta.id\n\n\t\tconst validated = await this.validateMigrations(db, schema, schemaWithMeta.meta.version ?? null, migrationsToExecute, { ignoreOrder, skipExecuted })\n\t\tif (validated.length === 0) {\n\t\t\treturn\n\t\t}\n\n\t\tconst sorted = [...validated].sort((a, b) => a.version.localeCompare(b.version))\n\n\t\tconst metadataStore = this.migrationsDatabaseMetadataResolverStoreFactory.create(db)\n\n\t\tfor (const migration of sorted) {\n\n\t\t\tif (migration.type === 'schema') {\n\t\t\t\tconst formatVersion = migration.formatVersion\n\n\t\t\t\tfor (const modification of migration.modifications) {\n\t\t\t\t\t[schema] = await this.applyModification(\n\t\t\t\t\t\tdb.client,\n\t\t\t\t\t\tstages,\n\t\t\t\t\t\tschema,\n\t\t\t\t\t\tmodification,\n\t\t\t\t\t\tformatVersion,\n\t\t\t\t\t\tmigration.version,\n\t\t\t\t\t\tproject,\n\t\t\t\t\t\tmetadataStore,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else if (migration.type === 'content') {\n\t\t\t\tif (!id) {\n\t\t\t\t\tthrow new FailedContentMigrationError(migration.version, `Cannot execute content migration without schema`)\n\t\t\t\t}\n\t\t\t\tfor (const query of migration.queries) {\n\t\t\t\t\tawait this.executeContentMigration({\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tquery,\n\t\t\t\t\t\tdb,\n\t\t\t\t\t\tschema,\n\t\t\t\t\t\tschemaMeta: { id },\n\t\t\t\t\t\tstages,\n\t\t\t\t\t\tdatabaseMetadata: await metadataStore.getMetadata(db.client.schema),\n\t\t\t\t\t\tidentity,\n\t\t\t\t\t\tproject,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\t// event log uses deferred constraint triggers, we need to fire them before ALTER\n\t\t\t\tawait db.client.query(`SET CONSTRAINTS ALL IMMEDIATE`)\n\t\t\t\tawait db.client.query(`SET CONSTRAINTS ALL DEFERRED`)\n\n\t\t\t}\n\t\t\tid = await db.commandBus.execute(new SaveMigrationCommand(migration))\n\t\t}\n\t\tif (!id) {\n\t\t\tthrow new ImplementationException()\n\t\t}\n\n\t\tawait db.commandBus.execute(new SaveSchemaCommand({\n\t\t\tschema,\n\t\t\tmeta: {\n\t\t\t\tid,\n\t\t\t\tupdatedAt: new Date(),\n\t\t\t\tversion: sorted[sorted.length - 1].version,\n\t\t\t\tchecksum: calculateSchemaChecksum(schema),\n\t\t\t},\n\t\t}))\n\t}\n\n\tprivate async executeContentMigration({ query, stages, version, ...ctx }: {\n\t\tversion: string\n\t\tdb: DatabaseContext\n\t\tquery: ContentMigrationQuery\n\t\tstages: Stage[]\n\t\tschema: Schema\n\t\tschemaMeta: { id: number }\n\t\tproject: { slug: string; systemSchema: string }\n\t\tidentity: { id: string }\n\t\tdatabaseMetadata: DatabaseMetadata\n\t}) {\n\t\tconst queryStages = query.stage ? stages.filter(it => it.slug === query.stage) : stages\n\t\tfor (const stage of queryStages) {\n\t\t\tconst response = await this.contentQueryExecutor.execute({\n\t\t\t\tstage,\n\t\t\t\t...ctx,\n\t\t\t}, query)\n\n\t\t\tif (!response.ok) {\n\t\t\t\tthrow new FailedContentMigrationError(version, response.errors.join(', '))\n\t\t\t}\n\t\t\tif (query.checkMutationResult !== false) {\n\t\t\t\tfor (const [key, value] of Object.entries(response.result.data ?? {})) {\n\t\t\t\t\tif (typeof value === 'object' && value !== null && 'ok' in value && !value.ok) {\n\t\t\t\t\t\tconst errorDetails = 'errorMessage' in value ? value.errorMessage : 'No details available. Please fetch errorMessage field.'\n\t\t\t\t\t\tthrow new NotSuccessfulContentMigrationError(version, `Mutation ${key} failed: ${errorDetails}`)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async validateMigrations(\n\t\tdb: DatabaseContext,\n\t\tschema: Schema,\n\t\tversion: string | null,\n\t\tmigrationsToExecute: readonly MigrationInput[],\n\t\t{ ignoreOrder, skipExecuted }: {\n\t\t\tignoreOrder: boolean\n\t\t\tskipExecuted: boolean\n\t\t},\n\t): Promise<readonly MigrationInput[]> {\n\t\tconst executedMigrations = await this.executedMigrationsResolver.getMigrations(db)\n\t\tconst toExecute: MigrationInput[] = []\n\t\tlet skippedErrors: SchemaValidatorSkippedErrors[] = []\n\t\tfor (const migration of migrationsToExecute) {\n\t\t\tconst executedMigration = executedMigrations.find(it => it.version === migration.version)\n\t\t\tif (executedMigration) {\n\t\t\t\tif (skipExecuted && (migration.type !== 'schema' || executedMigration.checksum === calculateMigrationChecksum(migration))) {\n\t\t\t\t\tcontinue\n\t\t\t\t} else {\n\t\t\t\t\tthrow new AlreadyExecutedMigrationError(migration.version, `Migration is already executed`)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (version && migration.version < version && !ignoreOrder) {\n\t\t\t\tthrow new MustFollowLatestMigrationError(migration.version, `Must follow latest executed migration ${version}`)\n\t\t\t}\n\t\t\tif (migration.type === 'schema') {\n\t\t\t\tconst described = this.migrationDescriber.describeModifications(schema, migration)\n\t\t\t\tif (described.length === 0) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst latestModification = described[described.length - 1]\n\t\t\t\tschema = latestModification.schema\n\t\t\t\tskippedErrors = [\n\t\t\t\t\t...skippedErrors.filter(it => it.skipUntil && MigrationVersionHelper.extractVersion(it.skipUntil) >= migration.version),\n\t\t\t\t\t...migration.skippedErrors ?? [],\n\t\t\t\t]\n\t\t\t\tconst errors = SchemaValidator.validate(schema, skippedErrors)\n\t\t\t\tif (errors.length > 0) {\n\t\t\t\t\tthrow new InvalidSchemaError(\n\t\t\t\t\t\tmigration.version,\n\t\t\t\t\t\t'Migration generates invalid schema: \\n' +\n\t\t\t\t\t\terrors.map(it => `${it.path.join('.')}: [${it.code}] ${it.message}`).join('\\n'),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\ttoExecute.push(migration)\n\t\t}\n\t\t// intentionally not using skippedErrors here\n\t\tconst errors = SchemaValidator.validate(schema)\n\t\tif (errors.length > 0) {\n\t\t\tthrow new InvalidSchemaError(\n\t\t\t\ttoExecute[toExecute.length - 1].version,\n\t\t\t\t'Migration generates invalid schema: \\n' +\n\t\t\t\terrors.map(it => it.path.join('.') + ': ' + it.message).join('\\n'),\n\t\t\t)\n\t\t}\n\t\treturn toExecute\n\t}\n\n\tprivate sqlPreprocessor(text: string, variables: MigrationVariables): string {\n\t\treturn Object.entries(variables).reduce(\n\t\t\t(content, [key, value]) => content.replace(new RegExp(`\\\\{\\\\{${key}\\\\}\\\\}`, 'g'), value),\n\t\t\ttext,\n\t\t)\n\t}\n\n\tprivate async applyModification(\n\t\tdb: Client,\n\t\tstages: Stage[],\n\t\tschema: Schema,\n\t\tmodification: Migration.Modification,\n\t\tformatVersion: number,\n\t\tmigrationVersion: string,\n\t\tproject: { slug: string; systemSchema: string },\n\t\tmetadataStore: SchemaDatabaseMetadataResolverStore,\n\t): Promise<[Schema]> {\n\t\tconst {\n\t\t\tgetSql,\n\t\t\tschema: newSchema,\n\t\t} = this.migrationDescriber.describeModification(schema, modification, { formatVersion })\n\t\tfor (const stage of stages) {\n\t\t\tconst databaseMetadata = await metadataStore.getMetadata(stage.schema)\n\t\t\tconst sql = getSql({\n\t\t\t\tsystemSchema: project.systemSchema,\n\t\t\t\tdatabaseMetadata,\n\t\t\t\tinvalidateDatabaseMetadata: () => metadataStore.invalidate(stage.schema),\n\t\t\t})\n\t\t\tconst processedSql = this.sqlPreprocessor(sql, {\n\t\t\t\tproject_slug: project.slug,\n\t\t\t\tsystem_schema: project.systemSchema,\n\t\t\t})\n\t\t\tawait this.executeOnStage(db, stage, processedSql, migrationVersion)\n\t\t}\n\t\treturn [newSchema]\n\t}\n\n\tprivate async executeOnStage(db: Client, stage: Stage, sql: string, migrationVersion: string) {\n\t\tawait db.query('SET LOCAL search_path TO ' + wrapIdentifier(stage.schema))\n\t\ttry {\n\t\t\tawait db.query(sql)\n\t\t} catch (e) {\n\t\t\tif (e instanceof QueryError) {\n\t\t\t\tlogger.error(e, { message: 'Migration failed' })\n\t\t\t\tthrow new MigrationFailedError(migrationVersion, e.message)\n\t\t\t}\n\t\t\tthrow e\n\t\t}\n\t}\n}\n\nexport abstract class MigrationError extends Error {\n\tpublic abstract code: MigrateErrorCode\n\n\tconstructor(public readonly version: string, public readonly migrationError: string) {\n\t\tsuper(`${version}: ${migrationError}`)\n\t}\n}\n\nexport class MustFollowLatestMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.MustFollowLatest\n}\n\nexport class AlreadyExecutedMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.AlreadyExecuted\n}\n\nexport class MigrationFailedError extends MigrationError {\n\tcode = MigrateErrorCode.MigrationFailed\n}\n\nexport class InvalidSchemaError extends MigrationError {\n\tcode = MigrateErrorCode.InvalidSchema\n}\n\nexport class FailedContentMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.ContentMigrationFailed\n}\n\nexport class NotSuccessfulContentMigrationError extends MigrationError {\n\tcode = MigrateErrorCode.ContentMigrationNotSuccessful\n}\n"],"names":["errors"],"mappings":";;;;;;;;;;;;;AAsBO,MAAM,gBAAgB;AAAA,EAC5B,YACkB,o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zB;"}
@@ -19,11 +19,8 @@ class SchemaProvider {
19
19
  const newMigrations = await this.executedMigrationsResolver.getMigrations(db);
20
20
  const partialResult = newMigrations.reduce(
21
21
  (schema, migration) => {
22
- if (migration.type !== "schema") {
23
- return schema;
24
- }
25
22
  return {
26
- schema: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),
23
+ schema: migration.type !== "schema" ? schema.schema : this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),
27
24
  meta: {
28
25
  version: migration.version,
29
26
  id: migration.id,
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaProvider.cjs","sources":["../../../../../../packages/engine-system-api/src/model/migrations/SchemaProvider.ts"],"sourcesContent":["import { SchemaQuery } from '../queries'\nimport { DatabaseContext } from '../database'\nimport { calculateSchemaChecksum, emptySchema, normalizeSchema } from '@contember/schema-utils'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { SchemaMigrator } from '@contember/schema-migrations'\nimport { Schema } from '@contember/schema'\n\nexport type SchemaMeta = {\n\tchecksum: string\n\tversion: string\n\tupdatedAt: Date\n\tid: number\n}\n\nexport const emptyVersionedSchema: SchemaWithMeta = {\n\tschema: emptySchema,\n\tmeta: {},\n}\n\nexport type SchemaWithMeta = {\n\tschema: Schema\n\tmeta: SchemaMeta | { [K in keyof SchemaMeta]?: undefined }\n}\n\nexport class SchemaProvider {\n\tconstructor(\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly schemaMigrator: SchemaMigrator,\n\t) {\n\t}\n\n\tasync buildSchemaFromMigrations(db: DatabaseContext): Promise<SchemaWithMeta> {\n\t\tconst newMigrations = (await this.executedMigrationsResolver.getMigrations(db))\n\n\t\tconst partialResult = newMigrations.reduce<SchemaWithMeta>(\n\t\t\t(schema, migration) => {\n\t\t\t\tif (migration.type !== 'schema') {\n\t\t\t\t\treturn schema\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tschema: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tid: migration.id,\n\t\t\t\t\t\tupdatedAt: migration.executedAt,\n\t\t\t\t\t\tchecksum: '',\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t},\n\t\t\temptyVersionedSchema,\n\t\t)\n\n\t\tif (!partialResult.meta.version) {\n\t\t\treturn emptyVersionedSchema\n\t\t}\n\n\t\treturn {\n\t\t\tschema: partialResult.schema,\n\t\t\tmeta: {\n\t\t\t\t...partialResult.meta,\n\t\t\t\tchecksum: calculateSchemaChecksum(partialResult.schema),\n\t\t\t},\n\t\t}\n\t}\n\n\tpublic async fetch({ db, currentSchema }: {\n\t\tdb: DatabaseContext\n\t\tcurrentSchema?: SchemaWithMeta | null\n\t}): Promise<SchemaWithMeta> {\n\t\tconst newSchema = await db.queryHandler.fetch(new SchemaQuery(currentSchema?.meta.checksum))\n\t\tif (!newSchema) {\n\t\t\treturn currentSchema ?? emptyVersionedSchema\n\t\t}\n\n\t\treturn newSchema\n\t}\n}\n"],"names":["emptySchema","calculateSchemaChecksum","SchemaQuery"],"mappings":";;;;;;;;AAcO,MAAM,uBAAuC;AAAA,EACnD,QAAQA,YAAA;AAAA,EACR,MAAM,CAAA;AACP;AAOO,MAAM,eAAe;AAAA,EAC3B,YACkB,4BACA,gBAChB;AAFgB,SAAA,6BAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIlB,MAAM,0BAA0B,IAA8C;AAC7E,UAAM,gBAAiB,MAAM,KAAK,2BAA2B,cAAc,EAAE;AAE7E,UAAM,gBAAgB,cAAc;AAAA,MACnC,CAAC,QAAQ,cAAc;AAClB,YAAA,UAAU,SAAS,UAAU;AACzB,iBAAA;AAAA,QAAA;AAED,eAAA;AAAA,UACN,QAAQ,KAAK,eAAe,mBAAmB,OAAO,QAAQ,UAAU,eAAe,UAAU,aAAa;AAAA,UAC9G,MAAM;AAAA,YACL,SAAS,UAAU;AAAA,YACnB,IAAI,UAAU;AAAA,YACd,WAAW,UAAU;AAAA,YACrB,UAAU;AAAA,UAAA;AAAA,QAEZ;AAAA,MACD;AAAA,MACA;AAAA,IACD;AAEI,QAAA,CAAC,cAAc,KAAK,SAAS;AACzB,aAAA;AAAA,IAAA;AAGD,WAAA;AAAA,MACN,QAAQ,cAAc;AAAA,MACtB,MAAM;AAAA,QACL,GAAG,cAAc;AAAA,QACjB,UAAUC,YAAAA,wBAAwB,cAAc,MAAM;AAAA,MAAA;AAAA,IAExD;AAAA,EAAA;AAAA,EAGD,MAAa,MAAM,EAAE,IAAI,iBAGG;AACrB,UAAA,YAAY,MAAM,GAAG,aAAa,MAAM,IAAIC,YAAAA,YAAY,eAAe,KAAK,QAAQ,CAAC;AAC3F,QAAI,CAAC,WAAW;AACf,aAAO,iBAAiB;AAAA,IAAA;AAGlB,WAAA;AAAA,EAAA;AAET;;;"}
1
+ {"version":3,"file":"SchemaProvider.cjs","sources":["../../../../../../packages/engine-system-api/src/model/migrations/SchemaProvider.ts"],"sourcesContent":["import { SchemaQuery } from '../queries'\nimport { DatabaseContext } from '../database'\nimport { calculateSchemaChecksum, emptySchema, normalizeSchema } from '@contember/schema-utils'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { SchemaMigrator } from '@contember/schema-migrations'\nimport { Schema } from '@contember/schema'\n\nexport type SchemaMeta = {\n\tchecksum: string\n\tversion: string\n\tupdatedAt: Date\n\tid: number\n}\n\nexport const emptyVersionedSchema: SchemaWithMeta = {\n\tschema: emptySchema,\n\tmeta: {},\n}\n\nexport type SchemaWithMeta = {\n\tschema: Schema\n\tmeta: SchemaMeta | { [K in keyof SchemaMeta]?: undefined }\n}\n\nexport class SchemaProvider {\n\tconstructor(\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly schemaMigrator: SchemaMigrator,\n\t) {\n\t}\n\n\tasync buildSchemaFromMigrations(db: DatabaseContext): Promise<SchemaWithMeta> {\n\t\tconst newMigrations = (await this.executedMigrationsResolver.getMigrations(db))\n\n\t\tconst partialResult = newMigrations.reduce<SchemaWithMeta>(\n\t\t\t(schema, migration) => {\n\t\t\t\treturn {\n\t\t\t\t\tschema: migration.type !== 'schema'\n\t\t\t\t\t\t? schema.schema\n\t\t\t\t\t\t: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tid: migration.id,\n\t\t\t\t\t\tupdatedAt: migration.executedAt,\n\t\t\t\t\t\tchecksum: '',\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t},\n\t\t\temptyVersionedSchema,\n\t\t)\n\n\t\tif (!partialResult.meta.version) {\n\t\t\treturn emptyVersionedSchema\n\t\t}\n\n\t\treturn {\n\t\t\tschema: partialResult.schema,\n\t\t\tmeta: {\n\t\t\t\t...partialResult.meta,\n\t\t\t\tchecksum: calculateSchemaChecksum(partialResult.schema),\n\t\t\t},\n\t\t}\n\t}\n\n\tpublic async fetch({ db, currentSchema }: {\n\t\tdb: DatabaseContext\n\t\tcurrentSchema?: SchemaWithMeta | null\n\t}): Promise<SchemaWithMeta> {\n\t\tconst newSchema = await db.queryHandler.fetch(new SchemaQuery(currentSchema?.meta.checksum))\n\t\tif (!newSchema) {\n\t\t\treturn currentSchema ?? emptyVersionedSchema\n\t\t}\n\n\t\treturn newSchema\n\t}\n}\n"],"names":["emptySchema","calculateSchemaChecksum","SchemaQuery"],"mappings":";;;;;;;;AAcO,MAAM,uBAAuC;AAAA,EACnD,QAAQA,YAAA;AAAA,EACR,MAAM,CAAA;AACP;AAOO,MAAM,eAAe;AAAA,EAC3B,YACkB,4BACA,gBAChB;AAFgB,SAAA,6BAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIlB,MAAM,0BAA0B,IAA8C;AAC7E,UAAM,gBAAiB,MAAM,KAAK,2BAA2B,cAAc,EAAE;AAE7E,UAAM,gBAAgB,cAAc;AAAA,MACnC,CAAC,QAAQ,cAAc;AACf,eAAA;AAAA,UACN,QAAQ,UAAU,SAAS,WACxB,OAAO,SACP,KAAK,eAAe,mBAAmB,OAAO,QAAQ,UAAU,eAAe,UAAU,aAAa;AAAA,UACzG,MAAM;AAAA,YACL,SAAS,UAAU;AAAA,YACnB,IAAI,UAAU;AAAA,YACd,WAAW,UAAU;AAAA,YACrB,UAAU;AAAA,UAAA;AAAA,QAEZ;AAAA,MACD;AAAA,MACA;AAAA,IACD;AAEI,QAAA,CAAC,cAAc,KAAK,SAAS;AACzB,aAAA;AAAA,IAAA;AAGD,WAAA;AAAA,MACN,QAAQ,cAAc;AAAA,MACtB,MAAM;AAAA,QACL,GAAG,cAAc;AAAA,QACjB,UAAUC,YAAAA,wBAAwB,cAAc,MAAM;AAAA,MAAA;AAAA,IAExD;AAAA,EAAA;AAAA,EAGD,MAAa,MAAM,EAAE,IAAI,iBAGG;AACrB,UAAA,YAAY,MAAM,GAAG,aAAa,MAAM,IAAIC,YAAAA,YAAY,eAAe,KAAK,QAAQ,CAAC;AAC3F,QAAI,CAAC,WAAW;AACf,aAAO,iBAAiB;AAAA,IAAA;AAGlB,WAAA;AAAA,EAAA;AAET;;;"}
@@ -17,11 +17,8 @@ class SchemaProvider {
17
17
  const newMigrations = await this.executedMigrationsResolver.getMigrations(db);
18
18
  const partialResult = newMigrations.reduce(
19
19
  (schema, migration) => {
20
- if (migration.type !== "schema") {
21
- return schema;
22
- }
23
20
  return {
24
- schema: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),
21
+ schema: migration.type !== "schema" ? schema.schema : this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),
25
22
  meta: {
26
23
  version: migration.version,
27
24
  id: migration.id,
@@ -1 +1 @@
1
- {"version":3,"file":"SchemaProvider.js","sources":["../../../../../../packages/engine-system-api/src/model/migrations/SchemaProvider.ts"],"sourcesContent":["import { SchemaQuery } from '../queries'\nimport { DatabaseContext } from '../database'\nimport { calculateSchemaChecksum, emptySchema, normalizeSchema } from '@contember/schema-utils'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { SchemaMigrator } from '@contember/schema-migrations'\nimport { Schema } from '@contember/schema'\n\nexport type SchemaMeta = {\n\tchecksum: string\n\tversion: string\n\tupdatedAt: Date\n\tid: number\n}\n\nexport const emptyVersionedSchema: SchemaWithMeta = {\n\tschema: emptySchema,\n\tmeta: {},\n}\n\nexport type SchemaWithMeta = {\n\tschema: Schema\n\tmeta: SchemaMeta | { [K in keyof SchemaMeta]?: undefined }\n}\n\nexport class SchemaProvider {\n\tconstructor(\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly schemaMigrator: SchemaMigrator,\n\t) {\n\t}\n\n\tasync buildSchemaFromMigrations(db: DatabaseContext): Promise<SchemaWithMeta> {\n\t\tconst newMigrations = (await this.executedMigrationsResolver.getMigrations(db))\n\n\t\tconst partialResult = newMigrations.reduce<SchemaWithMeta>(\n\t\t\t(schema, migration) => {\n\t\t\t\tif (migration.type !== 'schema') {\n\t\t\t\t\treturn schema\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tschema: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tid: migration.id,\n\t\t\t\t\t\tupdatedAt: migration.executedAt,\n\t\t\t\t\t\tchecksum: '',\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t},\n\t\t\temptyVersionedSchema,\n\t\t)\n\n\t\tif (!partialResult.meta.version) {\n\t\t\treturn emptyVersionedSchema\n\t\t}\n\n\t\treturn {\n\t\t\tschema: partialResult.schema,\n\t\t\tmeta: {\n\t\t\t\t...partialResult.meta,\n\t\t\t\tchecksum: calculateSchemaChecksum(partialResult.schema),\n\t\t\t},\n\t\t}\n\t}\n\n\tpublic async fetch({ db, currentSchema }: {\n\t\tdb: DatabaseContext\n\t\tcurrentSchema?: SchemaWithMeta | null\n\t}): Promise<SchemaWithMeta> {\n\t\tconst newSchema = await db.queryHandler.fetch(new SchemaQuery(currentSchema?.meta.checksum))\n\t\tif (!newSchema) {\n\t\t\treturn currentSchema ?? emptyVersionedSchema\n\t\t}\n\n\t\treturn newSchema\n\t}\n}\n"],"names":[],"mappings":";;;;;;AAcO,MAAM,uBAAuC;AAAA,EACnD,QAAQ;AAAA,EACR,MAAM,CAAA;AACP;AAOO,MAAM,eAAe;AAAA,EAC3B,YACkB,4BACA,gBAChB;AAFgB,SAAA,6BAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIlB,MAAM,0BAA0B,IAA8C;AAC7E,UAAM,gBAAiB,MAAM,KAAK,2BAA2B,cAAc,EAAE;AAE7E,UAAM,gBAAgB,cAAc;AAAA,MACnC,CAAC,QAAQ,cAAc;AAClB,YAAA,UAAU,SAAS,UAAU;AACzB,iBAAA;AAAA,QAAA;AAED,eAAA;AAAA,UACN,QAAQ,KAAK,eAAe,mBAAmB,OAAO,QAAQ,UAAU,eAAe,UAAU,aAAa;AAAA,UAC9G,MAAM;AAAA,YACL,SAAS,UAAU;AAAA,YACnB,IAAI,UAAU;AAAA,YACd,WAAW,UAAU;AAAA,YACrB,UAAU;AAAA,UAAA;AAAA,QAEZ;AAAA,MACD;AAAA,MACA;AAAA,IACD;AAEI,QAAA,CAAC,cAAc,KAAK,SAAS;AACzB,aAAA;AAAA,IAAA;AAGD,WAAA;AAAA,MACN,QAAQ,cAAc;AAAA,MACtB,MAAM;AAAA,QACL,GAAG,cAAc;AAAA,QACjB,UAAU,wBAAwB,cAAc,MAAM;AAAA,MAAA;AAAA,IAExD;AAAA,EAAA;AAAA,EAGD,MAAa,MAAM,EAAE,IAAI,iBAGG;AACrB,UAAA,YAAY,MAAM,GAAG,aAAa,MAAM,IAAI,YAAY,eAAe,KAAK,QAAQ,CAAC;AAC3F,QAAI,CAAC,WAAW;AACf,aAAO,iBAAiB;AAAA,IAAA;AAGlB,WAAA;AAAA,EAAA;AAET;"}
1
+ {"version":3,"file":"SchemaProvider.js","sources":["../../../../../../packages/engine-system-api/src/model/migrations/SchemaProvider.ts"],"sourcesContent":["import { SchemaQuery } from '../queries'\nimport { DatabaseContext } from '../database'\nimport { calculateSchemaChecksum, emptySchema, normalizeSchema } from '@contember/schema-utils'\nimport { ExecutedMigrationsResolver } from './ExecutedMigrationsResolver'\nimport { SchemaMigrator } from '@contember/schema-migrations'\nimport { Schema } from '@contember/schema'\n\nexport type SchemaMeta = {\n\tchecksum: string\n\tversion: string\n\tupdatedAt: Date\n\tid: number\n}\n\nexport const emptyVersionedSchema: SchemaWithMeta = {\n\tschema: emptySchema,\n\tmeta: {},\n}\n\nexport type SchemaWithMeta = {\n\tschema: Schema\n\tmeta: SchemaMeta | { [K in keyof SchemaMeta]?: undefined }\n}\n\nexport class SchemaProvider {\n\tconstructor(\n\t\tprivate readonly executedMigrationsResolver: ExecutedMigrationsResolver,\n\t\tprivate readonly schemaMigrator: SchemaMigrator,\n\t) {\n\t}\n\n\tasync buildSchemaFromMigrations(db: DatabaseContext): Promise<SchemaWithMeta> {\n\t\tconst newMigrations = (await this.executedMigrationsResolver.getMigrations(db))\n\n\t\tconst partialResult = newMigrations.reduce<SchemaWithMeta>(\n\t\t\t(schema, migration) => {\n\t\t\t\treturn {\n\t\t\t\t\tschema: migration.type !== 'schema'\n\t\t\t\t\t\t? schema.schema\n\t\t\t\t\t\t: this.schemaMigrator.applyModifications(schema.schema, migration.modifications, migration.formatVersion),\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\tversion: migration.version,\n\t\t\t\t\t\tid: migration.id,\n\t\t\t\t\t\tupdatedAt: migration.executedAt,\n\t\t\t\t\t\tchecksum: '',\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t},\n\t\t\temptyVersionedSchema,\n\t\t)\n\n\t\tif (!partialResult.meta.version) {\n\t\t\treturn emptyVersionedSchema\n\t\t}\n\n\t\treturn {\n\t\t\tschema: partialResult.schema,\n\t\t\tmeta: {\n\t\t\t\t...partialResult.meta,\n\t\t\t\tchecksum: calculateSchemaChecksum(partialResult.schema),\n\t\t\t},\n\t\t}\n\t}\n\n\tpublic async fetch({ db, currentSchema }: {\n\t\tdb: DatabaseContext\n\t\tcurrentSchema?: SchemaWithMeta | null\n\t}): Promise<SchemaWithMeta> {\n\t\tconst newSchema = await db.queryHandler.fetch(new SchemaQuery(currentSchema?.meta.checksum))\n\t\tif (!newSchema) {\n\t\t\treturn currentSchema ?? emptyVersionedSchema\n\t\t}\n\n\t\treturn newSchema\n\t}\n}\n"],"names":[],"mappings":";;;;;;AAcO,MAAM,uBAAuC;AAAA,EACnD,QAAQ;AAAA,EACR,MAAM,CAAA;AACP;AAOO,MAAM,eAAe;AAAA,EAC3B,YACkB,4BACA,gBAChB;AAFgB,SAAA,6BAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAIlB,MAAM,0BAA0B,IAA8C;AAC7E,UAAM,gBAAiB,MAAM,KAAK,2BAA2B,cAAc,EAAE;AAE7E,UAAM,gBAAgB,cAAc;AAAA,MACnC,CAAC,QAAQ,cAAc;AACf,eAAA;AAAA,UACN,QAAQ,UAAU,SAAS,WACxB,OAAO,SACP,KAAK,eAAe,mBAAmB,OAAO,QAAQ,UAAU,eAAe,UAAU,aAAa;AAAA,UACzG,MAAM;AAAA,YACL,SAAS,UAAU;AAAA,YACnB,IAAI,UAAU;AAAA,YACd,WAAW,UAAU;AAAA,YACrB,UAAU;AAAA,UAAA;AAAA,QAEZ;AAAA,MACD;AAAA,MACA;AAAA,IACD;AAEI,QAAA,CAAC,cAAc,KAAK,SAAS;AACzB,aAAA;AAAA,IAAA;AAGD,WAAA;AAAA,MACN,QAAQ,cAAc;AAAA,MACtB,MAAM;AAAA,QACL,GAAG,cAAc;AAAA,QACjB,UAAU,wBAAwB,cAAc,MAAM;AAAA,MAAA;AAAA,IAExD;AAAA,EAAA;AAAA,EAGD,MAAa,MAAM,EAAE,IAAI,iBAGG;AACrB,UAAA,YAAY,MAAM,GAAG,aAAa,MAAM,IAAI,YAAY,eAAe,KAAK,QAAQ,CAAC;AAC3F,QAAI,CAAC,WAAW;AACf,aAAO,iBAAiB;AAAA,IAAA;AAGlB,WAAA;AAAA,EAAA;AAET;"}