Skip to content

Commit

Permalink
[backend] Fix constraint on delete set null to cascade
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 authored Sep 26, 2024
1 parent 45848f3 commit 5d42857
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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);");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class InjectImporter implements Base {
@NotBlank
private String importTypeValue;

@OneToOne
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "importer_injector_contract_id")
@JsonProperty("inject_importer_injector_contract")
@JsonSerialize(using = MonoIdDeserializer.class)
Expand Down

0 comments on commit 5d42857

Please sign in to comment.