Author: tomdz
Date: Sat Mar  8 11:08:09 2008
New Revision: 635049

URL: http://svn.apache.org/viewvc?rev=635049&view=rev
Log:
Implementation of DDLUTILS-180: DropAllTables command

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/task/
    
db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestTaskBase.java
Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java
    db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java
URL: 
http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java?rev=635049&r1=635048&r2=635049&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/model/Database.java Sat Mar  
8 11:08:09 2008
@@ -57,6 +57,22 @@
     private transient DynaClassCache _dynaClassCache = null;
 
     /**
+     * Creates an empty model without a name.
+     */
+    public Database()
+    {}
+
+    /**
+     * Creates an empty model with the given name.
+     * 
+     * @param name The name
+     */
+    public Database(String name)
+    {
+        _name = name;
+    }
+
+    /**
      * Adds all tables from the other database to this database.
      * Note that the other database is not changed.
      * 

Modified: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java
URL: 
http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java?rev=635049&r1=635048&r2=635049&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java 
(original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DatabaseToDdlTask.java 
Sat Mar  8 11:08:09 2008
@@ -140,6 +140,16 @@
     }
 
     /**
+     * Adds the "drop tables"-command.
+     * 
+     * @param command The command
+     */
+    public void addDropTables(DropTablesCommand command)
+    {
+        addCommand(command);
+    }
+
+    /**
      * Returns the table types to recognize.
      * 
      * @return The table types

Added: 
db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java
URL: 
http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java?rev=635049&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java 
(added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/task/DropTablesCommand.java 
Sat Mar  8 11:08:09 2008
@@ -0,0 +1,69 @@
+package org.apache.ddlutils.task;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.ddlutils.Platform;
+import org.apache.ddlutils.model.Database;
+import org.apache.tools.ant.BuildException;
+
+/**
+ * Sub task for dropping tables.
+ * 
+ * @version $Revision: $
+ * @ant.task name="dropTables"
+ */
+public class DropTablesCommand extends DatabaseCommand
+{
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public boolean isRequiringModel()
+    {
+        return true;
+    }
+
+    /**
+     * [EMAIL PROTECTED]
+     */
+    public void execute(DatabaseTaskBase task, Database model) throws 
BuildException
+    {
+        BasicDataSource dataSource = getDataSource();
+
+        if (dataSource == null)
+        {
+            throw new BuildException("No database specified.");
+        }
+
+        Platform platform    = getPlatform();
+        Database targetModel = new Database();
+
+        try
+        {
+            platform.alterModel(model, targetModel, isFailOnError());
+
+            _log.info("Dropped tables");
+        }
+        catch (Exception ex)
+        {
+            handleException(ex, ex.getMessage());
+        }
+    }
+}

Added: 
db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java
URL: 
http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java?rev=635049&view=auto
==============================================================================
--- 
db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java 
(added)
+++ 
db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestDropTablesCommand.java 
Sat Mar  8 11:08:09 2008
@@ -0,0 +1,257 @@
+package org.apache.ddlutils.task;
+
+/*
+ * 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.
+ */
+
+import org.apache.ddlutils.model.Database;
+
+/**
+ * Tests the dropTables sub task.
+ * 
+ * @version $Revision: $
+ */
+public class TestDropTablesCommand extends TestTaskBase
+{
+    /**
+     * Tests the task against an empty database. 
+     */
+    public void testEmptyDatabase()
+    {
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of a single table. 
+     */
+    public void testSingleTable()
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of a single table with an auto increment column. 
+     */
+    public void testSingleTableWithAutoIncrementColumn()
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true' autoIncrement='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of a single table with an index. 
+     */
+    public void testSingleTableWithIndex()
+    {
+        if (!getPlatformInfo().isIndicesSupported())
+        {
+            return;
+        }
+
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true' autoIncrement='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <index name='test'>\n"+
+            "      <index-column name='avalue'/>\n"+
+            "    </index>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of a single table with a unique index. 
+     */
+    public void testSingleTableWithUniqeIndex()
+    {
+        if (!getPlatformInfo().isIndicesSupported())
+        {
+            return;
+        }
+
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true' autoIncrement='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <unique name='test'>\n"+
+            "      <unique-column name='avalue'/>\n"+
+            "    </unique>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of a table with a self-referencing foreign key. 
+     */
+    public void testSingleTablesWithSelfReferencingFK()
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of two tables with a foreign key between them. 
+     */
+    public void testTwoTablesWithFK()
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    /**
+     * Tests the removal of two tables with circular foreign keys between 
them. 
+     */
+    public void testTwoTablesWithCircularFK()
+    {
+        final String modelXml = 
+            "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
+            "<database name='roundtriptest'>\n"+
+            "  <table name='roundtrip1'>\n"+
+            "    <column name='pk' type='VARCHAR' size='32' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='INTEGER'/>\n"+
+            "    <foreign-key foreignTable='roundtrip2'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "  <table name='roundtrip2'>\n"+
+            "    <column name='pk' type='INTEGER' primaryKey='true' 
required='true'/>\n"+
+            "    <column name='avalue' type='VARCHAR' size='32'/>\n"+
+            "    <foreign-key foreignTable='roundtrip1'>\n"+
+            "      <reference local='avalue' foreign='pk'/>\n"+
+            "    </foreign-key>\n"+
+            "  </table>\n"+
+            "</database>";
+
+        createDatabase(modelXml);
+
+        DatabaseToDdlTask task = getDatabaseToDdlTaskInstance();
+
+        task.addDropTables(new DropTablesCommand());
+        task.execute();
+
+        assertEquals(new Database("roundtriptest"),
+                     readModelFromDatabase("roundtriptest"));
+    }
+
+    // circular fks
+}

Added: db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestTaskBase.java
URL: 
http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestTaskBase.java?rev=635049&view=auto
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestTaskBase.java 
(added)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/task/TestTaskBase.java Sat 
Mar  8 11:08:09 2008
@@ -0,0 +1,47 @@
+package org.apache.ddlutils.task;
+
+/*
+ * 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.
+ */
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.ddlutils.TestDatabaseWriterBase;
+import org.apache.tools.ant.Project;
+
+/**
+ * Base class for ant task tests. 
+ * 
+ * @version $Revision: $
+ */
+public abstract class TestTaskBase extends TestDatabaseWriterBase
+{
+    /**
+     * Returns an instance of the [EMAIL PROTECTED] DatabaseToDdlTask}, 
already configured with
+     * a project and the tested database.
+     * 
+     * @return The task object
+     */
+    protected DatabaseToDdlTask getDatabaseToDdlTaskInstance()
+    {
+        DatabaseToDdlTask task = new DatabaseToDdlTask();
+
+        task.setProject(new Project());
+        task.addConfiguredDatabase((BasicDataSource)getDataSource());
+        return task;
+    }
+}


Reply via email to