持续集成实践之数据库篇

上一篇 / 下一篇  2012-07-10 10:05:07 / 个人分类:杂谈

前不久在公司内部刊物上写的,借论坛和大家分享!51Testing软件测试网/] Hf*p[
水平有限,欢迎拍砖指正!51Testing软件测试网W7wAQ `0ZL7t

bS0NSxJU0                                   持续集成实践之数据库51Testing软件测试网4UK*XO/X~
                                                    51Testing软件测试网YO H y+Ef!Y
互联网世界中虚虚实实让人难以分辨!技术圈子里虽说好一些,但也有不乏只为炒作不务实的”流行风”!但”持续集成”风的盛行绝不仅是因其提出者Martin Fowler大名和努力推广,更重要的是其思想的启发性和精准性!它使我们看到了软件开发的瓶颈何在,更言简意赅地告诉了解决之道,可谓字字珠玑!持续集成涵盖多方面,其中数据库的持续集成是不得不说的。所有的软件业务都是为了处理数据,软件开发过程中难免会对数据结构及其数据做修改。将数据库集成引入持续集成,业界给了一个专用名词:Continuous Database Integration通常被缩写为CDBI。此类集成通常是由开发人员负责的,期间若是遇到数据库方面难题则可以向专业DBA咨询。为了更清楚的阐述,,我们会以一个例子给大家说明。51Testing软件测试网#?d-X;RS'`
示例环境如下:51Testing软件测试网2|:U'b0ljB
实例工程:        Appfuse example,使用appfuse命令创建标准的MVC工程(具体操作略)。51Testing软件测试网2l5uL#ep^ D_Ap+^O
数据库:          Mysql 5.5.1051Testing软件测试网a*jb x\.E$su1m0d
JDK:                1.6.0_23"       51Testing软件测试网5? bK\p X!?pBc
Maven:                3.0.4
ez:QENf0注:所有操作步骤都以maven、jdk、mysql,CI服务器等环境正常安装完成为前提。下面会分别介绍通过两种方案实现CDBI。
4FR$uCEb-AurD v0一、        使用dbunit完成CDBI51Testing软件测试网5Uu0QY#ta
我们使用的是Appfus原始生成的代码。由于Appfuse自动生成的代码中包含了使用dbunit完成数据库持续集成方案,首先让我们一起来学习这种解决方案:51Testing软件测试网i%wW1N9b6a6~4j
(一)        为了自动生成数据库结构,首先在Hibernate配置文件hibernate.cfg.xml定义所要创建数据库表:51Testing软件测试网5[T ByGp3X
    <hibernate-configuration>51Testing软件测试网6|5SZKRF
      <session-factory>
oIF{)LT0        <mapping class="com.mycompany.model.User"/>
\2B5a5F!S"E2l_8e#~2Ff0        <mapping class="com.mycompany.model.Role"/>51Testing软件测试网ay G&sx-E%k
     </session-factory>51Testing软件测试网#Qz}C:l[j
</hibernate-configuration>51Testing软件测试网7W G,Dsb;n2a8xa$p

7|9Ym"F$w&~e0(二)        如何执行和什么时候执行都是在项目的pom中定义。我们使用hibernate maven插件到达:
3C:~MBp Dy5ag8Z0
]lL*R}KB1sR0        <plugin>
P7u+`?NV9Q*kF;E^y0                <groupId>org.codehaus.mojo</groupId>
3h1ic.^4t~_u0                <artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网&nNdo+uB)Y[
                <version>2.2</version>51Testing软件测试网 iMU3U~ L(A&VI
                <configuration>
;rW6V xnTY0                  <components>
"XxcsNE$Ry0                        <component>
,A&f1tfF0         <name>hbm2ddl</name>o
qiy5Q7Q `O0<implementation>annotationconfiguration</implementation>51Testing软件测试网n\,ctZ'c }M6C
                   <!--自动生成schema-->51Testing软件测试网7~E;T6Sg N3K
                </component>51Testing软件测试网_#vI GH-m rS
                </components>51Testing软件测试网 gp}nf/OP
                <componentProperties>
