使用maven-sql-plugin实现持续数据库集成(CDBI)

http://juvenshun.iteye.com/blog/207326
数据库持续集成(Continuous Database Integration, CDBI)是持续集成(Continuous Ingeration, CI)不可或缺的重要组成部分。在典型的情况下,版本控制系统管理数据库脚本,包括数据库定义语言(DDL)和数据库操纵语言(DML)。开发成员在开发过程中添加或者修改数据库脚本,在本地运行过之后,提交至版本控制系统,并由此激发一次持续构建。CI服务器执行数据库脚本,并返回成功或者错误报告。

首先简单介绍一下 SQL Maven Plugin ,它通过Maven来执行配置好的数据库脚本,可以通过在POM中配置sql命令,或者将脚本写在文件中,在POM中配置文件位置。最后,运行 mvn sql:execute 以执行所有脚本。

以下通过一个比较完整的例子来解释下该插件的用法,这里例子能在这里找到:http://mojo.codehaus.org/sql-maven-plugin/examples/execute.html

首先,配置对 sql-maven-plugin 的依赖:
<build>  
  [...]  
      <plugin>  
        <groupId>org.codehaus.mojo</groupId>  
        <artifactId>sql-maven-plugin</artifactId>  

紧接着,由于该插件需要使用JDBC来执行SQL脚本,因此还需要配置对JDBC驱动的依赖,如:
<dependencies>  
  <!-- specify the dependent jdbc driver here -->  
  <dependency>  
    <groupId>postgresql</groupId>  
    <artifactId>postgresql</artifactId>  
    <version>8.1-407.jdbc3</version>  
  </dependency>  
</dependencies>  

在此基础上便是数据库的基本配置了,通常包括URL,Username和Password。这是一个通用的配置,默认情况下,在此之后的目标(goal)都会使用这个配置,除非将其覆写。
<!-- common configuration shared by all executions -->  
<configuration>  
   <username>postgres</username>  
   <password>password</password>  
   <url>jdbc:postgressql://localhost:5432:yourdb</url>  
</configuration>  


典型的脚本包括以下步骤:
删除旧数据库
创建新数据库
创建表,索引等
插入初始数据
删除数据库
当然我们可以根据具体情况裁剪这些步骤,比如,如果我们需要在一段时间内使用这个脚本创建的干净数据库环境,我们可以不执行最后一步:删除数据库。需要注意的是,执行很多DDL的时候我们一般需要最高的数据库权限。

首先来看一下删除旧数据库,注意这里的URL不是我们上面配置的基本配置,因为我们需要删除'yourdb',因此我们只能使用系统的初始数据库。如果'yourdb'数据库不存在,执行出错,则跳过继续下一步:
<executions>  
  
  <execution>  
    <id>drop-db-before-test-if-any</id>  
    <phase>process-test-resources</phase>  
    <goals>  
      <goal>execute</goal>  
    </goals>  
    <configuration>  
      <!-- need another database to drop the targeted one -->  
      <url>jdbc:postgressql://localhost:5432:bootstrapdb</url>  
      <autocommit>true</autocommit>  
      <sqlCommand>drop database yourdb</sqlCommand>  
      <!-- ignore error when database is not avaiable -->  
      <onError>continue</onError>  
    </configuration>  
  </execution>
 
现在需要创建我们的数据库yourdb,和上面一样,URL指向系统初始数据库,这里的sql comand为建库命令:
<execution>  
  <id>create-db</id>  
  <phase>process-test-resources</phase>  
  <goals>  
    <goal>execute</goal>  
  </goals>  
  <configuration>  
    <url>jdbc:postgressql://localhost:5432:yourdb</url>  
    <!-- no transaction -->  
    <autocommit>true</autocommit>  
    <sqlCommand>create database yourdb</sqlCommand>  
  </configuration>  
</execution>  

然后执行的建表命令,这里配置了srcFiles,我们可以将schema的脚本写在sql文件里,然后配置在这里,顺序执行:
<execution>  
  <id>create-schema</id>  
  <phase>process-test-resources</phase>  
  <goals>  
    <goal>execute</goal>  
  </goals>  
  <configuration>  
    <autocommit>true</autocommit>  
    <srcFiles>  
      <srcFile>src/main/resourced/your-schema.sql</srcFile>  
    </srcFiles>  
  </configuration>  
</execution>  

DB和Schema没问题之后,我们通常需要插入一些系统的初始数据,或者测试的初始数据:
<execution>  
  <id>create-data</id>  
  <phase>process-test-resources</phase>  
  <goals>  
    <goal>execute</goal>  
  </goals>  
  <configuration>  
    <orderFile>ascending</orderFile>  
    <fileset>  
      <basedir>${basedir}</basedir>  
      <includes>  
        <include>test/sql/test-data2.sql</include>  
        <include>test/sql/test-data1.sql</include>  
      </includes>  
    </fileset>  
  </configuration>  
</execution>  

最后,测试完了之后,删除数据库:
        <!-- drop db after test -->  
        <execution>  
          <id>drop-db-after-test</id>  
          <phase>test/phase>  
          <goals>  
            <goal>execute</goal>  
          </goals>  
          <configuration>  
            <autocommit>true</autocommit>  
            <sqlCommand>drop database yourdb</sqlCommand>  
          </configuration>  
        </execution>  
      </executions>  
    </plugin>  

最后让我们回头再来看看这个五个步骤,其实它们都用了 sql-maven-plugin 的 execute 目标,除了drop-db-after-test,它们都配置了 process-test-resources 生命周期。这样配置的目的是让删旧库,建库,建表,插数据这些步骤在测试之前完成,这样,测试的时候就有一个完好的数据库了,测试完了之后,再把建好的数据库删除。

通过建一个Maven项目,使用 maven-sql-plugin 管理数据库脚本的执行,然后使用CI服务器来调用这个Maven项目,我们就可以实现基于Maven的CDBI了。

猜你喜欢

转载自panyongzheng.iteye.com/blog/2039769