@contember/schema-migrations 1.3.6 → 1.4.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/SchemaDiffer.d.ts.map +1 -1
- package/dist/src/SchemaDiffer.js +2 -0
- package/dist/src/SchemaDiffer.js.map +1 -1
- package/dist/src/modifications/ModificationHandler.d.ts +2 -2
- package/dist/src/modifications/ModificationHandler.d.ts.map +1 -1
- package/dist/src/modifications/ModificationHandlerFactory.d.ts.map +1 -1
- package/dist/src/modifications/ModificationHandlerFactory.js +2 -0
- package/dist/src/modifications/ModificationHandlerFactory.js.map +1 -1
- package/dist/src/modifications/constraints/CreateUniqueConstraintModification.d.ts.map +1 -1
- package/dist/src/modifications/constraints/CreateUniqueConstraintModification.js +8 -1
- package/dist/src/modifications/constraints/CreateUniqueConstraintModification.js.map +1 -1
- package/dist/src/modifications/constraints/RemoveUniqueConstraintModification.d.ts.map +1 -1
- package/dist/src/modifications/constraints/RemoveUniqueConstraintModification.js +2 -2
- package/dist/src/modifications/constraints/RemoveUniqueConstraintModification.js.map +1 -1
- package/dist/src/modifications/indexes/RemoveIndexModification.js +1 -1
- package/dist/src/modifications/indexes/RemoveIndexModification.js.map +1 -1
- package/dist/src/modifications/relations/ConvertOneHasManyToOneHasOneRelationModification.d.ts +28 -0
- package/dist/src/modifications/relations/ConvertOneHasManyToOneHasOneRelationModification.d.ts.map +1 -0
- package/dist/src/modifications/relations/ConvertOneHasManyToOneHasOneRelationModification.js +98 -0
- package/dist/src/modifications/relations/ConvertOneHasManyToOneHasOneRelationModification.js.map +1 -0
- package/dist/src/modifications/relations/ConvertOneToManyRelationModification.d.ts.map +1 -1
- package/dist/src/modifications/relations/ConvertOneToManyRelationModification.js +7 -5
- package/dist/src/modifications/relations/ConvertOneToManyRelationModification.js.map +1 -1
- package/dist/src/modifications/relations/helpers.d.ts +2 -2
- package/dist/src/modifications/relations/helpers.d.ts.map +1 -1
- package/dist/src/modifications/relations/helpers.js +2 -2
- package/dist/src/modifications/relations/helpers.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.d.ts +2 -0
- package/dist/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.d.ts.map +1 -0
- package/dist/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.js +59 -0
- package/dist/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.js.map +1 -0
- package/dist/tests/cases/integration/convertOneToManyRelation.test.js +60 -0
- package/dist/tests/cases/integration/convertOneToManyRelation.test.js.map +1 -1
- package/dist/tests/cases/integration/createAndDropIndex.test.js +10 -0
- package/dist/tests/cases/integration/createAndDropIndex.test.js.map +1 -1
- package/dist/tests/cases/integration/createRelationInverseSide.test.js +17 -0
- package/dist/tests/cases/integration/createRelationInverseSide.test.js.map +1 -1
- package/dist/tests/cases/integration/createUnique.test.d.ts +2 -0
- package/dist/tests/cases/integration/createUnique.test.d.ts.map +1 -0
- package/dist/tests/cases/integration/createUnique.test.js +168 -0
- package/dist/tests/cases/integration/createUnique.test.js.map +1 -0
- package/dist/tests/cases/integration/removeField.test.js +12 -0
- package/dist/tests/cases/integration/removeField.test.js.map +1 -1
- package/dist/tests/cases/integration/updateRelationOnDelete.test.js +17 -0
- package/dist/tests/cases/integration/updateRelationOnDelete.test.js.map +1 -1
- package/dist/tests/src/tests.d.ts +4 -2
- package/dist/tests/src/tests.d.ts.map +1 -1
- package/dist/tests/src/tests.js +5 -4
- package/dist/tests/src/tests.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -6
- package/src/SchemaDiffer.ts +2 -0
- package/src/modifications/ModificationHandler.ts +2 -2
- package/src/modifications/ModificationHandlerFactory.ts +2 -0
- package/src/modifications/constraints/CreateUniqueConstraintModification.ts +8 -1
- package/src/modifications/constraints/RemoveUniqueConstraintModification.ts +8 -10
- package/src/modifications/indexes/RemoveIndexModification.ts +1 -1
- package/src/modifications/relations/ConvertOneHasManyToOneHasOneRelationModification.ts +145 -0
- package/src/modifications/relations/ConvertOneToManyRelationModification.ts +7 -5
- package/src/modifications/relations/helpers.ts +4 -4
- package/src/tsconfig.json +1 -0
- package/tests/cases/integration/convertManyHasOneToOneHasOneRelation.test.ts +48 -0
- package/tests/cases/integration/convertOneToManyRelation.test.ts +65 -0
- package/tests/cases/integration/createAndDropIndex.test.ts +10 -0
- package/tests/cases/integration/createRelationInverseSide.test.ts +17 -0
- package/tests/cases/integration/createUnique.test.ts +146 -0
- package/tests/cases/integration/removeField.test.ts +12 -0
- package/tests/cases/integration/updateRelationOnDelete.test.ts +17 -0
- package/tests/src/tests.ts +12 -5
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { c, createSchema } from '@contember/schema-definition'
|
|
2
|
+
import { SQL } from '../../src/tags'
|
|
3
|
+
import { testMigrations } from '../../src/tests'
|
|
4
|
+
import { createUniqueConstraintModification, removeUniqueConstraintModification } from '../../../src'
|
|
5
|
+
import { createDatabaseMetadata } from '@contember/database'
|
|
6
|
+
|
|
7
|
+
testMigrations('create unique (immediate)', {
|
|
8
|
+
original: createSchema({
|
|
9
|
+
Author: class Author {
|
|
10
|
+
email = c.stringColumn()
|
|
11
|
+
},
|
|
12
|
+
}),
|
|
13
|
+
updated: createSchema({
|
|
14
|
+
Author: class Author {
|
|
15
|
+
email = c.stringColumn().unique()
|
|
16
|
+
},
|
|
17
|
+
}),
|
|
18
|
+
diff: [
|
|
19
|
+
createUniqueConstraintModification.createModification({
|
|
20
|
+
entityName: 'Author',
|
|
21
|
+
unique: {
|
|
22
|
+
fields: ['email'],
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
],
|
|
26
|
+
sql: SQL`ALTER TABLE "author" ADD UNIQUE ("email");`,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
testMigrations('create unique (deferrable)', {
|
|
30
|
+
original: createSchema({
|
|
31
|
+
Author: class Author {
|
|
32
|
+
email = c.stringColumn()
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
updated: createSchema({
|
|
36
|
+
Author: class Author {
|
|
37
|
+
email = c.stringColumn().unique({ timing: 'deferrable' })
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
diff: [
|
|
41
|
+
createUniqueConstraintModification.createModification({
|
|
42
|
+
entityName: 'Author',
|
|
43
|
+
unique: {
|
|
44
|
+
fields: ['email'],
|
|
45
|
+
timing: 'deferrable',
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
49
|
+
sql: SQL`ALTER TABLE "author"
|
|
50
|
+
ADD UNIQUE ("email") DEFERRABLE;`,
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
testMigrations('create unique (deferred)', {
|
|
55
|
+
original: createSchema({
|
|
56
|
+
Author: class Author {
|
|
57
|
+
email = c.stringColumn()
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
updated: createSchema({
|
|
61
|
+
Author: class Author {
|
|
62
|
+
email = c.stringColumn().unique({ timing: 'deferred' })
|
|
63
|
+
},
|
|
64
|
+
}),
|
|
65
|
+
diff: [
|
|
66
|
+
|
|
67
|
+
createUniqueConstraintModification.createModification({
|
|
68
|
+
entityName: 'Author',
|
|
69
|
+
unique: {
|
|
70
|
+
fields: ['email'],
|
|
71
|
+
timing: 'deferred',
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
],
|
|
75
|
+
sql: SQL`ALTER TABLE "author"
|
|
76
|
+
ADD UNIQUE ("email") INITIALLY DEFERRED;`,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
testMigrations('change unique timing', {
|
|
81
|
+
original: createSchema({
|
|
82
|
+
Author: class Author {
|
|
83
|
+
email = c.stringColumn().unique()
|
|
84
|
+
},
|
|
85
|
+
}),
|
|
86
|
+
updated: createSchema({
|
|
87
|
+
Author: class Author {
|
|
88
|
+
email = c.stringColumn().unique({ timing: 'deferrable' })
|
|
89
|
+
},
|
|
90
|
+
}),
|
|
91
|
+
diff: [
|
|
92
|
+
removeUniqueConstraintModification.createModification({
|
|
93
|
+
entityName: 'Author',
|
|
94
|
+
fields: ['email'],
|
|
95
|
+
}),
|
|
96
|
+
createUniqueConstraintModification.createModification({
|
|
97
|
+
entityName: 'Author',
|
|
98
|
+
unique: {
|
|
99
|
+
fields: ['email'],
|
|
100
|
+
timing: 'deferrable',
|
|
101
|
+
},
|
|
102
|
+
}),
|
|
103
|
+
],
|
|
104
|
+
sql: SQL`ALTER TABLE "author" DROP CONSTRAINT "uniq_author_email";
|
|
105
|
+
ALTER TABLE "author" ADD UNIQUE ("email") DEFERRABLE;`,
|
|
106
|
+
databaseMetadata: createDatabaseMetadata({
|
|
107
|
+
foreignKeys: [],
|
|
108
|
+
indexes: [],
|
|
109
|
+
uniqueConstraints: [{
|
|
110
|
+
constraintName: 'uniq_author_email',
|
|
111
|
+
columnNames: ['email'],
|
|
112
|
+
tableName: 'author',
|
|
113
|
+
deferred: false,
|
|
114
|
+
deferrable: false,
|
|
115
|
+
}],
|
|
116
|
+
}),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
namespace SchemaWithUniqueDecorator {
|
|
121
|
+
|
|
122
|
+
@c.Unique({ fields: ['email'], timing: 'deferred' })
|
|
123
|
+
export class Author {
|
|
124
|
+
email = c.stringColumn()
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
testMigrations('create unique using decorator', {
|
|
129
|
+
original: createSchema({
|
|
130
|
+
Author: class Author {
|
|
131
|
+
email = c.stringColumn()
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
updated: createSchema(SchemaWithUniqueDecorator),
|
|
135
|
+
diff: [
|
|
136
|
+
createUniqueConstraintModification.createModification({
|
|
137
|
+
entityName: 'Author',
|
|
138
|
+
unique: {
|
|
139
|
+
fields: ['email'],
|
|
140
|
+
timing: 'deferred',
|
|
141
|
+
},
|
|
142
|
+
}),
|
|
143
|
+
],
|
|
144
|
+
sql: SQL`ALTER TABLE "author"
|
|
145
|
+
ADD UNIQUE ("email") INITIALLY DEFERRED;`,
|
|
146
|
+
})
|
|
@@ -3,6 +3,7 @@ import { createSchema, SchemaBuilder } from '@contember/schema-definition'
|
|
|
3
3
|
import { Model } from '@contember/schema'
|
|
4
4
|
import { SQL } from '../../src/tags'
|
|
5
5
|
import { SchemaDefinition as def } from '@contember/schema-definition'
|
|
6
|
+
import { createDatabaseMetadata } from '@contember/database'
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
testMigrations('remove relation (many has one)', {
|
|
@@ -67,6 +68,17 @@ testMigrations('remove relation (one has many)', {
|
|
|
67
68
|
],
|
|
68
69
|
sql: SQL`ALTER TABLE "post_locale" DROP CONSTRAINT "uniq_post_locale_post_id_locale";
|
|
69
70
|
ALTER TABLE "post_locale" DROP "post_id";`,
|
|
71
|
+
databaseMetadata: createDatabaseMetadata({
|
|
72
|
+
foreignKeys: [],
|
|
73
|
+
indexes: [],
|
|
74
|
+
uniqueConstraints: [{
|
|
75
|
+
constraintName: 'uniq_post_locale_post_id_locale',
|
|
76
|
+
columnNames: ['post_id', 'locale'],
|
|
77
|
+
tableName: 'post_locale',
|
|
78
|
+
deferrable: false,
|
|
79
|
+
deferred: false,
|
|
80
|
+
}],
|
|
81
|
+
}),
|
|
70
82
|
})
|
|
71
83
|
|
|
72
84
|
testMigrations('remove relation (many has many)', {
|
|
@@ -2,6 +2,7 @@ import { testMigrations } from '../../src/tests'
|
|
|
2
2
|
import { SchemaBuilder } from '@contember/schema-definition'
|
|
3
3
|
import { Model } from '@contember/schema'
|
|
4
4
|
import { SQL } from '../../src/tags'
|
|
5
|
+
import { ForeignKeyDeleteAction, createDatabaseMetadata } from '@contember/database'
|
|
5
6
|
|
|
6
7
|
testMigrations('update relation ondelete to cascade', {
|
|
7
8
|
original: {
|
|
@@ -32,4 +33,20 @@ testMigrations('update relation ondelete to cascade', {
|
|
|
32
33
|
],
|
|
33
34
|
sql: SQL`ALTER TABLE "post" DROP CONSTRAINT "fk_post_category_id_category_id";
|
|
34
35
|
ALTER TABLE "post" ADD FOREIGN KEY ("category_id") REFERENCES "category"("id") ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE;`,
|
|
36
|
+
databaseMetadata: createDatabaseMetadata({
|
|
37
|
+
foreignKeys: [
|
|
38
|
+
{
|
|
39
|
+
constraintName: 'fk_post_category_id_category_id',
|
|
40
|
+
deleteAction: ForeignKeyDeleteAction.cascade,
|
|
41
|
+
fromColumn: 'category_id',
|
|
42
|
+
fromTable: 'post',
|
|
43
|
+
toColumn: 'id',
|
|
44
|
+
toTable: 'category',
|
|
45
|
+
deferred: false,
|
|
46
|
+
deferrable: false,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
indexes: [],
|
|
50
|
+
uniqueConstraints: [],
|
|
51
|
+
}),
|
|
35
52
|
})
|
package/tests/src/tests.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { Migration, ModificationHandlerFactory, SchemaDiffer, SchemaMigrator, VE
|
|
|
2
2
|
import { Schema } from '@contember/schema'
|
|
3
3
|
import { createMigrationBuilder } from '@contember/database-migrations'
|
|
4
4
|
import { assert, describe, it } from 'vitest'
|
|
5
|
-
import {
|
|
5
|
+
import { emptySchema } from '@contember/schema-utils'
|
|
6
|
+
import { DatabaseMetadata, emptyDatabaseMetadata } from '@contember/database'
|
|
6
7
|
|
|
7
8
|
const modificationFactory = new ModificationHandlerFactory(ModificationHandlerFactory.defaultFactoryMap)
|
|
8
9
|
const schemaMigrator = new SchemaMigrator(modificationFactory)
|
|
@@ -14,6 +15,7 @@ export interface TestContext {
|
|
|
14
15
|
updated: Partial<Schema>
|
|
15
16
|
sql: string
|
|
16
17
|
noDiff?: boolean
|
|
18
|
+
databaseMetadata?: DatabaseMetadata
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export function testDiffSchemas(
|
|
@@ -61,7 +63,12 @@ export function testApplyDiff(
|
|
|
61
63
|
})
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
export function testGenerateSql(
|
|
66
|
+
export function testGenerateSql(
|
|
67
|
+
original: Partial<Schema>,
|
|
68
|
+
diff: Migration.Modification[],
|
|
69
|
+
expectedSql: string,
|
|
70
|
+
databaseMetadata: DatabaseMetadata = emptyDatabaseMetadata,
|
|
71
|
+
) {
|
|
65
72
|
let schema = { ...emptySchema, ...original }
|
|
66
73
|
const builder = createMigrationBuilder()
|
|
67
74
|
for (let { modification, ...data } of diff) {
|
|
@@ -70,7 +77,7 @@ export function testGenerateSql(original: Partial<Schema>, diff: Migration.Modif
|
|
|
70
77
|
})
|
|
71
78
|
modificationHandler.createSql(builder, {
|
|
72
79
|
systemSchema: 'system',
|
|
73
|
-
databaseMetadata
|
|
80
|
+
databaseMetadata,
|
|
74
81
|
invalidateDatabaseMetadata: () => null,
|
|
75
82
|
})
|
|
76
83
|
schema = modificationHandler.getSchemaUpdater()({ schema })
|
|
@@ -79,7 +86,7 @@ export function testGenerateSql(original: Partial<Schema>, diff: Migration.Modif
|
|
|
79
86
|
assert.equal(actual, expectedSql)
|
|
80
87
|
}
|
|
81
88
|
|
|
82
|
-
export function testMigrations(title: string, { original, updated, diff, noDiff, sql }: TestContext) {
|
|
89
|
+
export function testMigrations(title: string, { original, updated, diff, noDiff, sql, databaseMetadata }: TestContext) {
|
|
83
90
|
describe(title, () => {
|
|
84
91
|
it('diff schemas', () => {
|
|
85
92
|
if (noDiff) {
|
|
@@ -91,7 +98,7 @@ export function testMigrations(title: string, { original, updated, diff, noDiff,
|
|
|
91
98
|
testApplyDiff(original, updated, diff)
|
|
92
99
|
})
|
|
93
100
|
it('generate sql', () => {
|
|
94
|
-
testGenerateSql(original, diff, sql)
|
|
101
|
+
testGenerateSql(original, diff, sql, databaseMetadata)
|
|
95
102
|
})
|
|
96
103
|
})
|
|
97
104
|
}
|