使用DdlUtils和Ant进行不同数据库之间schema和data的转换
使用DdlUtils和Ant进行不同数据库之间schema和data的转换
在开发过程中常常要用到多种数据库类型,比如项目可能需要支持Oracle, MySQL, MSSQL Server, Derby等等。已经有很多各种各样的小工具可以帮助开发人员进行数据库之间的schema、data转换与同步,DdlUtils 就是其中之一。它可以结合Ant或者程序代码进行数据库schema/data和xml文件的相互转换。
例如,项目开发支持的是MySQL,而为了单元测试的整洁和方便,开发人员决定使用Derby来支持单元测试。DdlUtils就可以把schema和数据从MySQL中导出,变成xml文件,然后根据需要导入到Derby中;反之亦可。显然,这些导出的xml文件也是对于数据库的一个很好的备份。
下面是apache ddlutils官网给出的一个xml的例子,table和column标签里面支持的child和attribute远不止示例中的这些。
- <?xml version="1.0"?>
- <!DOCTYPE database SYSTEM "http://db.apache.org/torque/dtd/database.dtd">
- <database name="testdb">
- <table name="author">
- <column name="author_id"
- type="INTEGER"
- primaryKey="true"
- required="true"/>
- <column name="name"
- type="VARCHAR"
- size="50"
- required="true"/>
- <column name="organisation"
- type="VARCHAR"
- size="50"
- required="false"/>
- </table>
- <table name="book">
- <column name="book_id"
- type="INTEGER"
- required="true"
- primaryKey="true"
- autoIncrement="true"/>
- <column name="isbn"
- type="VARCHAR"
- size="15"
- required="true"/>
- <column name="author_id"
- type="INTEGER"
- required="true"/>
- <column name="title"
- type="VARCHAR"
- size="255"
- defaultValue="N/A"
- required="true"/>
- <foreign-key foreignTable="author">
- <reference local="author_id" foreign="author_id"/>
- </foreign-key>
- <index name="book_isbn">
- <index-column name="isbn"/>
- </index>
- </table>
- </database>
DdlUtils包含了3个Ant Task,分别是databaseToDdl, ddlToDatabase以及dumpMetadata。
使用ddlToDatabase的示例:
这个Ant任务创建了一个PostgreSQL数据库,把xml中定义的schema和data导入到数据库中。
- <path id="runtime-classpath">
- <fileset dir="lib">
- <include name="**/*.jar"/>
- <include name="**/*.zip"/>
- </fileset>
- </path>
- <target name="database-setup"
- description="Creates the database structure and inserts data into the database">
- <taskdef name="ddlToDatabase"
- classname="org.apache.ddlutils.task.DdlToDatabaseTask">
- <classpath refid="runtime-classpath"/>
- </taskdef>
- <ddlToDatabase>
- <database url="jdbc:postgresql://localhost/test"
- driverClassName="org.postgresql.Driver"
- username="someuser"
- password="somepassword"/>
- <fileset dir="src/schema">
- <include name="project-schema.xml"/>
- </fileset>
- <createDatabase failonerror="false"/>
- <writeSchemaToDatabase/>
- <writeDataToDatabase datafile="src/data/data.xml"/>
- </ddlToDatabase>
- </target>
使用databaseToDdl的示例:
这个Ant任务把derby数据库中的schema和data导出到xml文件中。
- <path id="runtime-classpath">
- <fileset dir="lib">
- <include name="**/*.jar"/>
- <include name="**/*.zip"/>
- </fileset>
- </path>
- <target name="database-dump" description="Dumps the database structure">
- <taskdef name="databaseToDdl"
- classname="org.apache.ddlutils.task.DatabaseToDdlTask">
- <classpath refid="runtime-classpath"/>
- </taskdef>
- <databaseToDdl modelName="MyModel">
- <database url="jdbc:derby:ddlutils"
- driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
- username=""
- password=""/>
- <writeSchemaToFile outputFile="db-schema.xml"/>
- <writeDataToFile outputFile="data.xml"/>
- </databaseToDdl>
- </target>
评论暂时关闭