!C y0Q6J Ns0                            <drop>true</drop>
jQ5|T:C0                   <jdk5>true</jdk5>51Testing软件测试网 pr!dy X0?:Pw
<propertyfile>target/classes/jdbc.properties</propertyfile>
*j Q qBp!U o3m8uN0<!—数据库连接-->51Testing软件测试网\a,Sc dD~qw
                <skip>false</skip>
"wA}X-Kj b0                </componentProperties>
8w2X4lJ:R0        </configuration>51Testing软件测试网7|K2}U@J)]
        <executions>51Testing软件测试网S#e(~;PSN#j$Q y(H
                <execution>51Testing软件测试网.fW+S ?E4J$_*JK F;P
                <phase>process-test-resources</phase>51Testing软件测试网/\2c6^HyQO&X
                <goals>51Testing软件测试网 l,oJ-ht,q
                        <goal>hbm2ddl</goal>
%Y b$m"n,uG!kb J]*`*O0                </goals>
%G7J }p@0io0           </execution>
v``/}x/B0K0        </executions>51Testing软件测试网(I%U'ak,C'hX
        <dependencies>51Testing软件测试网;Jk(QRnDV*FV,z
     <!—jdbc连接所需要的jar依赖-->51Testing软件测试网,}Ts+io&|n
                        <dependency>51Testing软件测试网Xf8G3Lhq.~%mq
                        <groupId>mysql</groupId>
;v0CQ-ErS9k0                        <artifactId>mysql-connector-java</artifactId>
!d'NW(jg{*V0                <version>5.1.14</version>
GzU[na9tEr0                </dependency>51Testing软件测试网Pu$VwF8g/F%e
        </dependencies>51Testing软件测试网Nj.aQ"D
        </plugin>51Testing软件测试网$bz @'TL"M wb
对以上配置的解释:
oX X0uEhFrz0当maven运行到process-test-resources阶段是程序会自动生成数据库表结构。51Testing软件测试网w qTmcMP6_
       hibernate3-maven-plugin另外功能的介绍:51Testing软件测试网Sdh6yopU s Pe
hibernate3-maven-plugin的功能非常强大,还可以自动把项目中的module转化成初始化表用的sql语句。只需如下配置(当然本次演示中不需要这个功能!):
w4]{1|b c$R051Testing软件测试网 V)MT%t w _%R
     <groupId>org.codehaus.mojo</groupId>51Testing软件测试网-n.vH.@OY#Ln
        <artifactId>hibernate3-maven-plugin</artifactId>51Testing软件测试网.^h/bGQ n0~ f
        <version>2.2</version>
P ]pH,DI `N2D*WS0        <configuration>
H o#b"~)YO'htU0          <componentProperties>
"V!W(L?p?0            <outputfilename>schema.sql</outputfilename>
$K,g6fHM F b0          </componentProperties>51Testing软件测试网?CJ"Qy dK ~`/y
        </configuration>
6m~ K0mB G(a0      </plugin>
.] ?-\;[2uO q0c9B0(三)        有了表结构,我们还需要使用maven的Dbunit插件完成数据初始化,其连接配置:
%z4hU4W1Z&Z8p |]0Dbunit在这里主要是用来初始化数据51Testing软件测试网:Ns3N]Y%a!^@
   <plugin>
/P8|+g9M9\4]2p_"D0                <groupId>org.codehaus.mojo</groupId>51Testing软件测试网8|C4pw@5g kzZ
                <artifactId>dbunit-maven-plugin</artifactId>
