Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dbdeploy-ant/src/main/java/com/dbdeploy/AntTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class AntTarget extends Task {
+ "\n\t\tchangeLogTableName=\"[CHANGE LOG TABLE NAME]\""
+ "\n\t\tdelimiter=\"[STATEMENT DELIMITER - default ;]\""
+ "\n\t\tdelimitertype=\"[STATEMENT DELIMITER TYPE - row or normal, default normal]\""
+ "\n\t\tfake=\"[BOOLEAN - fake migrations]\""
+ "\n\t\tquiet=\"[BOOLEAN - be quieter (omit messages about each statement)]\""
+ "\n\t/>"
+ "\n\n* - Indicates mandatory parameter";

Expand Down Expand Up @@ -102,5 +104,14 @@ public void setEncoding(String encoding) {
public void setLineEnding(LineEnding lineEnding) {
dbDeploy.setLineEnding(lineEnding);
}

public void setQuiet(boolean quiet) {
dbDeploy.setQuiet(quiet);
}

public void setFake(boolean fake) {
dbDeploy.setFake(fake);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ private void copyValuesFromCommandLineToDbDeployBean(DbDeploy dbDeploy, CommandL
if (p.getPropertyType().isAssignableFrom(File.class)) {
value = new File((String) value);
}

p.getWriteMethod().invoke(dbDeploy, value);
if (p.getPropertyType() == Long.class) {
value = Long.parseLong(value.toString());
}
if (value != null) {
p.getWriteMethod().invoke(dbDeploy, value);
}
}
}

if (commandLine.hasOption("fake")) {
dbDeploy.setFake(true);
}

if (commandLine.hasOption("delimitertype")) {
dbDeploy.setDelimiterType(DelimiterType.valueOf(commandLine.getOptionValue("delimitertype")));
}
Expand All @@ -60,6 +68,7 @@ private void copyValuesFromCommandLineToDbDeployBean(DbDeploy dbDeploy, CommandL
dbDeploy.setLineEnding(LineEnding.valueOf(commandLine.getOptionValue("lineending")));
}


} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -153,6 +162,17 @@ private Options getOptions() {
.withLongOpt("lineending")
.create());

options.addOption(OptionBuilder
.hasArg().withType(Long.class)
.withDescription("The highest numbered delta script to apply")
.withLongOpt("lastChangeToApply")
.create());


options.addOption(OptionBuilder
.withDescription("fake a migration - only make changes to Changelog")
.withLongOpt("fake")
.create());

return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void checkAllOfTheOtherFieldsParseOkHere() throws Exception {
"--changeLogTableName my-change-log " +
"--dbms ora " +
"--templatedir /tmp/mytemplates " +
"--fake " +
"--lastChangeToApply 2345 " +
"--delimiter \\ --delimitertype row").split(" "), dbDeploy);

assertThat(dbDeploy.getUserid(), is("userid"));
Expand All @@ -48,6 +50,8 @@ public void checkAllOfTheOtherFieldsParseOkHere() throws Exception {
assertThat(dbDeploy.getDelimiter(), is("\\"));
assertThat(dbDeploy.getDelimiterType(), is(DelimiterType.row));
assertThat(dbDeploy.getTemplatedir().getPath(), is(File.separator + "tmp" + File.separator + "mytemplates"));
assertThat(dbDeploy.getLastChangeToApply(), is(2345L));
assertThat(dbDeploy.getFake(), is(true));
}

