1
1
package ch .ips .g2 .applyalter ;
2
2
3
3
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
+
16
5
import java .io .File ;
17
6
import java .io .FileInputStream ;
18
7
import java .io .FileNotFoundException ;
26
15
import java .util .HashSet ;
27
16
import java .util .List ;
28
17
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
+
29
33
import com .google .common .collect .ArrayListMultimap ;
30
34
import com .google .common .collect .Multimap ;
31
35
import com .thoughtworks .xstream .XStream ;
@@ -51,6 +55,10 @@ public class ApplyAlter
51
55
* Run mode parameter name
52
56
*/
53
57
public static final String RUN_MODE = "r" ;
58
+ /**
59
+ * Incremental mode parameter name
60
+ */
61
+ public static final String INC_MODE = "s" ;
54
62
/**
55
63
* Ignore failures parameter name
56
64
*/
@@ -549,6 +557,9 @@ else if ( s.getIgnoredSqlCodes() != null && s.getIgnoredSqlCodes().contains( e.g
549
557
550
558
protected boolean executeChecks ( Alter alter , DbInstance d , Connection connection )
551
559
{
560
+ if (checkInc (alter , d , connection )) {
561
+ return true ;
562
+ }
552
563
if ( check ( connection , alter .getCheckok () ) )
553
564
{
554
565
//checkOK is sufficient
@@ -571,6 +582,31 @@ protected boolean executeChecks( Alter alter, DbInstance d, Connection connectio
571
582
return true ;
572
583
}
573
584
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
+
574
610
/**
575
611
* Logs successful alter to stdout and applyalter_log table
576
612
* @param d database instance
@@ -660,20 +696,21 @@ public String getUnappliedAlters() {
660
696
return s .toString ();
661
697
}
662
698
699
+ static Options o = new Options ();
663
700
/**
664
701
* Main function, which can be called from command line.
665
702
*
666
703
* @param args
667
704
*/
668
705
public static void main (String [] args )
669
706
{
670
- Options o = new Options ();
671
707
o .addOption ( IGNORE_FAILURES , false , "ignore failures" );
672
708
o .addOption ( PRINTSTACKTRACE , false , "print stacktrace" );
673
709
o .addOption ( RUN_MODE , true , "runmode, possible values: " + Arrays .toString ( RunMode .values () ) );
674
710
o .addOption ( USER_NAME , true , "user name" );
675
711
o .addOption ( NO_VALIDATE_XML , false , "disables XML file with alter script validation" );
676
712
o .addOption ( NO_LOG_TABLE , false , "disables log table" );
713
+ o .addOption (INC_MODE , false , "incremental mode" );
677
714
678
715
boolean ignfail = false ;
679
716
boolean printstacktrace = false ;
0 commit comments