Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check 4 use cases: * Migration of pending migrations to the DB. * Dry-run mode (printing the pending change set to the console). * Migration of limited amount of changes. * Help page (description of the command and available arguments)
- Loading branch information
Showing
5 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
dropwizard-migrations/src/test/java/io/dropwizard/migrations/DbMigrateCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package io.dropwizard.migrations; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import io.dropwizard.db.DataSourceFactory; | ||
import io.dropwizard.db.DatabaseConfiguration; | ||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.inf.Namespace; | ||
import net.sourceforge.argparse4j.inf.Subparser; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.skife.jdbi.v2.DBI; | ||
import org.skife.jdbi.v2.Handle; | ||
|
||
import java.io.*; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class DbMigrateCommandTest { | ||
|
||
private DbMigrateCommand<TestMigrateConfiguration> migrateCommand = new DbMigrateCommand<>( | ||
new DatabaseConfiguration<TestMigrateConfiguration>() { | ||
@Override | ||
public DataSourceFactory getDataSourceFactory(TestMigrateConfiguration configuration) { | ||
return configuration.getDataSource(); | ||
} | ||
}, TestMigrateConfiguration.class); | ||
private TestMigrateConfiguration conf; | ||
private String databaseUrl; | ||
|
||
private static String createTempFile() { | ||
try { | ||
return File.createTempFile("test-example", null).getAbsolutePath(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
databaseUrl = "jdbc:h2:" + createTempFile(); | ||
|
||
final DataSourceFactory dataSource = new DataSourceFactory(); | ||
dataSource.setDriverClass("org.h2.Driver"); | ||
dataSource.setUser("sa"); | ||
dataSource.setUrl(databaseUrl); | ||
conf = new TestMigrateConfiguration(dataSource); | ||
} | ||
|
||
@Test | ||
public void testRun() throws Exception { | ||
migrateCommand.run(null, new Namespace(ImmutableMap.<String, Object>of()), conf); | ||
try (Handle handle = new DBI(databaseUrl, "sa", "").open()) { | ||
final List<Map<String, Object>> rows = handle.select("select * from persons"); | ||
assertThat(rows).hasSize(1); | ||
assertThat(rows.get(0)).isEqualTo( | ||
ImmutableMap.of("id", 1, "name", "Bill Smith", "email", "bill@smith.me")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRunFirstTwoMigration() throws Exception { | ||
migrateCommand.run(null, new Namespace(ImmutableMap.of("count", (Object) 2)), conf); | ||
try (Handle handle = new DBI(databaseUrl, "sa", "").open()) { | ||
assertThat(handle.select("select * from persons")).isEmpty(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDryRun() throws Exception { | ||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
migrateCommand.setOutputStream(new PrintStream(baos)); | ||
migrateCommand.run(null, new Namespace(ImmutableMap.of("dry-run", (Object) true)), conf); | ||
assertThat(baos.toString("UTF-8")).startsWith(String.format( | ||
"-- *********************************************************************%n" + | ||
"-- Update Database Script%n" + | ||
"-- *********************************************************************%n")); | ||
} | ||
|
||
@Test | ||
public void testPrintHelp() throws Exception { | ||
final Subparser subparser = ArgumentParsers.newArgumentParser("db") | ||
.addSubparsers() | ||
.addParser(migrateCommand.getName()) | ||
.description(migrateCommand.getDescription()); | ||
migrateCommand.configure(subparser); | ||
|
||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
subparser.printHelp(new PrintWriter(baos, true)); | ||
|
||
assertThat(baos.toString("UTF-8")).isEqualTo(String.format( | ||
"usage: db migrate [-h] [--migrations MIGRATIONS-FILE] [--catalog CATALOG]%n" + | ||
" [--schema SCHEMA] [-n] [-c COUNT] [-i CONTEXTS] [file]%n" + | ||
"%n" + | ||
"Apply all pending change sets.%n" + | ||
"%n" + | ||
"positional arguments:%n" + | ||
" file application configuration file%n" + | ||
"%n" + | ||
"optional arguments:%n" + | ||
" -h, --help show this help message and exit%n" + | ||
" --migrations MIGRATIONS-FILE%n" + | ||
" the file containing the Liquibase migrations for%n" + | ||
" the application%n" + | ||
" --catalog CATALOG Specify the database catalog (use database%n" + | ||
" default if omitted)%n" + | ||
" --schema SCHEMA Specify the database schema (use database default%n" + | ||
" if omitted)%n" + | ||
" -n, --dry-run output the DDL to stdout, don't run it%n" + | ||
" -c COUNT, --count COUNT%n" + | ||
" only apply the next N change sets%n" + | ||
" -i CONTEXTS, --include CONTEXTS%n" + | ||
" include change sets from the given context%n")); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
dropwizard-migrations/src/test/java/io/dropwizard/migrations/TestMigrateConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.dropwizard.migrations; | ||
|
||
import io.dropwizard.Configuration; | ||
import io.dropwizard.db.DataSourceFactory; | ||
|
||
public class TestMigrateConfiguration extends Configuration { | ||
|
||
private DataSourceFactory dataSource; | ||
|
||
public TestMigrateConfiguration(DataSourceFactory dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public DataSourceFactory getDataSource() { | ||
return dataSource; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> | ||
|
||
<changeSet id="1" author="db_dev"> | ||
<createTable tableName="persons"> | ||
<column name="id" type="int" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="name" type="varchar(256)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
|
||
<changeSet id="2" author="db_dev"> | ||
<addColumn tableName="persons"> | ||
<column name="email" type="varchar(128)"/> | ||
</addColumn> | ||
</changeSet> | ||
|
||
<changeSet id="3" author="db_dev"> | ||
<insert tableName="persons"> | ||
<column name="name" value="Bill Smith"/> | ||
<column name="email" value="bill@smith.me"/> | ||
</insert> | ||
</changeSet> | ||
</databaseChangeLog> |