Skip to content
Snippets Groups Projects
Commit dbf58925 authored by tpylak's avatar tpylak
Browse files

fixes in test database migration

SVN: 4043
parent 00b0a429
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package ch.systemsx.cisd.dbmigration; package ch.systemsx.cisd.dbmigration;
import java.io.File; import java.io.File;
import java.io.IOException;
import ch.systemsx.cisd.common.Script; import ch.systemsx.cisd.common.Script;
...@@ -33,11 +34,14 @@ public interface ISqlScriptProvider ...@@ -33,11 +34,14 @@ public interface ISqlScriptProvider
*/ */
public boolean isDumpRestore(String version); public boolean isDumpRestore(String version);
/** marks that this script provider is suitable for a dump restore */
public void markAsDumpRestorable(String version) throws IOException;
/** /**
* Returns the folder where all dump files for <var>version</var> reside. * Returns the folder where all dump files for <var>version</var> reside.
*/ */
public File getDumpFolder(String version); public File getDumpFolder(String version);
/** /**
* Returns the script to create database schemas. * Returns the script to create database schemas.
* *
......
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
package ch.systemsx.cisd.dbmigration; package ch.systemsx.cisd.dbmigration;
import java.io.File; import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.Script; import ch.systemsx.cisd.common.Script;
...@@ -40,9 +43,9 @@ public class SqlScriptProvider implements ISqlScriptProvider ...@@ -40,9 +43,9 @@ public class SqlScriptProvider implements ISqlScriptProvider
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, SqlScriptProvider.class); private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, SqlScriptProvider.class);
private final String genericScriptFolder; private final String genericScriptFolder;
private final String specificScriptFolder; private final String specificScriptFolder;
/** /**
* Creates an instance for the specified script folders. They are either resource folders or folders relative to the * Creates an instance for the specified script folders. They are either resource folders or folders relative to the
* working directory. * working directory.
...@@ -56,13 +59,23 @@ public class SqlScriptProvider implements ISqlScriptProvider ...@@ -56,13 +59,23 @@ public class SqlScriptProvider implements ISqlScriptProvider
this.genericScriptFolder = schemaScriptRootFolder + "/generic"; this.genericScriptFolder = schemaScriptRootFolder + "/generic";
this.specificScriptFolder = schemaScriptRootFolder + "/" + databaseEngineCode; this.specificScriptFolder = schemaScriptRootFolder + "/" + databaseEngineCode;
} }
/** /**
* Returns <code>true</code> if a &lt;finish script&gt; is found and <code>false</code> otherwise. * Returns <code>true</code> if a &lt;finish script&gt; is found and <code>false</code> otherwise.
*/ */
public boolean isDumpRestore(String version) public boolean isDumpRestore(String version)
{ {
return new File(getDumpFolder(version), DUMP_FILENAME).exists(); return getDumprestoreFile(version).exists();
}
public void markAsDumpRestorable(String version) throws IOException
{
FileUtils.touch(getDumprestoreFile(version));
}
private File getDumprestoreFile(String version)
{
return new File(getDumpFolder(version), DUMP_FILENAME);
} }
/** /**
...@@ -126,13 +139,13 @@ public class SqlScriptProvider implements ISqlScriptProvider ...@@ -126,13 +139,13 @@ public class SqlScriptProvider implements ISqlScriptProvider
{ {
return tryLoadScript(scriptName, scriptVersion, scriptVersion); return tryLoadScript(scriptName, scriptVersion, scriptVersion);
} }
private Script tryLoadScript(String scriptName, String scriptVersion, String prefix) private Script tryLoadScript(String scriptName, String scriptVersion, String prefix)
{ {
Script script = tryPrimLoadScript(specificScriptFolder + "/" + prefix, scriptName, scriptVersion); Script script = tryPrimLoadScript(specificScriptFolder + "/" + prefix, scriptName, scriptVersion);
if (script == null) if (script == null)
{ {
script = tryPrimLoadScript(genericScriptFolder + "/" + prefix, scriptName, scriptVersion); script = tryPrimLoadScript(genericScriptFolder + "/" + prefix, scriptName, scriptVersion);
} }
return script; return script;
} }
......
...@@ -39,20 +39,19 @@ import javax.swing.JFileChooser; ...@@ -39,20 +39,19 @@ import javax.swing.JFileChooser;
/** /**
* Helper application which creates 'mass upload' files to be used to setup the database from a PostgreSQL dump. * Helper application which creates 'mass upload' files to be used to setup the database from a PostgreSQL dump.
* <p> * <p>
* <b>Important:</b> When creating the dump file with <code>pg_dump</code> use the option <b>--no-owner</b>. * <b>Important:</b> When creating the dump file with <code>pg_dump</code> use the option <b>--no-owner</b>.
* <p> * <p>
* Note, that this is a stand alone class which only uses J2EE library classes. * Note, that this is a stand alone class which only uses J2EE library classes.
* *
* @author Franz-Josef Elmer * @author Franz-Josef Elmer
*/ */
public class DumpPreparator public class DumpPreparator
{ {
private static final Set<String> FILTERED_SCHEMA_LINES = private static final Set<String> FILTERED_SCHEMA_LINES =
new LinkedHashSet<String>(Arrays.asList("SET client_encoding = 'UTF8';", new LinkedHashSet<String>(Arrays.asList("SET client_encoding = 'UTF8';",
"COMMENT ON SCHEMA public IS 'Standard public schema';", "COMMENT ON SCHEMA public IS 'Standard public schema';", "CREATE PROCEDURAL LANGUAGE plpgsql;"));
"CREATE PROCEDURAL LANGUAGE plpgsql;"));
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
if (args.length < 1) if (args.length < 1)
...@@ -85,8 +84,8 @@ public class DumpPreparator ...@@ -85,8 +84,8 @@ public class DumpPreparator
/** /**
* Creates all files necessary to setup a database from the specified PostgreSQL dump file. * Creates all files necessary to setup a database from the specified PostgreSQL dump file.
* *
* @param destination Destination folder in which the folder with the files will be created. The folder * @param destination Destination folder in which the folder with the files will be created. The folder will be
* will be named after the database version extracted from the dump. * named after the database version extracted from the dump.
*/ */
public static void createUploadFiles(File dumpFile, File destination) throws IOException public static void createUploadFiles(File dumpFile, File destination) throws IOException
{ {
...@@ -94,11 +93,12 @@ public class DumpPreparator ...@@ -94,11 +93,12 @@ public class DumpPreparator
try try
{ {
createUploadFiles(reader, destination); createUploadFiles(reader, destination);
} finally { } finally
{
reader.close(); reader.close();
} }
} }
static void createUploadFiles(Reader dumpReader, File destinationFolder) throws IOException static void createUploadFiles(Reader dumpReader, File destinationFolder) throws IOException
{ {
BufferedReader reader = new BufferedReader(dumpReader); BufferedReader reader = new BufferedReader(dumpReader);
...@@ -114,7 +114,7 @@ public class DumpPreparator ...@@ -114,7 +114,7 @@ public class DumpPreparator
} }
uploadFileManager.save(); uploadFileManager.save();
} }
private static enum State private static enum State
{ {
SCHEMA() SCHEMA()
...@@ -139,12 +139,13 @@ public class DumpPreparator ...@@ -139,12 +139,13 @@ public class DumpPreparator
if (matcher.matches()) if (matcher.matches())
{ {
manager.createUploadFile(matcher.group(1)); manager.createUploadFile(matcher.group(1));
} else { } else
{
throw new IllegalArgumentException("Couldn't extract table name from the following line: " + line); throw new IllegalArgumentException("Couldn't extract table name from the following line: " + line);
} }
return IN_COPY; return IN_COPY;
} }
}, },
IN_COPY IN_COPY
{ {
@Override @Override
...@@ -174,7 +175,7 @@ public class DumpPreparator ...@@ -174,7 +175,7 @@ public class DumpPreparator
return this; return this;
} }
}; };
private static final Pattern COPY_PATTERN = Pattern.compile("COPY (\\w*).*"); private static final Pattern COPY_PATTERN = Pattern.compile("COPY (\\w*).*");
State processLine(String line, UploadFileManager manager) State processLine(String line, UploadFileManager manager)
...@@ -183,22 +184,29 @@ public class DumpPreparator ...@@ -183,22 +184,29 @@ public class DumpPreparator
} }
} }
private static class UploadFileManager private static class UploadFileManager
{ {
private static final String VERSION_LOGS_TABLE = "database_version_logs"; private static final String VERSION_LOGS_TABLE = "database_version_logs";
private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d{3})\\t.*"); private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d{3})\\t.*");
private final File destinationFolder; private final File destinationFolder;
private final Set<String> filteredSchemaLines; private final Set<String> filteredSchemaLines;
private final StringWriter schemaScript = new StringWriter(); private final StringWriter schemaScript = new StringWriter();
private final PrintWriter schemaPrinter = new PrintWriter(schemaScript, true); private final PrintWriter schemaPrinter = new PrintWriter(schemaScript, true);
private final StringWriter finishScript = new StringWriter(); private final StringWriter finishScript = new StringWriter();
private final PrintWriter finishPrinter = new PrintWriter(finishScript, true); private final PrintWriter finishPrinter = new PrintWriter(finishScript, true);
private final Map<String, Table> tables = new LinkedHashMap<String, Table>(); private final Map<String, Table> tables = new LinkedHashMap<String, Table>();
private Table currentTable; private Table currentTable;
UploadFileManager(File destinationFolder, Set<String> filteredSchemaLines) UploadFileManager(File destinationFolder, Set<String> filteredSchemaLines)
{ {
this.destinationFolder = destinationFolder; this.destinationFolder = destinationFolder;
...@@ -212,7 +220,7 @@ public class DumpPreparator ...@@ -212,7 +220,7 @@ public class DumpPreparator
schemaPrinter.println(line); schemaPrinter.println(line);
} }
} }
void addFinishLine(String line) void addFinishLine(String line)
{ {
finishPrinter.println(line); finishPrinter.println(line);
...@@ -223,7 +231,7 @@ public class DumpPreparator ...@@ -223,7 +231,7 @@ public class DumpPreparator
currentTable = new Table(tableName); currentTable = new Table(tableName);
tables.put(tableName, currentTable); tables.put(tableName, currentTable);
} }
boolean addTableLine(String line) boolean addTableLine(String line)
{ {
if (currentTable == null) if (currentTable == null)
...@@ -235,13 +243,13 @@ public class DumpPreparator ...@@ -235,13 +243,13 @@ public class DumpPreparator
return true; return true;
} }
currentTable.addRow(line); currentTable.addRow(line);
return false; return false;
} }
void save() throws IOException void save() throws IOException
{ {
String databaseVersion = getDatabaseVersion(); String databaseVersion = getDatabaseVersion();
File folder = createFolder(databaseVersion); File folder = createDestinationFolder();
writeTo(folder, "schema-" + databaseVersion + ".sql", Arrays.asList(schemaScript.toString())); writeTo(folder, "schema-" + databaseVersion + ".sql", Arrays.asList(schemaScript.toString()));
for (Table table : tables.values()) for (Table table : tables.values())
{ {
...@@ -276,9 +284,9 @@ public class DumpPreparator ...@@ -276,9 +284,9 @@ public class DumpPreparator
} }
} }
private File createFolder(String databaseVersion) private File createDestinationFolder()
{ {
File folder = new File(destinationFolder, databaseVersion); File folder = destinationFolder;
if (folder.exists()) if (folder.exists())
{ {
if (folder.isDirectory()) if (folder.isDirectory())
...@@ -317,7 +325,7 @@ public class DumpPreparator ...@@ -317,7 +325,7 @@ public class DumpPreparator
} }
return folder; return folder;
} }
private String getDatabaseVersion() private String getDatabaseVersion()
{ {
Table logTable = tables.get(VERSION_LOGS_TABLE); Table logTable = tables.get(VERSION_LOGS_TABLE);
...@@ -343,19 +351,20 @@ public class DumpPreparator ...@@ -343,19 +351,20 @@ public class DumpPreparator
return result; return result;
} }
} }
private static class Table private static class Table
{ {
private static int counter; private static int counter;
private final String uploadFileName; private final String uploadFileName;
private final List<String> rows = new ArrayList<String>(); private final List<String> rows = new ArrayList<String>();
Table(String tableName) Table(String tableName)
{ {
uploadFileName = String.format("%03d=%s.tsv", ++counter, tableName); uploadFileName = String.format("%03d=%s.tsv", ++counter, tableName);
} }
void addRow(String line) void addRow(String line)
{ {
rows.add(line); rows.add(line);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment