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

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 (50) hide show
  1. package/dist/development/src/SchemaDiffer.cjs +1 -1
  2. package/dist/development/src/SchemaDiffer.cjs.map +1 -1
  3. package/dist/development/src/SchemaDiffer.js +1 -1
  4. package/dist/development/src/SchemaDiffer.js.map +1 -1
  5. package/dist/development/src/modifications/ModificationHandlerFactory.cjs +2 -0
  6. package/dist/development/src/modifications/ModificationHandlerFactory.cjs.map +1 -1
  7. package/dist/development/src/modifications/ModificationHandlerFactory.js +2 -0
  8. package/dist/development/src/modifications/ModificationHandlerFactory.js.map +1 -1
  9. package/dist/development/src/modifications/actions/PatchTriggerModification.cjs +44 -0
  10. package/dist/development/src/modifications/actions/PatchTriggerModification.cjs.map +1 -0
  11. package/dist/development/src/modifications/actions/PatchTriggerModification.js +44 -0
  12. package/dist/development/src/modifications/actions/PatchTriggerModification.js.map +1 -0
  13. package/dist/development/src/modifications/actions/UpdateTriggerModification.cjs +16 -1
  14. package/dist/development/src/modifications/actions/UpdateTriggerModification.cjs.map +1 -1
  15. package/dist/development/src/modifications/actions/UpdateTriggerModification.js +16 -1
  16. package/dist/development/src/modifications/actions/UpdateTriggerModification.js.map +1 -1
  17. package/dist/production/src/SchemaDiffer.cjs +1 -1
  18. package/dist/production/src/SchemaDiffer.cjs.map +1 -1
  19. package/dist/production/src/SchemaDiffer.js +1 -1
  20. package/dist/production/src/SchemaDiffer.js.map +1 -1
  21. package/dist/production/src/modifications/ModificationHandlerFactory.cjs +2 -0
  22. package/dist/production/src/modifications/ModificationHandlerFactory.cjs.map +1 -1
  23. package/dist/production/src/modifications/ModificationHandlerFactory.js +2 -0
  24. package/dist/production/src/modifications/ModificationHandlerFactory.js.map +1 -1
  25. package/dist/production/src/modifications/actions/PatchTriggerModification.cjs +44 -0
  26. package/dist/production/src/modifications/actions/PatchTriggerModification.cjs.map +1 -0
  27. package/dist/production/src/modifications/actions/PatchTriggerModification.js +44 -0
  28. package/dist/production/src/modifications/actions/PatchTriggerModification.js.map +1 -0
  29. package/dist/production/src/modifications/actions/UpdateTriggerModification.cjs +16 -1
  30. package/dist/production/src/modifications/actions/UpdateTriggerModification.cjs.map +1 -1
  31. package/dist/production/src/modifications/actions/UpdateTriggerModification.js +16 -1
  32. package/dist/production/src/modifications/actions/UpdateTriggerModification.js.map +1 -1
  33. package/dist/tests/cases/integration/patchTrigger.test.d.ts +2 -0
  34. package/dist/tests/cases/integration/patchTrigger.test.d.ts.map +1 -0
  35. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  36. package/dist/types/modifications/ModificationHandlerFactory.d.ts.map +1 -1
  37. package/dist/types/modifications/actions/PatchTriggerModification.d.ts +18 -0
  38. package/dist/types/modifications/actions/PatchTriggerModification.d.ts.map +1 -0
  39. package/dist/types/modifications/actions/UpdateTriggerModification.d.ts +5 -1
  40. package/dist/types/modifications/actions/UpdateTriggerModification.d.ts.map +1 -1
  41. package/dist/types/modifications/actions/index.d.ts +1 -0
  42. package/dist/types/modifications/actions/index.d.ts.map +1 -1
  43. package/dist/types/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +7 -7
  45. package/src/SchemaDiffer.ts +1 -1
  46. package/src/modifications/ModificationHandlerFactory.ts +2 -1
  47. package/src/modifications/actions/PatchTriggerModification.ts +51 -0
  48. package/src/modifications/actions/UpdateTriggerModification.ts +22 -1
  49. package/src/modifications/actions/index.ts +1 -0
  50. package/tests/cases/integration/patchTrigger.test.ts +184 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/schema-migrations",
