2727import java .text .SimpleDateFormat ;
2828import java .util .Calendar ;
2929import java .util .HashMap ;
30+ import java .util .regex .Matcher ;
31+ import java .util .regex .Pattern ;
3032
3133import nl .cwi .monetdb .mcl .io .BufferedMCLReader ;
3234import nl .cwi .monetdb .mcl .io .BufferedMCLWriter ;
@@ -53,6 +55,8 @@ public class CopyTool
5355 private Object lastRunValue ;
5456
5557 private int lastRunColType ;
58+
59+ private Pattern versionPattern = Pattern .compile ("[0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}$" );
5660
5761 /**
5862 * @param args
@@ -82,9 +86,7 @@ public static void main(String[] args)
8286 System .exit (1 );
8387 }
8488 }
85-
86-
87-
89+
8890 public void run (CopyToolConfig config ) throws CopyToolException
8991 {
9092 if (config == null )
@@ -231,15 +233,29 @@ private void doCopy () throws CopyToolException
231233 else {
232234 LOG .info ("Switch-Only requested with flag. Therefore, PHASE 1 and 2 skipped" );
233235 }
234-
236+
235237 //if switch-ONly flag set or when the no-switch-Flag is NOT set, than switching
236238 if (switchOnly || !noSwitch ){
237239
238240 LOG .info ("STARTING PHASE 3: switching all view-based tables to new data" );
239241
242+ // when switching only we need to find the latest load date for each table
243+ if (switchOnly )
244+ {
245+ for (CopyTable copyTable : tablesToCopy .values ())
246+ {
247+ // find latest version of table
248+ try {
249+ String newestVersion = findNewestTable (copyTable );
250+ copyTable .setLoadDate (newestVersion );
251+ } catch (SQLException e ) {
252+ LOG .warn ("Unable to find newest version of table '" + copyTable .getToName () + "'" );
253+ }
254+ }
255+ }
256+
240257 // we need another loop through the tables for temp table copying and view
241258 // switching. We do this after the copy actions to reduce down-time
242-
243259 // phase 3: switch views (for view-based tables)
244260 for (CopyTable copyTable : tablesToCopy .values ())
245261 {
@@ -256,8 +272,15 @@ private void doCopy () throws CopyToolException
256272 // set view to current table because it contains the new data now
257273 if (copyTable .isUseFastViewSwitching ())
258274 {
259- MonetDBUtil .dropAndRecreateViewForTable (copyTable .getSchema (),
260- copyTable .getToName (), copyTable .getCurrentTable ());
275+ if (StringUtils .isEmpty (copyTable .getLoadDate ()))
276+ {
277+ LOG .error ("Unable to switch view of table '" + copyTable .getToName () + "' due to missing load date" );
278+ }
279+ else
280+ {
281+ MonetDBUtil .dropAndRecreateViewForTable (copyTable .getSchema (),
282+ copyTable .getToName (), copyTable .getCurrentTable ());
283+ }
261284 }
262285 }
263286 catch (SQLException e )
@@ -267,7 +290,6 @@ private void doCopy () throws CopyToolException
267290 EmailUtil .sendMail ("Unable to create view" + copyTable .getToViewSql () + " with the following error: " + e .toString (), "Unable to create view in monetdb" , config .getDatabaseProperties ());
268291 }
269292 }
270-
271293 LOG .info ("PHASE 3 FINISHED: all views have been switched" );
272294
273295
@@ -503,6 +525,42 @@ private boolean writeTriggerInfo (Object newValue, int colType)
503525
504526 }
505527
528+ /**
529+ * Find newest version of tables
530+ * @throws SQLException
531+ */
532+ private String findNewestTable (CopyTable table ) throws SQLException
533+ {
534+ Statement q =
535+ CopyToolConnectionManager .getInstance ().getMonetDbConnection ().createStatement ();
536+
537+ ResultSet result =
538+ q .executeQuery ("SELECT name FROM sys.tables WHERE name LIKE '" + table .getToName ()
539+ + "_20%_%' AND name <> '" + table .getToName () + "' "
540+ + "AND schema_id = (SELECT id from sys.schemas WHERE name = '" + table .getSchema ()
541+ + "') AND query IS NULL ORDER BY name DESC" );
542+
543+ String version = "" ;
544+
545+ if (result .next ())
546+ {
547+ String name = result .getString ("name" );
548+ Matcher matcher = versionPattern .matcher (name );
549+
550+ if (matcher .find ())
551+ {
552+ version = matcher .group ();
553+ }
554+
555+
556+ }
557+
558+ result .close ();
559+ q .close ();
560+
561+ return version ;
562+ }
563+
506564 /**
507565 * Drops older versions of tables that are no longer used by the view.
508566 * @throws SQLException
0 commit comments