@@ -5,6 +5,7 @@ const _ = require('lodash');
5
5
module . exports = ( app , ddlProvider ) => {
6
6
const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ( _ ) ;
7
7
const { AlterScriptDto } = require ( '../types/AlterScriptDto' ) ;
8
+ const { compareObjectsByProperties } = require ( '../../../utils/general' ) ( _ ) ;
8
9
9
10
const changeToComputed = ( fullName , columnName , columnDefinition ) => {
10
11
return [
@@ -24,13 +25,17 @@ module.exports = (app, ddlProvider) => {
24
25
] ;
25
26
} ;
26
27
28
+ const propsToDetectChange = [ 'computed' , 'computedExpression' , 'persisted' , 'unique' , 'primaryKey' ] ;
29
+
27
30
const generateSqlAlterScript = ( {
28
31
collectionSchema,
29
32
prevJsonSchema,
30
33
jsonSchema,
31
34
fullName,
32
35
columnName,
33
36
schemaName,
37
+ toAddNotNull,
38
+ toRemoveNotNull,
34
39
} ) => {
35
40
const schemaData = { schemaName } ;
36
41
const columnDefinition = createColumnDefinitionBySchema ( {
@@ -40,6 +45,7 @@ module.exports = (app, ddlProvider) => {
40
45
ddlProvider,
41
46
schemaData,
42
47
} ) ;
48
+ columnDefinition . nullable = toRemoveNotNull ;
43
49
44
50
let sqlScripts = [ ] ;
45
51
@@ -48,8 +54,9 @@ module.exports = (app, ddlProvider) => {
48
54
const isComputedModified =
49
55
prevJsonSchema . computed &&
50
56
jsonSchema . computed &&
51
- ( prevJsonSchema . computedExpression !== jsonSchema . computedExpression ||
52
- prevJsonSchema . persisted !== jsonSchema . persisted ) ;
57
+ ( compareObjectsByProperties ( prevJsonSchema , jsonSchema , propsToDetectChange ) ||
58
+ toAddNotNull ||
59
+ toRemoveNotNull ) ;
53
60
54
61
if ( ( isComputedRemoved || isComputedModified ) && ! jsonSchema . computedExpression ) {
55
62
sqlScripts = changeToNonComputed ( fullName , columnName , columnDefinition ) ;
@@ -67,6 +74,14 @@ module.exports = (app, ddlProvider) => {
67
74
_ . toPairs ( collection . properties ) . reduce ( ( result , [ columnName , jsonSchema ] ) => {
68
75
const oldJsonSchema = _ . omit ( collection . role ?. properties ?. [ columnName ] , [ 'compMod' ] ) ;
69
76
77
+ const currentRequiredColumnNames = collection . required || [ ] ;
78
+ const previousRequiredColumnNames = collection . role . required || [ ] ;
79
+
80
+ const toAddNotNull =
81
+ _ . difference ( currentRequiredColumnNames , previousRequiredColumnNames ) . indexOf ( columnName ) !== - 1 ;
82
+ const toRemoveNotNull =
83
+ _ . difference ( previousRequiredColumnNames , currentRequiredColumnNames ) . indexOf ( columnName ) !== - 1 ;
84
+
70
85
result . push (
71
86
generateSqlAlterScript ( {
72
87
collectionSchema,
@@ -75,6 +90,8 @@ module.exports = (app, ddlProvider) => {
75
90
fullName,
76
91
columnName,
77
92
schemaName,
93
+ toAddNotNull,
94
+ toRemoveNotNull,
78
95
} ) ,
79
96
) ;
80
97
0 commit comments