Skip to content

Commit

Permalink
#880 - Model.deleteCascadeExcept() ignore third-party association ex…
Browse files Browse the repository at this point in the history
…clusion
  • Loading branch information
yanchevsky committed Aug 20, 2019
1 parent 05d9da3 commit ab18db6
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 84 deletions.
19 changes: 13 additions & 6 deletions activejdbc/src/main/java/org/javalite/activejdbc/MetaModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,36 +394,43 @@ public String getFKName() {
}

//TODO AY: add prepared association's lists by type
protected List<OneToManyAssociation> getOneToManyAssociations(List<Association> exclusions) {
protected List<OneToManyAssociation> getOneToManyAssociations(Association... excludedAssociations) {
List<OneToManyAssociation> one2Manies = new ArrayList<>();
for (Association association : associations) {
if(association.getClass().equals(OneToManyAssociation.class) && !exclusions.contains(association)){
if(association.getClass().equals(OneToManyAssociation.class) && !isExcluded(association, excludedAssociations)){
one2Manies.add((OneToManyAssociation)association);
}
}
return one2Manies;
}

protected List<OneToManyPolymorphicAssociation> getPolymorphicAssociations(List<Association> exclusions) {
protected List<OneToManyPolymorphicAssociation> getPolymorphicAssociations(Association... excludedAssociations) {
List<OneToManyPolymorphicAssociation> one2Manies = new ArrayList<>();
for (Association association : associations) {
if(association.getClass().equals(OneToManyPolymorphicAssociation.class) && !exclusions.contains(association)){
if(association.getClass().equals(OneToManyPolymorphicAssociation.class) && !isExcluded(association, excludedAssociations)){
one2Manies.add((OneToManyPolymorphicAssociation)association);
}
}
return one2Manies;
}

protected List<Many2ManyAssociation> getManyToManyAssociations(List<Association> excludedAssociations) {
protected List<Many2ManyAssociation> getManyToManyAssociations(Association... excludedAssociations) {
List<Many2ManyAssociation> many2Manies = new ArrayList<>();
for (Association association : associations) {
if(association.getClass().equals(Many2ManyAssociation.class) && !excludedAssociations.contains(association)){
if(association.getClass().equals(Many2ManyAssociation.class) && !isExcluded(association, excludedAssociations)){
many2Manies .add((Many2ManyAssociation)association);
}
}
return many2Manies ;
}

private boolean isExcluded(Association association, Association... excludedAssociation) {
for(Association a : excludedAssociation) {
if (association.equals(a)) return true;
}
return false;
}

public String getDbType(){
return dbType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void addMetaModel(MetaModel mm, Class<? extends Model> modelClass) {
LogFilter.log(LOGGER, LogLevel.WARNING, "Double-register: {}: {}", modelClass, o);
}
o = metaModelsByTableName.put(mm.getTableName(), mm);
many2ManyAssociations.addAll(mm.getManyToManyAssociations(Collections.<Association>emptyList()));
many2ManyAssociations.addAll(mm.getManyToManyAssociations());
if (o != null) {
LogFilter.log(LOGGER, LogLevel.WARNING, "Double-register: {}: {}", mm.getTableName(), o);
}
Expand Down
23 changes: 11 additions & 12 deletions activejdbc/src/main/java/org/javalite/activejdbc/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,16 @@ public void deleteCascade(){
* @see #deleteCascade()
* @param excludedAssociations associations
*/
public void deleteCascadeExcept(Association ... excludedAssociations){
List<Association> excludedAssociationsList = Arrays.asList(excludedAssociations);
deleteMany2ManyDeep(metaModelLocal.getManyToManyAssociations(excludedAssociationsList));
deleteChildrenDeep(metaModelLocal.getOneToManyAssociations(excludedAssociationsList));
deleteChildrenDeep(metaModelLocal.getPolymorphicAssociations(excludedAssociationsList));
public void deleteCascadeExcept(Association... excludedAssociations){
deleteMany2ManyDeep(metaModelLocal.getManyToManyAssociations(excludedAssociations), excludedAssociations);
deleteChildrenDeep(metaModelLocal.getOneToManyAssociations(excludedAssociations), excludedAssociations);
deleteChildrenDeep(metaModelLocal.getPolymorphicAssociations(excludedAssociations), excludedAssociations);
delete();
}



private void deleteMany2ManyDeep(List<Many2ManyAssociation> many2ManyAssociations){
private void deleteMany2ManyDeep(List<Many2ManyAssociation> many2ManyAssociations, Association... excludedAssociations){
List<Model> allMany2ManyChildren = new ArrayList<>();
for (Association association : many2ManyAssociations) {
Class<? extends Model> targetModelClass = association.getTargetClass();
Expand All @@ -620,7 +619,7 @@ private void deleteMany2ManyDeep(List<Many2ManyAssociation> many2ManyAssociation

deleteJoinsForManyToMany();
for (Model model : allMany2ManyChildren) {
model.deleteCascade();
model.deleteCascadeExcept(excludedAssociations);
}
}

Expand All @@ -645,7 +644,7 @@ public void deleteCascadeShallow(){


private void deleteJoinsForManyToMany() {
List<? extends Many2ManyAssociation> associations = metaModelLocal.getManyToManyAssociations(Collections.<Association>emptyList());
List<? extends Many2ManyAssociation> associations = metaModelLocal.getManyToManyAssociations();
for (Many2ManyAssociation association : associations) {
deleteManyToManyLinks(association);
}
Expand All @@ -664,7 +663,7 @@ private void deleteManyToManyLinks(Many2ManyAssociation association){


private void deleteOne2ManyChildrenShallow() {
List<OneToManyAssociation> childAssociations = metaModelLocal.getOneToManyAssociations(Collections.<Association>emptyList());
List<OneToManyAssociation> childAssociations = metaModelLocal.getOneToManyAssociations();
for (OneToManyAssociation association : childAssociations) {
deleteOne2ManyChildrenShallow(association);
}
Expand All @@ -679,7 +678,7 @@ private void deleteOne2ManyChildrenShallow(OneToManyAssociation association){
}

private void deletePolymorphicChildrenShallow() {
List<OneToManyPolymorphicAssociation> polymorphics = metaModelLocal.getPolymorphicAssociations(new ArrayList<Association>());
List<OneToManyPolymorphicAssociation> polymorphics = metaModelLocal.getPolymorphicAssociations();
for (OneToManyPolymorphicAssociation association : polymorphics) {
deletePolymorphicChildrenShallow(association);
}
Expand All @@ -694,7 +693,7 @@ private void deletePolymorphicChildrenShallow(OneToManyPolymorphicAssociation as
new DB(metaModelLocal.getDbName()).exec("DELETE FROM " + targetTable + " WHERE parent_id = ? AND parent_type = ?", getId(), parentType);
}

private void deleteChildrenDeep(List<? extends Association> childAssociations){
private void deleteChildrenDeep(List<? extends Association> childAssociations, Association... excludedAssociations){
for (Association association : childAssociations) {
String targetTableName = metaModelOf(association.getTargetClass()).getTableName();
Class c = Registry.instance().getModelClass(targetTableName, false);
Expand All @@ -706,7 +705,7 @@ private void deleteChildrenDeep(List<? extends Association> childAssociations){
else{
List<Model> dependencies = getAll(c);
for (Model model : dependencies) {
model.deleteCascade();
model.deleteCascadeExcept(excludedAssociations);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.javalite.activejdbc.associations.Many2ManyAssociation;
import org.javalite.activejdbc.associations.OneToManyAssociation;
import org.javalite.activejdbc.test.ActiveJDBCTest;
import org.javalite.activejdbc.test_models.Doctor;
import org.javalite.activejdbc.test_models.DoctorsPatients;
import org.javalite.activejdbc.test_models.Patient;
import org.javalite.activejdbc.test_models.Person;
import org.javalite.activejdbc.test_models.*;
import org.junit.Test;

/**
Expand Down Expand Up @@ -51,7 +48,11 @@ public void shouldReturnCorrectAssociations(){
a(associations.get(0) instanceof OneToManyAssociation).shouldBeTrue();

a(associations.get(1).getSourceClass()).shouldBeEqual(Doctor.class);
a(associations.get(1).getTargetClass()).shouldBeEqual(Patient.class);
a(associations.get(1) instanceof Many2ManyAssociation).shouldBeTrue();
a(associations.get(1).getTargetClass()).shouldBeEqual(Prescription.class);
a(associations.get(1) instanceof OneToManyAssociation).shouldBeTrue();

a(associations.get(2).getSourceClass()).shouldBeEqual(Doctor.class);
a(associations.get(2).getTargetClass()).shouldBeEqual(Patient.class);
a(associations.get(2) instanceof Many2ManyAssociation).shouldBeTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Ignore;
import org.junit.Test;

import javax.print.Doc;
import java.util.List;


Expand Down Expand Up @@ -124,8 +125,6 @@ public void shouldDeleteMany2ManyDeepSkippingAssociation() {
Registry.cacheManager().flush(new CacheEvent("patients", ""));
Registry.cacheManager().flush(new CacheEvent("prescriptions", ""));



Doctor.findAll().dump();
Patient.findAll().dump();
Prescription.findAll().dump();
Expand All @@ -134,7 +133,10 @@ public void shouldDeleteMany2ManyDeepSkippingAssociation() {
a(Patient.count()).shouldBeEqual(3);
a(Prescription.count()).shouldBeEqual(5);

Patient.findById(3).deleteCascadeExcept(Patient.getMetaModel().getAssociationForTarget(Prescription.class));
Patient.findById(3).deleteCascadeExcept(
Doctor.getMetaModel().getAssociationForTarget(Prescription.class),
Patient.getMetaModel().getAssociationForTarget(Prescription.class)
);

a(Doctor.count()).shouldBeEqual(3);
a(Patient.count()).shouldBeEqual(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions (id, name, patient_id) VALUES(1, 'Viagra', 1)",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(2, 'Prozac', 1)",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(3, 'Valium', 2)",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(4, 'Marijuana (medicinal) ', 2)",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(5, 'CML treatment', 3)"
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(1, 'Viagra', 1, 1)",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(2, 'Prozac', 1, 2)",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(3, 'Valium', 2, 1)",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(4, 'Marijuana (medicinal) ', 2, 1)",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(5, 'CML treatment', 3, 3)"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3);"
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1, 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1, 2);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2, 1);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions (id, name, patient_id) VALUES(1, 'Viagra', 1);",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(2, 'Prozac', 1);",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(3, 'Valium', 2);",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(4, 'Marijuana (medicinal) ', 2);",
"INSERT INTO prescriptions (id, name, patient_id) VALUES(5, 'CML treatment', 3);"
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(1, 'Viagra', 1, 1);",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(2, 'Prozac', 1, 2);",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(3, 'Valium', 2, 1, 1);",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(4, 'Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions (id, name, patient_id, doctor_id) VALUES(5, 'CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3);"
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1, 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1, 2);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2, 1);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1)",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1)",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2)",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2)",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3)"
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1, 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1, 2);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2, 1);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions (name, patient_id) VALUES('Viagra', 1);",
"INSERT INTO prescriptions (name, patient_id) VALUES('Prozac', 1);",
"INSERT INTO prescriptions (name, patient_id) VALUES('Valium', 2);",
"INSERT INTO prescriptions (name, patient_id) VALUES('Marijuana (medicinal) ', 2);",
"INSERT INTO prescriptions (name, patient_id) VALUES('CML treatment', 3);"
"INSERT INTO prescriptions (name, patient_id, doctor_id) VALUES('Viagra', 1, 1);",
"INSERT INTO prescriptions (name, patient_id, doctor_id) VALUES('Prozac', 1, 2);",
"INSERT INTO prescriptions (name, patient_id, doctor_id) VALUES('Valium', 2, 1);",
"INSERT INTO prescriptions (name, patient_id, doctor_id) VALUES('Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions (name, patient_id, doctor_id) VALUES('CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ public List<String> getPopulateStatements(String table) {
);
} else if (table.equals("prescriptions")) {
statements = Arrays.asList(
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3);"
"INSERT INTO prescriptions VALUES(1, 'Viagra', 1, 1);",
"INSERT INTO prescriptions VALUES(2, 'Prozac', 1, 2);",
"INSERT INTO prescriptions VALUES(3, 'Valium', 2, 1);",
"INSERT INTO prescriptions VALUES(4, 'Marijuana (medicinal) ', 2, 1);",
"INSERT INTO prescriptions VALUES(5, 'CML treatment', 3, 3);"
);
} else if (table.equals("doctors")) {
statements = Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.javalite.activejdbc.test_models;

import org.javalite.activejdbc.Model;
import org.javalite.activejdbc.annotations.Cached;

/**
* @author Igor Polevoy
*/
@Cached
public class Prescription extends Model {}
2 changes: 1 addition & 1 deletion activejdbc/src/test/resources/db2_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CALL dropTable('shard1_patients');
CREATE TABLE shard1_patients (id int primary key GENERATED ALWAYS AS IDENTITY, first_name VARCHAR(56), last_name VARCHAR(56));

CALL dropTable('prescriptions');
CREATE TABLE prescriptions (id int primary key GENERATED ALWAYS AS IDENTITY, name VARCHAR(56), patient_id int);
CREATE TABLE prescriptions (id int primary key GENERATED ALWAYS AS IDENTITY, name VARCHAR(56), patient_id int, doctor_id int);

CALL dropTable('doctors');
CREATE TABLE doctors (id int primary key GENERATED ALWAYS AS IDENTITY, first_name VARCHAR(56), last_name VARCHAR(56), discipline varchar(56));
Expand Down
2 changes: 1 addition & 1 deletion activejdbc/src/test/resources/h2_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DROP TABLE IF EXISTS shard1_patients;
CREATE TABLE shard1_patients (id int(11) NOT NULL auto_increment PRIMARY KEY, first_name VARCHAR(56), last_name VARCHAR(56));

DROP TABLE IF EXISTS prescriptions;
CREATE TABLE prescriptions (id int(11) NOT NULL auto_increment PRIMARY KEY, name VARCHAR(56), patient_id int(11));
CREATE TABLE prescriptions (id int(11) NOT NULL auto_increment PRIMARY KEY, name VARCHAR(56), patient_id int(11), doctor_id int(11));

DROP TABLE IF EXISTS doctors;
CREATE TABLE doctors (id int(11) NOT NULL auto_increment PRIMARY KEY, first_name VARCHAR(56), last_name VARCHAR(56), discipline varchar(56));
Expand Down
2 changes: 1 addition & 1 deletion activejdbc/src/test/resources/mssql_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ IF object_id('dbo.prescriptions') IS NOT NULL
BEGIN
DROP TABLE [dbo].[prescriptions]
END
CREATE TABLE prescriptions (id INT IDENTITY PRIMARY KEY, name VARCHAR(56), patient_id INT);
CREATE TABLE prescriptions (id INT IDENTITY PRIMARY KEY, name VARCHAR(56), patient_id INT, doctor_id INT);

IF object_id('dbo.doctors') IS NOT NULL
BEGIN
Expand Down

0 comments on commit ab18db6

Please sign in to comment.