Skip to content

Commit c7b629e

Browse files
committed
Fix so that switch-only works by finding the newest version
1 parent 720a6f2 commit c7b629e

File tree

4 files changed

+67
-41
lines changed

4 files changed

+67
-41
lines changed

src/main/java/nl/topicus/mssql2monetdb/CopyTable.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,11 @@ public class CopyTable
4545

4646
// copies the table to a temp table and then replaces the 'to' table with the temp
4747
// table to reduce down-time
48-
private boolean copyViaTempTable = true;
48+
private boolean copyViaTempTable = false;
4949

5050
// prefix of the temp table that is created
5151
private String tempTablePrefix = "tmp_";
5252

53-
private String backupTablePrefix = "backup_";
54-
55-
private String currentTablePrefix = "current_";
56-
5753
// this will create views with the table.example.to name and will backup your table to
5854
// a backup table this will make it possible to switch the underlying table of the
5955
// view when data copying is complete resulting in almost no down-time of your
@@ -190,15 +186,7 @@ public void setTempTablePrefix(String tempTablePrefix)
190186
this.tempTablePrefix = tempTablePrefix;
191187
}
192188

193-
public String getCurrentTablePrefix()
194-
{
195-
return currentTablePrefix;
196-
}
197189

198-
public void setCurrentTablePrefix(String currentTablePrefix)
199-
{
200-
this.currentTablePrefix = currentTablePrefix;
201-
}
202190

203191
public boolean isUseFastViewSwitching()
204192
{
@@ -209,16 +197,6 @@ public void setUseFastViewSwitching(boolean useViews)
209197
{
210198
this.useFastViewSwitching = useViews;
211199
}
212-
213-
public String getBackupTablePrefix()
214-
{
215-
return backupTablePrefix;
216-
}
217-
218-
public void setBackupTablePrefix(String backupTablePrefix)
219-
{
220-
this.backupTablePrefix = backupTablePrefix;
221-
}
222200

223201
public void setUseLockedMode(boolean useLockedMode)
224202
{

src/main/java/nl/topicus/mssql2monetdb/CopyTool.java

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.text.SimpleDateFormat;
2828
import java.util.Calendar;
2929
import java.util.HashMap;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
3032

3133
import nl.cwi.monetdb.mcl.io.BufferedMCLReader;
3234
import 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

src/main/java/nl/topicus/mssql2monetdb/CopyToolConfig.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,6 @@ else if (key.equals("temptableprefix"))
631631
{
632632
table.setTempTablePrefix(propValue);
633633
}
634-
else if (key.equals("backuptableprefix"))
635-
{
636-
table.setBackupTablePrefix(propValue);
637-
}
638-
else if (key.equals("currenttableprefix"))
639-
{
640-
table.setCurrentTablePrefix(propValue);
641-
}
642634
else if (key.equals("usefastviewswitching"))
643635
{
644636
table.setUseFastViewSwitching(boolValue);

src/main/java/nl/topicus/mssql2monetdb/MonetDBTable.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public String getNameWithPrefixes()
3232
{
3333
if (tempTable)
3434
return copyTable.getTempTablePrefix() + name;
35-
else if (backupTable)
36-
return copyTable.getBackupTablePrefix() + name;
3735
else if (copyTable.isUseFastViewSwitching())
3836
return name + "_" + copyTable.getLoadDate();
3937

0 commit comments

Comments
 (0)