4f5bPww{ ^5?0                <version>1.0-beta-3</version>
hz`+ko0                <configuration>
x%F+zA1DY t9Fr~0n0                                                        <dataTypeFactoryName>org.dbunit.ext.mysql.MySqlDataTypeFactory</dataTypeFactoryName>
9G7?7ahz j0                <driver>com.mysql.jdbc.Driver</driver>51Testing软件测试网/}fU-{0`"SHa9Z#I
                <username>root</username>51Testing软件测试网%c+e |r.fY
                <password>password</password>
G7m"o8o/W9jA0                                                                <url>jdbc:mysql://localhost/${db.name}?createDatabaseIfNotExist=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8&amp;amp;autoReconnect=true</url>51Testing软件测试网$N_Z TR0HkDJ
                <src>src/test/resources/sample-data.xml</src>51Testing软件测试网l(W}An#k!x f%K
                <type>CLEAN_INSERT</type>
{baR'm;OT0       <!-type表示对数据库的操作,这是dbunit约定的,可以看参考dbunit网站。这里是指删除数据后执行insert--->51Testing软件测试网x(Cj c'f|iM0{M
                <transaction>true</transaction>
p{bV$N;O R0<!-transaction等于true表示在操作是在transaction中执行-->51Testing软件测试网!JU}Lf?9rE
解析:“<src>src/test/resources/sample-data.xml</src>”是我们是使用的Dataset的数据文件。其他都是常用的mysql jdbc连接属性,这里就不用解释了。51Testing软件测试网!J|%kx2T"rY
     51Testing软件测试网L5{Gn5~Iyr
(四)          执行阶段的设定:
Br"k^:W3q} fM0           <execution>51Testing软件测试网B&kXvf:o#a
                                <id>test-compile</id>
0p1Q.u^(~7M3m0                                <phase>test-compile</phase>
9QNzm#tgt{0                                <goals>51Testing软件测试网I0R9t6b{ F8@M
                                        <goal>operation</goal>51Testing软件测试网&Ij OK@!gO#NQ l
                                </goals>
a#nI"p0}pZ6bz9p0                </execution>51Testing软件测试网.vz`&hmfOY+S
                <execution>
%c-ic m0x'{(X"M0                            <id>test</id>51Testing软件测试网n^.~3FQEh;\
                        <phase>test</phase>
%CW*?P fR0                                <goals>
1]k:]rYU0                                        <goal>operation</goal>
*[R,]%y#O5F*A0                                </goals>
+D Q6|(d&sO/q0</execution>51Testing软件测试网4|x1xj[B+c
解析:以上配置也是maven插件常见的形势,不同生命周期执行不同的goal。<phase>test-compile</phase>和 <phase>test</phase>分别代表每个goals执行的阶段,这使插件运用相当灵活。能在那个阶段执行,是有插件开发者所编写的代码决定的。51Testing软件测试网P3wo^6PjA

sU7_3Nbgny0(五)        Dbunit dataset xml文件格式介绍:
r(t2m[p!o0前文中已经讲到使用dbunit-maven-pluginge定义了src使用的数据为:src/test/resources/sample-data.xml进行初始化数据库:
wPjk%y qs}0     第一行为数据库表名:51Testing软件测试网GV ^0@N
<table name="app_user"> <!-此段为table name的定义-->
_4C4G.c$e0|s0   接下来是对表的字段的定义:
+XMYWP,f a0column代表字段名,row代表要初始化的数据。
w.A1M So9lAVb0        <column>id</column>51Testing软件测试网5jl\0~C4o h-|_m
        <column>username</column>51Testing软件测试网2^[#Gk#u
        <column>password</column>
+U)O"| N5Ay]0     。。。。。。。51Testing软件测试网w+]wTay
        <column>credentials_expired</column>
IS jV1EdG&N|0        <row>51Testing软件测试网+N\W+P^m
            <value description="id">-1</value>51Testing软件测试网BKS\ T
            <value description="username">user</value>
2^e8H!mm)_V H&j0         。。。。。。51Testing软件测试网^;E}i w%bo+B(M
            <value description="credentials_expired">0</value>
4`(s2G)M,hQi0p0        </row>51Testing软件测试网N,t e W r:{X7y)i X
到此我们完成了数据库表结构的创建和初始数据的插入。这种方式是开发者比较喜欢的方式,或许他们是不需要自已写sql语句。这样只需确认对应关系,编写相应的xml文件即可!
yxws(P2To0(六)  验证:可以通过以下命令测试:
([d%{3W#~u0     运行命令:mvn test -Pdbunit-maven-plugin
F'k`[]%fjk0
i.j d+F6A-LTG2R? f0[INFO] <<< hibernate3-maven-plugin:2.2:hbm2ddl (default) @ myproject <<<51Testing软件测试网NT3M+q-q @BF
[INFO]
%g@/MM\V6?"L,b0[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) @ myproject ---51Testing软件测试网#D/nw]&b$Na(XVL
[INFO] Configuration XML file loaded: file:/E:/MyDocument/Dropbox/workspace/appfuse/cody-ci-demo/src/main/resources/hibernate.cfg.xml51Testing软件测试网*Jq%dJ3t*JO
[INFO] Configuration XML file loaded: file:/E:/MyDocument/Dropbox/workspace/appfuse/cody-ci-demo/src/main/resources/hibernate.cfg.xml51Testing软件测试网o.J&pEAtN z
[INFO] Configuration Properties file loaded: E:\MyDocument\Dropbox\workspace\appfuse\cody-ci-demo\target\classes\jdbc.properties
MnSDg{v H0alter table user_role drop foreign key FK143BF46A9B523CC9;
@A m }H0alter table user_role drop foreign key FK143BF46A407D00A9;
.t:j(_?m+bwp/l7Q0drop table if exists app_user;
LIS B9W0}j0drop table if exists role;51Testing软件测试网7j6T:x1sdJ {%l
drop table if exists user_role;
)Y V4b&Eh0IT0create table app_user (id bigint not null auto_increment, account_expired bit not null, account_locked bit not null, address varchar(150), city varchar(50), country varchar(100), postal_code varchar(15), province varchar(100), credentials_expired bit not null, email varchar(255) not null unique, account_enabled bit, first_name varchar(50) not null, last_name varchar(50) not null, password varchar(255) not null, password_hint varchar(255), phone_number varchar(255), username varchar(50) not null unique, version integer, website varchar(255), primary key (id)) ENGINE=InnoDB;
&@:u%[zS0create table role (id bigint not null auto_increment, description varchar(64), name varchar(20), primary key (id)) ENGINE=InnoDB;
9g&iy5O$iM$I1FV0create table user_role (user_id bigint not null, role_id bigint not null, primary key (user_id, role_id)) ENGINE=InnoDB;51Testing软件测试网BT b S+qaH
alter table user_role add index FK143BF46A9B523CC9 (role_id), add constraint FK143BF46A9B523CC9 foreign key (role_id) references role (id);
YZa_)q,L.S(l&X|0alter table user_role add index FK143BF46A407D00A9 (user_id), add constraint FK143BF46A407D00A9 foreign key (user_id) references app_user (id);51Testing软件测试网\g2oY;s3Y:nC
51Testing软件测试网wnc#u/kks
二、        使用sql-maven-plugin做CDBI
[2DNoTr`)^$?(n0除了hibernate+dbunit的工作方式,我们来看看网上介绍的比较多使用sql-maven-plugin执行sql语句的方式。51Testing软件测试网cE/p(~&`
首先在执行方式的不同,请看项目pom设置:51Testing软件测试网 d1]/BA`@"B
(一)        配置支持插件:51Testing软件测试网 qh(if9G$o
   plugin>51Testing软件测试网wn%KN-^Y
                                <groupId>org.codehaus.mojo</groupId>51Testing软件测试网g\+E m,l
                                <artifactId>sql-maven-plugin</artifactId>
(KY3p)x.~&~d0                                <version>1.4</version>
,cT1oK)sbT E6Z0</plugin>51Testing软件测试网5?/ML!gu-z+mvkA
sql-maven-plugin:可以用于执行数据脚本文件也可以直接执行sql语句。后面所有的操作都是用此插件完成。51Testing软件测试网pj!e+\y2i~n/M `
(二)        插件依赖支持:为了jdbc连接必须添加相应的jar依赖。
:}*FJx^'q{W0     <dependencies>
8l.JmD!^l1JKx0                <dependency>
`F$]N%`%W0                        <groupId>mysql</groupId>51Testing软件测试网#Y\,K.X5nf4G'miP
                        <artifactId>mysql-connector-java</artifactId>51Testing软件测试网8c#P}e@%Eo5R)\
                        <version>5.1.14</version>
I'h"u(S6m(f6T&h3GB0                        </dependency>51Testing软件测试网!W7m'K$X2jVv6d
                </dependencies>51Testing软件测试网"iKnz\l V0q w
       
dCB{ r~0(三)        具体数据库连接配置: 我们使用的数据库名为myporject。
.kh)_2LDa5E0<configuration>51Testing软件测试网8iq~Jm;`"Pa%q-^
<driver>com.mysql.jdbc.Driver</driver>                                        <url>jdbc:mysql://localhost/myproject?createDatabaseIfNotExist=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8&amp;amp;autoReconnect=true</url>51Testing软件测试网*kd ca$B,u t
<username>root</username>
Mc-ASN C:HH,[6G7S0<password>password</password>51Testing软件测试网R4}4s$M:J:r}y
</configuration>
E/SgEG_$PV2X9yI0       
+~ac7X i-`6N3^u0(四)        插件可以连接数据库后,接下来是具体的数据库操作,我们会列出常用的操作类型:
Y!X V%qqRw$K*Qy"m01.        删除数据:为了防止有”脏数据”,我们通过会在开始集成测试前,删除原来的数据。51Testing软件测试网X3a0p)X8L#t"b-RK
这里我们采取drop整个数据库的方式。
+D `M#W_0  <executions>51Testing软件测试网#q0H5_ r$L
        <execution>51Testing软件测试网/h&{ A!N zC }
                <id>drop-db-before-test-if-any</id>51Testing软件测试网$e1Z;Bf;f o~
                <phase>process-test-resources</phase>
v!h],he?#l6i"s0                <goals>
/I+wxa d#Z:ua1l0                        <goal>execute</goal>51Testing软件测试网v'V @ P$q0Y D
                </goals>
1V:siHggM0                        <configuration>
C%~&dp5],U3Q0<!-- need another database to drop the targeted one51Testing软件测试网.]'e NA,|S"}
注意:这里url中的数据库名不能是我们的测试数据库名,须是一个已存在的数据库 -->51Testing软件测试网kM q_7vo
                        <url>jdbc:mysql://localhost:3306/test</url>
8PJj^M)T7U8] j0                        <autocommit>true</autocommit>51Testing软件测试网%q-PZ-`I-?h.z
                        <sqlCommand>drop database myproject</sqlCommand>51Testing软件测试网/a2b5`B+w VuW
                        <!—continue表示当有错误时,忽略错误,继续执行! -->
M qx5A!r O7y7Ww qrP0                        <onError>continue</onError>
G-WTUX1K0                        </configuration>
[wdBS!B ou0        </execution>51Testing软件测试网/TJ:zM|)z
51Testing软件测试网3J/G#Iw%c*tl~n;H;x&{

~ e l3I A"b02.        创建数据库:前一段删除了数据库,所以起始需要重新创建一个数据库。51Testing软件测试网&r)Ru JAa4q
   <execution>51Testing软件测试网 h&C*U-z1@S,L;i
                <id>create-db</id>51Testing软件测试网}s \*Ue n;a;W,C
                <phase>process-test-resources</phase>51Testing软件测试网g/j-T*V1q5s+Voq
                <goals>51Testing软件测试网w3VpY0u U B:NF;s"S
                        <goal>execute</goal>
Du1N/vK1NR0                </goals>51Testing软件测试网 o#Kiu#K!O-a VT*wy1U
<configuration>51Testing软件测试网,d\t&xu'@*~
<!—同上:这里url中的数据库名不能是我们的测试数据库名 -->51Testing软件测试网e@@$jt_
                        <url>jdbc:mysql://localhost:3306/test</url>51Testing软件测试网.`4yf;|4C2f.JC5v
                        <!-- no transaction -->51Testing软件测试网c J!I6O,T"{
                        <autocommit>true</autocommit>51Testing软件测试网X$w2X&tA JpL(o
                        <sqlCommand>create database myproject</sqlCommand>
FE(^y B*wy$n4F1R0<!—这里同样采用直接执行sql语句的方式创建数据库。-->
1V!wA,}nK}&EP0                </configuration>
)X!l!yX5I2kq,w*r0        </execution>
5R@XQ6_2Lu0                                               
'o$W U1Wf1z [03.        初始化数据库:有了干净的数据库后,我们着手准备需要的数据,包括:创建表、初始化数据等。这里Maven-sql-plugin与dbunit有所不同,dbunit使用的是xml,maven-sql-plugin是直接使用sql脚本。
;|"?0uO3`v0   <execution>51Testing软件测试网)fz4\"^!N
                <id>create-schema</id>51Testing软件测试网6[1k WB`7gqz
                <phase>process-test-resources</phase>
O&ShG_*}m0                <goals>
/s f5w a!^fK0                        <goal>execute</goal>
9]8me?S)l/j0                </goals>51Testing软件测试网 lvL7u|'In
                <configuration>51Testing软件测试网d(N t!@l M T.N9}
                  <autocommit>true</autocommit>
vTrI5{ `!diF0                <srcFiles>
;]8k%By({0L]0                <srcFile>src/main/db/mysql/${project.version}-createTables.sql</srcFile>
t|Y%I"Y9}1n0<!—此处建议使用占位符${project.version}定义脚本名,可以支持不同版本的脚本执行--->
(?5W+kY3E.Q/?0                    </srcFiles>51Testing软件测试网J/[3hcC_
                </configuration>51Testing软件测试网4P d2tGss%y8e+QZ
        </execution>
0o \"Eh c$T%D0        <execution>51Testing软件测试网.Dy;q:C)k[V
                <id>create-data</id>
9YD ~*Z^ RN2F0                <phase>process-test-resources</phase>51Testing软件测试网Q#l4W q Aia7q2t
                <goals>51Testing软件测试网u%Ld3m W;feo
                          <goal>execute</goal>51Testing软件测试网 _+_w7N:o,G&lr
                </goals>51Testing软件测试网Y'aSB1^
                <configuration>
I:J X_h$h f9E0                <fileset>51Testing软件测试网(Nb X9~0FWh*Sg
                        <basedir>${basedir}</basedir>51Testing软件测试网|x k;BMU$z
                <includes>                                                                        <include>src/main/db/mysql/${project.version}-initData.sql</include>51Testing软件测试网3`6I)Oe#H+Dnl0C{
                </includes>
N At`sa c0                </fileset>
y9q3i:a w0                </configuration>51Testing软件测试网0v3PZ%BP&Q&F0P
        </execution>
2|?/d+_%{u1d GQ7y$v0       51Testing软件测试网e M2jS XKAx_+E A
4.        为了不干下次的集成或别的同事集成,可以考虑清理数据库。此段为可选择项。并不是所有项目都需要清楚测试数据的。
z3?;zrWeI/DQ0  <execution>51Testing软件测试网p$w5m;fi V2r
                <id>drop-db-after-test</id>
2`4N5Y,\#I8M0X*z0                <phase>test</phase>51Testing软件测试网'GY cz J X
                <goals>
Ax UEM,AF%q0                        <goal>execute</goal>
;M/P| qfm'@#c0                </goals>
v4A(Yhc0                <configuration>
lCN-gm6j0                        <autocommit>true</autocommit>51Testing软件测试网6ta~H)SCB]5d
                        <sqlCommand>drop database myproject</sqlCommand>
p*R!oBPS W5S0                </configuration>51Testing软件测试网/NL$CXYN7P
        </execution>51Testing软件测试网pSQ)d&lR z.~
(五)        验证:结果验证51Testing软件测试网 hPg#nvof x:lFL9F?
运行命令:mvn test  -Psql-maven-plugin51Testing软件测试网 Z@WSW#v0e

N*?_p ],P Su0[INFO] --- sql-maven-plugin:1.4:execute (drop-db-before-test-if-any) @ myproject ---51Testing软件测试网R2OR5N1| Vm;A*N
[INFO] Executing commands51Testing软件测试网!DK-smC JX5ak
[INFO] 1 of 1 SQL statements executed successfully51Testing软件测试网'gc6s:o4W@
[INFO]
hpJ,{K.dN'?Ad-}0[INFO] --- sql-maven-plugin:1.4:execute (create-db) @ myproject ---51Testing软件测试网;B9a"vr5Y,~m
[INFO] Executing commands51Testing软件测试网k#jw t6Bu*F2l?c4D
[INFO] 1 of 1 SQL statements executed successfully
lsbs&qIP2Z0[INFO]51Testing软件测试网7yQz8f-J-L2HV`
[INFO] --- sql-maven-plugin:1.4:execute (create-schema) @ myproject ---
.w;bf u$^ PQ0[INFO] Executing file: C:\DOCUME~1\ZHANGY~1\LOCALS~1\Temp\1.0.2-SNAPSHOT-createTables.1789050273sql
S0jYSMmq0[INFO] 4 of 4 SQL statements executed successfully
5jx-J"L"C:y;l0[INFO]51Testing软件测试网qLJq!QWd0HEqD
[INFO] --- sql-maven-plugin:1.4:execute (create-data) @ myproject ---
\#i%naMIAE@0[INFO] Executing file: E:\MyDocument\Dropbox\workspace\appfuse\cody-ci-demo\src\main\db\mysql\1.0.2-SNAPSHOT-initData.sql51Testing软件测试网X YIf_R-` [ I-u:F
[INFO] 6 of 6 SQL statements executed successfully
3gO1W"d;g1[&FM0[INFO]51Testing软件测试网\BI No^k;_
[INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ myproject –51Testing软件测试网kA$b-a-z-P

*^ qO!@%ZR7y0三、        两种方式比较:51Testing软件测试网VWM2L FB Q?)L?
以上两种方式中无论采用哪种都可以对数据库进行操作,但对于dbunit的方式更适合于新创建项目,因处于项目初期还在进行数据库的设计,可以在设计数据库的同时相应调整的hibernate的xml。而sql-maven-plugin可以用于在开发期的项目,已有现成的sql语句,修改数据结构也是使用sql脚本完成,所以显得更为方便!
5cPW"Ax5JKh0四、        持续数据库集成的注意事项:
7p]9a"QDy0         要做到数据库持续集成我们需要有以下准备:51Testing软件测试网X;Hg0e8N9xP
1.        数据库文件版本化:数据库相关文件需要在版本库中。数据库持续集成希望到达一个目标,在任何干净环境checkout代码,都可以做数据库持续集成,所以要求所有数据库相关文件都放到配置库中,最好后项目代码放在同一个库。
ex+d@d~A^02.        规范化数据库脚本命名:若是采用sql-maven-plugin方式时,有时候由于数据库脚本过于庞大,可以考虑增量方式执行(若是执行效率要求较高,可以采用内存数据库。)。所以,其中脚本名称最好规范,这样便于针对执行。如:
_j sr9?7S [M3@0数据库文件可以同时包含:增量脚本和完整脚本。可以根据需要执行不同脚本。命名需要规范。如:项目版本号+时间 1.0.2-20120619.sql
k]w3mJ_/s1@7{03.        CI服务器:做持续集成不能没有CI(continuous integration)服务器,篇幅有限,此次我们就不介绍如何把项目部署到CI中,因这不是此次介绍的重点。
'C GZ/Pc z04.   独立的schema:若是开发人员想自已做数据库集成可以考虑为其每人创建一个独立的schema。51Testing软件测试网!N!Ra4R)S#\0W9o$D
笔者在过去公司就是采用这样的方式,可以方便的统计每个成员各自的代码测试覆盖率。51Testing软件测试网'L|eWLK:W
后记:
)GJX7c-\YG*H0方便大家学习,代码已放到google code中。演示代码下载地址(为了避免版权问题,此演示都使用openSource资源):51Testing软件测试网 oUHPJ Uu
https://cody-ci-demo.googlecode. ... s/CDBI-cody-ci-demo51Testing软件测试网g O2y ^1O)Q
为使演示代码更为直观,对appfuse原有的pom做了修改,只支持mysql了。51Testing软件测试网S V(D2N/I
参考资料:         http://mojo.codehaus.org/sql-maven-plugin/examples/execute.html51Testing软件测试网 ]!uS1H uzU
http://www.dbunit.org/components.html
k%N A&ft7j(PO0与数据库集成紧密相关的实践有:持续测试和持续反馈等,若有机会以后会继续和大家分享!
KRV q^m.O051Testing软件测试网(Jf9S.o-y W
转载于:http://www.scmroad.com/forum.php?mod=viewthread&tid=25615&page=1&extra=#pid40395

TAG:

 

评分:0

我来说两句

Open Toolbar