1- const { AlterScriptDto } = require ( '../../types/AlterScriptDto' ) ;
1+ const { AlterScriptDto} = require ( '../../types/AlterScriptDto' ) ;
22const {
33 AlterCollectionDto,
44 AlterCollectionColumnDto,
55 AlterCollectionRoleCompModPKDto,
66 AlterCollectionColumnPrimaryKeyOptionDto
77} = require ( '../../types/AlterCollectionDto' ) ;
88
9-
10- /**
11- * @return {(collection: AlterCollectionDto, guid: string) => AlterCollectionColumnDto | undefined }
12- * */
13- const getPropertyByGuid = ( _ ) => ( collection , guid ) => {
14- const propertyInArray = _ . toPairs ( collection ?. role ?. properties )
15- . filter ( ( [ name , jsonSchema ] ) => jsonSchema . GUID === guid )
16- . map ( ( [ name , jsonSchema ] ) => jsonSchema ) ;
17- if ( ! propertyInArray . length ) {
18- return undefined ;
19- }
20- return propertyInArray [ 0 ] ;
21- }
22-
23- /**
24- * @return {(collection: AlterCollectionDto, guids: string[]) => Array<AlterCollectionColumnDto> }
25- * */
26- const getPropertiesByGuids = ( _ ) => ( collection , guids ) => {
27- return guids
28- . map ( guid => getPropertyByGuid ( _ ) ( collection , guid ) )
29- . filter ( Boolean ) ;
30- }
31-
329/**
3310 * @return {(collection: AlterCollectionDto) => boolean }
3411 * */
@@ -47,11 +24,11 @@ const didCompositePkChange = (_) => (collection) => {
4724}
4825
4926/**
50- * @param wrapInQuotes {(s: string) => string}
51- * @return {(entityName: string) => string }
27+ * @param entityName { string}
28+ * @return {string }
5229 * */
53- const getDefaultConstraintName = ( wrapInQuotes ) => ( entityName ) => {
54- return wrapInQuotes ( `${ entityName } _pk` ) ;
30+ const getDefaultConstraintName = ( entityName ) => {
31+ return `${ entityName } _pk` ;
5532}
5633
5734/**
@@ -116,7 +93,7 @@ const getDropCompositePkScripts = (_, ddlProvider) => (collection) => {
11693
11794 return oldPrimaryKeys
11895 . map ( ( oldPk ) => {
119- let constraintName = getDefaultConstraintName ( wrapInQuotes ) ( entityName ) ;
96+ let constraintName = getDefaultConstraintName ( entityName ) ;
12097 if ( oldPk . constraintName ) {
12198 constraintName = wrapInQuotes ( oldPk . constraintName ) ;
12299 }
@@ -139,6 +116,97 @@ const getModifyCompositePkScripts = (_, ddlProvider) => (collection) => {
139116 ] . filter ( Boolean ) ;
140117}
141118
119+ /**
120+ * @param columnJsonSchema {AlterCollectionColumnDto}
121+ * @param entityName {string}
122+ * @return {string }
123+ * */
124+ const getConstraintNameForRegularPk = ( columnJsonSchema , entityName ) => {
125+ const constraintOptions = columnJsonSchema . primaryKeyOptions ;
126+ if ( constraintOptions ?. length && constraintOptions ?. length > 0 ) {
127+ /**
128+ * @type {AlterCollectionColumnPrimaryKeyOptionDto }
129+ * */
130+ const constraintOption = constraintOptions [ 0 ] ;
131+ if ( constraintOption . constraintName ) {
132+ return constraintOption . constraintName ;
133+ }
134+ }
135+ return getDefaultConstraintName ( entityName ) ;
136+ }
137+
138+ /**
139+ * @param _
140+ * @param wrapInQuotes {(s: string) => string }
141+ * @return {(
142+ * name: string,
143+ * columnJsonSchema: AlterCollectionColumnDto,
144+ * entityName: string,
145+ * entityJsonSchema: AlterCollectionDto,
146+ * ) => {
147+ * name: string,
148+ * keyType: string,
149+ * columns: Array<{
150+ * isActivated: boolean,
151+ * name: string,
152+ * }>,
153+ * include: Array<{
154+ * isActivated: boolean,
155+ * name: string,
156+ * }>,
157+ * storageParameters: string,
158+ * tablespace: string,
159+ * }
160+ * }
161+ * */
162+ const getCreateRegularPKDDLProviderConfig = ( _ , wrapInQuotes ) => (
163+ columnName ,
164+ columnJsonSchema ,
165+ entityName ,
166+ entityJsonSchema
167+ ) => {
168+ const constraintName = getConstraintNameForRegularPk ( columnJsonSchema , entityName ) ;
169+ const pkColumns = [ {
170+ name : wrapInQuotes ( columnName ) ,
171+ isActivated : columnJsonSchema . isActivated ,
172+ } ] ;
173+
174+ let storageParameters = '' ;
175+ let indexTablespace = '' ;
176+ let includeColumns = [ ] ;
177+ const constraintOptions = columnJsonSchema . primaryKeyOptions ;
178+ if ( constraintOptions ?. length && constraintOptions ?. length > 0 ) {
179+ /**
180+ * @type {AlterCollectionColumnPrimaryKeyOptionDto }
181+ * */
182+ const constraintOption = constraintOptions [ 0 ] ;
183+ if ( constraintOption . indexStorageParameters ) {
184+ storageParameters = constraintOption . indexStorageParameters ;
185+ }
186+ if ( constraintOption . indexTablespace ) {
187+ indexTablespace = constraintOption . indexTablespace ;
188+ }
189+ if ( constraintOption . indexInclude ) {
190+ includeColumns = _ . toPairs ( entityJsonSchema . properties )
191+ . filter ( ( [ name , jsonSchema ] ) => Boolean ( constraintOption . indexInclude . find ( keyDto => keyDto . keyId === jsonSchema . id ) ) )
192+ . map ( ( [ name , jsonSchema ] ) => ( {
193+ name,
194+ isActivated : jsonSchema . isActivated ,
195+ } ) ) ;
196+ }
197+ }
198+
199+ return {
200+ name : constraintName ,
201+ keyType : 'PRIMARY KEY' ,
202+ columns : pkColumns ,
203+ include : includeColumns ,
204+ storageParameters,
205+ tablespace : indexTablespace ,
206+ }
207+ }
208+
209+
142210/**
143211 * @return {(collection: AlterCollectionDto) => Array<AlterScriptDto> }
144212 * */
@@ -149,11 +217,10 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
149217 getEntityName,
150218 wrapInQuotes
151219 } = require ( '../../../utils/general' ) ( _ ) ;
152- const collectionSchema = getSchemaOfAlterCollection ( collection ) ;
153220
221+ const collectionSchema = getSchemaOfAlterCollection ( collection ) ;
154222 const fullTableName = getFullCollectionName ( collectionSchema ) ;
155223 const entityName = getEntityName ( collectionSchema ) ;
156- const constraintName = getDefaultConstraintName ( wrapInQuotes ) ( entityName ) ;
157224
158225 return _ . toPairs ( collection . properties )
159226 . filter ( ( [ name , jsonSchema ] ) => {
@@ -163,21 +230,12 @@ const getAddPkScripts = (_, ddlProvider) => (collection) => {
163230 return isRegularPrimaryKey && ! wasTheFieldAPrimaryKey ;
164231 } )
165232 . map ( ( [ name , jsonSchema ] ) => {
166- const pkColumns = [ {
167- name : wrapInQuotes ( name ) ,
168- isActivated : jsonSchema . isActivated ,
169- } ]
170-
233+ const ddlConfig = getCreateRegularPKDDLProviderConfig ( _ , wrapInQuotes ) ( name , jsonSchema , entityName , collection ) ;
171234 return ddlProvider . createKeyConstraint (
172235 fullTableName ,
173236 collection . isActivated ,
174- {
175- name : constraintName ,
176- columns : pkColumns ,
177- include : [ ] ,
178- storageParameters : '' ,
179- tablespace : '' ,
180- } ) ;
237+ ddlConfig
238+ ) ;
181239 } )
182240 . map ( scriptDto => AlterScriptDto . getInstance ( [ scriptDto . statement ] , scriptDto . isActivated , false ) )
183241 . filter ( Boolean ) ;
@@ -208,18 +266,7 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
208266 return wasTheFieldARegularPrimaryKey && isNotAPrimaryKey ;
209267 } )
210268 . map ( ( [ name , jsonSchema ] ) => {
211- const constraintOptions = jsonSchema . primaryKeyOptions ;
212- let constraintName = getDefaultConstraintName ( wrapInQuotes ) ( entityName ) ;
213- if ( constraintOptions ?. length && constraintOptions ?. length > 0 ) {
214- /**
215- * @type {AlterCollectionColumnPrimaryKeyOptionDto }
216- * */
217- const constraintOption = constraintOptions [ 0 ] ;
218- if ( constraintOption . constraintName ) {
219- constraintName = wrapInQuotes ( constraintOption . constraintName ) ;
220- }
221- }
222-
269+ const constraintName = wrapInQuotes ( getConstraintNameForRegularPk ( jsonSchema , entityName ) ) ;
223270 return ddlProvider . dropPkConstraint ( fullTableName , constraintName ) ;
224271 } )
225272 . map ( scriptLine => AlterScriptDto . getInstance ( [ scriptLine ] , collection . isActivated , true ) )
@@ -231,11 +278,11 @@ const getDropPkScript = (_, ddlProvider) => (collection) => {
231278 * */
232279const getModifyPkScripts = ( _ , ddlProvider ) => ( collection ) => {
233280 const dropPkScripts = getDropPkScript ( _ , ddlProvider ) ( collection ) ;
234- // const addPkScripts = getAddPkScripts(_, ddlProvider)(collection);
281+ const addPkScripts = getAddPkScripts ( _ , ddlProvider ) ( collection ) ;
235282
236283 return [
237284 ...dropPkScripts ,
238- // ...addPkScripts,
285+ ...addPkScripts ,
239286 ] . filter ( Boolean ) ;
240287}
241288
0 commit comments