@Test
Expand Down
25 changes: 23 additions & 2 deletions dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class DbDeploy {
private String delimiter = ";";
private DelimiterType delimiterType = DelimiterType.normal;
private File templatedir;
private boolean fake = false;
private boolean quiet = false;

public void setDriver(String driver) {
this.driver = driver;
Expand Down Expand Up @@ -80,6 +82,19 @@ public void setLineEnding(LineEnding lineEnding) {
this.lineEnding = lineEnding;
}

public void setFake(boolean fake) {
this.fake = fake;
}

public void setQuiet(boolean quiet) {
this.quiet = quiet;
}

public boolean getQuiet()
{
return this.quiet;
}

public void go() throws Exception {
System.err.println(getWelcomeString());

Expand All @@ -100,13 +115,15 @@ public void go() throws Exception {
if (outputfile != null) {
doScriptApplier = new TemplateBasedApplier(
new PrintWriter(outputfile, encoding), dbms,
changeLogTableName, delimiter, delimiterType, getTemplatedir());
changeLogTableName, delimiter, delimiterType, getTemplatedir(), fake);
} else {
QueryStatementSplitter splitter = new QueryStatementSplitter();
splitter.setDelimiter(getDelimiter());
splitter.setDelimiterType(getDelimiterType());
splitter.setOutputLineEnding(lineEnding);
doScriptApplier = new DirectToDbApplier(queryExecuter, databaseSchemaVersionManager, splitter);
DirectToDbApplier applier = new DirectToDbApplier(queryExecuter, databaseSchemaVersionManager, splitter, fake);
applier.setQuiet(quiet);
doScriptApplier = applier;
}

ChangeScriptApplier undoScriptApplier = null;
Expand Down Expand Up @@ -224,4 +241,8 @@ public String getEncoding() {
public LineEnding getLineEnding() {
return lineEnding;
}

public boolean getFake() {
return fake;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,36 @@ public class DirectToDbApplier implements ChangeScriptApplier {
private final QueryExecuter queryExecuter;
private final DatabaseSchemaVersionManager schemaVersionManager;
private final QueryStatementSplitter splitter;
private final boolean fake;
private boolean quiet=false;

public DirectToDbApplier(QueryExecuter queryExecuter, DatabaseSchemaVersionManager schemaVersionManager, QueryStatementSplitter splitter) {
public DirectToDbApplier(QueryExecuter queryExecuter, DatabaseSchemaVersionManager schemaVersionManager, QueryStatementSplitter splitter,
boolean fake) {
this.queryExecuter = queryExecuter;
this.schemaVersionManager = schemaVersionManager;
this.splitter = splitter;
this.fake = fake;
}

public void setQuiet(boolean quiet) {
this.quiet = quiet;
}

public boolean getQuiet() {
return this.quiet;
}

public void apply(List<ChangeScript> changeScript) {
begin();

String applyType = fake ? "Faking " : "Applying ";

for (ChangeScript script : changeScript) {
System.err.println("Applying " + script + "...");
System.err.println(applyType + script + "...");

applyChangeScript(script);
if (! fake) {
applyChangeScript(script);
}
insertToSchemaVersionTable(script);

commitTransaction();
Expand All @@ -48,7 +64,7 @@ protected void applyChangeScript(ChangeScript script) {
for (int i = 0; i < statements.size(); i++) {
String statement = statements.get(i);
try {
if (statements.size() > 1) {
if (statements.size() > 1 && !quiet) {
System.err.println(" -> statement " + (i+1) + " of " + statements.size() + "...");
}
queryExecuter.execute(statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public class TemplateBasedApplier implements ChangeScriptApplier {
private String changeLogTableName;
private String delimiter;
private DelimiterType delimiterType;
private boolean fake;

public TemplateBasedApplier(Writer writer, String syntax, String changeLogTableName, String delimiter, DelimiterType delimiterType, File templateDirectory) throws IOException {
public TemplateBasedApplier(Writer writer, String syntax, String changeLogTableName, String delimiter, DelimiterType delimiterType, File templateDirectory,
boolean fake) throws IOException {
this.syntax = syntax;
this.changeLogTableName = changeLogTableName;
this.delimiter = delimiter;
this.delimiterType = delimiterType;
this.writer = writer;
this.configuration = new Configuration();
this.fake = fake;

FileTemplateLoader fileTemplateLoader = createFileTemplateLoader(templateDirectory);
this.configuration.setTemplateLoader(
Expand Down Expand Up @@ -74,7 +77,10 @@ public void apply(List<ChangeScript> changeScripts) {
}

protected String getTemplateQualifier() {
return "apply";
if (fake)
return "fake";
else
return "apply";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

public class UndoTemplateBasedApplier extends TemplateBasedApplier {
public UndoTemplateBasedApplier(Writer writer, String syntax,
String changeLogTableName, String delimiter, DelimiterType delimiterType, File templateDirectory) throws IOException {
super(writer, syntax, changeLogTableName, delimiter, delimiterType, templateDirectory);
super(writer, syntax, changeLogTableName, delimiter, delimiterType, templateDirectory, false);
}

@Override
Expand Down
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/db2_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="delimiter" type="java.lang.String" --]
[#-- @ftlvariable name="separator" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, CURRENT TIMESTAMP, USER, '${script.description}')${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

[/#list]
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/hsql_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="delimiter" type="java.lang.String" --]
[#-- @ftlvariable name="separator" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, CURRENT_TIMESTAMP, USER(), '${script.description}')${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

[/#list]
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/mssql_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, getdate(), user_name(), '${script.description}')
GO

COMMIT
GO

-- END CHANGE SCRIPT ${script}

[/#list]
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/mysql_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="delimiter" type="java.lang.String" --]
[#-- @ftlvariable name="separator" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, CURRENT_TIMESTAMP, USER(), '${script.description}')${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

[/#list]
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/ora_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="delimiter" type="java.lang.String" --]
[#-- @ftlvariable name="separator" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, CURRENT_TIMESTAMP, USER, '${script.description}')${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

[/#list]
17 changes: 17 additions & 0 deletions dbdeploy-core/src/main/resources/pgsql_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="delimiter" type="java.lang.String" --]
[#-- @ftlvariable name="separator" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}

INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, current_timestamp, current_user, '${script.description}')${separator}${delimiter}

COMMIT${separator}${delimiter}

-- END CHANGE SCRIPT ${script}

[/#list]
18 changes: 18 additions & 0 deletions dbdeploy-core/src/main/resources/syb-ase_fake.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[#ftl]
[#-- @ftlvariable name="changeLogTableName" type="java.lang.String" --]
[#-- @ftlvariable name="scripts" type="java.util.List<com.dbdeploy.scripts.ChangeScript>" --]
[#list scripts as script]

-- START CHANGE SCRIPT ${script}


INSERT INTO ${changeLogTableName} (change_number, complete_dt, applied_by, description)
VALUES (${script.id?c}, getdate(), user_name(), '${script.description}')
GO

COMMIT
GO

-- END CHANGE SCRIPT ${script}

[/#list]
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class DirectToDbApplierTest {

@Before
public void setUp() {
applier = new DirectToDbApplier(queryExecuter, schemaVersionManager, splitter);
applier = new DirectToDbApplier(queryExecuter, schemaVersionManager, splitter, false);
}

@Test
Expand Down
Loading