`
hyw520110
  • 浏览: 210381 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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

阅读更多

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

 

第18界Jolt大奖,技术类图书获得者: Continuous Integration: Improving Software Quality and Reducing Risk 用一章的篇幅介绍的CDBI的相关概念及实践,并给出了一个基于Ant的样例实现,本文将介绍使用Maven来实现CDBI。

 

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

 

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

 

首先,配置对 sql-maven-plugin 的依赖:

 

Xml代码 复制代码 收藏代码
  1. <build>  
  2.   [...]   
  3.       <plugin>  
  4.         <groupId>org.codehaus.mojo</groupId>  
  5.         <artifactId>sql-maven-plugin</artifactId>  
<build>
  [...]
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>

紧接着,由于该插件需要使用JDBC来执行SQL脚本,因此还需要配置对JDBC驱动的依赖,如:

 

Xml代码 复制代码 收藏代码
  1. <dependencies>  
  2.   <!-- specify the dependent jdbc driver here -->  
  3.   <dependency>  
  4.     <groupId>postgresql</groupId>  
  5.     <artifactId>postgresql</artifactId>  
  6.     <version>8.1-407.jdbc3</version>  
  7.   </dependency>  
  8. </dependencies>  
        <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)都会使用这个配置,除非将其覆写。

 

Xml代码 复制代码 收藏代码
  1. <!-- common configuration shared by all executions -->  
  2. <configuration>  
  3.    <username>postgres</username>  
  4.    <password>password</password>  
  5.    <url>jdbc:postgressql://localhost:5432:yourdb</url>  
  6. </configuration>  
        <!-- common configuration shared by all executions -->
        <configuration>
           <username>postgres</username>
           <password>password</password>
           <url>jdbc:postgressql://localhost:5432:yourdb</url>
        </configuration>

 

典型的脚本包括以下步骤:

  1. 删除旧数据库
  2. 创建新数据库
  3. 创建表,索引等
  4. 插入初始数据
  5. 删除数据库

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

首先来看一下删除旧数据库,注意这里的URL不是我们上面配置的基本配置,因为我们需要删除'yourdb',因此我们只能使用系统的初始数据库。如果'yourdb'数据库不存在,执行出错,则跳过继续下一步:

 

Xml代码 复制代码 收藏代码
  1. <executions>  
  2.   
  3.   <execution>  
  4.     <id>drop-db-before-test-if-any</id>  
  5.     <phase>process-test-resources</phase>  
  6.     <goals>  
  7.       <goal>execute</goal>  
  8.     </goals>  
  9.     <configuration>  
  10.       <!-- need another database to drop the targeted one -->  
  11.       <url>jdbc:postgressql://localhost:5432:bootstrapdb</url>  
  12.       <autocommit>true</autocommit>  
  13.       <sqlCommand>drop database yourdb</sqlCommand>  
  14.       <!-- ignore error when database is not avaiable -->  
  15.       <onError>continue</onError>  
  16.     </configuration>  
  17.   </execution>  
        <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为建库命令:

 

Xml代码 复制代码 收藏代码
  1. <execution>  
  2.   <id>create-db</id>  
  3.   <phase>process-test-resources</phase>  
  4.   <goals>  
  5.     <goal>execute</goal>  
  6.   </goals>  
  7.   <configuration>  
  8.     <url>jdbc:postgressql://localhost:5432:yourdb</url>  
  9.     <!-- no transaction -->  
  10.     <autocommit>true</autocommit>  
  11.     <sqlCommand>create database yourdb</sqlCommand>  
  12.   </configuration>  
  13. </execution>  
          <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文件里,然后配置在这里,顺序执行:

 

Xml代码 复制代码 收藏代码
  1. <execution>  
  2.   <id>create-schema</id>  
  3.   <phase>process-test-resources</phase>  
  4.   <goals>  
  5.     <goal>execute</goal>  
  6.   </goals>  
  7.   <configuration>  
  8.     <autocommit>true</autocommit>  
  9.     <srcFiles>  
  10.       <srcFile>src/main/resourced/your-schema.sql</srcFile>  
  11.     </srcFiles>  
  12.   </configuration>  
  13. </execution>  
          <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没问题之后,我们通常需要插入一些系统的初始数据,或者测试的初始数据:

 

Xml代码 复制代码 收藏代码
  1. <execution>  
  2.   <id>create-data</id>  
  3.   <phase>process-test-resources</phase>  
  4.   <goals>  
  5.     <goal>execute</goal>  
  6.   </goals>  
  7.   <configuration>  
  8.     <orderFile>ascending</orderFile>  
  9.     <fileset>  
  10.       <basedir>${basedir}</basedir>  
  11.       <includes>  
  12.         <include>test/sql/test-data2.sql</include>  
  13.         <include>test/sql/test-data1.sql</include>  
  14.       </includes>  
  15.     </fileset>  
  16.   </configuration>  
  17. </execution>  
          <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>

最后,测试完了之后,删除数据库:

 

Xml代码 复制代码 收藏代码
  1.         <!-- drop db after test -->  
  2.         <execution>  
  3.           <id>drop-db-after-test</id>  
  4.           <phase>test/phase>  
  5.           <goals>  
  6.             <goal>execute</goal>  
  7.           </goals>  
  8.           <configuration>  
  9.             <autocommit>true</autocommit>  
  10.             <sqlCommand>drop database yourdb</sqlCommand>  
  11.           </configuration>  
  12.         </execution>  
  13.       </executions>  
  14.     </plugin>  
  15.   
  16. [...]  
          <!-- 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了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics