@contember/schema-migrations 2.0.2 → 2.1.0-alpha.10

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 (163) hide show
  1. package/dist/development/src/MigrationDescriber.cjs +2 -1
  2. package/dist/development/src/MigrationDescriber.cjs.map +1 -1
  3. package/dist/development/src/MigrationDescriber.js +2 -1
  4. package/dist/development/src/MigrationDescriber.js.map +1 -1
  5. package/dist/development/src/modifications/columns/CreateColumnModification.cjs +2 -1
  6. package/dist/development/src/modifications/columns/CreateColumnModification.cjs.map +1 -1
  7. package/dist/development/src/modifications/columns/CreateColumnModification.js +2 -1
  8. package/dist/development/src/modifications/columns/CreateColumnModification.js.map +1 -1
  9. package/dist/development/src/modifications/columns/UpdateColumnDefinitionModification.cjs +8 -3
  10. package/dist/development/src/modifications/columns/UpdateColumnDefinitionModification.cjs.map +1 -1
  11. package/dist/development/src/modifications/columns/UpdateColumnDefinitionModification.js +8 -3
  12. package/dist/development/src/modifications/columns/UpdateColumnDefinitionModification.js.map +1 -1
  13. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.cjs +15 -9
  14. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.cjs.map +1 -1
  15. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.js +15 -9
  16. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.js.map +1 -1
  17. package/dist/development/src/modifications/constraints/RemoveUniqueConstraintModification.cjs +34 -16
  18. package/dist/development/src/modifications/constraints/RemoveUniqueConstraintModification.cjs.map +1 -1
  19. package/dist/development/src/modifications/constraints/RemoveUniqueConstraintModification.js +34 -16
  20. package/dist/development/src/modifications/constraints/RemoveUniqueConstraintModification.js.map +1 -1
  21. package/dist/development/src/modifications/entities/CreateEntityModification.cjs +2 -1
  22. package/dist/development/src/modifications/entities/CreateEntityModification.cjs.map +1 -1
  23. package/dist/development/src/modifications/entities/CreateEntityModification.js +2 -1
  24. package/dist/development/src/modifications/entities/CreateEntityModification.js.map +1 -1
  25. package/dist/development/src/modifications/entities/CreateViewModification.cjs +12 -2
  26. package/dist/development/src/modifications/entities/CreateViewModification.cjs.map +1 -1
  27. package/dist/development/src/modifications/entities/CreateViewModification.js +12 -2
  28. package/dist/development/src/modifications/entities/CreateViewModification.js.map +1 -1
  29. package/dist/development/src/modifications/entities/RemoveEntityModification.cjs +5 -1
  30. package/dist/development/src/modifications/entities/RemoveEntityModification.cjs.map +1 -1
  31. package/dist/development/src/modifications/entities/RemoveEntityModification.js +5 -1
  32. package/dist/development/src/modifications/entities/RemoveEntityModification.js.map +1 -1
  33. package/dist/development/src/modifications/indexes/CreateIndexModification.cjs +2 -2
  34. package/dist/development/src/modifications/indexes/CreateIndexModification.cjs.map +1 -1
  35. package/dist/development/src/modifications/indexes/CreateIndexModification.js +2 -2
  36. package/dist/development/src/modifications/indexes/CreateIndexModification.js.map +1 -1
  37. package/dist/development/src/modifications/indexes/RemoveIndexModification.cjs +3 -3
  38. package/dist/development/src/modifications/indexes/RemoveIndexModification.cjs.map +1 -1
  39. package/dist/development/src/modifications/indexes/RemoveIndexModification.js +3 -3
  40. package/dist/development/src/modifications/indexes/RemoveIndexModification.js.map +1 -1
  41. package/dist/development/src/modifications/upgrade/RemoveIndexNamesModification.cjs +8 -2
  42. package/dist/development/src/modifications/upgrade/RemoveIndexNamesModification.cjs.map +1 -1
  43. package/dist/development/src/modifications/upgrade/RemoveIndexNamesModification.js +8 -2
  44. package/dist/development/src/modifications/upgrade/RemoveIndexNamesModification.js.map +1 -1
  45. package/dist/development/src/modifications/utils/columnUtils.cjs +1 -1
  46. package/dist/development/src/modifications/utils/columnUtils.cjs.map +1 -1
  47. package/dist/development/src/modifications/utils/columnUtils.js +1 -1
  48. package/dist/development/src/modifications/utils/columnUtils.js.map +1 -1
  49. package/dist/development/src/modifications/utils/schemaUpdateUtils.cjs +1 -0
  50. package/dist/development/src/modifications/utils/schemaUpdateUtils.cjs.map +1 -1
  51. package/dist/development/src/modifications/utils/schemaUpdateUtils.js +1 -0
  52. package/dist/development/src/modifications/utils/schemaUpdateUtils.js.map +1 -1
  53. package/dist/production/src/MigrationDescriber.cjs +2 -1
  54. package/dist/production/src/MigrationDescriber.cjs.map +1 -1
  55. package/dist/production/src/MigrationDescriber.js +2 -1
  56. package/dist/production/src/MigrationDescriber.js.map +1 -1
  57. package/dist/production/src/modifications/columns/CreateColumnModification.cjs +2 -1
  58. package/dist/production/src/modifications/columns/CreateColumnModification.cjs.map +1 -1
  59. package/dist/production/src/modifications/columns/CreateColumnModification.js +2 -1
  60. package/dist/production/src/modifications/columns/CreateColumnModification.js.map +1 -1
  61. package/dist/production/src/modifications/columns/UpdateColumnDefinitionModification.cjs +8 -3
  62. package/dist/production/src/modifications/columns/UpdateColumnDefinitionModification.cjs.map +1 -1
  63. package/dist/production/src/modifications/columns/UpdateColumnDefinitionModification.js +8 -3
  64. package/dist/production/src/modifications/columns/UpdateColumnDefinitionModification.js.map +1 -1
  65. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.cjs +15 -9
  66. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.cjs.map +1 -1
  67. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.js +15 -9
  68. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.js.map +1 -1
  69. package/dist/production/src/modifications/constraints/RemoveUniqueConstraintModification.cjs +34 -16
  70. package/dist/production/src/modifications/constraints/RemoveUniqueConstraintModification.cjs.map +1 -1
  71. package/dist/production/src/modifications/constraints/RemoveUniqueConstraintModification.js +34 -16
  72. package/dist/production/src/modifications/constraints/RemoveUniqueConstraintModification.js.map +1 -1
  73. package/dist/production/src/modifications/entities/CreateEntityModification.cjs +2 -1
  74. package/dist/production/src/modifications/entities/CreateEntityModification.cjs.map +1 -1
  75. package/dist/production/src/modifications/entities/CreateEntityModification.js +2 -1
  76. package/dist/production/src/modifications/entities/CreateEntityModification.js.map +1 -1
  77. package/dist/production/src/modifications/entities/CreateViewModification.cjs +12 -2
  78. package/dist/production/src/modifications/entities/CreateViewModification.cjs.map +1 -1
  79. package/dist/production/src/modifications/entities/CreateViewModification.js +12 -2
  80. package/dist/production/src/modifications/entities/CreateViewModification.js.map +1 -1
  81. package/dist/production/src/modifications/entities/RemoveEntityModification.cjs +5 -1
  82. package/dist/production/src/modifications/entities/RemoveEntityModification.cjs.map +1 -1
  83. package/dist/production/src/modifications/entities/RemoveEntityModification.js +5 -1
  84. package/dist/production/src/modifications/entities/RemoveEntityModification.js.map +1 -1
  85. package/dist/production/src/modifications/indexes/CreateIndexModification.cjs +2 -2
  86. package/dist/production/src/modifications/indexes/CreateIndexModification.cjs.map +1 -1
  87. package/dist/production/src/modifications/indexes/CreateIndexModification.js +2 -2
  88. package/dist/production/src/modifications/indexes/CreateIndexModification.js.map +1 -1
  89. package/dist/production/src/modifications/indexes/RemoveIndexModification.cjs +3 -3
  90. package/dist/production/src/modifications/indexes/RemoveIndexModification.cjs.map +1 -1
  91. package/dist/production/src/modifications/indexes/RemoveIndexModification.js +3 -3
  92. package/dist/production/src/modifications/indexes/RemoveIndexModification.js.map +1 -1
  93. package/dist/production/src/modifications/upgrade/RemoveIndexNamesModification.cjs +8 -2
  94. package/dist/production/src/modifications/upgrade/RemoveIndexNamesModification.cjs.map +1 -1
  95. package/dist/production/src/modifications/upgrade/RemoveIndexNamesModification.js +8 -2
  96. package/dist/production/src/modifications/upgrade/RemoveIndexNamesModification.js.map +1 -1
  97. package/dist/production/src/modifications/utils/columnUtils.cjs +1 -1
  98. package/dist/production/src/modifications/utils/columnUtils.cjs.map +1 -1
  99. package/dist/production/src/modifications/utils/columnUtils.js +1 -1
  100. package/dist/production/src/modifications/utils/columnUtils.js.map +1 -1
  101. package/dist/production/src/modifications/utils/schemaUpdateUtils.cjs +1 -0
  102. package/dist/production/src/modifications/utils/schemaUpdateUtils.cjs.map +1 -1
  103. package/dist/production/src/modifications/utils/schemaUpdateUtils.js +1 -0
  104. package/dist/production/src/modifications/utils/schemaUpdateUtils.js.map +1 -1
  105. package/dist/tests/src/tags.d.ts.map +1 -1
  106. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  107. package/dist/types/Migration.d.ts.map +1 -1
  108. package/dist/types/modifications/ModificationHandler.d.ts.map +1 -1
  109. package/dist/types/modifications/columns/CreateColumnModification.d.ts.map +1 -1
  110. package/dist/types/modifications/columns/UpdateColumnDefinitionModification.d.ts.map +1 -1
  111. package/dist/types/modifications/columns/columnUtils.d.ts.map +1 -1
  112. package/dist/types/modifications/constraints/CreateUniqueConstraintModification.d.ts +2 -2
  113. package/dist/types/modifications/constraints/CreateUniqueConstraintModification.d.ts.map +1 -1
  114. package/dist/types/modifications/constraints/RemoveUniqueConstraintModification.d.ts +7 -2
  115. package/dist/types/modifications/constraints/RemoveUniqueConstraintModification.d.ts.map +1 -1
  116. package/dist/types/modifications/constraints/utils.d.ts.map +1 -1
  117. package/dist/types/modifications/entities/CreateEntityModification.d.ts.map +1 -1
  118. package/dist/types/modifications/entities/CreateViewModification.d.ts +1 -0
  119. package/dist/types/modifications/entities/CreateViewModification.d.ts.map +1 -1
  120. package/dist/types/modifications/entities/RemoveEntityModification.d.ts.map +1 -1
  121. package/dist/types/modifications/enums/enumUtils.d.ts.map +1 -1
  122. package/dist/types/modifications/indexes/utils.d.ts.map +1 -1
  123. package/dist/types/modifications/relations/helpers.d.ts.map +1 -1
  124. package/dist/types/modifications/relations/normalization.d.ts.map +1 -1
  125. package/dist/types/modifications/upgrade/RemoveIndexNamesModification.d.ts.map +1 -1
  126. package/dist/types/modifications/utils/columnUtils.d.ts +1 -1
  127. package/dist/types/modifications/utils/columnUtils.d.ts.map +1 -1
  128. package/dist/types/modifications/utils/createJunctionTable.d.ts.map +1 -1
  129. package/dist/types/modifications/utils/diffUtils.d.ts.map +1 -1
  130. package/dist/types/modifications/utils/getPrimaryColumnType.d.ts.map +1 -1
  131. package/dist/types/modifications/utils/schemaUpdateUtils.d.ts.map +1 -1
  132. package/dist/types/modifications/utils/sqlUpdateUtils.d.ts.map +1 -1
  133. package/dist/types/modifications/utils/stringArrayEquals.d.ts.map +1 -1
  134. package/dist/types/modifications/utils/valueUtils.d.ts.map +1 -1
  135. package/dist/types/modifications/utils/viewDependencies.d.ts.map +1 -1
  136. package/dist/types/tsconfig.tsbuildinfo +1 -1
  137. package/dist/types/utils/PartialEntity.d.ts +1 -1
  138. package/dist/types/utils/PartialEntity.d.ts.map +1 -1
  139. package/dist/types/utils/dbHelpers.d.ts.map +1 -1
  140. package/dist/types/utils/isDefined.d.ts.map +1 -1
  141. package/dist/types/utils/isIt.d.ts.map +1 -1
  142. package/package.json +7 -7
  143. package/src/MigrationDescriber.ts +2 -2
  144. package/src/modifications/columns/CreateColumnModification.ts +1 -0
  145. package/src/modifications/columns/UpdateColumnDefinitionModification.ts +8 -3
  146. package/src/modifications/constraints/CreateUniqueConstraintModification.ts +19 -10
  147. package/src/modifications/constraints/RemoveUniqueConstraintModification.ts +52 -19
  148. package/src/modifications/entities/CreateEntityModification.ts +1 -0
  149. package/src/modifications/entities/CreateViewModification.ts +13 -2
  150. package/src/modifications/entities/RemoveEntityModification.ts +5 -1
  151. package/src/modifications/indexes/CreateIndexModification.ts +2 -2
  152. package/src/modifications/indexes/RemoveIndexModification.ts +3 -3
  153. package/src/modifications/upgrade/RemoveIndexNamesModification.ts +8 -2
  154. package/src/modifications/utils/columnUtils.ts +6 -3
  155. package/src/modifications/utils/schemaUpdateUtils.ts +1 -0
  156. package/src/utils/PartialEntity.ts +1 -1
  157. package/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.ts +1 -0
  158. package/tests/cases/integration/createAndDropIndex.test.ts +1 -0
  159. package/tests/cases/integration/createColumn.test.ts +27 -0
  160. package/tests/cases/integration/createEntity.test.ts +69 -0
  161. package/tests/cases/integration/createUnique.test.ts +91 -1
  162. package/tests/cases/integration/removeField.test.ts +6 -4
  163. package/tests/cases/integration/updateColumnDefinition.test.ts +27 -0
@@ -10,26 +10,41 @@ class RemoveUniqueConstraintModificationHandler {
10
10
  }
