@@ -2,7 +2,11 @@ const _ = require('lodash');
22const { checkFieldPropertiesChanged, getNames, getBaseAndContainerNames } = require ( './common' ) ;
33const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ;
44const { commentIfDeactivated } = require ( '../commentHelpers/commentDeactivatedHelper' ) ;
5- const { getEntityName, getFullName, getName, toString } = require ( '../general' ) ;
5+ const { getEntityName, getFullName, getName } = require ( '../general' ) ;
6+ const { getSetTagValue, getUnsetTagValue } = require ( '../../helpers/tagHelper' ) ;
7+ const assignTemplates = require ( '../../utils/assignTemplates' ) ;
8+ const templates = require ( '../../configs/templates' ) ;
9+ const { escapeString } = require ( '../../utils/escapeString' ) ;
610
711const getAddCollectionScript =
812 ( { ddlProvider, scriptFormat } ) =>
@@ -96,52 +100,79 @@ const getDeleteColumnScript = collection => {
96100 . map ( ( [ name ] ) => `ALTER TABLE IF EXISTS ${ fullName } DROP COLUMN ${ name } ;` ) ;
97101} ;
98102
99- const getModifyColumnScript = collection => {
100- const { getSetTagValue, getUnsetTagValue } = require ( '../../helpers/tagHelper' ) ( { getName, toString } ) ;
101-
102- const collectionSchema = {
103- ...collection ,
104- ...( _ . omit ( collection ?. role , 'properties' ) || { } ) ,
105- } ;
106- const { schemaName, databaseName, tableName } = getNames ( collectionSchema , getName , getEntityName ) ;
107- const fullName = getFullName ( databaseName , getFullName ( schemaName , tableName ) ) ;
108-
109- const renameColumnScripts = _ . values ( collection . properties )
110- . filter ( jsonSchema => checkFieldPropertiesChanged ( jsonSchema . compMod , [ 'name' ] ) )
111- . map (
112- jsonSchema =>
113- `ALTER TABLE IF EXISTS ${ fullName } RENAME COLUMN ${ jsonSchema . compMod . oldField . name } TO ${ jsonSchema . compMod . newField . name } ;` ,
114- ) ;
115-
116- const changeTypeScripts = _ . toPairs ( collection . properties )
117- . filter ( ( [ name , jsonSchema ] ) => checkFieldPropertiesChanged ( jsonSchema . compMod , [ 'type' , 'mode' ] ) )
118- . map (
119- ( [ name , jsonSchema ] ) =>
120- `ALTER TABLE IF EXISTS ${ fullName } ALTER COLUMN ${ name } SET DATA TYPE ${
121- jsonSchema . compMod . newField . mode || jsonSchema . compMod . newField . type
122- } ;`,
123- ) ;
124-
125- const changeTagScripts = _ . toPairs ( collection . properties ) . reduce ( ( result , [ name , jsonSchema ] ) => {
126- const tags = jsonSchema . columnTags ;
127- const oldTags = collection . role ?. properties ?. [ name ] ?. columnTags ;
128- const isCaseSensitive = collection . role ?. isCaseSensitive ;
129- const tagsToSet = getSetTagValue ( { tags, oldTags, isCaseSensitive } ) ;
130- const tagsToUnset = getUnsetTagValue ( { tags, oldTags, isCaseSensitive } ) ;
103+ const getModifyColumnScript =
104+ ( { scriptFormat } ) =>
105+ collection => {
106+ const collectionSchema = {
107+ ...collection ,
108+ ..._ . omit ( collection ?. role , 'properties' ) ,
109+ } ;
110+ const { schemaName, databaseName, tableName } = getNames ( collectionSchema , getName , getEntityName ) ;
111+ const fullName = getFullName ( databaseName , getFullName ( schemaName , tableName ) ) ;
131112
132- if ( tagsToSet ) {
133- result . push ( `ALTER TABLE IF EXISTS ${ fullName } MODIFY COLUMN ${ name } SET ${ tagsToSet } ;` ) ;
134- }
113+ const renameColumnScripts = _ . values ( collection . properties )
114+ . filter ( jsonSchema => checkFieldPropertiesChanged ( jsonSchema . compMod , [ 'name' ] ) )
115+ . map (
116+ jsonSchema =>
117+ `ALTER TABLE IF EXISTS ${ fullName } RENAME COLUMN ${ jsonSchema . compMod . oldField . name } TO ${ jsonSchema . compMod . newField . name } ;` ,
118+ ) ;
135119
136- if ( tagsToUnset ) {
137- result . push ( `ALTER TABLE IF EXISTS ${ fullName } MODIFY COLUMN ${ name } UNSET ${ tagsToUnset } ;` ) ;
138- }
120+ const nameToJsonSchemaPairs = _ . toPairs ( collection . properties ) ;
139121
140- return result ;
141- } , [ ] ) ;
122+ const changeTypeScripts = nameToJsonSchemaPairs
123+ . filter ( ( [ name , jsonSchema ] ) => checkFieldPropertiesChanged ( jsonSchema . compMod , [ 'type' , 'mode' ] ) )
124+ . map (
125+ ( [ name , jsonSchema ] ) =>
126+ `ALTER TABLE IF EXISTS ${ fullName } ALTER COLUMN ${ name } SET DATA TYPE ${
127+ jsonSchema . compMod . newField . mode || jsonSchema . compMod . newField . type
128+ } ;`,
129+ ) ;
142130
143- return [ ...renameColumnScripts , ...changeTypeScripts , ...changeTagScripts ] ;
144- } ;
131+ const changeTagScripts = nameToJsonSchemaPairs . reduce ( ( result , [ name , jsonSchema ] ) => {
132+ const tags = jsonSchema . columnTags ;
133+ const oldTags = collection . role ?. properties ?. [ name ] ?. columnTags ;
134+ const isCaseSensitive = collection . role ?. isCaseSensitive ;
135+ const tagsToSet = getSetTagValue ( { tags, oldTags, isCaseSensitive } ) ;
136+ const tagsToUnset = getUnsetTagValue ( { tags, oldTags, isCaseSensitive } ) ;
137+
138+ if ( tagsToSet ) {
139+ result . push ( `ALTER TABLE IF EXISTS ${ fullName } MODIFY COLUMN ${ name } SET ${ tagsToSet } ;` ) ;
140+ }
141+
142+ if ( tagsToUnset ) {
143+ result . push ( `ALTER TABLE IF EXISTS ${ fullName } MODIFY COLUMN ${ name } UNSET ${ tagsToUnset } ;` ) ;
144+ }
145+
146+ return result ;
147+ } , [ ] ) ;
148+
149+ const modifyCommentScripts = nameToJsonSchemaPairs
150+ . map ( ( [ name , jsonSchema ] ) => {
151+ const columnName = getName ( collectionSchema . isCaseSensitive , name ) ;
152+
153+ const comment = jsonSchema . description ;
154+ const oldComment = collection . role ?. properties ?. [ name ] ?. description ;
155+
156+ // comment was removed
157+ if ( oldComment && ! comment ) {
158+ return assignTemplates ( templates . alterTable , {
159+ name : fullName ,
160+ action : `MODIFY COLUMN ${ columnName } UNSET COMMENT` ,
161+ } ) ;
162+ }
163+
164+ // new or modified comment
165+ if ( oldComment !== comment ) {
166+ return assignTemplates ( templates . alterTable , {
167+ name : fullName ,
168+ action : `MODIFY COLUMN ${ columnName } COMMENT = ${ escapeString ( scriptFormat , comment ) } ` ,
169+ } ) ;
170+ }
171+ } )
172+ . filter ( Boolean ) ;
173+
174+ return [ ...renameColumnScripts , ...changeTypeScripts , ...changeTagScripts , ...modifyCommentScripts ] ;
175+ } ;
145176
146177module . exports = {
147178 getAddCollectionScript,
0 commit comments