-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend] Fix constraint on delete set null to cascade
- Loading branch information
1 parent
45848f3
commit 5d42857
Showing
2 changed files
with
28 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...as-api/src/main/java/io/openbas/migration/V3_41__Update_constraint_on_delete_cascade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.openbas.migration; | ||
|
||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
|
||
@Component | ||
public class V3_41__Update_constraint_on_delete_cascade extends BaseJavaMigration { | ||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Connection connection = context.getConnection(); | ||
Statement select = connection.createStatement(); | ||
select.execute("ALTER TABLE inject_importers DROP CONSTRAINT inject_importers_injector_contract_id_fkey;"); | ||
select.execute("ALTER TABLE rule_attributes DROP CONSTRAINT rule_attributes_importer_id_fkey;"); | ||
|
||
// Add the new foreign key constraint with ON DELETE CASCADE | ||
select.execute("ALTER TABLE inject_importers ADD CONSTRAINT inject_importers_injector_contract_id_fkey FOREIGN KEY (importer_injector_contract_id) REFERENCES injectors_contracts(injector_contract_id) ON DELETE CASCADE;"); | ||
select.execute("ALTER TABLE rule_attributes ADD CONSTRAINT rule_attributes_importer_id_fkey FOREIGN KEY (attribute_inject_importer_id) REFERENCES inject_importers(importer_id) ON DELETE CASCADE;"); | ||
|
||
// Optionally, you can reindex if necessary (usually not required just for FK changes) | ||
select.execute("CREATE INDEX IF NOT EXISTS idx_inject_importers_injector_contracts ON inject_importers(importer_injector_contract_id);"); | ||
select.execute("CREATE INDEX IF NOT EXISTS idx_rule_attributes_inject_importers ON rule_attributes(attribute_inject_importer_id);"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters