Skip to content

Commit eb4c0aa

Browse files
author
fedonind
committed
SME-256 Incremental mode + check alter_log, not tested yet.
1 parent 383bdc1 commit eb4c0aa

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
/classes
33
.idea
44
*.iml
5+
.settings
6+
.project
7+
.classpath

src/main/java/ch/ips/g2/applyalter/Alter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import java.util.ArrayList;
44
import java.util.HashSet;
55
import java.util.List;
6-
import java.util.Set;
76
import java.util.Map;
7+
import java.util.Set;
8+
89
import com.thoughtworks.xstream.annotations.XStreamAlias;
910
import com.thoughtworks.xstream.annotations.XStreamImplicit;
1011

@@ -43,6 +44,8 @@ public class Alter
4344
public List<Check> checks = new ArrayList<Check>();
4445
@XStreamImplicit(itemFieldName = "instance")
4546
public Set<String> instance = new HashSet<String>();
47+
@XStreamImplicit
48+
public Boolean synchronization = false;
4649

4750
@XStreamImplicit(itemFieldName = "datafile")
4851
public List<String> datafile;

src/main/java/ch/ips/g2/applyalter/ApplyAlter.java

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
package ch.ips.g2.applyalter;
22

33
import static ch.ips.g2.applyalter.ReportLevel.*;
4-
import org.apache.commons.cli.BasicParser;
5-
import org.apache.commons.cli.CommandLine;
6-
import org.apache.commons.cli.CommandLineParser;
7-
import org.apache.commons.cli.HelpFormatter;
8-
import org.apache.commons.cli.Options;
9-
import org.apache.commons.cli.UnrecognizedOptionException;
10-
import org.apache.commons.io.IOUtils;
11-
import org.xml.sax.SAXException;
12-
import javax.xml.transform.stream.StreamSource;
13-
import javax.xml.validation.Schema;
14-
import javax.xml.validation.SchemaFactory;
15-
import javax.xml.validation.Validator;
4+
165
import java.io.File;
176
import java.io.FileInputStream;
187
import java.io.FileNotFoundException;
@@ -26,6 +15,21 @@
2615
import java.util.HashSet;
2716
import java.util.List;
2817
import java.util.Set;
18+
19+
import javax.xml.transform.stream.StreamSource;
20+
import javax.xml.validation.Schema;
21+
import javax.xml.validation.SchemaFactory;
22+
import javax.xml.validation.Validator;
23+
24+
import org.apache.commons.cli.BasicParser;
25+
import org.apache.commons.cli.CommandLine;
26+
import org.apache.commons.cli.CommandLineParser;
27+
import org.apache.commons.cli.HelpFormatter;
28+
import org.apache.commons.cli.Options;
29+
import org.apache.commons.cli.UnrecognizedOptionException;
30+
import org.apache.commons.io.IOUtils;
31+
import org.xml.sax.SAXException;
32+
2933
import com.google.common.collect.ArrayListMultimap;
3034
import com.google.common.collect.Multimap;
3135
import com.thoughtworks.xstream.XStream;
@@ -51,6 +55,10 @@ public class ApplyAlter
5155
* Run mode parameter name
5256
*/
5357
public static final String RUN_MODE = "r";
58+
/**
59+
* Incremental mode parameter name
60+
*/
61+
public static final String INC_MODE = "s";
5462
/**
5563
* Ignore failures parameter name
5664
*/
@@ -549,6 +557,9 @@ else if ( s.getIgnoredSqlCodes() != null && s.getIgnoredSqlCodes().contains( e.g
549557

550558
protected boolean executeChecks( Alter alter, DbInstance d, Connection connection )
551559
{
560+
if (checkInc(alter, d, connection)) {
561+
return true;
562+
}
552563
if ( check( connection, alter.getCheckok() ) )
553564
{
554565
//checkOK is sufficient
@@ -571,6 +582,31 @@ protected boolean executeChecks( Alter alter, DbInstance d, Connection connectio
571582
return true;
572583
}
573584

585+
boolean checkInc(Alter alter, DbInstance d, Connection c) {
586+
if (alter.synchronization) {
587+
// continue with checks
588+
return false;
589+
}
590+
if (o.getOption(INC_MODE) == null) {
591+
return false;
592+
}
593+
PreparedStatement s = null;
594+
try {
595+
s = c.prepareStatement("select hash from " + d.getLogTable() + " where id = ?");
596+
s.setString(2, alter.getId());
597+
if (s.execute()) {
598+
// TODO check hash
599+
// the only case to skip script if option is set, sync is not set and result set is not empty
600+
return true;
601+
}
602+
} catch (SQLException e) {
603+
runContext.report(ReportLevel.ERROR, "failed to insert applyalter_log record: %s", e.getMessage());
604+
} finally {
605+
DbUtils.close(s);
606+
}
607+
return false;
608+
}
609+
574610
/**
575611
* Logs successful alter to stdout and applyalter_log table
576612
* @param d database instance
@@ -660,20 +696,21 @@ public String getUnappliedAlters() {
660696
return s.toString();
661697
}
662698

699+
static Options o = new Options();
663700
/**
664701
* Main function, which can be called from command line.
665702
*
666703
* @param args
667704
*/
668705
public static void main(String[] args)
669706
{
670-
Options o = new Options();
671707
o.addOption( IGNORE_FAILURES, false, "ignore failures" );
672708
o.addOption( PRINTSTACKTRACE, false, "print stacktrace" );
673709
o.addOption( RUN_MODE, true, "runmode, possible values: " + Arrays.toString( RunMode.values() ) );
674710
o.addOption( USER_NAME, true, "user name" );
675711
o.addOption( NO_VALIDATE_XML, false, "disables XML file with alter script validation" );
676712
o.addOption( NO_LOG_TABLE, false, "disables log table" );
713+
o.addOption(INC_MODE, false, "incremental mode");
677714

678715
boolean ignfail = false;
679716
boolean printstacktrace = false;

0 commit comments

Comments
 (0)