By default many-to-many are flattened during reverse engineering. But if a user un-flattens a given N:M manually, weâd like this choice to be preserved on the next run
Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/dd6920c9 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/dd6920c9 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/dd6920c9 Branch: refs/heads/master Commit: dd6920c9f1e6fbf7d5d65228c413aa051a079795 Parents: 3eaed4a Author: alexkolonitsky <alex.kolonit...@gmail.com> Authored: Wed Dec 10 15:45:09 2014 +0300 Committer: alexkolonitsky <alex.kolonit...@gmail.com> Committed: Wed Dec 10 15:45:09 2014 +0300 ---------------------------------------------------------------------- .../cayenne/tools/DbImporterMojoTest.java | 38 +++++++++++--- .../dbimport/testUnFlattensManyToMany-pom.xml | 39 ++++++++++++++ .../dbimport/testUnFlattensManyToMany.map.xml | 54 ++++++++++++++++++++ .../testUnFlattensManyToMany.map.xml-result | 54 ++++++++++++++++++++ .../tools/dbimport/testUnFlattensManyToMany.sql | 36 +++++++++++++ 5 files changed, 213 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/dd6920c9/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java index 0f5b6f6..5aa2093 100644 --- a/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java +++ b/plugins/maven-cayenne-plugin/src/test/java/org/apache/cayenne/tools/DbImporterMojoTest.java @@ -117,7 +117,8 @@ public class DbImporterMojoTest extends AbstractMojoTestCase { } /** - * what happens if a relationship existed over a column that was later deleted? and âskipRelLoadingâ is true + * Q: what happens if a relationship existed over a column that was later deleted? and âskipRelLoadingâ is true + * A: it should remove relationship and column * * @throws Exception */ @@ -125,6 +126,16 @@ public class DbImporterMojoTest extends AbstractMojoTestCase { test("testPreserveRelationships"); } + /** + * By default many-to-many are flattened during reverse engineering. + * But if a user un-flattens a given N:M manually, weâd like this choice to be preserved on the next run + * + * @throws Exception + */ + public void testUnFlattensManyToMany() throws Exception { + test("testUnFlattensManyToMany"); + } + private void test(String name) throws Exception { DbImporterMojo cdbImport = getCdbImport("dbimport/" + name + "-pom.xml"); File mapFile = cdbImport.getMap(); @@ -156,28 +167,39 @@ public class DbImporterMojoTest extends AbstractMojoTestCase { ResultSet views = connection.getMetaData().getTables(null, null, null, new String[] { "VIEW" }); while (views.next()) { String schema = views.getString("TABLE_SCHEM"); - System.out.println("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); - stmt.execute("DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); + execute(stmt, "DROP VIEW " + (isBlank(schema) ? "" : schema + ".") + views.getString("TABLE_NAME")); } ResultSet tables = connection.getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (tables.next()) { String schema = tables.getString("TABLE_SCHEM"); - System.out.println("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); - stmt.execute("DROP TABLE " + (isBlank(schema) ? "" : schema + ".") + tables.getString("TABLE_NAME")); + String tableName = tables.getString("TABLE_NAME"); + String tableNameFull = (isBlank(schema) ? "" : schema + ".") + tableName; + + ResultSet keys = connection.getMetaData().getExportedKeys(null, schema, tableName); + while (keys.next()) { + execute(stmt, "ALTER TABLE " + keys.getString("FKTABLE_NAME") + " DROP CONSTRAINT " + keys.getString("FK_NAME")); + } + + String sql = "DROP TABLE " + tableNameFull; + execute(stmt, sql); } ResultSet schemas = connection.getMetaData().getSchemas(); while (schemas.next()) { String schem = schemas.getString("TABLE_SCHEM"); if (schem.startsWith("SCHEMA")) { - System.out.println("DROP SCHEMA " + schem); - stmt.execute("DROP SCHEMA " + schem + " RESTRICT"); + execute(stmt, "DROP SCHEMA " + schem + " RESTRICT"); } } } - private void verifyResult(File map, File mapFileCopy) { + private void execute(Statement stmt, String sql) throws SQLException { + System.out.println(sql); + stmt.execute(sql); + } + + private void verifyResult(File map, File mapFileCopy) { try { FileReader control = new FileReader(map.getAbsolutePath() + "-result"); FileReader test = new FileReader(mapFileCopy); http://git-wip-us.apache.org/repos/asf/cayenne/blob/dd6920c9/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany-pom.xml ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany-pom.xml b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany-pom.xml new file mode 100644 index 0000000..c62b2b3 --- /dev/null +++ b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany-pom.xml @@ -0,0 +1,39 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <name>DbImporterMojo Test1</name> + + <build> + <plugins> + <plugin> + <artifactId>maven-cayenne-plugin</artifactId> + <configuration> + <map>target/test-classes/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml</map> + <driver>org.apache.derby.jdbc.EmbeddedDriver</driver> + <url>jdbc:derby:memory:DbImporterMojoTest;create=true</url> + </configuration> + </plugin> + </plugins> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/cayenne/blob/dd6920c9/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml new file mode 100644 index 0000000..3aaa235 --- /dev/null +++ b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + --> +<data-map xmlns="http://cayenne.apache.org/schema/7/modelMap" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://cayenne.apache.org/schema/7/modelMap http://cayenne.apache.org/schema/7/modelMap.xsd" + project-version="7"> + <db-entity name="A" schema="APP"> + <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="10"/> + </db-entity> + <db-entity name="A_B" schema="APP"> + <db-attribute name="A_ID" type="INTEGER" isMandatory="true" length="10"/> + <db-attribute name="B_ID" type="INTEGER" isMandatory="true" length="10"/> + </db-entity> + <db-entity name="B" schema="APP"> + <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="10"/> + </db-entity> + <obj-entity name="A" className="A" dbEntityName="A"> + </obj-entity> + <obj-entity name="AB" className="AB" dbEntityName="A_B"> + </obj-entity> + <obj-entity name="B" className="B" dbEntityName="B"> + </obj-entity> + <db-relationship name="aBArray" source="A" target="A_B" toMany="true"> + <db-attribute-pair source="ID" target="A_ID"/> + </db-relationship> + <db-relationship name="toA" source="A_B" target="A" toMany="false"> + <db-attribute-pair source="A_ID" target="ID"/> + </db-relationship> + <db-relationship name="toB" source="A_B" target="B" toMany="false"> + <db-attribute-pair source="B_ID" target="ID"/> + </db-relationship> + <db-relationship name="aBArray" source="B" target="A_B" toMany="true"> + <db-attribute-pair source="ID" target="B_ID"/> + </db-relationship> + <obj-relationship name="toA" source="AB" target="A" deleteRule="Nullify" db-relationship-path="toA"/> + <obj-relationship name="aBArray" source="B" target="AB" deleteRule="Deny" db-relationship-path="aBArray"/> +</data-map> http://git-wip-us.apache.org/repos/asf/cayenne/blob/dd6920c9/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml-result ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml-result b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml-result new file mode 100644 index 0000000..5b85aa0 --- /dev/null +++ b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.map.xml-result @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<data-map xmlns="http://cayenne.apache.org/schema/7/modelMap" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://cayenne.apache.org/schema/7/modelMap http://cayenne.apache.org/schema/7/modelMap.xsd" + project-version="7"> + <db-entity name="A" schema="APP"> + <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="10"/> + </db-entity> + <db-entity name="A_B" schema="APP"> + <db-attribute name="A_ID" type="INTEGER" isMandatory="true" length="10"/> + <db-attribute name="B_ID" type="INTEGER" isMandatory="true" length="10"/> + </db-entity> + <db-entity name="B" schema="APP"> + <db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="10"/> + </db-entity> + <obj-entity name="A" className="A" dbEntityName="A"> + </obj-entity> + <obj-entity name="AB" className="AB" dbEntityName="A_B"> + </obj-entity> + <obj-entity name="B" className="B" dbEntityName="B"> + </obj-entity> + <db-relationship name="aBArray" source="A" target="A_B" toMany="true"> + <db-attribute-pair source="ID" target="A_ID"/> + </db-relationship> + <db-relationship name="toA" source="A_B" target="A" toMany="false"> + <db-attribute-pair source="A_ID" target="ID"/> + </db-relationship> + <db-relationship name="toB" source="A_B" target="B" toMany="false"> + <db-attribute-pair source="B_ID" target="ID"/> + </db-relationship> + <db-relationship name="aBArray" source="B" target="A_B" toMany="true"> + <db-attribute-pair source="ID" target="B_ID"/> + </db-relationship> + <obj-relationship name="toA" source="AB" target="A" deleteRule="Nullify" db-relationship-path="toA"/> + <obj-relationship name="aBArray" source="B" target="AB" deleteRule="Deny" db-relationship-path="aBArray"/> +</data-map> http://git-wip-us.apache.org/repos/asf/cayenne/blob/dd6920c9/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.sql ---------------------------------------------------------------------- diff --git a/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.sql b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.sql new file mode 100644 index 0000000..0241d1d --- /dev/null +++ b/plugins/maven-cayenne-plugin/src/test/resources/org/apache/cayenne/tools/dbimport/testUnFlattensManyToMany.sql @@ -0,0 +1,36 @@ +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. + +CREATE TABLE APP.A ( + id INTEGER NOT NULL, + + PRIMARY KEY (id) +); + +CREATE TABLE APP.B ( + id INTEGER NOT NULL, + + PRIMARY KEY (id) +); + +CREATE TABLE APP.A_B ( + A_ID INTEGER NOT NULL, + B_ID INTEGER NOT NULL, + + CONSTRAINT A_B_A FOREIGN KEY (A_ID) REFERENCES APP.A (ID), + CONSTRAINT A_B_B FOREIGN KEY (B_ID) REFERENCES APP.B (ID) +); \ No newline at end of file