@@ -25,9 +25,12 @@ const getFullChildTableName = (_) => (relationship) => {
2525}
2626
2727/**
28- * @return {(relationship: AlterRelationshipDto) => string }
28+ * @return {(relationship: AlterRelationshipDto) => {
29+ * isActivated: boolean,
30+ * statement: string,
31+ * }}
2932 * */
30- const getAddSingleForeignKeyScript = ( ddlProvider , _ ) => ( relationship ) => {
33+ const getAddSingleForeignKeyStatementDto = ( ddlProvider , _ ) => ( relationship ) => {
3134 const compMod = relationship . role . compMod ;
3235
3336 const relationshipName = compMod . name ?. new || getRelationshipName ( relationship ) || '' ;
@@ -43,7 +46,8 @@ const getAddSingleForeignKeyScript = (ddlProvider, _) => (relationship) => {
4346 primaryTable : compMod . parent . collection . name ,
4447 primarySchemaName : compMod . parent . bucket . name ,
4548 primaryTableActivated : compMod . parent . collection . isActivated ,
46- } ) . statement ;
49+ isActivated : Boolean ( relationship . role ?. compMod ?. isActivated ?. new ) ,
50+ } ) ;
4751}
4852
4953/**
@@ -73,26 +77,35 @@ const getAddForeignKeyScriptDtos = (ddlProvider, _) => (addedRelationships) => {
7377 return addedRelationships
7478 . filter ( ( relationship ) => canRelationshipBeAdded ( relationship ) )
7579 . map ( relationship => {
76- const script = getAddSingleForeignKeyScript ( ddlProvider , _ ) ( relationship ) ;
77- const isActivated = Boolean ( relationship . role ?. compMod ?. isActivated ?. new ) ;
78- return AlterScriptDto . getInstance ( [ script ] , isActivated , false ) ;
80+ const scriptDto = getAddSingleForeignKeyStatementDto ( ddlProvider , _ ) ( relationship ) ;
81+ return AlterScriptDto . getInstance ( [ scriptDto . statement ] , scriptDto . isActivated , false ) ;
7982 } )
8083 . filter ( Boolean )
8184 . filter ( res => res . scripts . some ( scriptDto => Boolean ( scriptDto . script ) ) ) ;
8285}
8386
8487/**
85- * @return {(relationship: AlterRelationshipDto) => string }
88+ * @return {(relationship: AlterRelationshipDto) => {
89+ * isActivated: boolean,
90+ * statement: string,
91+ * }}
8692 * */
87- const getDeleteSingleForeignKeyScript = ( ddlProvider , _ ) => ( relationship ) => {
93+ const getDeleteSingleForeignKeyStatementDto = ( ddlProvider , _ ) => ( relationship ) => {
8894 const { wrapInQuotes} = require ( '../../utils/general' ) ( _ ) ;
8995 const compMod = relationship . role . compMod ;
9096
9197 const ddlChildEntityName = getFullChildTableName ( _ ) ( relationship ) ;
9298
9399 const relationshipName = compMod . name ?. old || getRelationshipName ( relationship ) || '' ;
94100 const ddlRelationshipName = wrapInQuotes ( relationshipName ) ;
95- return ddlProvider . dropForeignKey ( ddlChildEntityName , ddlRelationshipName ) ;
101+ const statement = ddlProvider . dropForeignKey ( ddlChildEntityName , ddlRelationshipName ) ;
102+
103+ const isRelationshipActivated = Boolean ( relationship . role ?. compMod ?. isActivated ?. new ) ;
104+ const isChildTableActivated = compMod . child . collection . isActivated ;
105+ return {
106+ statement,
107+ isActivated : isRelationshipActivated && isChildTableActivated ,
108+ }
96109}
97110
98111/**
@@ -118,9 +131,8 @@ const getDeleteForeignKeyScriptDtos = (ddlProvider, _) => (deletedRelationships)
118131 return deletedRelationships
119132 . filter ( ( relationship ) => canRelationshipBeDeleted ( relationship ) )
120133 . map ( relationship => {
121- const script = getDeleteSingleForeignKeyScript ( ddlProvider , _ ) ( relationship ) ;
122- const isActivated = Boolean ( relationship . role ?. compMod ?. isActivated ?. new ) ;
123- return AlterScriptDto . getInstance ( [ script ] , isActivated , true ) ;
134+ const scriptDto = getDeleteSingleForeignKeyStatementDto ( ddlProvider , _ ) ( relationship ) ;
135+ return AlterScriptDto . getInstance ( [ scriptDto . statement ] , scriptDto . isActivated , true ) ;
124136 } )
125137 . filter ( Boolean )
126138 . filter ( res => res . scripts . some ( scriptDto => Boolean ( scriptDto . script ) ) ) ;
@@ -133,10 +145,10 @@ const getModifyForeignKeyScriptDtos = (ddlProvider, _) => (modifiedRelationships
133145 return modifiedRelationships
134146 . filter ( relationship => canRelationshipBeAdded ( relationship ) && canRelationshipBeDeleted ( relationship ) )
135147 . map ( relationship => {
136- const deleteScript = getDeleteSingleForeignKeyScript ( ddlProvider , _ ) ( relationship ) ;
137- const addScript = getAddSingleForeignKeyScript ( ddlProvider , _ ) ( relationship ) ;
138- const isActivated = Boolean ( relationship . role ?. compMod ?. isActivated ?. new ) ;
139- return AlterScriptDto . getDropAndRecreateInstance ( deleteScript , addScript , isActivated ) ;
148+ const deleteScriptDto = getDeleteSingleForeignKeyStatementDto ( ddlProvider , _ ) ( relationship ) ;
149+ const addScriptDto = getAddSingleForeignKeyStatementDto ( ddlProvider , _ ) ( relationship ) ;
150+ const isActivated = addScriptDto . isActivated && deleteScriptDto . isActivated ;
151+ return AlterScriptDto . getDropAndRecreateInstance ( deleteScriptDto . statement , addScriptDto . statement , isActivated ) ;
140152 } )
141153 . filter ( Boolean )
142154 . filter ( res => res . scripts . some ( scriptDto => Boolean ( scriptDto . script ) ) ) ;
0 commit comments