`
juvenshun
  • 浏览: 1588465 次
  • 性别: 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 的依赖:

 

<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>
 

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

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

当然我们可以根据具体情况裁剪这些步骤,比如,如果我们需要在一段时间内使用这个脚本创建的干净数据库环境,我们可以不执行最后一步:删除数据库。需要注意的是,执行很多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了。

24
1
分享到:
评论
13 楼 zlele 2016-07-15  
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:sql-maven-plugin:1.5:execute (execution: create-db, phase: process-test-resources)
问题已解决。
http://blog.163.com/xh_ding/blog/static/1939032892015222368827/
12 楼 zlele 2016-07-15  
endual 写道
tzq668766 写道
juven,我在用sql-maven-plunin时报错:
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:sql-maven-plugin:1.5:execute (execution: create-db, phase: process-test-resources)

你遇到过吗?


我也遇到了,貌似是插件不支持。

这个问题要怎么解决呢
11 楼 phlsbg 2015-05-26  
我只创建一个<id>create-schema</id>  用命令行 mvn sql:execute 运行 发现没有执行。。
10 楼 endual 2014-02-28  
tzq668766 写道
juven,我在用sql-maven-plunin时报错:
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:sql-maven-plugin:1.5:execute (execution: create-db, phase: process-test-resources)

你遇到过吗?


我也遇到了,貌似是插件不支持。
9 楼 tzq668766 2012-09-12  
juven,我在用sql-maven-plunin时报错:
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:sql-maven-plugin:1.5:execute (execution: create-db, phase: process-test-resources)

你遇到过吗?
8 楼 doujiang327 2012-03-12  
先说,《maven实战》写的不错,我买了两本。
提个问题,我在创建数据库的时候要是想指定编码格式该怎么写?
7 楼 hobitton 2011-11-25  
http://mojo.codehaus.org/sql-maven-plugin/examples/execute.html。。。
6 楼 fanwenqiang666 2010-01-07  
爷们很专业,顶一个
5 楼 juvenshun 2008-07-25  
maven-sql-plugin不是用来做单元测试的,而是CDBI。dbunit或者spring基于事务的单元测试框架都能很好的服务于依赖于数据库的单元测试。而CDBI主要的作用还是自动化维护项目的sql脚本。
4 楼 lovefly_zero 2008-07-25  
很好 谢谢~
3 楼 nasvel 2008-07-22  
很有启发,我一直用dbunit来做单元测试,觉得相对sql-plugin来:
1 不需要另外维护SQL脚本
2 可以对各单元测试插入不同的测试数据,避免unit test A更改数据造成unit test B的失败
3 toplink 和 hibernate 都支持动态创建删除表

另外我看到代码中的各executions已经连接到maven的lifecycle中了,所以我觉得执行以上命令应该使用:mvn package 而不是 mvn sql:execute
2 楼 yeah1900 2008-06-27  
有效的把数据库操作和测试集成起来,不错
1 楼 jgyhuzhou 2008-06-25  
it's very useful for integrate test, thanks!

相关推荐

Global site tag (gtag.js) - Google Analytics