@contember/schema-migrations 2.1.0-alpha.26 → 2.1.0-alpha.28

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 (25) hide show
  1. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.cjs +2 -1
  2. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.cjs.map +1 -1
  3. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.js +2 -1
  4. package/dist/development/src/modifications/constraints/CreateUniqueConstraintModification.js.map +1 -1
  5. package/dist/development/src/modifications/indexes/CreateIndexModification.cjs +4 -2
  6. package/dist/development/src/modifications/indexes/CreateIndexModification.cjs.map +1 -1
  7. package/dist/development/src/modifications/indexes/CreateIndexModification.js +4 -2
  8. package/dist/development/src/modifications/indexes/CreateIndexModification.js.map +1 -1
  9. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.cjs +2 -1
  10. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.cjs.map +1 -1
  11. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.js +2 -1
  12. package/dist/production/src/modifications/constraints/CreateUniqueConstraintModification.js.map +1 -1
  13. package/dist/production/src/modifications/indexes/CreateIndexModification.cjs +4 -2
  14. package/dist/production/src/modifications/indexes/CreateIndexModification.cjs.map +1 -1
  15. package/dist/production/src/modifications/indexes/CreateIndexModification.js +4 -2
  16. package/dist/production/src/modifications/indexes/CreateIndexModification.js.map +1 -1
  17. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  18. package/dist/types/modifications/constraints/CreateUniqueConstraintModification.d.ts.map +1 -1
  19. package/dist/types/modifications/indexes/CreateIndexModification.d.ts.map +1 -1
  20. package/dist/types/tsconfig.tsbuildinfo +1 -1
  21. package/package.json +7 -7
  22. package/src/modifications/constraints/CreateUniqueConstraintModification.ts +2 -1
  23. package/src/modifications/indexes/CreateIndexModification.ts +4 -2
  24. package/tests/cases/integration/createAndDropIndex.test.ts +100 -0
  25. package/tests/cases/integration/createUnique.test.ts +153 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/schema-migrations",
3
- "version": "2.1.0-alpha.26",
3
+ "version": "2.1.0-alpha.28",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/production/index.js",
6
6
  "typings": "./dist/types/index.d.ts",
@@ -23,16 +23,16 @@
23
23
  }
24
24
  },
25
25
  "dependencies": {
26
- "@contember/database": "2.1.0-alpha.26",
27
- "@contember/database-migrations": "2.1.0-alpha.26",
28
- "@contember/engine-common": "2.1.0-alpha.26",
29
- "@contember/schema": "2.1.0-alpha.26",
30
- "@contember/schema-utils": "2.1.0-alpha.26",
26
+ "@contember/database": "2.1.0-alpha.28",
27
+ "@contember/database-migrations": "2.1.0-alpha.28",
28
+ "@contember/engine-common": "2.1.0-alpha.28",
29
+ "@contember/schema": "2.1.0-alpha.28",
30
+ "@contember/schema-utils": "2.1.0-alpha.28",
31
31
  "fast-deep-equal": "^3.1.3",
32
32
  "rfc6902": "^5.1.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@contember/schema-definition": "2.1.0-alpha.26"
35
+ "@contember/schema-definition": "2.1.0-alpha.28"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "pg": "^8.9.0"
@@ -35,7 +35,8 @@ export class CreateUniqueConstraintModificationHandler implements ModificationHa
35
35
 
36
36
  if ('index' in this.data.unique && this.data.unique.index) {
37
37
  const nulls = this.data.unique.nulls === 'not distinct' ? ' NULLS NOT DISTINCT' : ''
38
- builder.sql(`CREATE UNIQUE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})${nulls}`)
38
+ const methodClause = this.data.unique.method ? ` USING ${this.data.unique.method}` : ''
39
+ builder.sql(`CREATE UNIQUE INDEX ON ${tableNameId}${methodClause} (${columnNameIds.join(', ')})${nulls}`)
39
40
  } else {
40
41
  let checkModifier = ''
41
42
  if (this.data.unique.timing === 'deferred') {
@@ -28,8 +28,9 @@ export class CreateIndexModificationHandler implements ModificationHandler<Creat
28
28
 
29
29
  const tableNameId = wrapIdentifier(entity.tableName)
30
30
  const columnNameIds = columns.map(wrapIdentifier)
31
+ const methodClause = this.data.index.method ? ` USING ${this.data.index.method}` : ''
31
32
 
32
- builder.sql(`CREATE INDEX ON ${tableNameId} (${columnNameIds.join(', ')})`)
33
+ builder.sql(`CREATE INDEX ON ${tableNameId}${methodClause} (${columnNameIds.join(', ')})`)
33
34
 
34
35
  invalidateDatabaseMetadata()
35
36
  }
@@ -47,8 +48,9 @@ export class CreateIndexModificationHandler implements ModificationHandler<Creat
47
48
  }
48
49
 
49
50
  describe() {
51
+ const methodText = this.data.index.method ? ` using ${this.data.index.method}` : ''
50
52
  return {
51
- message: `Create index(${this.data.index.fields.join(', ')}) on entity ${this.data.entityName}`,
53
+ message: `Create index(${this.data.index.fields.join(', ')})${methodText} on entity ${this.data.entityName}`,
52
54
  }
53
55
  }
54
56
  }
@@ -17,6 +17,34 @@ namespace SchemaWithIndex {
17
17
  }
18
18
  }
19
19
 
20
+ namespace SchemaWithGinIndex {
21
+ @def.Index({ fields: ['title'], method: 'gin' })
22
+ export class Article {
23
+ title = def.stringColumn()
24
+ }
25
+ }
26
+
27
+ namespace SchemaWithGistIndex {
28
+ @def.Index({ fields: ['title'], method: 'gist' })
29
+ export class Article {
30
+ title = def.stringColumn()
31
+ }
32
+ }
33
+
34
+ namespace SchemaWithBtreeIndex {
35
+ @def.Index({ fields: ['title'], method: 'btree' })
36
+ export class Article {
37
+ title = def.stringColumn()
38
+ }
39
+ }
40
+
41
+ namespace SchemaWithHashIndex {
42
+ @def.Index({ fields: ['title'], method: 'hash' })
43
+ export class Article {
44
+ title = def.stringColumn()
45
+ }
46
+ }
47
+
20
48
 
21
49
  describe('create index', () => testMigrations({
22
50
  original: createSchema(SchemaWithoutIndex),
@@ -54,3 +82,75 @@ describe('drop index', () => testMigrations({
54
82
  uniqueConstraints: [],
55
83
  }),
56
84
  }))
85
+
86
+
87
+ describe('create index with GIN method', () => testMigrations({
88
+ original: createSchema(SchemaWithoutIndex),
89
+ updated: createSchema(SchemaWithGinIndex),
90
+ diff: [
91
+ {
92
+ modification: 'createIndex',
93
+ entityName: 'Article',
94
+ index: { fields: ['title'], method: 'gin' },
95
+ },
96
+ ],
97
+ sql: SQL`CREATE INDEX ON "article" USING gin ("title");`,
98
+ }))
99
+
100
+ describe('change index method from btree to gin', () => testMigrations({
101
+ original: createSchema(SchemaWithBtreeIndex),
102
+ updated: createSchema(SchemaWithGinIndex),
103
+ diff: [
104
+ {
105
+ modification: 'removeIndex',
106
+ entityName: 'Article',
107
+ fields: ['title'],
108
+ },
109
+ {
110
+ modification: 'createIndex',
111
+ entityName: 'Article',
112
+ index: { fields: ['title'], method: 'gin' },
113
+ },
114
+ ],
115
+ sql: SQL`DROP INDEX "idx_article_title";
116
+ CREATE INDEX ON "article" USING gin ("title");`,
117
+ databaseMetadata: createDatabaseMetadata({
118
+ foreignKeys: [],
119
+ indexes: [{
120
+ tableName: 'article',
121
+ columnNames: ['title'],
122
+ indexName: 'idx_article_title',
123
+ unique: false,
124
+ }],
125
+ uniqueConstraints: [],
126
+ }),
127
+ }))
128
+
129
+ describe('change from regular index to method-specific index', () => testMigrations({
130
+ original: createSchema(SchemaWithIndex),
131
+ updated: createSchema(SchemaWithHashIndex),
132
+ diff: [
133
+ {
134
+ modification: 'removeIndex',
135
+ entityName: 'Article',
136
+ fields: ['title'],
137
+ },
138
+ {
139
+ modification: 'createIndex',
140
+ entityName: 'Article',
141
+ index: { fields: ['title'], method: 'hash' },
142
+ },
143
+ ],
144
+ sql: SQL`DROP INDEX "idx_article_title";
145
+ CREATE INDEX ON "article" USING hash ("title");`,
146
+ databaseMetadata: createDatabaseMetadata({
147
+ foreignKeys: [],
148
+ indexes: [{
149
+ tableName: 'article',
150
+ columnNames: ['title'],
151
+ indexName: 'idx_article_title',
152
+ unique: false,
153
+ }],
154
+ uniqueConstraints: [],
155
+ }),
156
+ }))
@@ -104,7 +104,7 @@ describe('change unique timing', () => testMigrations({
104
104
  },
105
105
  }),
106
106
  ],
