@@ -89,53 +89,63 @@ const getModifyCollectionScriptDtos = (app) => (collection) => {
8989 ] . filter ( Boolean ) ;
9090}
9191
92+ /**
93+ * @return {(collection: Object, predicate: ([name: string, jsonSchema: Object]) => boolean) => AlterScriptDto[] }
94+ * */
95+ const getAddColumnsByConditionScriptDtos = ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions} ) =>
96+ ( collection , predicate ) => {
97+ const _ = app . require ( 'lodash' ) ;
98+ const { getEntityName, getNamePrefixedWithSchemaName} = require ( '../../utils/general' ) ( _ ) ;
99+ const { createColumnDefinitionBySchema} = require ( './createColumnDefinition' ) ( app ) ;
100+ const ddlProvider = require ( '../../ddlProvider/ddlProvider' ) ( null , null , app ) ;
101+ const { getDefinitionByReference} = app . require ( '@hackolade/ddl-fe-utils' ) ;
102+
103+ const collectionSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
104+ const tableName = getEntityName ( collectionSchema ) ;
105+ const schemaName = collectionSchema . compMod ?. keyspaceName ;
106+ const fullName = getNamePrefixedWithSchemaName ( tableName , schemaName ) ;
107+ const schemaData = { schemaName, dbVersion} ;
108+
109+ return _ . toPairs ( collection . properties )
110+ . filter ( ( [ name , jsonSchema ] ) => predicate ( [ name , jsonSchema ] ) )
111+ . map ( ( [ name , jsonSchema ] ) => {
112+ const definitionJsonSchema = getDefinitionByReference ( {
113+ propertySchema : jsonSchema ,
114+ modelDefinitions,
115+ internalDefinitions,
116+ externalDefinitions,
117+ } ) ;
118+
119+ return createColumnDefinitionBySchema ( {
120+ name,
121+ jsonSchema,
122+ parentJsonSchema : collectionSchema ,
123+ ddlProvider,
124+ schemaData,
125+ definitionJsonSchema,
126+ } ) ;
127+ } )
128+ . map ( ddlProvider . convertColumnDefinition )
129+ . map ( columnDefinition => ddlProvider . addColumn ( fullName , columnDefinition ) )
130+ . map ( addColumnScript => AlterScriptDto . getInstance ( [ addColumnScript ] , true , false ) )
131+ . filter ( Boolean ) ;
132+ } ;
133+
92134/**
93135 * @return {(collection: Object) => AlterScriptDto[] }
94136 * */
95137const getAddColumnScriptDtos =
96138 ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions} ) =>
97139 collection => {
98- const _ = app . require ( 'lodash' ) ;
99- const { getEntityName, getNamePrefixedWithSchemaName} = require ( '../../utils/general' ) ( _ ) ;
100- const { createColumnDefinitionBySchema} = require ( './createColumnDefinition' ) ( app ) ;
101- const ddlProvider = require ( '../../ddlProvider/ddlProvider' ) ( null , null , app ) ;
102- const { getDefinitionByReference} = app . require ( '@hackolade/ddl-fe-utils' ) ;
103-
104- const collectionSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
105- const tableName = getEntityName ( collectionSchema ) ;
106- const schemaName = collectionSchema . compMod ?. keyspaceName ;
107- const fullName = getNamePrefixedWithSchemaName ( tableName , schemaName ) ;
108- const schemaData = { schemaName, dbVersion} ;
109-
110- return _ . toPairs ( collection . properties )
111- . filter ( ( [ name , jsonSchema ] ) => ! jsonSchema . compMod )
112- . map ( ( [ name , jsonSchema ] ) => {
113- const definitionJsonSchema = getDefinitionByReference ( {
114- propertySchema : jsonSchema ,
115- modelDefinitions,
116- internalDefinitions,
117- externalDefinitions,
118- } ) ;
119-
120- return createColumnDefinitionBySchema ( {
121- name,
122- jsonSchema,
123- parentJsonSchema : collectionSchema ,
124- ddlProvider,
125- schemaData,
126- definitionJsonSchema,
127- } ) ;
128- } )
129- . map ( ddlProvider . convertColumnDefinition )
130- . map ( columnDefinition => ddlProvider . addColumn ( fullName , columnDefinition ) )
131- . map ( addColumnScript => AlterScriptDto . getInstance ( [ addColumnScript ] , true , false ) )
132- . filter ( Boolean ) ;
133- } ;
140+ return getAddColumnsByConditionScriptDtos ( {
141+ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions
142+ } ) ( collection , ( [ name , jsonSchema ] ) => ! jsonSchema . compMod ) ;
143+ }
134144
135145/**
136- * @return {(collection: Object) => AlterScriptDto[] }
146+ * @return {(collection: Object, predicate: ([name: string, jsonSchema: Object]) => boolean ) => AlterScriptDto[] }
137147 * */
138- const getDeleteColumnScriptDtos = app => collection => {
148+ const getDeleteColumnsByConditionScriptDtos = app => ( collection , predicate ) => {
139149 const _ = app . require ( 'lodash' ) ;
140150 const ddlProvider = require ( '../../ddlProvider/ddlProvider' ) ( null , null , app ) ;
141151 const { getEntityName, getNamePrefixedWithSchemaName, wrapInQuotes} = require ( '../../utils/general' ) ( _ ) ;
@@ -146,7 +156,7 @@ const getDeleteColumnScriptDtos = app => collection => {
146156 const fullTableName = getNamePrefixedWithSchemaName ( tableName , schemaName ) ;
147157
148158 return _ . toPairs ( collection . properties )
149- . filter ( ( [ name , jsonSchema ] ) => ! jsonSchema . compMod )
159+ . filter ( ( [ name , jsonSchema ] ) => predicate ( [ name , jsonSchema ] ) )
150160 . map ( ( [ name ] ) => {
151161 const columnNameForDDL = wrapInQuotes ( name ) ;
152162 return ddlProvider . dropColumn ( fullTableName , columnNameForDDL )
@@ -158,18 +168,61 @@ const getDeleteColumnScriptDtos = app => collection => {
158168/**
159169 * @return {(collection: Object) => AlterScriptDto[] }
160170 * */
161- const getModifyColumnScriptDtos = app => collection => {
171+ const getDeleteColumnScriptDtos = app => collection => {
172+ return getDeleteColumnsByConditionScriptDtos ( app ) ( collection , ( [ name , jsonSchema ] ) => ! jsonSchema . compMod )
173+ } ;
174+
175+ const getModifyGeneratedColumnsScriptDtos = ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions} ) =>
176+ ( collection ) => {
177+ const _ = app . require ( 'lodash' ) ;
178+
179+ return _ . toPairs ( collection . properties )
180+ . filter ( ( [ name , jsonSchema ] ) => {
181+ const oldName = jsonSchema . compMod . oldField . name ;
182+ const oldProperty = collection . role . properties [ oldName ] ;
183+
184+ return oldProperty . generatedColumn !== jsonSchema . generatedColumn
185+ || oldProperty . columnGenerationExpression !== jsonSchema . columnGenerationExpression ;
186+ } )
187+ . flatMap ( ( [ name , jsonSchema ] ) => {
188+ const collectionWithJustThisProperty = {
189+ ...collection ,
190+ properties : _ . fromPairs ( [
191+ [ name , jsonSchema ]
192+ ] ) ,
193+ }
194+ const deleteColumnsScriptDtos = getDeleteColumnsByConditionScriptDtos ( app ) ( collectionWithJustThisProperty , ( ) => true ) ;
195+ const addColumnsScriptDtos = getAddColumnsByConditionScriptDtos ( {
196+ app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions
197+ } ) ( collectionWithJustThisProperty , ( ) => true ) ;
198+
199+ return [
200+ ...deleteColumnsScriptDtos ,
201+ ...addColumnsScriptDtos ,
202+ ]
203+ } )
204+ . filter ( Boolean ) ;
205+ }
206+
207+ /**
208+ * @return {(collection: Object) => AlterScriptDto[] }
209+ * */
210+ const getModifyColumnScriptDtos = (
211+ { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions}
212+ ) => collection => {
162213 const _ = app . require ( 'lodash' ) ;
163214 const ddlProvider = require ( '../../ddlProvider/ddlProvider' ) ( null , null , app ) ;
164215
165216 const renameColumnScriptDtos = getRenameColumnScriptDtos ( _ , ddlProvider ) ( collection ) ;
166217 const updateTypeScriptDtos = getUpdateTypesScriptDtos ( _ , ddlProvider ) ( collection ) ;
218+ const modifyGeneratedColumnsScriptDtos = getModifyGeneratedColumnsScriptDtos ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions} ) ( collection ) ;
167219 const modifyNotNullScriptDtos = getModifyNonNullColumnsScriptDtos ( _ , ddlProvider ) ( collection ) ;
168220 const modifyCommentScriptDtos = getModifiedCommentOnColumnScriptDtos ( _ , ddlProvider ) ( collection ) ;
169221
170222 return [
171223 ...renameColumnScriptDtos ,
172224 ...updateTypeScriptDtos ,
225+ ...modifyGeneratedColumnsScriptDtos ,
173226 ...modifyNotNullScriptDtos ,
174227 ...modifyCommentScriptDtos ,
175228 ] . filter ( Boolean ) ;
0 commit comments