11
11
  createSql(builder, { databaseMetadata, invalidateDatabaseMetadata }) {
12
12
  const entity = this.schema.model.entities[this.data.entityName];
13
- if (entity.view) {
13
+ const unique = this.getUnique();
14
+ const shouldExecuteSql = !entity.view || unique.index && entity.view.materialized;
15
+ if (!shouldExecuteSql) {
14
16
  return;
15
17
  }
16
- const fields = this.getFields();
17
18
  const columns = getUniqueConstraintColumns({
18
19
  entity,
19
- fields,
20
+ fields: unique.fields,
20
21
  model: this.schema.model
21
22
  });
22
- const constraintNames = databaseMetadata.uniqueConstraints.filter({ tableName: entity.tableName, columnNames: columns }).getNames();
23
- for (const name of constraintNames) {
24
- builder.sql(`ALTER TABLE ${wrapIdentifier(entity.tableName)} DROP CONSTRAINT ${wrapIdentifier(name)}`);
23
+ if (unique.index) {
24
+ const indexNames = databaseMetadata.indexes.filter({
25
+ tableName: entity.tableName,
26
+ columnNames: columns,
27
+ unique: true
28
+ }).getNames();
29
+ for (const name of indexNames) {
30
+ builder.sql(`DROP INDEX ${wrapIdentifier(name)}`);
31
+ }
32
+ } else {
33
+ const constraintNames = databaseMetadata.uniqueConstraints.filter({
34
+ tableName: entity.tableName,
35
+ columnNames: columns
36
+ }).getNames();
37
+ for (const name of constraintNames) {
38
+ builder.sql(`ALTER TABLE ${wrapIdentifier(entity.tableName)} DROP CONSTRAINT ${wrapIdentifier(name)}`);
39
+ }
25
40
  }
26
41
  invalidateDatabaseMetadata();
27
42
  }
28
43
  getSchemaUpdater() {
29
- const fields = this.getFields();
44
+ const thisUnique = this.getUnique();
30
45
  return updateModel(
31
46
  updateEntity(this.data.entityName, ({ entity }) => {
32
- const unique = entity.unique.filter((it) => !deepEqual(it.fields, fields));
47
+ const unique = entity.unique.filter((it) => !deepEqual(it, thisUnique));
33
48
  return {
34
49
  ...entity,
35
50
  unique
@@ -38,16 +53,19 @@ class RemoveUniqueConstraintModificationHandler {
38
53
  );
39
54
  }
40
55
  describe() {
41
- const fields = this.getFields();
42
- return { message: `Remove unique constraint (${fields.join(", ")}) on entity ${this.data.entityName}` };
56
+ const unique = this.getUnique();
57
+ return { message: `Remove unique ${unique.index ? "index" : "constraint"} (${unique.fields.join(", ")}) on entity ${this.data.entityName}` };
43
58
  }
44
- getFields() {
59
+ getUnique() {
60
+ if ("unique" in this.data) {
61
+ return this.data.unique;
62
+ }
45
63
  const entity = this.schema.model.entities[this.data.entityName];
46
- const fields = "constraintName" in this.data ? entity.unique.find((it) => it.name === this.data.constraintName)?.fields : this.data.fields;
47
- if (!fields) {
64
+ const unique = "constraintName" in this.data ? entity.unique.find((it) => "name" in it && it.name === this.data.constraintName) : entity.unique.find((it) => deepEqual(it.fields, this.data.fields));
65
+ if (!unique) {
48
66
  throw new Error();
49
67
  }
50
- return fields;
68
+ return unique;
51
69
  }
52
70
  }
53
71
  const removeUniqueConstraintModification = createModificationType({
@@ -62,11 +80,11 @@ class RemoveUniqueConstraintDiffer {
62
80
  if (!updatedEntity) {
63
81
  return false;
64
82
  }
65
- return !updatedEntity.unique.find((uniq) => deepEqual(uniq.fields, it.fields) && it.timing === uniq.timing);
83
+ return !updatedEntity.unique.find((uniq) => deepEqual(uniq, it));
66
84
  }).map(
67
85
  (unique) => removeUniqueConstraintModification.createModification({
68
86
  entityName: entity.name,
69
- fields: unique.fields
87
+ unique
70
88
  })
71
89
  )
72
90
  );
@@ -1 +1 @@
1
- {"version":3,"file":"RemoveUniqueConstraintModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/constraints/RemoveUniqueConstraintModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport deepEqual from 'fast-deep-equal'\nimport { getUniqueConstraintColumns } from './utils'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\n\nexport class RemoveUniqueConstraintModificationHandler implements ModificationHandler<RemoveUniqueConstraintModificationData> {\n\tconstructor(private readonly data: RemoveUniqueConstraintModificationData, private readonly schema: Schema) {\n\t}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\treturn\n\t\t}\n\n\t\tconst fields = this.getFields()\n\n\t\tconst columns = getUniqueConstraintColumns({\n\t\t\tentity,\n\t\t\tfields: fields,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst constraintNames = databaseMetadata.uniqueConstraints.filter({ tableName: entity.tableName, columnNames: columns }).getNames()\n\n\t\tfor (const name of constraintNames) {\n\t\t\tbuilder.sql(`ALTER TABLE ${wrapIdentifier(entity.tableName)} DROP CONSTRAINT ${wrapIdentifier(name)}`)\n\t\t}\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\tconst fields = this.getFields()\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => {\n\t\t\t\tconst unique = entity.unique.filter(it => !deepEqual(it.fields, fields))\n\t\t\t\treturn {\n\t\t\t\t\t...entity,\n\t\t\t\t\tunique,\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\tconst fields = this.getFields()\n\t\treturn { message: `Remove unique constraint (${fields.join(', ')}) on entity ${this.data.entityName}` }\n\t}\n\n\tgetFields() {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tconst fields = 'constraintName' in this.data\n\t\t\t? entity.unique.find(it => it.name === this.data.constraintName)?.fields\n\t\t\t: this.data.fields\n\n\t\tif (!fields) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn fields\n\t}\n}\n\nexport const removeUniqueConstraintModification = createModificationType({\n\tid: 'removeUniqueConstraint',\n\thandler: RemoveUniqueConstraintModificationHandler,\n})\n\nexport type RemoveUniqueConstraintModificationData =\n\t| {\n\t\tentityName: string\n\t\tconstraintName: string\n\t\tfields?: never\n\t}\n\t| {\n\t\tentityName: string\n\t\tfields: readonly string[]\n\t\tconstraintName?: never\n\t}\n\n\nexport class RemoveUniqueConstraintDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(originalSchema.model.entities).flatMap(entity =>\n\t\t\tentity.unique\n\t\t\t\t.filter(it => {\n\t\t\t\t\tconst updatedEntity = updatedSchema.model.entities[entity.name]\n\t\t\t\t\tif (!updatedEntity) {\n\t\t\t\t\t\treturn false\n\t\t\t\t\t}\n\t\t\t\t\treturn !updatedEntity.unique.find(uniq => deepEqual(uniq.fields, it.fields) && it.timing === uniq.timing)\n\t\t\t\t})\n\t\t\t\t.map(unique =>\n\t\t\t\t\tremoveUniqueConstraintModification.createModification({\n\t\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\t\tfields: unique.fields,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAaO,MAAM,0CAAiH;AAAA,EAC7H,YAA6B,MAA+D,QAAgB;AAA/E,SAAA,OAAA;AAA+D,SAAA,SAAA;AAAA,EAAA;AAAA,EAGrF,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AAChB;AAAA,IAAA;AAGK,UAAA,SAAS,KAAK,UAAU;AAE9B,UAAM,UAAU,2BAA2B;AAAA,MAC1C;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAED,UAAM,kBAAkB,iBAAiB,kBAAkB,OAAO,EAAE,WAAW,OAAO,WAAW,aAAa,QAAS,CAAA,EAAE,SAAS;AAElI,eAAW,QAAQ,iBAAiB;AAC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,KAAK,QAAQ,GAAG,MAAM,KAAK,GAAG,WAAW,KAAK,MAAM;AAAA,MACxG,CAAA,EACA;AAAA,QAAI,CAAA,WACJ,mCAAmC,mBAAmB;AAAA,UACrD,YAAY,OAAO;AAAA,UACnB,QAAQ,OAAO;AAAA,QACf,CAAA;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEF;"}
1
+ {"version":3,"file":"RemoveUniqueConstraintModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/constraints/RemoveUniqueConstraintModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport deepEqual from 'fast-deep-equal'\nimport { getUniqueConstraintColumns } from './utils'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\n\nexport class RemoveUniqueConstraintModificationHandler implements ModificationHandler<RemoveUniqueConstraintModificationData> {\n\tconstructor(private readonly data: RemoveUniqueConstraintModificationData, private readonly schema: Schema) {\n\t}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tconst unique = this.getUnique()\n\n\t\tconst shouldExecuteSql =\n\t\t\t!entity.view\n\t\t\t|| (unique.index && entity.view.materialized)\n\n\t\tif (!shouldExecuteSql) {\n\t\t\treturn\n\t\t}\n\n\n\t\tconst columns = getUniqueConstraintColumns({\n\t\t\tentity,\n\t\t\tfields: unique.fields,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tif (unique.index) {\n\t\t\tconst indexNames = databaseMetadata.indexes.filter({\n\t\t\t\ttableName: entity.tableName,\n\t\t\t\tcolumnNames: columns,\n\t\t\t\tunique: true,\n\t\t\t}).getNames()\n\n\t\t\tfor (const name of indexNames) {\n\t\t\t\tbuilder.sql(`DROP INDEX ${wrapIdentifier(name)}`)\n\t\t\t}\n\n\t\t} else {\n\n\t\t\tconst constraintNames = databaseMetadata.uniqueConstraints.filter({\n\t\t\t\ttableName: entity.tableName,\n\t\t\t\tcolumnNames: columns,\n\t\t\t}).getNames()\n\n\t\t\tfor (const name of constraintNames) {\n\t\t\t\tbuilder.sql(`ALTER TABLE ${wrapIdentifier(entity.tableName)} DROP CONSTRAINT ${wrapIdentifier(name)}`)\n\t\t\t}\n\t\t}\n\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\tconst thisUnique = this.getUnique()\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => {\n\t\t\t\tconst unique = entity.unique.filter(it => !deepEqual(it, thisUnique))\n\t\t\t\treturn {\n\t\t\t\t\t...entity,\n\t\t\t\t\tunique,\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\tconst unique = this.getUnique()\n\t\treturn { message: `Remove unique ${unique.index ? 'index' : 'constraint'} (${unique.fields.join(', ')}) on entity ${this.data.entityName}` }\n\t}\n\n\tgetUnique(): Model.Unique {\n\t\tif ('unique' in this.data) {\n\t\t\treturn this.data.unique\n\t\t}\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tconst unique = 'constraintName' in this.data\n\t\t\t? entity.unique.find(it => 'name' in it && it.name === this.data.constraintName)\n\t\t\t: entity.unique.find(it => deepEqual(it.fields, this.data.fields))\n\n\t\tif (!unique) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn unique\n\t}\n}\n\nexport const removeUniqueConstraintModification = createModificationType({\n\tid: 'removeUniqueConstraint',\n\thandler: RemoveUniqueConstraintModificationHandler,\n})\n\nexport type RemoveUniqueConstraintModificationData =\n\t| {\n\t\tentityName: string\n\t\tfields?: never\n\t\tconstraintName?: never\n\t\tunique: Model.Unique\n\t}\n\t// deprecated\n\t| {\n\t\tentityName: string\n\t\tconstraintName: string\n\t\tfields?: never\n\t}\n\t| {\n\t\tentityName: string\n\t\tfields: readonly string[]\n\t\tconstraintName?: never\n\t}\n\n\nexport class RemoveUniqueConstraintDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(originalSchema.model.entities).flatMap(entity =>\n\t\t\tentity.unique\n\t\t\t\t.filter(it => {\n\t\t\t\t\tconst updatedEntity = updatedSchema.model.entities[entity.name]\n\t\t\t\t\tif (!updatedEntity) {\n\t\t\t\t\t\treturn false\n\t\t\t\t\t}\n\t\t\t\t\treturn !updatedEntity.unique.find(uniq => deepEqual(uniq, it))\n\t\t\t\t})\n\t\t\t\t.map(unique =>\n\t\t\t\t\tremoveUniqueConstraintModification.createModification({\n\t\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\t\tunique: unique,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAaO,MAAM,0CAAiH;AAAA,EAC7H,YAA6B,MAA+D,QAAgB;AAA/E,SAAA,OAAA;AAA+D,SAAA,SAAA;AAAA,EAAA;AAAA,EAGrF,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AACxD,UAAA,SAAS,KAAK,UAAU;AAE9B,UAAM,mBACL,CAAC,OAAO,QACJ,OAAO,SAAS,OAAO,KAAK;AAEjC,QAAI,CAAC,kBAAkB;AACtB;AAAA,IAAA;AAID,UAAM,UAAU,2BAA2B;AAAA,MAC1C;AAAA,MACA,QAAQ,OAAO;AAAA,MACf,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAED,QAAI,OAAO,OAAO;AACX,YAAA,aAAa,iBAAiB,QAAQ,OAAO;AAAA,QAClD,WAAW,OAAO;AAAA,QAClB,aAAa;AAAA,QACb,QAA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,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,MAAM;AAAA,IAAA;AAEV,WAAA;AAAA,EAAA;AAET;AAEO,MAAM,qCAAqC,uBAAuB;AAAA,EACxE,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAsBM,MAAM,6BAA+C;AAAA,EAC3D,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,eAAe,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAC3D,WAAA,OAAO,OACL,OAAO,CAAM,OAAA;AACb,cAAM,gBAAgB,cAAc,MAAM,SAAS,OAAO,IAAI;AAC9D,YAAI,CAAC,eAAe;AACZ,iBAAA;AAAA,QAAA;AAED,eAAA,CAAC,cAAc,OAAO,KAAK,UAAQ,UAAU,MAAM,EAAE,CAAC;AAAA,MAC7D,CAAA,EACA;AAAA,QAAI,CAAA,WACJ,mCAAmC,mBAAmB;AAAA,UACrD,YAAY,OAAO;AAAA,UACnB;AAAA,QACA,CAAA;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEF;"}
@@ -63,7 +63,8 @@ class CreateEntityDiffer {
63
63
  fields: {
64
64
  [entity.primary]: entity.fields[entity.primary]
65
65
  },
66
- unique: []
66
+ unique: [],
67
+ indexes: []
67
68
  }
68
69
  })
69
70
  );
@@ -1 +1 @@
1
- {"version":3,"file":"CreateEntityModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { createEventTrigger, createEventTrxTrigger } from '../utils/sqlUpdateUtils'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\nimport { getColumnSqlType } from '../utils/columnUtils'\nimport { VERSION_CORRECT_PRIMARY_COLUMN_NAME } from '../ModificationVersions'\n\nexport class CreateEntityModificationHandler implements ModificationHandler<CreateEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: CreateEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, { systemSchema }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.data.entity\n\t\tif (entity.view) {\n\t\t\t// BC\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t\treturn\n\t\t}\n\t\tconst primaryColumn = entity.fields[entity.primary] as Model.AnyColumn\n\n\t\tconst primaryColumnName = this.options.formatVersion >= VERSION_CORRECT_PRIMARY_COLUMN_NAME\n\t\t\t? primaryColumn.columnName\n\t\t\t: primaryColumn.name\n\t\tbuilder.createTable(entity.tableName, {\n\t\t\t[primaryColumnName]: {\n\t\t\t\tprimaryKey: true,\n\t\t\t\ttype: getColumnSqlType(primaryColumn),\n\t\t\t\tnotNull: true,\n\t\t\t\tsequenceGenerated: primaryColumn.sequence,\n\t\t\t},\n\t\t})\n\n\t\tif (entity.eventLog?.enabled !== false) {\n\t\t\tcreateEventTrigger(builder, systemSchema, entity.tableName, [entity.primaryColumn])\n\t\t\tcreateEventTrxTrigger(builder, systemSchema, entity.tableName)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true },\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add entity ${this.data.entity.name}` }\n\t}\n\n}\n\nexport const createEntityModification = createModificationType({\n\tid: 'createEntity',\n\thandler: CreateEntityModificationHandler,\n})\n\nexport class CreateEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !it.view)\n\t\t\t.map(entity =>\n\t\t\t\tcreateEntityModification.createModification({\n\t\t\t\t\tentity: {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields: {\n\t\t\t\t\t\t\t[entity.primary]: entity.fields[entity.primary],\n\t\t\t\t\t\t},\n\t\t\t\t\t\tunique: [],\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t)\n\t}\n}\n\nexport interface CreateEntityModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":["VERSION_CORRECT_PRIMARY_COLUMN_NAME","getColumnSqlType","createEventTrigger","createEventTrxTrigger","updateModel","createModificationType"],"mappings":";;;;;;;AAcO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,EAAE,gBAA2D;AAClG,UAAA,SAAS,KAAK,KAAK;AACzB,QAAI,OAAO,MAAM;AAEhB,cAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AACxD;AAAA,IAAA;AAED,UAAM,gBAAgB,OAAO,OAAO,OAAO,OAAO;AAElD,UAAM,oBAAoB,KAAK,QAAQ,iBAAiBA,qBAAAA,sCACrD,cAAc,aACd,cAAc;AACT,YAAA,YAAY,OAAO,WAAW;AAAA,MACrC,CAAC,iBAAiB,GAAG;AAAA,QACpB,YAAY;AAAA,QACZ,MAAMC,6BAAiB,aAAa;AAAA,QACpC,SAAS;AAAA,QACT,mBAAmB,cAAc;AAAA,MAAA;AAAA,IAClC,CACA;AAEI,QAA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+ {"version":3,"file":"CreateEntityModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { createEventTrigger, createEventTrxTrigger } from '../utils/sqlUpdateUtils'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\nimport { getColumnSqlType } from '../utils/columnUtils'\nimport { VERSION_CORRECT_PRIMARY_COLUMN_NAME } from '../ModificationVersions'\n\nexport class CreateEntityModificationHandler implements ModificationHandler<CreateEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: CreateEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, { systemSchema }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.data.entity\n\t\tif (entity.view) {\n\t\t\t// BC\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t\treturn\n\t\t}\n\t\tconst primaryColumn = entity.fields[entity.primary] as Model.AnyColumn\n\n\t\tconst primaryColumnName = this.options.formatVersion >= VERSION_CORRECT_PRIMARY_COLUMN_NAME\n\t\t\t? primaryColumn.columnName\n\t\t\t: primaryColumn.name\n\t\tbuilder.createTable(entity.tableName, {\n\t\t\t[primaryColumnName]: {\n\t\t\t\tprimaryKey: true,\n\t\t\t\ttype: getColumnSqlType(primaryColumn),\n\t\t\t\tnotNull: true,\n\t\t\t\tsequenceGenerated: primaryColumn.sequence,\n\t\t\t},\n\t\t})\n\n\t\tif (entity.eventLog?.enabled !== false) {\n\t\t\tcreateEventTrigger(builder, systemSchema, entity.tableName, [entity.primaryColumn])\n\t\t\tcreateEventTrxTrigger(builder, systemSchema, entity.tableName)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true },\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add entity ${this.data.entity.name}` }\n\t}\n\n}\n\nexport const createEntityModification = createModificationType({\n\tid: 'createEntity',\n\thandler: CreateEntityModificationHandler,\n})\n\nexport class CreateEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !it.view)\n\t\t\t.map(entity =>\n\t\t\t\tcreateEntityModification.createModification({\n\t\t\t\t\tentity: {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields: {\n\t\t\t\t\t\t\t[entity.primary]: entity.fields[entity.primary],\n\t\t\t\t\t\t},\n\t\t\t\t\t\tunique: [],\n\t\t\t\t\t\tindexes: [],\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t)\n\t}\n}\n\nexport interface CreateEntityModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":["VERSION_CORRECT_PRIMARY_COLUMN_NAME","getColumnSqlType","createEventTrigger","createEventTrxTrigger","updateModel","createModificationType"],"mappings":";;;;;;;AAcO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,EAAE,gBAA2D;AAClG,U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EACjD,WAAW,gBAAwB,eAAuB;AAClD,WAAA,OAAO,OAAO,cAAc,MAAM,QAAQ,EAC/C,OAAO,QAAM,CAAC,eAAe,MAAM,SAAS,GAAG,IAAI,CAAC,EACpD,OAAO,CAAM,OAAA,CAAC,GAAG,IAAI,EACrB;AAAA,MAAI,CAAA,WACJ,yBAAyB,mBAAmB;AAAA,QAC3C,QAAQ;AAAA,UACP,GAAG;AAAA,UACH,QAAQ;AAAA,YACP,CAAC,OAAO,OAAO,GAAG,OAAO,OAAO,OAAO,OAAO;AAAA,UAC/C;AAAA,UACA,QAAQ,CAAC;AAAA,UACT,SAAS,CAAA;AAAA,QAAC;AAAA,MAEX,CAAA;AAAA,IACF;AAAA,EAAA;AAEH;;;;"}
@@ -61,7 +61,8 @@ class CreateEntityDiffer {
61
61
  fields: {
62
62
  [entity.primary]: entity.fields[entity.primary]
63
63
  },
64
- unique: []
64
+ unique: [],
65
+ indexes: []
65
66
  }
66
67
  })
67
68
  );
@@ -1 +1 @@
1
- {"version":3,"file":"CreateEntityModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { createEventTrigger, createEventTrxTrigger } from '../utils/sqlUpdateUtils'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\nimport { getColumnSqlType } from '../utils/columnUtils'\nimport { VERSION_CORRECT_PRIMARY_COLUMN_NAME } from '../ModificationVersions'\n\nexport class CreateEntityModificationHandler implements ModificationHandler<CreateEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: CreateEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, { systemSchema }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.data.entity\n\t\tif (entity.view) {\n\t\t\t// BC\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t\treturn\n\t\t}\n\t\tconst primaryColumn = entity.fields[entity.primary] as Model.AnyColumn\n\n\t\tconst primaryColumnName = this.options.formatVersion >= VERSION_CORRECT_PRIMARY_COLUMN_NAME\n\t\t\t? primaryColumn.columnName\n\t\t\t: primaryColumn.name\n\t\tbuilder.createTable(entity.tableName, {\n\t\t\t[primaryColumnName]: {\n\t\t\t\tprimaryKey: true,\n\t\t\t\ttype: getColumnSqlType(primaryColumn),\n\t\t\t\tnotNull: true,\n\t\t\t\tsequenceGenerated: primaryColumn.sequence,\n\t\t\t},\n\t\t})\n\n\t\tif (entity.eventLog?.enabled !== false) {\n\t\t\tcreateEventTrigger(builder, systemSchema, entity.tableName, [entity.primaryColumn])\n\t\t\tcreateEventTrxTrigger(builder, systemSchema, entity.tableName)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true },\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add entity ${this.data.entity.name}` }\n\t}\n\n}\n\nexport const createEntityModification = createModificationType({\n\tid: 'createEntity',\n\thandler: CreateEntityModificationHandler,\n})\n\nexport class CreateEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !it.view)\n\t\t\t.map(entity =>\n\t\t\t\tcreateEntityModification.createModification({\n\t\t\t\t\tentity: {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields: {\n\t\t\t\t\t\t\t[entity.primary]: entity.fields[entity.primary],\n\t\t\t\t\t\t},\n\t\t\t\t\t\tunique: [],\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t)\n\t}\n}\n\nexport interface CreateEntityModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":[],"mappings":";;;;;AAcO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,EAAE,gBAA2D;AAClG,UAAA,SAAS,KAAK,KAAK;AACzB,QAAI,OAAO,MAAM;AAEhB,cAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AACxD;AAAA,IAAA;AAED,UAAM,gBAAgB,OAAO,OAAO,OAAO,OAAO;AAElD,UAAM,oBAAoB,KAAK,QAAQ,iBAAiB,sCACrD,cAAc,aACd,cAAc;AACT,YAAA,YAAY,OAAO,WAAW;AAAA,MACrC,CAAC,iBAAiB,GAAG;AAAA,QACpB,YAAY;AAAA,QACZ,MAAM,iBAAiB,aAAa;AAAA,QACpC,SAAS;AAAA,QACT,mBAAmB,cAAc;AAAA,MAAA;AAAA,IAClC,CACA;AAEI,QAAA,OAAO,UAAU,YAAY,OAAO;AACxC,yBAAmB,SAAS,cAAc,OAAO,WAAW,CAAC,OAAO,aAAa,CAAC;AAC5D,4BAAA,SAAS,cAAc,OAAO,SAAS;AAAA,IAAA;AAAA,EAC9D;AAAA,EAGM,mBAAkC;AACxC,WAAO,YAAY,CAAC,EAAE,aAAa;AAAA,MAClC,GAAG;AAAA,MACH,UAAU;AAAA,QACT,GAAG,MAAM;AAAA,QACT,CAAC,KAAK,KAAK,OAAO,IAAI,GAAG;AAAA,UACxB,UAAU,EAAE,SAAS,KAAK;AAAA,UAC1B,GAAG,KAAK,KAAK;AAAA,UACb,QAAQ,OAAO,OAAO,KAAK,KAAK,OAAO,MAAM;AAAA,UAC7C,SAAS,OAAO,OAAO,KAAK,KAAK,OAAO,WAAW,CAAE,CAAA;AAAA,QAAA;AAAA,MACtD;AAAA,IACD,EACC;AAAA,EAAA;AAAA,EAIH,WAAW;AACV,WAAO,EAAE,SAAS,cAAc,KAAK,KAAK,OAAO,IAAI,GAAG;AAAA,EAAA;AAG1D;AAEO,MAAM,2BAA2B,uBAAuB;AAAA,EAC9D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAEM,MAAM,mBAAqC;AAAA,EACjD,WAAW,gBAAwB,eAAuB;AAClD,WAAA,OAAO,OAAO,cAAc,MAAM,QAAQ,EAC/C,OAAO,QAAM,CAAC,eAAe,MAAM,SAAS,GAAG,IAAI,CAAC,EACpD,OAAO,CAAM,OAAA,CAAC,GAAG,IAAI,EACrB;AAAA,MAAI,CAAA,WACJ,yBAAyB,mBAAmB;AAAA,QAC3C,QAAQ;AAAA,UACP,GAAG;AAAA,UACH,QAAQ;AAAA,YACP,CAAC,OAAO,OAAO,GAAG,OAAO,OAAO,OAAO,OAAO;AAAA,UAC/C;AAAA,UACA,QAAQ,CAAA;AAAA,QAAC;AAAA,MAEV,CAAA;AAAA,IACF;AAAA,EAAA;AAEH;"}
1
+ {"version":3,"file":"CreateEntityModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { createEventTrigger, createEventTrxTrigger } from '../utils/sqlUpdateUtils'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\nimport { getColumnSqlType } from '../utils/columnUtils'\nimport { VERSION_CORRECT_PRIMARY_COLUMN_NAME } from '../ModificationVersions'\n\nexport class CreateEntityModificationHandler implements ModificationHandler<CreateEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: CreateEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, { systemSchema }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.data.entity\n\t\tif (entity.view) {\n\t\t\t// BC\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t\treturn\n\t\t}\n\t\tconst primaryColumn = entity.fields[entity.primary] as Model.AnyColumn\n\n\t\tconst primaryColumnName = this.options.formatVersion >= VERSION_CORRECT_PRIMARY_COLUMN_NAME\n\t\t\t? primaryColumn.columnName\n\t\t\t: primaryColumn.name\n\t\tbuilder.createTable(entity.tableName, {\n\t\t\t[primaryColumnName]: {\n\t\t\t\tprimaryKey: true,\n\t\t\t\ttype: getColumnSqlType(primaryColumn),\n\t\t\t\tnotNull: true,\n\t\t\t\tsequenceGenerated: primaryColumn.sequence,\n\t\t\t},\n\t\t})\n\n\t\tif (entity.eventLog?.enabled !== false) {\n\t\t\tcreateEventTrigger(builder, systemSchema, entity.tableName, [entity.primaryColumn])\n\t\t\tcreateEventTrxTrigger(builder, systemSchema, entity.tableName)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true },\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add entity ${this.data.entity.name}` }\n\t}\n\n}\n\nexport const createEntityModification = createModificationType({\n\tid: 'createEntity',\n\thandler: CreateEntityModificationHandler,\n})\n\nexport class CreateEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !it.view)\n\t\t\t.map(entity =>\n\t\t\t\tcreateEntityModification.createModification({\n\t\t\t\t\tentity: {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields: {\n\t\t\t\t\t\t\t[entity.primary]: entity.fields[entity.primary],\n\t\t\t\t\t\t},\n\t\t\t\t\t\tunique: [],\n\t\t\t\t\t\tindexes: [],\n\t\t\t\t\t},\n\t\t\t\t}),\n\t\t\t)\n\t}\n}\n\nexport interface CreateEntityModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":[],"mappings":";;;;;AAcO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,EAAE,gBAA2D;AAClG,UAAA,SAAS,KAAK,KAAK;AACzB,QAAI,OAAO,MAAM;AAEhB,cAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AACxD;AAAA,IAAA;AAED,UAAM,gBAAgB,OAAO,OAAO,OAAO,OAAO;AAElD,UAAM,oBAAoB,KAAK,QAAQ,iBAAiB,sCACrD,cAAc,aACd,cAAc;AACT,YAAA,YAAY,OAAO,WAAW;AAAA,MACrC,CAAC,iBAAiB,GAAG;AAAA,QACpB,YAAY;AAAA,QACZ,MAAM,iBAAiB,aAAa;AAAA,QACpC,SAAS;AAAA,QACT,mBAAmB,cAAc;AAAA,MAAA;AAAA,IA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@ -12,7 +12,13 @@ class CreateViewModificationHandler {
12
12
  if (!entity.view) {
13
13
  throw new Error();
14
14
  }
15
- builder.createView(entity.tableName, {}, entity.view.sql);
15
+ if (entity.view.materialized) {
16
+ builder.createMaterializedView(entity.tableName, {
17
+ data: this.data.noData !== true
18
+ }, entity.view.sql);
19
+ } else {
20
+ builder.createView(entity.tableName, {}, entity.view.sql);
21
+ }
16
22
  }
17
23
  getSchemaUpdater() {
18
24
  return schemaUpdateUtils.updateModel(({ model }) => ({
@@ -51,7 +57,11 @@ class CreateViewDiffer {
51
57
  cascadeCreate(updatedSchema.model.entities[dependency]);
52
58
  }
53
59
  modifications.push(createViewModification.createModification({
54
- entity
60
+ entity: {
61
+ ...entity,
62
+ indexes: [],
63
+ unique: []
64
+ }
55
65
  }));
56
66
  };
57
67
  for (const view of newViews) {
@@ -1 +1 @@
1
- {"version":3,"file":"CreateViewModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateViewModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport { createModificationType, Differ, ModificationHandler } from '../ModificationHandler'\nimport { Migration } from '../../Migration'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\n\nexport class CreateViewModificationHandler implements ModificationHandler<CreateViewModificationData> {\n\tconstructor(private readonly data: CreateViewModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder): void {\n\t\tconst entity = this.data.entity\n\t\tif (!entity.view) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true }, // not relevant here...\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add view ${this.data.entity.name}` }\n\t}\n}\n\nexport const createViewModification = createModificationType({\n\tid: 'createView',\n\thandler: CreateViewModificationHandler,\n})\n\nexport class CreateViewDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\tconst newViews = Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !!it.view)\n\t\tconst created = new Set<string>()\n\t\tconst modifications: Migration.Modification[] = []\n\t\tconst cascadeCreate = (entity: Model.Entity) => {\n\t\t\tif (originalSchema.model.entities[entity.name] || created.has(entity.name)) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcreated.add(entity.name)\n\t\t\tfor (const dependency of entity.view?.dependencies ?? []) {\n\t\t\t\tcascadeCreate(updatedSchema.model.entities[dependency])\n\t\t\t}\n\t\t\tmodifications.push(createViewModification.createModification({\n\t\t\t\tentity: entity,\n\t\t\t}))\n\t\t}\n\t\tfor (const view of newViews) {\n\t\t\tcascadeCreate(view)\n\t\t}\n\t\treturn modifications\n\t}\n}\n\nexport interface CreateViewModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":["updateModel","createModificationType"],"mappings":";;;;AAOO,MAAM,8BAAyF;AAAA,EACrG,YAA6B,MAAmD,QAAgB;AAAnE,SAAA,OAAA;AAAmD,SAAA,SAAA;AAAA,EAAA;AAAA,EAEzE,UAAU,SAAiC;AAC3C,UAAA,SAAS,KAAK,KAAK;AACrB,QAAA,CAAC,OAAO,MAAM;AACjB,YAAM,IAAI,MAAM;AAAA,IAAA;AAEjB,YAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AAAA,EAAA;AAAA,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AAQ,UAAU;AAC5B,oBAAc,IAAI;AAAA,IAAA;AAEZ,WAAA;AAAA,EAAA;AAET;;;;"}
1
+ {"version":3,"file":"CreateViewModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateViewModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport { createModificationType, Differ, ModificationHandler } from '../ModificationHandler'\nimport { Migration } from '../../Migration'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\n\nexport class CreateViewModificationHandler implements ModificationHandler<CreateViewModificationData> {\n\tconstructor(private readonly data: CreateViewModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder): void {\n\t\tconst entity = this.data.entity\n\t\tif (!entity.view) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tif (entity.view.materialized) {\n\t\t\tbuilder.createMaterializedView(entity.tableName, {\n\t\t\t\tdata: this.data.noData !== true,\n\t\t\t}, entity.view.sql)\n\t\t} else {\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true }, // not relevant here...\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add view ${this.data.entity.name}` }\n\t}\n}\n\nexport const createViewModification = createModificationType({\n\tid: 'createView',\n\thandler: CreateViewModificationHandler,\n})\n\nexport class CreateViewDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\tconst newViews = Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !!it.view)\n\t\tconst created = new Set<string>()\n\t\tconst modifications: Migration.Modification[] = []\n\t\tconst cascadeCreate = (entity: Model.Entity) => {\n\t\t\tif (originalSchema.model.entities[entity.name] || created.has(entity.name)) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcreated.add(entity.name)\n\t\t\tfor (const dependency of entity.view?.dependencies ?? []) {\n\t\t\t\tcascadeCreate(updatedSchema.model.entities[dependency])\n\t\t\t}\n\t\t\tmodifications.push(createViewModification.createModification({\n\t\t\t\tentity: {\n\t\t\t\t\t...entity,\n\t\t\t\t\tindexes: [],\n\t\t\t\t\tunique: [],\n\t\t\t\t},\n\t\t\t}))\n\t\t}\n\t\tfor (const view of newViews) {\n\t\t\tcascadeCreate(view)\n\t\t}\n\t\treturn modifications\n\t}\n}\n\nexport interface CreateViewModificationData {\n\tentity: PossibleEntityShapeInMigrations\n\tnoData?: boolean\n}\n"],"names":["updateModel","createModificationType"],"mappings":";;;;AAOO,MAAM,8BAAyF;AAAA,EACrG,YAA6B,MAAmD,QAAgB;AAAnE,SAAA,OAAA;AAAmD,SAAA,SAAA;AAAA,EAAA;AAAA,EAEzE,UAAU,SAAiC;AAC3C,UAAA,SAAS,KAAK,KAAK;AACrB,QAAA,CAAC,OAAO,MAAM;AACjB,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,OAAO,KAAK,cAAc;AACrB,cAAA,uBAAuB,OAAO,WAAW;AAAA,QAChD,MAAM,KAAK,KAAK,WAAW;AAAA,MAAA,GACzB,OAAO,KAAK,GAAG;AAAA,IAAA,OACZ;AACN,cAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AAAA,IAAA;AAAA,EACzD;AAAA,EAGM,mBAAkC;AACxC,WAAOA,kBAAY,YAAA,CAAC,EAAE,aAAa;AAAA,MAClC,GA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eAAW,QAAQ,UAAU;AAC5B,oBAAc,IAAI;AAAA,IAAA;AAEZ,WAAA;AAAA,EAAA;AAET;;;;"}
@@ -10,7 +10,13 @@ class CreateViewModificationHandler {
10
10
  if (!entity.view) {
11
11
  throw new Error();
12
12
  }
13
- builder.createView(entity.tableName, {}, entity.view.sql);
13
+ if (entity.view.materialized) {
14
+ builder.createMaterializedView(entity.tableName, {
15
+ data: this.data.noData !== true
16
+ }, entity.view.sql);
17
+ } else {
18
+ builder.createView(entity.tableName, {}, entity.view.sql);
19
+ }
14
20
  }
15
21
  getSchemaUpdater() {
16
22
  return updateModel(({ model }) => ({
@@ -49,7 +55,11 @@ class CreateViewDiffer {
49
55
  cascadeCreate(updatedSchema.model.entities[dependency]);
50
56
  }
51
57
  modifications.push(createViewModification.createModification({
52
- entity
58
+ entity: {
59
+ ...entity,
60
+ indexes: [],
61
+ unique: []
62
+ }
53
63
  }));
54
64
  };
55
65
  for (const view of newViews) {
@@ -1 +1 @@
1
- {"version":3,"file":"CreateViewModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateViewModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport { createModificationType, Differ, ModificationHandler } from '../ModificationHandler'\nimport { Migration } from '../../Migration'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\n\nexport class CreateViewModificationHandler implements ModificationHandler<CreateViewModificationData> {\n\tconstructor(private readonly data: CreateViewModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder): void {\n\t\tconst entity = this.data.entity\n\t\tif (!entity.view) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true }, // not relevant here...\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add view ${this.data.entity.name}` }\n\t}\n}\n\nexport const createViewModification = createModificationType({\n\tid: 'createView',\n\thandler: CreateViewModificationHandler,\n})\n\nexport class CreateViewDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\tconst newViews = Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !!it.view)\n\t\tconst created = new Set<string>()\n\t\tconst modifications: Migration.Modification[] = []\n\t\tconst cascadeCreate = (entity: Model.Entity) => {\n\t\t\tif (originalSchema.model.entities[entity.name] || created.has(entity.name)) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcreated.add(entity.name)\n\t\t\tfor (const dependency of entity.view?.dependencies ?? []) {\n\t\t\t\tcascadeCreate(updatedSchema.model.entities[dependency])\n\t\t\t}\n\t\t\tmodifications.push(createViewModification.createModification({\n\t\t\t\tentity: entity,\n\t\t\t}))\n\t\t}\n\t\tfor (const view of newViews) {\n\t\t\tcascadeCreate(view)\n\t\t}\n\t\treturn modifications\n\t}\n}\n\nexport interface CreateViewModificationData {\n\tentity: PossibleEntityShapeInMigrations\n}\n"],"names":[],"mappings":";;AAOO,MAAM,8BAAyF;AAAA,EACrG,YAA6B,MAAmD,QAAgB;AAAnE,SAAA,OAAA;AAAmD,SAAA,SAAA;AAAA,EAAA;AAAA,EAEzE,UAAU,SAAiC;AAC3C,UAAA,SAAS,KAAK,KAAK;AACrB,QAAA,CAAC,OAAO,MAAM;AACjB,YAAM,IAAI,MAAM;AAAA,IAAA;AAEjB,YAAQ,WAAW,O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,MAAA,CACA,CAAC;AAAA,IACH;AACA,eAAW,QAAQ,UAAU;AAC5B,oBAAc,IAAI;AAAA,IAAA;AAEZ,WAAA;AAAA,EAAA;AAET;"}
1
+ {"version":3,"file":"CreateViewModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/CreateViewModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateModel } from '../utils/schemaUpdateUtils'\nimport { createModificationType, Differ, ModificationHandler } from '../ModificationHandler'\nimport { Migration } from '../../Migration'\nimport { PossibleEntityShapeInMigrations } from '../../utils/PartialEntity.js'\n\nexport class CreateViewModificationHandler implements ModificationHandler<CreateViewModificationData> {\n\tconstructor(private readonly data: CreateViewModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder): void {\n\t\tconst entity = this.data.entity\n\t\tif (!entity.view) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tif (entity.view.materialized) {\n\t\t\tbuilder.createMaterializedView(entity.tableName, {\n\t\t\t\tdata: this.data.noData !== true,\n\t\t\t}, entity.view.sql)\n\t\t} else {\n\t\t\tbuilder.createView(entity.tableName, {}, entity.view.sql)\n\t\t}\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(({ model }) => ({\n\t\t\t...model,\n\t\t\tentities: {\n\t\t\t\t...model.entities,\n\t\t\t\t[this.data.entity.name]: {\n\t\t\t\t\teventLog: { enabled: true }, // not relevant here...\n\t\t\t\t\t...this.data.entity,\n\t\t\t\t\tunique: Object.values(this.data.entity.unique),\n\t\t\t\t\tindexes: Object.values(this.data.entity.indexes ?? []),\n\t\t\t\t},\n\t\t\t},\n\t\t}))\n\t}\n\n\n\tdescribe() {\n\t\treturn { message: `Add view ${this.data.entity.name}` }\n\t}\n}\n\nexport const createViewModification = createModificationType({\n\tid: 'createView',\n\thandler: CreateViewModificationHandler,\n})\n\nexport class CreateViewDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\tconst newViews = Object.values(updatedSchema.model.entities)\n\t\t\t.filter(it => !originalSchema.model.entities[it.name])\n\t\t\t.filter(it => !!it.view)\n\t\tconst created = new Set<string>()\n\t\tconst modifications: Migration.Modification[] = []\n\t\tconst cascadeCreate = (entity: Model.Entity) => {\n\t\t\tif (originalSchema.model.entities[entity.name] || created.has(entity.name)) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcreated.add(entity.name)\n\t\t\tfor (const dependency of entity.view?.dependencies ?? []) {\n\t\t\t\tcascadeCreate(updatedSchema.model.entities[dependency])\n\t\t\t}\n\t\t\tmodifications.push(createViewModification.createModification({\n\t\t\t\tentity: {\n\t\t\t\t\t...entity,\n\t\t\t\t\tindexes: [],\n\t\t\t\t\tunique: [],\n\t\t\t\t},\n\t\t\t}))\n\t\t}\n\t\tfor (const view of newViews) {\n\t\t\tcascadeCreate(view)\n\t\t}\n\t\treturn modifications\n\t}\n}\n\nexport interface CreateViewModificationData {\n\tentity: PossibleEntityShapeInMigrations\n\tnoData?: boolean\n}\n"],"names":[],"mappings":";;AAOO,MAAM,8BAAyF;AAAA,EACrG,YAA6B,MAAmD,QAAgB;AAAnE,SAAA,OAAA;AAAmD,SAAA,SAAA;AAAA,EAAA;AAAA,EAEzE,UAAU,SAAiC;AAC3C,UAAA,SAAS,KAAK,KAAK;AACrB,QAAA,CAAC,OAAO,MAAM;AACjB,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,OAAO,KAAK,cAAc;AACrB,cAAA,uBAAuB,OAAO,WAAW;AAAA,QAChD,MAAM,KAAK,KAAK,WAAW;AAAA,MAAA,GACzB,OAAO,KAAK,GAAG;AAAA,IAAA,OACZ;AACN,cAAQ,WAAW,OAAO,WAAW,CAAA,GAAI,OAAO,KAAK,GAAG;AAAA,IAAA;AAAA,EACzD;AAAA,EAGM,mBAAkC;AACxC,WAAO,YAAY,CAAC,EAAE,aAAa;AAA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,eAAW,QAAQ,UAAU;AAC5B,oBAAc,IAAI;AAAA,IAAA;AAEZ,WAAA;AAAA,EAAA;AAET;"}
@@ -17,7 +17,11 @@ class RemoveEntityModificationHandler {
17
17
  createSql(builder, options) {
18
18
  const entity = this.schema.model.entities[this.data.entityName];
19
19
  if (entity.view) {
20
- builder.dropView(entity.tableName);
20
+ if (entity.view.materialized) {
21
+ builder.dropMaterializedView(entity.tableName);
22
+ } else {
23
+ builder.dropView(entity.tableName);
24
+ }
21
25
  return;
22
26
  }
23
27
  if (this.options.formatVersion >= ModificationVersions.VERSION_REMOVE_REFERENCING_RELATIONS) {
@@ -1 +1 @@
1
- {"version":3,"file":"RemoveEntityModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/RemoveEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Acl, Model, Schema } from '@contember/schema'\nimport {\n\tremoveField,\n\tSchemaUpdater,\n\tupdateAcl,\n\tupdateAclEntities,\n\tupdateAclEveryEntity,\n\tupdateAclEveryPredicate,\n\tupdateAclEveryRole,\n\tupdateModel,\n\tupdateSchema,\n} from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_REFERENCING_RELATIONS } from '../ModificationVersions'\nimport { acceptEveryFieldVisitor, isInverseRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { removeFieldModification } from '../fields'\n\nexport class RemoveEntityModificationHandler implements ModificationHandler<RemoveEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: RemoveEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, options: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\tbuilder.dropView(entity.tableName)\n\t\t\treturn\n\t\t}\n\t\tif (this.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS) {\n\t\t\tthis.getFieldsToRemove(this.schema).forEach(([entityName, fieldName]) => {\n\t\t\t\tconst removeFieldHandler = removeFieldModification.createHandler({ entityName, fieldName }, this.schema, this.options)\n\t\t\t\tremoveFieldHandler.createSql(builder, options)\n\t\t\t})\n\t\t}\n\t\tacceptEveryFieldVisitor(this.schema.model, entity, {\n\t\t\tvisitManyHasManyOwning: ({ relation }) => {\n\t\t\t\tbuilder.dropTable(relation.joiningTable.tableName)\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {},\n\t\t\tvisitColumn: () => {},\n\t\t\tvisitOneHasOneInverse: () => {},\n\t\t\tvisitOneHasOneOwning: () => {},\n\t\t\tvisitManyHasOne: () => {},\n\t\t\tvisitOneHasMany: () => {},\n\t\t})\n\t\tbuilder.dropTable(entity.tableName)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateSchema(\n\t\t\tthis.options.formatVersion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\t({ role }) => ({\n\t\t\t\t\t\t\t...role,\n\t\t\t\t\t\t\tvariables: Object.fromEntries(\n\t\t\t\t\t\t\t\tObject.entries(role.variables).filter(([, variable]) =>\n\t\t\t\t\t\t\t\t\tvariable.type !== Acl.VariableType.entity || variable.entityName !== this.data.entityName,\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEntities(({ entities }) => {\n\t\t\t\t\t\t\tconst { [this.data.entityName]: removed, ...other } = entities\n\t\t\t\t\t\t\treturn other\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName, schema }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\tconst currentEntity = schema.model.entities[entityName]\n\t\t\t\t\t\t\t\treturn processor.process(currentEntity, predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t )\n\t\t\t\t: undefined,\n\t\t\tthis.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS\n\t\t\t\t? ({ schema }) => {\n\t\t\t\t\tconst fieldsToRemove = this.getFieldsToRemove(schema)\n\t\t\t\t\treturn fieldsToRemove.reduce(\n\t\t\t\t\t\t(schema, [entity, field]) => removeField(entity, field, this.options.formatVersion)({ schema }),\n\t\t\t\t\t\tschema,\n\t\t\t\t\t)\n\t\t\t\t }\n\t\t\t\t: undefined,\n\t\t\tupdateModel(({ model }) => {\n\t\t\t\tconst { [this.data.entityName]: removed, ...entities } = model.entities\n\t\t\t\treturn {\n\t\t\t\t\t...model,\n\t\t\t\t\tentities: { ...entities },\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tprivate getFieldsToRemove(schema: Schema): [entity: string, field: string][] {\n\t\treturn Object.values(schema.model.entities).flatMap(entity =>\n\t\t\tObject.values(entity.fields)\n\t\t\t\t.filter((field): field is Model.AnyRelation => isRelation(field) && field.target === this.data.entityName)\n\t\t\t\t.filter(field => !isInverseRelation(field) || entity.name !== this.data.entityName) // skip self-referencing inverse relations\n\t\t\t\t.map((field): [string, string] => [entity.name, field.name]),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn { message: `Remove entity ${this.data.entityName}`, isDestructive: true }\n\t}\n\n}\n\nexport interface RemoveEntityModificationData {\n\tentityName: string\n}\n\nexport const removeEntityModification = createModificationType({\n\tid: 'removeEntity',\n\thandler: RemoveEntityModificationHandler,\n})\n\nexport class RemoveEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.keys(originalSchema.model.entities)\n\t\t\t.filter(name => !updatedSchema.model.entities[name])\n\t\t\t.map(entityName => removeEntityModification.createModification({ entityName }))\n\t}\n}\n"],"names":["schema","VERSION_REMOVE_REFERENCING_RELATIONS","removeFieldModification","acceptEveryFieldVisitor","updateSchema","VERSION_ACL_PATCH","updateAcl","updateAclEveryRole","Acl","updateAclEntities","updateAclEveryEntity","updateAclEveryPredicate","PredicateDefinitionProcessor","removeField","updateModel","isRelation","isInverseRelation","createModificationType"],"mappings":";;;;;;;;;;AAuBO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACAA,SACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,SAAoD;AAC/F,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AACR,cAAA,SAAS,OAAO,SAAS;AACjC;AAAA,IAAA;AAEG,QAAA,KAAK,QAAQ,iBAAiBC,2DAAsC;AAClE,WAAA,kBAAkB,KAAK,MAAM,EAAE,QAAQ,CAAC,CAAC,YAAY,SAAS,MAAM;AAClE,cAAA,qBAAqBC,wBAAAA,wBAAwB,cAAc,EAAE,YAAY,UAAa,GAAA,KAAK,QAAQ,KAAK,OAAO;AAClG,2BAAA,UAAU,SAAS,OAAO;AAAA,MAAA,CAC7C;AAAA,IAAA;AAEsBC,gBAAAA,wBAAA,KAAK,OAAO,OAAO,QAAQ;AAAA,MAClD,wBAAwB,CAAC,EA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C;AAEM,MAAM,mBAAqC;AAAA,EACjD,WAAW,gBAAwB,eAAuB;AAClD,WAAA,OAAO,KAAK,eAAe,MAAM,QAAQ,EAC9C,OAAO,CAAQ,SAAA,CAAC,cAAc,MAAM,SAAS,IAAI,CAAC,EAClD,IAAI,CAAA,eAAc,yBAAyB,mBAAmB,EAAE,WAAW,CAAC,CAAC;AAAA,EAAA;AAEjF;;;;"}
1
+ {"version":3,"file":"RemoveEntityModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/RemoveEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Acl, Model, Schema } from '@contember/schema'\nimport {\n\tremoveField,\n\tSchemaUpdater,\n\tupdateAcl,\n\tupdateAclEntities,\n\tupdateAclEveryEntity,\n\tupdateAclEveryPredicate,\n\tupdateAclEveryRole,\n\tupdateModel,\n\tupdateSchema,\n} from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_REFERENCING_RELATIONS } from '../ModificationVersions'\nimport { acceptEveryFieldVisitor, isInverseRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { removeFieldModification } from '../fields'\n\nexport class RemoveEntityModificationHandler implements ModificationHandler<RemoveEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: RemoveEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, options: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\tif (entity.view.materialized) {\n\t\t\t\tbuilder.dropMaterializedView(entity.tableName)\n\t\t\t} else {\n\t\t\t\tbuilder.dropView(entity.tableName)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (this.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS) {\n\t\t\tthis.getFieldsToRemove(this.schema).forEach(([entityName, fieldName]) => {\n\t\t\t\tconst removeFieldHandler = removeFieldModification.createHandler({ entityName, fieldName }, this.schema, this.options)\n\t\t\t\tremoveFieldHandler.createSql(builder, options)\n\t\t\t})\n\t\t}\n\t\tacceptEveryFieldVisitor(this.schema.model, entity, {\n\t\t\tvisitManyHasManyOwning: ({ relation }) => {\n\t\t\t\tbuilder.dropTable(relation.joiningTable.tableName)\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {},\n\t\t\tvisitColumn: () => {},\n\t\t\tvisitOneHasOneInverse: () => {},\n\t\t\tvisitOneHasOneOwning: () => {},\n\t\t\tvisitManyHasOne: () => {},\n\t\t\tvisitOneHasMany: () => {},\n\t\t})\n\t\tbuilder.dropTable(entity.tableName)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateSchema(\n\t\t\tthis.options.formatVersion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\t({ role }) => ({\n\t\t\t\t\t\t\t...role,\n\t\t\t\t\t\t\tvariables: Object.fromEntries(\n\t\t\t\t\t\t\t\tObject.entries(role.variables).filter(([, variable]) =>\n\t\t\t\t\t\t\t\t\tvariable.type !== Acl.VariableType.entity || variable.entityName !== this.data.entityName,\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEntities(({ entities }) => {\n\t\t\t\t\t\t\tconst { [this.data.entityName]: removed, ...other } = entities\n\t\t\t\t\t\t\treturn other\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName, schema }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\tconst currentEntity = schema.model.entities[entityName]\n\t\t\t\t\t\t\t\treturn processor.process(currentEntity, predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t )\n\t\t\t\t: undefined,\n\t\t\tthis.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS\n\t\t\t\t? ({ schema }) => {\n\t\t\t\t\tconst fieldsToRemove = this.getFieldsToRemove(schema)\n\t\t\t\t\treturn fieldsToRemove.reduce(\n\t\t\t\t\t\t(schema, [entity, field]) => removeField(entity, field, this.options.formatVersion)({ schema }),\n\t\t\t\t\t\tschema,\n\t\t\t\t\t)\n\t\t\t\t }\n\t\t\t\t: undefined,\n\t\t\tupdateModel(({ model }) => {\n\t\t\t\tconst { [this.data.entityName]: removed, ...entities } = model.entities\n\t\t\t\treturn {\n\t\t\t\t\t...model,\n\t\t\t\t\tentities: { ...entities },\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tprivate getFieldsToRemove(schema: Schema): [entity: string, field: string][] {\n\t\treturn Object.values(schema.model.entities).flatMap(entity =>\n\t\t\tObject.values(entity.fields)\n\t\t\t\t.filter((field): field is Model.AnyRelation => isRelation(field) && field.target === this.data.entityName)\n\t\t\t\t.filter(field => !isInverseRelation(field) || entity.name !== this.data.entityName) // skip self-referencing inverse relations\n\t\t\t\t.map((field): [string, string] => [entity.name, field.name]),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn { message: `Remove entity ${this.data.entityName}`, isDestructive: true }\n\t}\n\n}\n\nexport interface RemoveEntityModificationData {\n\tentityName: string\n}\n\nexport const removeEntityModification = createModificationType({\n\tid: 'removeEntity',\n\thandler: RemoveEntityModificationHandler,\n})\n\nexport class RemoveEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.keys(originalSchema.model.entities)\n\t\t\t.filter(name => !updatedSchema.model.entities[name])\n\t\t\t.map(entityName => removeEntityModification.createModification({ entityName }))\n\t}\n}\n"],"names":["schema","VERSION_REMOVE_REFERENCING_RELATIONS","removeFieldModification","acceptEveryFieldVisitor","updateSchema","VERSION_ACL_PATCH","updateAcl","updateAclEveryRole","Acl","updateAclEntities","updateAclEveryEntity","updateAclEveryPredicate","PredicateDefinitionProcessor","removeField","updateModel","isRelation","isInverseRelation","createModificationType"],"mappings":";;;;;;;;;;AAuBO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACAA,SACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,SAAoD;AAC/F,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AACZ,UAAA,OAAO,KAAK,cAAc;AACrB,gBAAA,qBAAqB,OAAO,SAAS;AAAA,MAAA,OACvC;AACE,gBAAA,SAAS,OAAO,SAAS;AAAA,MAAA;AAElC;AAAA,IAAA;AAEG,QAAA,KAAK,QAAQ,iBAAiBC,2DAAsC;AAClE,WAAA,kBAAkB,KAAK,MAAM,EAAE,QAAQ,CAAC,CAAC,YAAY,SAAS,MAAM;AAClE,cAAA,qBAAqBC,wBAAAA,wBAAwB,cAAc,EAAE,YAAY,UAAa,GAAA,KAAK,QAAQ,KAAK,OAAO;AAClG,2BAAA,UAAU,SAAS,OAAO;AAAA,MAAA,CAC7C;AAAA,IAAA;AAEsBC,gBAAAA,wBAAA,KAAK,OAAO,OAAO,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9D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAEM,MAAM,mBAAqC;AAAA,EACjD,WAAW,gBAAwB,eAAuB;AAClD,WAAA,OAAO,KAAK,eAAe,MAAM,QAAQ,EAC9C,OAAO,CAAQ,SAAA,CAAC,cAAc,MAAM,SAAS,IAAI,CAAC,EAClD,IAAI,CAAA,eAAc,yBAAyB,mBAAmB,EAAE,WAAW,CAAC,CAAC;AAAA,EAAA;AAEjF;;;;"}
@@ -15,7 +15,11 @@ class RemoveEntityModificationHandler {
15
15
  createSql(builder, options) {
16
16
  const entity = this.schema.model.entities[this.data.entityName];
17
17
  if (entity.view) {
18
- builder.dropView(entity.tableName);
18
+ if (entity.view.materialized) {
19
+ builder.dropMaterializedView(entity.tableName);
20
+ } else {
21
+ builder.dropView(entity.tableName);
22
+ }
19
23
  return;
20
24
  }
21
25
  if (this.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS) {
@@ -1 +1 @@
1
- {"version":3,"file":"RemoveEntityModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/RemoveEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Acl, Model, Schema } from '@contember/schema'\nimport {\n\tremoveField,\n\tSchemaUpdater,\n\tupdateAcl,\n\tupdateAclEntities,\n\tupdateAclEveryEntity,\n\tupdateAclEveryPredicate,\n\tupdateAclEveryRole,\n\tupdateModel,\n\tupdateSchema,\n} from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_REFERENCING_RELATIONS } from '../ModificationVersions'\nimport { acceptEveryFieldVisitor, isInverseRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { removeFieldModification } from '../fields'\n\nexport class RemoveEntityModificationHandler implements ModificationHandler<RemoveEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: RemoveEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, options: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\tbuilder.dropView(entity.tableName)\n\t\t\treturn\n\t\t}\n\t\tif (this.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS) {\n\t\t\tthis.getFieldsToRemove(this.schema).forEach(([entityName, fieldName]) => {\n\t\t\t\tconst removeFieldHandler = removeFieldModification.createHandler({ entityName, fieldName }, this.schema, this.options)\n\t\t\t\tremoveFieldHandler.createSql(builder, options)\n\t\t\t})\n\t\t}\n\t\tacceptEveryFieldVisitor(this.schema.model, entity, {\n\t\t\tvisitManyHasManyOwning: ({ relation }) => {\n\t\t\t\tbuilder.dropTable(relation.joiningTable.tableName)\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {},\n\t\t\tvisitColumn: () => {},\n\t\t\tvisitOneHasOneInverse: () => {},\n\t\t\tvisitOneHasOneOwning: () => {},\n\t\t\tvisitManyHasOne: () => {},\n\t\t\tvisitOneHasMany: () => {},\n\t\t})\n\t\tbuilder.dropTable(entity.tableName)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateSchema(\n\t\t\tthis.options.formatVersion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\t({ role }) => ({\n\t\t\t\t\t\t\t...role,\n\t\t\t\t\t\t\tvariables: Object.fromEntries(\n\t\t\t\t\t\t\t\tObject.entries(role.variables).filter(([, variable]) =>\n\t\t\t\t\t\t\t\t\tvariable.type !== Acl.VariableType.entity || variable.entityName !== this.data.entityName,\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEntities(({ entities }) => {\n\t\t\t\t\t\t\tconst { [this.data.entityName]: removed, ...other } = entities\n\t\t\t\t\t\t\treturn other\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName, schema }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\tconst currentEntity = schema.model.entities[entityName]\n\t\t\t\t\t\t\t\treturn processor.process(currentEntity, predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t )\n\t\t\t\t: undefined,\n\t\t\tthis.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS\n\t\t\t\t? ({ schema }) => {\n\t\t\t\t\tconst fieldsToRemove = this.getFieldsToRemove(schema)\n\t\t\t\t\treturn fieldsToRemove.reduce(\n\t\t\t\t\t\t(schema, [entity, field]) => removeField(entity, field, this.options.formatVersion)({ schema }),\n\t\t\t\t\t\tschema,\n\t\t\t\t\t)\n\t\t\t\t }\n\t\t\t\t: undefined,\n\t\t\tupdateModel(({ model }) => {\n\t\t\t\tconst { [this.data.entityName]: removed, ...entities } = model.entities\n\t\t\t\treturn {\n\t\t\t\t\t...model,\n\t\t\t\t\tentities: { ...entities },\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tprivate getFieldsToRemove(schema: Schema): [entity: string, field: string][] {\n\t\treturn Object.values(schema.model.entities).flatMap(entity =>\n\t\t\tObject.values(entity.fields)\n\t\t\t\t.filter((field): field is Model.AnyRelation => isRelation(field) && field.target === this.data.entityName)\n\t\t\t\t.filter(field => !isInverseRelation(field) || entity.name !== this.data.entityName) // skip self-referencing inverse relations\n\t\t\t\t.map((field): [string, string] => [entity.name, field.name]),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn { message: `Remove entity ${this.data.entityName}`, isDestructive: true }\n\t}\n\n}\n\nexport interface RemoveEntityModificationData {\n\tentityName: string\n}\n\nexport const removeEntityModification = createModificationType({\n\tid: 'removeEntity',\n\thandler: RemoveEntityModificationHandler,\n})\n\nexport class RemoveEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.keys(originalSchema.model.entities)\n\t\t\t.filter(name => !updatedSchema.model.entities[name])\n\t\t\t.map(entityName => removeEntityModification.createModification({ entityName }))\n\t}\n}\n"],"names":["schema"],"mappings":";;;;;;;;AAuBO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,SAAoD;AAC/F,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AACR,cAAA,SAAS,OAAO,SAAS;AACjC;AAAA,IAAA;AAEG,QAAA,KAAK,QAAQ,iBAAiB,sCAAsC;AAClE,WAAA,kBAAkB,KAAK,MAAM,EAAE,QAAQ,CAAC,CAAC,YAAY,SAAS,MAAM;AAClE,cAAA,qBAAqB,wBAAwB,cAAc,EAAE,YAAY,UAAa,GAAA,KAAK,QAAQ,KAAK,OAAO;AAClG,2BAAA,UAAU,SAAS,OAAO;AAAA,MAAA,CAC7C;AAAA,IAAA;AAEsB,4BAAA,KAAK,OAAO,OAAO,QAAQ;AAAA,MAClD,wBAAwB,CAAC,EAAE,eAAe;AACjC,gBAAA,UAA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,IAAI,CAAC,EAClD,IAAI,CAAA,eAAc,yBAAyB,mBAAmB,EAAE,WAAW,CAAC,CAAC;AAAA,EAAA;AAEjF;"}
1
+ {"version":3,"file":"RemoveEntityModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/entities/RemoveEntityModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Acl, Model, Schema } from '@contember/schema'\nimport {\n\tremoveField,\n\tSchemaUpdater,\n\tupdateAcl,\n\tupdateAclEntities,\n\tupdateAclEveryEntity,\n\tupdateAclEveryPredicate,\n\tupdateAclEveryRole,\n\tupdateModel,\n\tupdateSchema,\n} from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler, ModificationHandlerCreateSqlOptions,\n\tModificationHandlerOptions,\n} from '../ModificationHandler'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_REFERENCING_RELATIONS } from '../ModificationVersions'\nimport { acceptEveryFieldVisitor, isInverseRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { removeFieldModification } from '../fields'\n\nexport class RemoveEntityModificationHandler implements ModificationHandler<RemoveEntityModificationData> {\n\tconstructor(\n\t\tprivate readonly data: RemoveEntityModificationData,\n\t\tprivate readonly schema: Schema,\n\t\tprivate readonly options: ModificationHandlerOptions,\n\t) {}\n\n\tpublic createSql(builder: MigrationBuilder, options: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\tif (entity.view.materialized) {\n\t\t\t\tbuilder.dropMaterializedView(entity.tableName)\n\t\t\t} else {\n\t\t\t\tbuilder.dropView(entity.tableName)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif (this.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS) {\n\t\t\tthis.getFieldsToRemove(this.schema).forEach(([entityName, fieldName]) => {\n\t\t\t\tconst removeFieldHandler = removeFieldModification.createHandler({ entityName, fieldName }, this.schema, this.options)\n\t\t\t\tremoveFieldHandler.createSql(builder, options)\n\t\t\t})\n\t\t}\n\t\tacceptEveryFieldVisitor(this.schema.model, entity, {\n\t\t\tvisitManyHasManyOwning: ({ relation }) => {\n\t\t\t\tbuilder.dropTable(relation.joiningTable.tableName)\n\t\t\t},\n\t\t\tvisitManyHasManyInverse: () => {},\n\t\t\tvisitColumn: () => {},\n\t\t\tvisitOneHasOneInverse: () => {},\n\t\t\tvisitOneHasOneOwning: () => {},\n\t\t\tvisitManyHasOne: () => {},\n\t\t\tvisitOneHasMany: () => {},\n\t\t})\n\t\tbuilder.dropTable(entity.tableName)\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateSchema(\n\t\t\tthis.options.formatVersion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\t({ role }) => ({\n\t\t\t\t\t\t\t...role,\n\t\t\t\t\t\t\tvariables: Object.fromEntries(\n\t\t\t\t\t\t\t\tObject.entries(role.variables).filter(([, variable]) =>\n\t\t\t\t\t\t\t\t\tvariable.type !== Acl.VariableType.entity || variable.entityName !== this.data.entityName,\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEntities(({ entities }) => {\n\t\t\t\t\t\t\tconst { [this.data.entityName]: removed, ...other } = entities\n\t\t\t\t\t\t\treturn other\n\t\t\t\t\t\t}),\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName, schema }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\tconst currentEntity = schema.model.entities[entityName]\n\t\t\t\t\t\t\t\treturn processor.process(currentEntity, predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => {\n\t\t\t\t\t\t\t\t\t\treturn ctx.entity.name === this.data.entityName ? undefined : ctx.value\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t )\n\t\t\t\t: undefined,\n\t\t\tthis.options.formatVersion >= VERSION_REMOVE_REFERENCING_RELATIONS\n\t\t\t\t? ({ schema }) => {\n\t\t\t\t\tconst fieldsToRemove = this.getFieldsToRemove(schema)\n\t\t\t\t\treturn fieldsToRemove.reduce(\n\t\t\t\t\t\t(schema, [entity, field]) => removeField(entity, field, this.options.formatVersion)({ schema }),\n\t\t\t\t\t\tschema,\n\t\t\t\t\t)\n\t\t\t\t }\n\t\t\t\t: undefined,\n\t\t\tupdateModel(({ model }) => {\n\t\t\t\tconst { [this.data.entityName]: removed, ...entities } = model.entities\n\t\t\t\treturn {\n\t\t\t\t\t...model,\n\t\t\t\t\tentities: { ...entities },\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tprivate getFieldsToRemove(schema: Schema): [entity: string, field: string][] {\n\t\treturn Object.values(schema.model.entities).flatMap(entity =>\n\t\t\tObject.values(entity.fields)\n\t\t\t\t.filter((field): field is Model.AnyRelation => isRelation(field) && field.target === this.data.entityName)\n\t\t\t\t.filter(field => !isInverseRelation(field) || entity.name !== this.data.entityName) // skip self-referencing inverse relations\n\t\t\t\t.map((field): [string, string] => [entity.name, field.name]),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn { message: `Remove entity ${this.data.entityName}`, isDestructive: true }\n\t}\n\n}\n\nexport interface RemoveEntityModificationData {\n\tentityName: string\n}\n\nexport const removeEntityModification = createModificationType({\n\tid: 'removeEntity',\n\thandler: RemoveEntityModificationHandler,\n})\n\nexport class RemoveEntityDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.keys(originalSchema.model.entities)\n\t\t\t.filter(name => !updatedSchema.model.entities[name])\n\t\t\t.map(entityName => removeEntityModification.createModification({ entityName }))\n\t}\n}\n"],"names":["schema"],"mappings":";;;;;;;;AAuBO,MAAM,gCAA6F;AAAA,EACzG,YACkB,MACA,QACA,SAChB;AAHgB,SAAA,OAAA;AACA,SAAA,SAAA;AACA,SAAA,UAAA;AAAA,EAAA;AAAA,EAGX,UAAU,SAA2B,SAAoD;AAC/F,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AACZ,UAAA,OAAO,KAAK,cAAc;AACrB,gBAAA,qBAAqB,OAAO,SAAS;AAAA,MAAA,OACvC;AACE,gBAAA,SAAS,OAAO,SAAS;AAAA,MAAA;AAElC;AAAA,IAAA;AAEG,QAAA,KAAK,QAAQ,iBAAiB,sCAAsC;AAClE,WAAA,kBAAkB,KAAK,MAAM,EAAE,QAAQ,CAAC,CAAC,YAAY,SAAS,MAAM;AAClE,cAAA,qBAAqB,wBAAwB,cAAc,EAAE,YAAY,UAAa,GAAA,KAAK,QAAQ,KAAK,OAAO;AAClG,2BAAA,UAAU,SAAS,OAAO;AAAA,MAAA,CAC7C;AAAA,IAAA;AAEsB,4BAAA,KAAK,OAAO,OAAO,QAAQ;AAAA,MAClD,wBAAwB,CAAC,EAAE,eAAe;AACjC,gBAAA,UAAU,SAAS,aAAa,SAAS;AAAA,MAClD;AAAA,MACA,yBAAyB,MAAM;AAAA,MAAC;AAAA,MAChC,aAAa,MAAM;AAAA,MAAC;AAAA,MACpB,uBAAuB,MAAM;AAAA,MAAC;AAAA,MAC9B,sBAAsB,MAAM;AAAA,MAAC;AAAA,MAC7B,iBAAiB,MAAM;AAAA,MAAC;AAAA,MACxB,iBAAiB,MAAM;AAAA,MAAA;AAAA,IAAC,CACxB;AACO,YAAA,UAAU,OAAO,SAAS;A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C,IAAA;AAAA,MACH,YAAY,CAAC,EAAE,YAAY;AACpB,cAAA,EAAE,CAAC,KAAK,KAAK,UAAU,GAAG,SAAS,GAAG,aAAa,MAAM;AACxD,eAAA;AAAA,UACN,GAAG;AAAA,UACH,UAAU,EAAE,GAAG,SAAS;AAAA,QACzB;AAAA,MACA,CAAA;AAAA,IACF;AAAA,EAAA;AAAA,EAGO,kBAAkB,QAAmD;AAC5E,WAAO,OAAO,OAAO,OAAO,MAAM,QAAQ,EAAE;AAAA,MAAQ,YACnD,OAAO,OAAO,OAAO,MAAM,EACzB,OAAO,CAAC,UAAsC,WAAW,KAAK,KAAK,MAAM,WAAW,KAAK,KAAK,UAAU,EACxG,OAAO,CAAS,UAAA,CAAC,kBAAkB,KAAK,KAAK,OAAO,SAAS,KAAK,KAAK,UAAU,EACjF,IAAI,CAAC,UAA4B,CAAC,OAAO,MAAM,MAAM,IAAI,CAAC;AAAA,IAC7D;AAAA,EAAA;AAAA,EAGD,WAAW;AACH,WAAA,EAAE,SAAS,iBAAiB,KAAK,KAAK,UAAU,IAAI,eAAe,KAAK;AAAA,EAAA;AAGjF;AAMO,MAAM,2BAA2B,uBAAuB;AAAA,EAC9D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAEM,MAAM,mBAAqC;AAAA,EACjD,WAAW,gBAAwB,eAAuB;AAClD,WAAA,OAAO,KAAK,eAAe,MAAM,QAAQ,EAC9C,OAAO,CAAQ,SAAA,CAAC,cAAc,MAAM,SAAS,IAAI,CAAC,EAClD,IAAI,CAAA,eAAc,yBAAyB,mBAAmB,EAAE,WAAW,CAAC,CAAC;AAAA,EAAA;AAEjF;"}
@@ -12,7 +12,7 @@ class CreateIndexModificationHandler {
12
12
  }
13
13
  createSql(builder, { databaseMetadata, invalidateDatabaseMetadata }) {
14
14
  const entity = this.schema.model.entities[this.data.entityName];
15
- if (entity.view) {
15
+ if (entity.view && !entity.view.materialized) {
16
16
  return;
17
17
  }
18
18
  const fields = this.data.index.fields;
@@ -50,7 +50,7 @@ const createIndexModification = ModificationHandler.createModificationType({
50
50
  class CreateIndexDiffer {
51
51
  createDiff(originalSchema, updatedSchema) {
52
52
  return Object.values(updatedSchema.model.entities).flatMap(
53
- (entity) => entity.indexes.filter((it) => !originalSchema.model.entities[entity.name].indexes.find((idx) => deepEqual(idx.fields, it.fields))).map((index) => createIndexModification.createModification({
53
+ (entity) => entity.indexes.filter((it) => !originalSchema.model.entities[entity.name].indexes.find((idx) => deepEqual(idx, it))).map((index) => createIndexModification.createModification({
54
54
  entityName: entity.name,
55
55
  index
56
56
  }))
@@ -1 +1 @@
1
- {"version":3,"file":"CreateIndexModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/CreateIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\nimport { getIndexColumns } from './utils'\nimport deepEqual from 'fast-deep-equal'\n\nexport class CreateIndexModificationHandler implements ModificationHandler<CreateIndexModificationData> {\n\tconstructor(private readonly data: CreateIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.data.index.fields\n\t\tconst columns = getIndexColumns({\n\t\t\tfields,\n\t\t\tentity,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst tableNameId = wrapIdentifier(entity.tableName)\n\t\tconst columnNameIds = columns.map(wrapIdentifier)\n\n\t\tbuilder.sql(`CREATE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})`)\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => ({\n\t\t\t\t...entity,\n\t\t\t\tindexes: [\n\t\t\t\t\t...entity.indexes,\n\t\t\t\t\tthis.data.index,\n\t\t\t\t],\n\t\t\t})),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn {\n\t\t\tmessage: `Create index(${this.data.index.fields.join(', ')}) on entity ${this.data.entityName}`,\n\t\t}\n\t}\n}\n\nexport const createIndexModification = createModificationType({\n\tid: 'createIndex',\n\thandler: CreateIndexModificationHandler,\n})\n\nexport interface CreateIndexModificationData {\n\tentityName: string\n\tindex: Model.Index\n}\n\nexport class CreateIndexDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(it => !originalSchema.model.entities[entity.name].indexes.find(idx => deepEqual(idx.fields, it.fields)))\n\t\t\t\t.map(index => createIndexModification.createModification({\n\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\tindex,\n\t\t\t\t})),\n\t\t)\n\t}\n}\n"],"names":["getIndexColumns","wrapIdentifier","updateModel","updateEntity","createModificationType"],"mappings":";;;;;;;AAaO,MAAM,+BAA2F;AAAA,EACvG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AAChB;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,KAAK,MAAM;AAC/B,UAAM,UAAUA,MAAAA,gBAAgB;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAEK,UAAA,cAAcC,UAAAA,eAAe,OAAO,SAAS;AAC7C,UAAA,gBAAgB,QAAQ,IAAIA,wBAAc;AAExC,YAAA,IAAI,mBAAmB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC,GAAG;AAE/C,+BAAA;AAAA,EAAA;AAAA,EAGrB,mBAAkC;AACjC,WAAAC,kBAAA;AAAA,MACNC,+BAAa,KAAK,KAAK,YAAY,CAAC,EAAE,cAAc;AAAA,QACnD,GAAG;AAAA,QACH,SAAS;AAAA,UACR,GAAG,OAAO;AAAA,UACV,KAAK,KAAK;AAAA,QAAA;AAAA,MACX,EACC;AAAA,IACH;AAAA,EAAA;AAAA,EAGD,WAAW;AACH,WAAA;AAAA,MACN,SAAS,gBAAgB,KAAK,KAAK,MAAM,OAAO,KAAK,IAAI,CAAC,eAAe,KAAK,KAAK,UAAU;AAAA,IAC9F;AAAA,EAAA;AAEF;AAEO,MAAM,0BAA0BC,oBAAAA,uBAAuB;AAAA,EAC7D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAOM,MAAM,kBAAoC;AAAA,EAChD,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,cAAc,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAAA,WAC1D,OAAO,QACL,OAAO,CAAA,OAAM,CAAC,eAAe,MAAM,SAAS,OAAO,IAAI,EAAE,QAAQ,KAAK,CAAO,QAAA,UAAU,IAAI,QAAQ,GAAG,MAAM,CAAC,CAAC,EAC9G,IAAI,CAAS,UAAA,wBAAwB,mBAAmB;AAAA,QACxD,YAAY,OAAO;AAAA,QACnB;AAAA,MAAA,CACA,CAAC;AAAA,IACJ;AAAA,EAAA;AAEF;;;;"}
1
+ {"version":3,"file":"CreateIndexModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/CreateIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\nimport { getIndexColumns } from './utils'\nimport deepEqual from 'fast-deep-equal'\n\nexport class CreateIndexModificationHandler implements ModificationHandler<CreateIndexModificationData> {\n\tconstructor(private readonly data: CreateIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view && !entity.view.materialized) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.data.index.fields\n\t\tconst columns = getIndexColumns({\n\t\t\tfields,\n\t\t\tentity,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst tableNameId = wrapIdentifier(entity.tableName)\n\t\tconst columnNameIds = columns.map(wrapIdentifier)\n\n\t\tbuilder.sql(`CREATE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})`)\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => ({\n\t\t\t\t...entity,\n\t\t\t\tindexes: [\n\t\t\t\t\t...entity.indexes,\n\t\t\t\t\tthis.data.index,\n\t\t\t\t],\n\t\t\t})),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn {\n\t\t\tmessage: `Create index(${this.data.index.fields.join(', ')}) on entity ${this.data.entityName}`,\n\t\t}\n\t}\n}\n\nexport const createIndexModification = createModificationType({\n\tid: 'createIndex',\n\thandler: CreateIndexModificationHandler,\n})\n\nexport interface CreateIndexModificationData {\n\tentityName: string\n\tindex: Model.Index\n}\n\nexport class CreateIndexDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(it => !originalSchema.model.entities[entity.name].indexes.find(idx => deepEqual(idx, it)))\n\t\t\t\t.map(index => createIndexModification.createModification({\n\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\tindex,\n\t\t\t\t})),\n\t\t)\n\t}\n}\n"],"names":["getIndexColumns","wrapIdentifier","updateModel","updateEntity","createModificationType"],"mappings":";;;;;;;AAaO,MAAM,+BAA2F;AAAA,EACvG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,QAAQ,CAAC,OAAO,KAAK,cAAc;AAC7C;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,KAAK,MAAM;AAC/B,UAAM,UAAUA,MAAAA,gBAAgB;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAEK,UAAA,cAAcC,UAAAA,eAAe,OAAO,SAAS;AAC7C,UAAA,gBAAgB,QAAQ,IAAIA,wBAAc;AAExC,YAAA,IAAI,mBAAmB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC,GAAG;AAE/C,+BAAA;AAAA,EAAA;AAAA,EAGrB,mBAAkC;AACjC,WAAAC,kBAAA;AAAA,MACNC,+BAAa,KAAK,KAAK,YAAY,CAAC,EAAE,cAAc;AAAA,QACnD,GAAG;AAAA,QACH,SAAS;AAAA,UACR,GAAG,OAAO;AAAA,UACV,KAAK,KAAK;AAAA,QAAA;AAAA,MACX,EACC;AAAA,IACH;AAAA,EAAA;AAAA,EAGD,WAAW;AACH,WAAA;AAAA,MACN,SAAS,gBAAgB,KAAK,KAAK,MAAM,OAAO,KAAK,IAAI,CAAC,eAAe,KAAK,KAAK,UAAU;AAAA,IAC9F;AAAA,EAAA;AAEF;AAEO,MAAM,0BAA0BC,oBAAAA,uBAAuB;AAAA,EAC7D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAOM,MAAM,kBAAoC;AAAA,EAChD,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,cAAc,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAAA,WAC1D,OAAO,QACL,OAAO,CAAA,OAAM,CAAC,eAAe,MAAM,SAAS,OAAO,IAAI,EAAE,QAAQ,KAAK,CAAA,QAAO,UAAU,KAAK,EAAE,CAAC,CAAC,EAChG,IAAI,CAAS,UAAA,wBAAwB,mBAAmB;AAAA,QACxD,YAAY,OAAO;AAAA,QACnB;AAAA,MAAA,CACA,CAAC;AAAA,IACJ;AAAA,EAAA;AAEF;;;;"}
@@ -10,7 +10,7 @@ class CreateIndexModificationHandler {
10
10
  }
11
11
  createSql(builder, { databaseMetadata, invalidateDatabaseMetadata }) {
12
12
  const entity = this.schema.model.entities[this.data.entityName];
13
- if (entity.view) {
13
+ if (entity.view && !entity.view.materialized) {
14
14
  return;
15
15
  }
16
16
  const fields = this.data.index.fields;
@@ -48,7 +48,7 @@ const createIndexModification = createModificationType({
48
48
  class CreateIndexDiffer {
49
49
  createDiff(originalSchema, updatedSchema) {
50
50
  return Object.values(updatedSchema.model.entities).flatMap(
51
- (entity) => entity.indexes.filter((it) => !originalSchema.model.entities[entity.name].indexes.find((idx) => deepEqual(idx.fields, it.fields))).map((index) => createIndexModification.createModification({
51
+ (entity) => entity.indexes.filter((it) => !originalSchema.model.entities[entity.name].indexes.find((idx) => deepEqual(idx, it))).map((index) => createIndexModification.createModification({
52
52
  entityName: entity.name,
53
53
  index
54
54
  }))
@@ -1 +1 @@
1
- {"version":3,"file":"CreateIndexModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/CreateIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\nimport { getIndexColumns } from './utils'\nimport deepEqual from 'fast-deep-equal'\n\nexport class CreateIndexModificationHandler implements ModificationHandler<CreateIndexModificationData> {\n\tconstructor(private readonly data: CreateIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.data.index.fields\n\t\tconst columns = getIndexColumns({\n\t\t\tfields,\n\t\t\tentity,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst tableNameId = wrapIdentifier(entity.tableName)\n\t\tconst columnNameIds = columns.map(wrapIdentifier)\n\n\t\tbuilder.sql(`CREATE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})`)\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => ({\n\t\t\t\t...entity,\n\t\t\t\tindexes: [\n\t\t\t\t\t...entity.indexes,\n\t\t\t\t\tthis.data.index,\n\t\t\t\t],\n\t\t\t})),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn {\n\t\t\tmessage: `Create index(${this.data.index.fields.join(', ')}) on entity ${this.data.entityName}`,\n\t\t}\n\t}\n}\n\nexport const createIndexModification = createModificationType({\n\tid: 'createIndex',\n\thandler: CreateIndexModificationHandler,\n})\n\nexport interface CreateIndexModificationData {\n\tentityName: string\n\tindex: Model.Index\n}\n\nexport class CreateIndexDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(it => !originalSchema.model.entities[entity.name].indexes.find(idx => deepEqual(idx.fields, it.fields)))\n\t\t\t\t.map(index => createIndexModification.createModification({\n\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\tindex,\n\t\t\t\t})),\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAaO,MAAM,+BAA2F;AAAA,EACvG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AAChB;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,KAAK,MAAM;AAC/B,UAAM,UAAU,gBAAgB;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAEK,UAAA,cAAc,eAAe,OAAO,SAAS;AAC7C,UAAA,gBAAgB,QAAQ,IAAI,cAAc;AAExC,YAAA,IAAI,mBAAmB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC,GAAG;AAE/C,+BAAA;AAAA,EAAA;AAAA,EAGrB,mBAAkC;AACjC,WAAA;AAAA,MACN,aAAa,KAAK,KAAK,YAAY,CAAC,EAAE,cAAc;AAAA,QACnD,GAAG;AAAA,QACH,SAAS;AAAA,UACR,GAAG,OAAO;AAAA,UACV,KAAK,KAAK;AAAA,QAAA;AAAA,MACX,EACC;AAAA,IACH;AAAA,EAAA;AAAA,EAGD,WAAW;AACH,WAAA;AAAA,MACN,SAAS,gBAAgB,KAAK,KAAK,MAAM,OAAO,KAAK,IAAI,CAAC,eAAe,KAAK,KAAK,UAAU;AAAA,IAC9F;AAAA,EAAA;AAEF;AAEO,MAAM,0BAA0B,uBAAuB;AAAA,EAC7D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAOM,MAAM,kBAAoC;AAAA,EAChD,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,cAAc,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAAA,WAC1D,OAAO,QACL,OAAO,CAAA,OAAM,CAAC,eAAe,MAAM,SAAS,OAAO,IAAI,EAAE,QAAQ,KAAK,CAAO,QAAA,UAAU,IAAI,QAAQ,GAAG,MAAM,CAAC,CAAC,EAC9G,IAAI,CAAS,UAAA,wBAAwB,mBAAmB;AAAA,QACxD,YAAY,OAAO;AAAA,QACnB;AAAA,MAAA,CACA,CAAC;AAAA,IACJ;AAAA,EAAA;AAEF;"}
1
+ {"version":3,"file":"CreateIndexModification.js","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/CreateIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Model, Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\nimport { getIndexColumns } from './utils'\nimport deepEqual from 'fast-deep-equal'\n\nexport class CreateIndexModificationHandler implements ModificationHandler<CreateIndexModificationData> {\n\tconstructor(private readonly data: CreateIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view && !entity.view.materialized) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.data.index.fields\n\t\tconst columns = getIndexColumns({\n\t\t\tfields,\n\t\t\tentity,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst tableNameId = wrapIdentifier(entity.tableName)\n\t\tconst columnNameIds = columns.map(wrapIdentifier)\n\n\t\tbuilder.sql(`CREATE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})`)\n\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => ({\n\t\t\t\t...entity,\n\t\t\t\tindexes: [\n\t\t\t\t\t...entity.indexes,\n\t\t\t\t\tthis.data.index,\n\t\t\t\t],\n\t\t\t})),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\treturn {\n\t\t\tmessage: `Create index(${this.data.index.fields.join(', ')}) on entity ${this.data.entityName}`,\n\t\t}\n\t}\n}\n\nexport const createIndexModification = createModificationType({\n\tid: 'createIndex',\n\thandler: CreateIndexModificationHandler,\n})\n\nexport interface CreateIndexModificationData {\n\tentityName: string\n\tindex: Model.Index\n}\n\nexport class CreateIndexDiffer implements Differ {\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(updatedSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(it => !originalSchema.model.entities[entity.name].indexes.find(idx => deepEqual(idx, it)))\n\t\t\t\t.map(index => createIndexModification.createModification({\n\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\tindex,\n\t\t\t\t})),\n\t\t)\n\t}\n}\n"],"names":[],"mappings":";;;;;AAaO,MAAM,+BAA2F;AAAA,EACvG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,QAAQ,CAAC,OAAO,KAAK,cAAc;AAC7C;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,KAAK,MAAM;AAC/B,UAAM,UAAU,gBAAgB;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAEK,UAAA,cAAc,eAAe,OAAO,SAAS;AAC7C,UAAA,gBAAgB,QAAQ,IAAI,cAAc;AAExC,YAAA,IAAI,mBAAmB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC,GAAG;AAE/C,+BAAA;AAAA,EAAA;AAAA,EAGrB,mBAAkC;AACjC,WAAA;AAAA,MACN,aAAa,KAAK,KAAK,YAAY,CAAC,EAAE,cAAc;AAAA,QACnD,GAAG;AAAA,QACH,SAAS;AAAA,UACR,GAAG,OAAO;AAAA,UACV,KAAK,KAAK;AAAA,QAAA;AAAA,MACX,EACC;AAAA,IACH;AAAA,EAAA;AAAA,EAGD,WAAW;AACH,WAAA;AAAA,MACN,SAAS,gBAAgB,KAAK,KAAK,MAAM,OAAO,KAAK,IAAI,CAAC,eAAe,KAAK,KAAK,UAAU;AAAA,IAC9F;AAAA,EAAA;AAEF;AAEO,MAAM,0BAA0B,uBAAuB;AAAA,EAC7D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAOM,MAAM,kBAAoC;AAAA,EAChD,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,cAAc,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAAA,WAC1D,OAAO,QACL,OAAO,CAAA,OAAM,CAAC,eAAe,MAAM,SAAS,OAAO,IAAI,EAAE,QAAQ,KAAK,CAAA,QAAO,UAAU,KAAK,EAAE,CAAC,CAAC,EAChG,IAAI,CAAS,UAAA,wBAAwB,mBAAmB;AAAA,QACxD,YAAY,OAAO;AAAA,QACnB;AAAA,MAAA,CACA,CAAC;AAAA,IACJ;AAAA,EAAA;AAEF;"}
@@ -12,7 +12,7 @@ class RemoveIndexModificationHandler {
12
12
  }
13
13
  createSql(builder, { databaseMetadata, invalidateDatabaseMetadata }) {
14
14
  const entity = this.schema.model.entities[this.data.entityName];
15
- if (entity.view) {
15
+ if (entity.view && !entity.view.materialized) {
16
16
  return;
17
17
  }
18
18
  const fields = this.getFields();
@@ -21,7 +21,7 @@ class RemoveIndexModificationHandler {
21
21
  fields,
22
22
  model: this.schema.model
23
23
  });
24
- const indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns }).getNames();
24
+ const indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns, unique: false }).getNames();
25
25
  for (const name of indexNames) {
26
26
  builder.sql(`DROP INDEX ${dbHelpers.wrapIdentifier(name)}`);
27
27
  }
@@ -65,7 +65,7 @@ class RemoveIndexDiffer {
65
65
  if (!updatedEntity) {
66
66
  return false;
67
67
  }
68
- return !updatedEntity.indexes.find((index) => deepEqual(index.fields, it.fields));
68
+ return !updatedEntity.indexes.find((index) => deepEqual(index, it));
69
69
  }
70
70
  ).map(
71
71
  (index) => removeIndexModification.createModification({
@@ -1 +1 @@
1
- {"version":3,"file":"RemoveIndexModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/RemoveIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport deepEqual from 'fast-deep-equal'\nimport { getIndexColumns } from './utils'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\n\nexport class RemoveIndexModificationHandler implements ModificationHandler<RemoveIndexModificationData> {\n\tconstructor(private readonly data: RemoveIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.getFields()\n\n\t\tconst columns = getIndexColumns({\n\t\t\tentity,\n\t\t\tfields,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns }).getNames()\n\n\t\tfor (const name of indexNames) {\n\t\t\tbuilder.sql(`DROP INDEX ${wrapIdentifier(name)}`)\n\t\t}\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\tconst fields = this.getFields()\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => {\n\t\t\t\tconst indexes = entity.indexes.filter(it => !deepEqual(it.fields, fields))\n\t\t\t\treturn {\n\t\t\t\t\t...entity,\n\t\t\t\t\tindexes,\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\tconst fields = this.getFields()\n\t\treturn { message: `Remove index (${fields.join(', ')}) on entity ${this.data.entityName}` }\n\t}\n\n\tgetFields() {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tconst fields = 'indexName' in this.data\n\t\t\t? entity.indexes.find(it => it.name === this.data.indexName)?.fields\n\t\t\t: this.data.fields\n\t\tif (!fields) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn fields\n\t}\n}\n\nexport const removeIndexModification = createModificationType({\n\tid: 'removeIndex',\n\thandler: RemoveIndexModificationHandler,\n})\n\n\nexport type RemoveIndexModificationData =\n\t| {\n\t\tentityName: string\n\t\tindexName: string\n\t\tfields?: never\n\t}\n\t| {\n\t\tentityName: string\n\t\tfields: readonly string[]\n\t\tindexName?: never\n\t}\n\nexport class RemoveIndexDiffer implements Differ {\n\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(originalSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(\n\t\t\t\t\tit => {\n\t\t\t\t\t\tconst updatedEntity = updatedSchema.model.entities[entity.name]\n\t\t\t\t\t\tif (!updatedEntity) {\n\t\t\t\t\t\t\treturn false\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !updatedEntity.indexes.find(index => deepEqual(index.fields, it.fields))\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t\t.map(index =>\n\t\t\t\t\tremoveIndexModification.createModification({\n\t\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\t\tfields: index.fields,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t)\n\t}\n}\n"],"names":["getIndexColumns","wrapIdentifier","updateModel","updateEntity","createModificationType"],"mappings":";;;;;;;AAaO,MAAM,+BAA4F;AAAA,EACxG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,MAAM;AAChB;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,UAAU;AAE9B,UAAM,UAAUA,MAAAA,gBAAgB;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,OAAO,KAAK,OAAO;AAAA,IAAA,CACnB;AAED,UAAM,aAAa,iBAAiB,QAAQ,OAAO,EAAE,WAAW,OAAO,WAAW,aAAa,QAAS,CAAA,EAAE,SAAS;AAEnH,eAAW,QAAQ,YAAY;AAC9B,cAAQ,IAAI,cAAcC,UAAAA,eAAe,IAAI,CAAC,EAAE;AAAA,IAAA;AAEtB,+BAAA;AAAA,EAAA;AAAA,EAGrB,mBAAkC;AAClC,UAAA,SAAS,KAAK,UAAU;AACvB,WAAAC,kBAAA;AAAA,MACNC,+BAAa,KAAK,KAAK,YAAY,CAAC,EAAE,aAAa;AAC5C,cAAA,UAAU,OAAO,QAAQ,OAAO,CAAA,OAAM,CAAC,UAAU,GAAG,QAAQ,MAAM,CAAC;AAClE,eAAA;AAAA,UACN,GAAG;AAAA,UACH;AAAA,QACD;AAAA,MACA,CAAA;AAAA,IACF;AAAA,EAAA;AAAA,EAGD,WAAW;AACJ,UAAA,SAAS,KAAK,UAAU;AACvB,WAAA,EAAE,SAAS,iBAAiB,OAAO,KAAK,IAAI,CAAC,eAAe,KAAK,KAAK,UAAU,GAAG;AAAA,EAAA;AAAA,EAG3F,YAAY;AACX,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,UAAM,SAAS,eAAe,KAAK,OAChC,OAAO,QAAQ,KAAK,CAAA,OAAM,GAAG,SAAS,KAAK,KAAK,SAAS,GAAG,SAC5D,KAAK,KAAK;AACb,QAAI,CAAC,QAAQ;AACZ,YAAM,IAAI,MAAM;AAAA,IAAA;AAEV,WAAA;AAAA,EAAA;AAET;AAEO,MAAM,0BAA0BC,oBAAAA,uBAAuB;AAAA,EAC7D,IAAI;AAAA,EACJ,SAAS;AACV,CAAC;AAeM,MAAM,kBAAoC;AAAA,EAEhD,WAAW,gBAAwB,eAAuB;AACzD,WAAO,OAAO,OAAO,eAAe,MAAM,QAAQ,EAAE;AAAA,MAAQ,CAAA,WAC3D,OAAO,QACL;AAAA,QACA,CAAM,OAAA;AACL,gBAAM,gBAAgB,cAAc,MAAM,SAAS,OAAO,IAAI;AAC9D,cAAI,CAAC,eAAe;AACZ,mBAAA;AAAA,UAAA;AAED,iBAAA,CAAC,cAAc,QAAQ,KAAK,CAAA,UAAS,UAAU,MAAM,QAAQ,GAAG,MAAM,CAAC;AAAA,QAAA;AAAA,MAC/E,EAEA;AAAA,QAAI,CAAA,UACJ,wBAAwB,mBAAmB;AAAA,UAC1C,YAAY,OAAO;AAAA,UACnB,QAAQ,MAAM;AAAA,QACd,CAAA;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEF;;;;"}
1
+ {"version":3,"file":"RemoveIndexModification.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/indexes/RemoveIndexModification.ts"],"sourcesContent":["import { MigrationBuilder } from '@contember/database-migrations'\nimport { Schema } from '@contember/schema'\nimport { SchemaUpdater, updateEntity, updateModel } from '../utils/schemaUpdateUtils'\nimport {\n\tcreateModificationType,\n\tDiffer,\n\tModificationHandler,\n\tModificationHandlerCreateSqlOptions,\n} from '../ModificationHandler'\nimport deepEqual from 'fast-deep-equal'\nimport { getIndexColumns } from './utils'\nimport { wrapIdentifier } from '../../utils/dbHelpers'\n\nexport class RemoveIndexModificationHandler implements ModificationHandler<RemoveIndexModificationData> {\n\tconstructor(private readonly data: RemoveIndexModificationData, private readonly schema: Schema) {}\n\n\tpublic createSql(builder: MigrationBuilder, { databaseMetadata, invalidateDatabaseMetadata }: ModificationHandlerCreateSqlOptions): void {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tif (entity.view && !entity.view.materialized) {\n\t\t\treturn\n\t\t}\n\t\tconst fields = this.getFields()\n\n\t\tconst columns = getIndexColumns({\n\t\t\tentity,\n\t\t\tfields,\n\t\t\tmodel: this.schema.model,\n\t\t})\n\n\t\tconst indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns, unique: false }).getNames()\n\n\t\tfor (const name of indexNames) {\n\t\t\tbuilder.sql(`DROP INDEX ${wrapIdentifier(name)}`)\n\t\t}\n\t\tinvalidateDatabaseMetadata()\n\t}\n\n\tpublic getSchemaUpdater(): SchemaUpdater {\n\t\tconst fields = this.getFields()\n\t\treturn updateModel(\n\t\t\tupdateEntity(this.data.entityName, ({ entity }) => {\n\t\t\t\tconst indexes = entity.indexes.filter(it => !deepEqual(it.fields, fields))\n\t\t\t\treturn {\n\t\t\t\t\t...entity,\n\t\t\t\t\tindexes,\n\t\t\t\t}\n\t\t\t}),\n\t\t)\n\t}\n\n\tdescribe() {\n\t\tconst fields = this.getFields()\n\t\treturn { message: `Remove index (${fields.join(', ')}) on entity ${this.data.entityName}` }\n\t}\n\n\tgetFields() {\n\t\tconst entity = this.schema.model.entities[this.data.entityName]\n\t\tconst fields = 'indexName' in this.data\n\t\t\t? entity.indexes.find(it => it.name === this.data.indexName)?.fields\n\t\t\t: this.data.fields\n\t\tif (!fields) {\n\t\t\tthrow new Error()\n\t\t}\n\t\treturn fields\n\t}\n}\n\nexport const removeIndexModification = createModificationType({\n\tid: 'removeIndex',\n\thandler: RemoveIndexModificationHandler,\n})\n\n\nexport type RemoveIndexModificationData =\n\t| {\n\t\tentityName: string\n\t\tindexName: string\n\t\tfields?: never\n\t}\n\t| {\n\t\tentityName: string\n\t\tfields: readonly string[]\n\t\tindexName?: never\n\t}\n\nexport class RemoveIndexDiffer implements Differ {\n\n\tcreateDiff(originalSchema: Schema, updatedSchema: Schema) {\n\t\treturn Object.values(originalSchema.model.entities).flatMap(entity =>\n\t\t\tentity.indexes\n\t\t\t\t.filter(\n\t\t\t\t\tit => {\n\t\t\t\t\t\tconst updatedEntity = updatedSchema.model.entities[entity.name]\n\t\t\t\t\t\tif (!updatedEntity) {\n\t\t\t\t\t\t\treturn false\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !updatedEntity.indexes.find(index => deepEqual(index, it))\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t\t.map(index =>\n\t\t\t\t\tremoveIndexModification.createModification({\n\t\t\t\t\t\tentityName: entity.name,\n\t\t\t\t\t\tfields: index.fields,\n\t\t\t\t\t}),\n\t\t\t\t),\n\t\t)\n\t}\n}\n"],"names":["getIndexColumns","wrapIdentifier","updateModel","updateEntity","createModificationType"],"mappings":";;;;;;;AAaO,MAAM,+BAA4F;AAAA,EACxG,YAA6B,MAAoD,QAAgB;AAApE,SAAA,OAAA;AAAoD,SAAA,SAAA;AAAA,EAAA;AAAA,EAE1E,UAAU,SAA2B,EAAE,kBAAkB,8BAAyE;AACxI,UAAM,SAAS,KAAK,OAAO,MAAM,SAAS,KAAK,KAAK,UAAU;AAC9D,QAAI,OAAO,QAAQ,CAAC,OAAO,KAAK,cAAc;AAC7C;AAAA,IAAA;AAEK,UAAA,SAAS,KAAK,UAAU;AAE9B,UAAM,UAAUA,MAAAA,gBAA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EAAE;AAAA,MAAQ,CAAA,WAC3D,OAAO,QACL;AAAA,QACA,CAAM,OAAA;AACL,gBAAM,gBAAgB,cAAc,MAAM,SAAS,OAAO,IAAI;AAC9D,cAAI,CAAC,eAAe;AACZ,mBAAA;AAAA,UAAA;AAED,iBAAA,CAAC,cAAc,QAAQ,KAAK,WAAS,UAAU,OAAO,EAAE,CAAC;AAAA,QAAA;AAAA,MACjE,EAEA;AAAA,QAAI,CAAA,UACJ,wBAAwB,mBAAmB;AAAA,UAC1C,YAAY,OAAO;AAAA,UACnB,QAAQ,MAAM;AAAA,QACd,CAAA;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAEF;;;;"}
@@ -10,7 +10,7 @@ class RemoveIndexModificationHandler {
10
10
  }
11
11
  createSql(builder, { databaseMetadata, invalidateDatabaseMetadata }) {
12
12
  const entity = this.schema.model.entities[this.data.entityName];
13
- if (entity.view) {
13
+ if (entity.view && !entity.view.materialized) {
14
14
  return;
15
15
  }
16
16
  const fields = this.getFields();
@@ -19,7 +19,7 @@ class RemoveIndexModificationHandler {
19
19
  fields,
20
20
  model: this.schema.model
21
21
  });
22
- const indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns }).getNames();
22
+ const indexNames = databaseMetadata.indexes.filter({ tableName: entity.tableName, columnNames: columns, unique: false }).getNames();
23
23
  for (const name of indexNames) {
24
24
  builder.sql(`DROP INDEX ${wrapIdentifier(name)}`);
25
25
  }
@@ -63,7 +63,7 @@ class RemoveIndexDiffer {
63
63
  if (!updatedEntity) {
64
64
  return false;
65
65
  }
66
- return !updatedEntity.indexes.find((index) => deepEqual(index.fields, it.fields));
66
+ return !updatedEntity.indexes.find((index) => deepEqual(index, it));
67
67
  }
68
68
  ).map(
69
69
  (index) => removeIndexModification.createModification({