Reuse existing database
This feature is primarily intended for development. A developer debugging an issue might want to keep reusing an database instance either for performance reasons or to save the time of recreating the test conditions.
When adding the optional configuration setting:
<configuration> ... <clearexistingdata>false</clearexistingdata> ... </configuration>
The plugin will behave as following:
- If there is already a folder structure under the folder provided by the configuration setting
data
, the plugin will try to reuse it. - If no folder exists at the
data
location the plugin will try to create a new database regardless of theclearexistingdata
value. - Calling Maven with
clean
will always delete the existing database. This will cause the plugin to create a new database regardless.
Run mvn clean install
to create a new database and mvn install
to reuse it.
Please note that the default value of the setting is true
, in which case the plugin will always delete and recreate the database.
The plugin also exports a maven variable jcabi.reused.database
indicating if an existing database could have been reused or if a new one was created. This can be useful to control if following tasks should be skipped.
Example, default data should only be loaded if this is a new database:
<plugin> <groupId>com.jcabi</groupId> <artifactId>jcabi-mysql-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <id>mysql-test</id> <goals> <goal>start</goal> </goals> <configuration> <port>3306</port> <dist>/opt/mysql-5.6.15-linux-glibc2.5-x86_64</dist> <data>/home/r/repo/target/mysql-data</data> <clearexistingdata>false</clearexistingdata> <options> <option>innodb_strict_mode=ON</option> <option>innodb_file_format=Barracuda</option> </options> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sql-maven-plugin</artifactId> <version>1.5</version> <dependencies> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>5.1.26</version> </dependency> </dependencies> <configuration> <skip>${jcabi.reused.database}</skip> <driver>com.mysql.jdbc.Driver</driver> <username>root</username> <password>root</password> </configuration> <executions> <execution> <id>create-tables</id> <phase>pre-integration-test</phase> <goals> <goal>execute</goal> </goals> <configuration> <orderFile>ascending</orderFile> <fileset> <basedir>${basedir}/src/test/resources</basedir> <includes> <include>testdata.sql</include> </includes> </fileset> </configuration> </execution> </executions> </plugin>
Note that it is not necessart to set the jcabi.reused.database
property under the properties
section. Doing so would overwrite the value the plugin sets, as the properties
will be set after the plugin was executed.