3
- "version": "2.1.0-alpha.24",
3
+ "version": "2.1.0-alpha.26",
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.24",
27
- "@contember/database-migrations": "2.1.0-alpha.24",
28
- "@contember/engine-common": "2.1.0-alpha.24",
29
- "@contember/schema": "2.1.0-alpha.24",
30
- "@contember/schema-utils": "2.1.0-alpha.24",
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",
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.24"
35
+ "@contember/schema-definition": "2.1.0-alpha.26"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "pg": "^8.9.0"
@@ -109,7 +109,7 @@ export class SchemaDiffer {
109
109
  new UpdateEntityOrderByDiffer(),
110
110
  new UpdateTargetDiffer(),
111
111
  new CreateTargetDiffer(),
112
- new UpdateTriggerDiffer(),
112
+ new UpdateTriggerDiffer(this.options),
113
113
  new CreateTriggerDiffer(),
114
114
  new RemoveTriggerDiffer(),
115
115
  new RemoveTargetDiffer(),
@@ -30,7 +30,7 @@ import {
30
30
  import { patchValidationSchemaModification, updateValidationSchemaModification } from './validation'
31
31
  import { createIndexModification, removeIndexModification } from './indexes'
32
32
  import { updateSettingsModification } from './settings'
33
- import { createTriggerModification, removeTriggerModification, updateTriggerModification } from './actions'
33
+ import { createTriggerModification, patchTriggerModification, removeTriggerModification, updateTriggerModification } from './actions'
34
34
  import { createTargetModification } from './actions/CreateTargetModification'
35
35
  import { removeTargetModification } from './actions/RemoveTargetModification'
36
36
  import { updateTargetModification } from './actions/UpdateTargetModification'
@@ -94,6 +94,7 @@ namespace ModificationHandlerFactory {
94
94
  updateEntityOrderByModification,
95
95
  createTriggerModification,
96
96
  updateTriggerModification,
97
+ patchTriggerModification,
97
98
  removeTriggerModification,
98
99
  createTargetModification,
99
100
  updateTargetModification,
@@ -0,0 +1,51 @@
1
+ import { Actions } from '@contember/schema'
2
+ import { SchemaUpdater } from '../utils/schemaUpdateUtils'
3
+ import { createModificationType, ModificationHandler } from '../ModificationHandler'
4
+ import { applyPatch, Operation } from 'rfc6902'
5
+ import deepCopy from '../../utils/deepCopy'
6
+
7
+ export class PatchTriggerModificationHandler implements ModificationHandler<PatchTriggerModificationData> {
8
+ constructor(private readonly data: PatchTriggerModificationData) {}
9
+
10
+ public createSql(): void {}
11
+
12
+ public getSchemaUpdater(): SchemaUpdater {
13
+ return ({ schema }) => {
14
+ const trigger = deepCopy(schema.actions.triggers[this.data.triggerName])
15
+
16
+ if (!trigger) {
17
+ throw new Error(`Trigger ${this.data.triggerName} not found`)
18
+ }
19
+
20
+ const result = applyPatch(trigger, this.data.patch).filter(it => it !== null)
21
+ if (result.length > 0) {
22
+ throw result[0]
23
+ }
24
+
25
+ return {
26
+ ...schema,
27
+ actions: {
28
+ ...schema.actions,
29
+ triggers: {
30
+ ...schema.actions.triggers,
31
+ [this.data.triggerName]: trigger,
32
+ },
33
+ },
34
+ }
35
+ }
36
+ }
37
+
38
+ describe() {
39
+ return { message: `Patch trigger ${this.data.triggerName}` }
40
+ }
41
+ }
42
+
43
+ export interface PatchTriggerModificationData {
44
+ triggerName: string
45
+ patch: Operation[]
46
+ }
47
+
48
+ export const patchTriggerModification = createModificationType({
49
+ id: 'patchTrigger',
50
+ handler: PatchTriggerModificationHandler,
51
+ })
@@ -2,6 +2,8 @@ import { Actions, Schema } from '@contember/schema'
2
2
  import { SchemaUpdater } from '../utils/schemaUpdateUtils'
3
3
  import { createModificationType, Differ, ModificationHandler } from '../ModificationHandler'
4
4
  import deepEqual from 'fast-deep-equal'
5
+ import { createPatch } from 'rfc6902'
6
+ import { patchTriggerModification } from './PatchTriggerModification'
5
7
 
6
8
  export class UpdateTriggerModificationHandler implements ModificationHandler<UpdateTriggerModificationData> {
7
9
  constructor(private readonly data: UpdateTriggerModificationData) {
@@ -40,9 +42,28 @@ export const updateTriggerModification = createModificationType({
40
42
  })
41
43
 
42
44
  export class UpdateTriggerDiffer implements Differ {
45
+ constructor(
46
+ private readonly options?: {
47
+ maxPatchSize?: number
48
+ },
49
+ ) {
50
+ }
51
+
43
52
  createDiff(originalSchema: Schema, updatedSchema: Schema) {
44
53
  return Object.entries(updatedSchema.actions.triggers)
45
54
  .filter(([name, trigger]) => originalSchema.actions.triggers[name] && !deepEqual(trigger, originalSchema.actions.triggers[name]))
46
- .map(([, trigger]) => updateTriggerModification.createModification({ trigger }))
55
+ .map(([name, trigger]) => {
56
+ const originalTrigger = originalSchema.actions.triggers[name]
57
+
58
+ const patch = createPatch(originalTrigger, trigger)
59
+ if (patch.length <= (this.options?.maxPatchSize ?? 100)) {
60
+ return patchTriggerModification.createModification({
61
+ triggerName: name,
62
+ patch,
63
+ })
64
+ }
65
+
66
+ return updateTriggerModification.createModification({ trigger })
67
+ })
47
68
  }
48
69
  }
@@ -1,3 +1,4 @@
1
1
  export * from './CreateTriggerModification'
2
2
  export * from './RemoveTriggerModification'
3
3
  export * from './UpdateTriggerModification'
4
+ export * from './PatchTriggerModification'
@@ -0,0 +1,184 @@
1
+ import { Actions } from '@contember/schema'
2
+ import { emptySchema } from '@contember/schema-utils'
3
+ import { describe } from 'bun:test'
4
+ import { testMigrations } from '../../src/tests'
5
+ import { c, createSchema } from '@contember/schema-definition'
6
+ namespace Schema {
7
+ export class Post {
8
+ title = c.stringColumn()
9
+ description = c.stringColumn()
10
+ category = c.manyHasOne(Category, 'articles')
11
+ }
12
+
13
+ export class Category {
14
+ name = c.stringColumn()
15
+ slug = c.stringColumn()
16
+ articles = c.oneHasMany(Post, 'category')
17
+ }
18
+
19
+
20
+ }
21
+
22
+ describe('patch trigger', () => testMigrations({
23
+ original: {
24
+ ...createSchema(Schema),
25
+ actions: {
26
+ triggers: {
27
+ 'post_created': {
28
+ type: 'basic',
29
+ name: 'post_created',
30
+ entity: 'Post',
31
+ create: true,
32
+ delete: false,
33
+ update: false,
34
+ target: 'webhook',
35
+ selection: [
36
+ 'id',
37
+ 'title',
38
+ ['category', {}, ['name']],
39
+ ],
40
+ } satisfies Actions.BasicTrigger,
41
+ },
42
+ targets: {
43
+ 'webhook': {
44
+ type: 'webhook',
45
+ name: 'webhook',
46
+ url: 'https://example.com/webhook',
47
+ } satisfies Actions.WebhookTarget,
48
+ },
49
+ },
50
+ },
51
+ updated: {
52
+ ...createSchema(Schema),
53
+ actions: {
54
+ triggers: {
55
+ 'post_created': {
56
+ type: 'basic',
57
+ name: 'post_created',
58
+ entity: 'Post',
59
+ create: true,
60
+ delete: false,
61
+ update: false,
62
+ target: 'webhook',
63
+ selection: [
64
+ 'id',
65
+ 'title',
66
+ 'description', // added field
67
+ ['category', {}, ['name', 'slug']], // added sub-field
68
+ ],
69
+ } satisfies Actions.BasicTrigger,
70
+ },
71
+ targets: {
72
+ 'webhook': {
73
+ type: 'webhook',
74
+ name: 'webhook',
75
+ url: 'https://example.com/webhook',
76
+ } satisfies Actions.WebhookTarget,
77
+ },
78
+ },
79
+ },
80
+ diff: [
81
+ {
82
+ modification: 'patchTrigger',
83
+ triggerName: 'post_created',
84
+ patch: [
85
+ {
86
+ op: 'replace',
87
+ path: '/selection/2',
88
+ value: 'description',
89
+ },
90
+ {
91
+ op: 'add',
92
+ path: '/selection/-',
93
+ value: [
94
+ 'category',
95
+ {},
96
+ [
97
+ 'name',
98
+ 'slug',
99
+ ],
100
+ ],
101
+ },
102
+ ],
103
+ },
104
+ ],
105
+ sql: '', // no SQL changes for action modifications
106
+ }))
107
+
108
+ describe('patch watch trigger', () => testMigrations({
109
+ original: {
110
+ ...createSchema(Schema),
111
+ actions: {
112
+ triggers: {
113
+ 'post_watch': {
114
+ type: 'watch',
115
+ name: 'post_watch',
116
+ entity: 'Post',
117
+ target: 'webhook',
118
+ watch: [
119
+ 'title',
120
+ ['category', {}, ['name']],
121
+ ],
122
+ } satisfies Actions.WatchTrigger,
123
+ },
124
+ targets: {
125
+ 'webhook': {
126
+ type: 'webhook',
127
+ name: 'webhook',
128
+ url: 'https://example.com/webhook',
129
+ } satisfies Actions.WebhookTarget,
130
+ },
131
+ },
132
+ },
133
+ updated: {
134
+ ...createSchema(Schema),
135
+ actions: {
136
+ triggers: {
137
+ 'post_watch': {
138
+ type: 'watch',
139
+ name: 'post_watch',
140
+ entity: 'Post',
141
+ target: 'webhook',
142
+ watch: [
143
+ 'title',
144
+ 'description', // added field
145
+ ['category', {}, ['name', 'slug']], // added sub-field
146
+ ],
147
+ } satisfies Actions.WatchTrigger,
148
+ },
149
+ targets: {
150
+ 'webhook': {
151
+ type: 'webhook',
152
+ name: 'webhook',
153
+ url: 'https://example.com/webhook',
154
+ } satisfies Actions.WebhookTarget,
155
+ },
156
+ },
157
+ },
158
+ diff: [
159
+ {
160
+ modification: 'patchTrigger',
161
+ triggerName: 'post_watch',
162
+ patch: [
163
+ {
164
+ op: 'replace',
165
+ path: '/watch/1',
166
+ value: 'description',
167
+ },
168
+ {
169
+ op: 'add',
170
+ path: '/watch/-',
171
+ value: [
172
+ 'category',
173
+ {},
174
+ [
175
+ 'name',
176
+ 'slug',
177
+ ],
178
+ ],
179
+ },
180
+ ],
181
+ },
182
+ ],
183
+ sql: '', // no SQL changes for action modifications
184
+ }))