107
- sql: SQL`ALTER TABLE "author" DROP CONSTRAINT "uniq_author_email";
107
+ sql: SQL`ALTER TABLE "author" DROP CONSTRAINT "uniq_author_email";
108
108
  ALTER TABLE "author" ADD UNIQUE ("email") DEFERRABLE;`,
109
109
  databaseMetadata: createDatabaseMetadata({
110
110
  foreignKeys: [],
@@ -235,3 +235,155 @@ describe('create unique index', () => testMigrations({
235
235
  ],
236
236
  sql: SQL`CREATE UNIQUE INDEX ON "author" ("email");`,
237
237
  }))
238
+
239
+ namespace AuthorWithUniqueGinIndex {
240
+ @c.Unique({ fields: ['email'], index: true, method: 'gin' })
241
+ export class Author {
242
+ email = c.stringColumn()
243
+ }
244
+ }
245
+
246
+ namespace AuthorWithUniqueGistIndex {
247
+ @c.Unique({ fields: ['email'], index: true, method: 'gist' })
248
+ export class Author {
249
+ email = c.stringColumn()
250
+ }
251
+ }
252
+
253
+ namespace AuthorWithUniqueBtreeIndex {
254
+ @c.Unique({ fields: ['email'], index: true, method: 'btree', nulls: 'not distinct' })
255
+ export class Author {
256
+ email = c.stringColumn()
257
+ }
258
+ }
259
+
260
+ describe('create unique index with GIN method', () => testMigrations({
261
+ original: createSchema({
262
+ Author: class Author {
263
+ email = c.stringColumn()
264
+ },
265
+ }),
266
+ updated: createSchema(AuthorWithUniqueGinIndex),
267
+ diff: [
268
+ createUniqueConstraintModification.createModification({
269
+ entityName: 'Author',
270
+ unique: {
271
+ fields: ['email'],
272
+ index: true,
273
+ method: 'gin',
274
+ },
275
+ }),
276
+ ],
277
+ sql: SQL`CREATE UNIQUE INDEX ON "author" USING gin ("email");`,
278
+ }))
279
+
280
+ describe('create unique index with GiST method', () => testMigrations({
281
+ original: createSchema({
282
+ Author: class Author {
283
+ email = c.stringColumn()
284
+ },
285
+ }),
286
+ updated: createSchema(AuthorWithUniqueGistIndex),
287
+ diff: [
288
+ createUniqueConstraintModification.createModification({
289
+ entityName: 'Author',
290
+ unique: {
291
+ fields: ['email'],
292
+ index: true,
293
+ method: 'gist',
294
+ },
295
+ }),
296
+ ],
297
+ sql: SQL`CREATE UNIQUE INDEX ON "author" USING gist ("email");`,
298
+ }))
299
+
300
+ describe('create unique index with btree method and nulls not distinct', () => testMigrations({
301
+ original: createSchema({
302
+ Author: class Author {
303
+ email = c.stringColumn()
304
+ },
305
+ }),
306
+ updated: createSchema(AuthorWithUniqueBtreeIndex),
307
+ diff: [
308
+ createUniqueConstraintModification.createModification({
309
+ entityName: 'Author',
310
+ unique: {
311
+ fields: ['email'],
312
+ index: true,
313
+ method: 'btree',
314
+ nulls: 'not distinct',
315
+ },
316
+ }),
317
+ ],
318
+ sql: SQL`CREATE UNIQUE INDEX ON "author" USING btree ("email") NULLS NOT DISTINCT;`,
319
+ }))
320
+
321
+ describe('change unique index method from gin to gist', () => testMigrations({
322
+ original: createSchema(AuthorWithUniqueGinIndex),
323
+ updated: createSchema(AuthorWithUniqueGistIndex),
324
+ diff: [
325
+ removeUniqueConstraintModification.createModification({
326
+ entityName: 'Author',
327
+ unique: {
328
+ fields: ['email'],
329
+ index: true,
330
+ method: 'gin',
331
+ },
332
+ }),
333
+ createUniqueConstraintModification.createModification({
334
+ entityName: 'Author',
335
+ unique: {
336
+ fields: ['email'],
337
+ index: true,
338
+ method: 'gist',
339
+ },
340
+ }),
341
+ ],
342
+ sql: SQL`DROP INDEX "uniq_author_email";
343
+ CREATE UNIQUE INDEX ON "author" USING gist ("email");`,
344
+ databaseMetadata: createDatabaseMetadata({
345
+ foreignKeys: [],
346
+ indexes: [{
347
+ tableName: 'author',
348
+ columnNames: ['email'],
349
+ indexName: 'uniq_author_email',
350
+ unique: true,
351
+ }],
352
+ uniqueConstraints: [],
353
+ }),
354
+ }))
355
+
356
+ describe('change from regular unique index to method-specific unique index', () => testMigrations({
357
+ original: createSchema(AuthorWithUniqueIndex),
358
+ updated: createSchema(AuthorWithUniqueGinIndex),
359
+ diff: [
360
+ removeUniqueConstraintModification.createModification({
361
+ entityName: 'Author',
362
+ unique: {
363
+ fields: ['email'],
364
+ index: true,
365
+ nulls: 'not distinct',
366
+ },
367
+ }),
368
+ createUniqueConstraintModification.createModification({
369
+ entityName: 'Author',
370
+ unique: {
371
+ fields: ['email'],
372
+ index: true,
373
+ method: 'gin',
374
+ },
375
+ }),
376
+ ],
377
+ sql: SQL`DROP INDEX "uniq_author_email";
378
+ CREATE UNIQUE INDEX ON "author" USING gin ("email");`,
379
+ databaseMetadata: createDatabaseMetadata({
380
+ foreignKeys: [],
381
+ indexes: [{
382
+ tableName: 'author',
383
+ columnNames: ['email'],
384
+ indexName: 'uniq_author_email',
385
+ unique: true,
386
+ }],
387
+ uniqueConstraints: [],
388
+ }),
389
+ }))