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
12 changes: 11 additions & 1 deletion dbdeploy-core/src/main/java/com/dbdeploy/DbDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.io.PrintWriter;
import java.util.List;

public class DbDeploy {
private String url;
Expand All @@ -31,6 +32,7 @@ public class DbDeploy {
private String delimiter = ";";
private DelimiterType delimiterType = DelimiterType.normal;
private File templatedir;
private List<Long> changeScriptIdsToIgnore;

public void setDriver(String driver) {
this.driver = driver;
Expand Down Expand Up @@ -79,6 +81,10 @@ public void setEncoding(String encoding) {
public void setLineEnding(LineEnding lineEnding) {
this.lineEnding = lineEnding;
}

public void setChangeScriptIdsToIgnore(List<Long> changeScriptIdsToIgnore) {
this.changeScriptIdsToIgnore = changeScriptIdsToIgnore;
}

public void go() throws Exception {
System.err.println(getWelcomeString());
Expand All @@ -93,7 +99,7 @@ public void go() throws Exception {
new DatabaseSchemaVersionManager(queryExecuter, changeLogTableName);

ChangeScriptRepository changeScriptRepository =
new ChangeScriptRepository(new DirectoryScanner(encoding).getChangeScriptsForDirectory(scriptdirectory));
new ChangeScriptRepository(new DirectoryScanner(encoding).getChangeScriptsForDirectory(scriptdirectory, changeScriptIdsToIgnore));

ChangeScriptApplier doScriptApplier;

Expand Down Expand Up @@ -224,4 +230,8 @@ public String getEncoding() {
public LineEnding getLineEnding() {
return lineEnding;
}

public List<Long> getChangeScriptIdsToIgnore() {
return changeScriptIdsToIgnore;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DirectoryScanner(String encoding) {
this.encoding = encoding;
}

public List<ChangeScript> getChangeScriptsForDirectory(File directory) {
public List<ChangeScript> getChangeScriptsForDirectory(File directory, List<Long> changeScriptIdsToIgnore) {
try {
System.err.println("Reading change scripts from directory " + directory.getCanonicalPath() + "...");
} catch (IOException e1) {
Expand All @@ -30,7 +30,13 @@ public List<ChangeScript> getChangeScriptsForDirectory(File directory) {
String filename = file.getName();
try {
long id = filenameParser.extractIdFromFilename(filename);
scripts.add(new ChangeScript(id, file, encoding));
if (changeScriptIdsToIgnore == null) {
scripts.add(new ChangeScript(id, file, encoding));
} else {
if (!changeScriptIdsToIgnore.contains(id)) {
scripts.add(new ChangeScript(id, file, encoding));
}
}
} catch (UnrecognisedFilenameException e) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.dbdeploy.scripts;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.mockito.Mockito;

public class DirectoryScannerTest {

@Test
public void shouldIgnoreChangeScriptsThatAreInTheIgnoreList() throws Exception {
DirectoryScanner scanner = new DirectoryScanner("UTF-8");
List<Long> changeScriptIdsToIgnore = Arrays.asList(new Long[] { 2l });

File file1 = mockFile("1_file1");
File file2 = mockFile("2_file2");
File file3 = mockFile("3_file3");
File[] files = new File[] { file1, file2, file3 };

File directory = Mockito.mock(File.class);
when(directory.listFiles()).thenReturn(files);

List<ChangeScript> changeScripts = scanner.getChangeScriptsForDirectory(directory, changeScriptIdsToIgnore);

for (ChangeScript changeScript : changeScripts) {
assertThat(changeScript.getFile(), not(equalTo(file2)));
}
}

private File mockFile(String fileName) {
File file = Mockito.mock(File.class);
when(file.isFile()).thenReturn(true);
when(file.getName()).thenReturn(fileName);
return file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.AbstractMojo;

import com.dbdeploy.DbDeploy;
import com.dbdeploy.database.DelimiterType;
import com.dbdeploy.database.LineEnding;
import org.apache.maven.plugin.AbstractMojo;

import java.io.File;

/**
* Abstract class that all dbdeploy database goals should extend.
Expand Down Expand Up @@ -116,6 +121,13 @@ public abstract class AbstractDbDeployMojo extends AbstractMojo {
*/
protected Long lastChangeToApply;

/**
* The change scripts to ignore as comma separated values.
*
* @parameter expression="${dbdeploy.changeScriptIdsToIgnore}"
*/
protected String changeScriptIdsToIgnore;

protected DbDeploy getConfiguredDbDeploy() {
DbDeploy dbDeploy = new DbDeploy();
dbDeploy.setScriptdirectory(scriptdirectory);
Expand Down Expand Up @@ -147,7 +159,22 @@ protected DbDeploy getConfiguredDbDeploy() {
if (lineEnding != null) {
dbDeploy.setLineEnding(LineEnding.valueOf(lineEnding));
}

if (StringUtils.isNotBlank(changeScriptIdsToIgnore)) {
List<Long> changeScriptLongIdsToIgnore = toListOfLongs(changeScriptIdsToIgnore);
dbDeploy.setChangeScriptIdsToIgnore(changeScriptLongIdsToIgnore);
}

return dbDeploy;
}

private List<Long> toListOfLongs(String idsAsString) {
List<String> stringIds = Arrays.asList(StringUtils.split(idsAsString, ","));
List<Long> longIds = new ArrayList<Long>();
for (String stringId : stringIds) {
longIds.add(Long.valueOf(stringId));
}
return longIds;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<dbms>hsql</dbms>
<outputfile>apply.sql</outputfile>
<undoOutputfile>undo.sql</undoOutputfile>
<changeScriptIdsToIgnore>1,2,3</changeScriptIdsToIgnore>
</configuration>
</plugin>
</plugins>
Expand Down