From: Aron Roberts Date: Tue, 18 Mar 2014 22:00:08 +0000 (-0700) Subject: CSPACE-6336: DROP DATABASE commands are now written to SQL script that initializes... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=ab1856d0fc44c7e25ea4e5ae6adfe6e74e3b2f45;p=tmp%2Fjakarta-migration.git CSPACE-6336: DROP DATABASE commands are now written to SQL script that initializes Nuxeo databases. --- diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java index 61d71b6dc..b7d65957e 100644 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java @@ -31,10 +31,10 @@ package org.collectionspace.services.common.api; import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; -import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -50,7 +50,7 @@ public class FileTools { public static String DEFAULT_ENCODING = ""; public static String UTF8_ENCODING = "UTF-8"; - public static Charset UTF8_CHARSET = Charset.forName(UTF8_ENCODING); + public static Charset UTF8_CHARSET = java.nio.charset.StandardCharsets.UTF_8; public static boolean FORCE_CREATE_PARENT_DIRS = true; private static String JAVA_TEMP_DIR_PROPERTY = "java.io.tmpdir"; @@ -178,7 +178,7 @@ public class FileTools { } public static List readFileAsLines(String filePath) { - List lines = null; + List lines = new ArrayList(); try { Path path = Paths.get(filePath); lines = Files.readAllLines(path, UTF8_CHARSET); @@ -192,7 +192,7 @@ public class FileTools { public static void writeFileFromLines(String filePath, Iterable lines) { try { Path path = Paths.get(filePath); - Files.write(path, lines, UTF8_CHARSET, StandardOpenOption.WRITE); + Files.write(path, lines, UTF8_CHARSET, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); } catch (Exception e) { System.out.println("ERROR: " + e); } diff --git a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java index 54c1632f9..c6d0b8e2c 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java @@ -79,6 +79,11 @@ public class ServiceMain { String.format(COMPONENT_EXTENSION_XPATH, "repository"); private static final String REPOSITORIES_EXTENSION_POINT_XPATH = String.format(COMPONENT_EXTENSION_XPATH, "repositories"); + + private static final String DROP_DATABASE_SQL_CMD = "DROP DATABASE"; + private static final String DROP_DATABASE_IF_EXISTS_SQL_CMD = DROP_DATABASE_SQL_CMD + " IF EXISTS %s"; + private static final String DROP_OBJECTS_SQL_COMMENT = "-- drop all the objects before dropping roles"; + private ServiceMain() { //empty @@ -884,6 +889,14 @@ public class ServiceMain { return repoConfigDoc; } + /** + * Update the current copy of the Nuxeo databases initialization script file, + * by removing all existing DROP DATABASE commands, then adding a DROP DATABASE + * command for each current database, at the top of that file. + * + * @param dbInitializationScriptFilePath + * @param dbsCheckedOrCreated + */ private void updateInitializationScript(String dbInitializationScriptFilePath, HashSet dbsCheckedOrCreated) { // Get the current copy of the Nuxeo databases initialization script file, @@ -898,21 +911,33 @@ public class ServiceMain { logger.warn(msg); } else { // Note: Exceptions are written only to the console, not thrown, by - // this method in the common-api package. + // the readFileAsLines() method in the common-api package. lines = FileTools.readFileAsLines(dbInitializationScriptFilePath); - for (String line : lines) { - // Elide current DROP DATABASE statements - if (line.toLowerCase().contains("DROP DATABASE".toLowerCase())) { - continue; + Iterator linesIterator = lines.iterator(); + String currentLine; + while (linesIterator.hasNext()) { + currentLine = linesIterator.next(); + // Elide all existing DROP DATABASE statements. + if (currentLine.toLowerCase().contains(DROP_DATABASE_SQL_CMD.toLowerCase())) { + linesIterator.remove(); + } + // Elide a comment pertaining to the existing + // DROP DATABASE statements. + if (currentLine.toLowerCase().contains(DROP_OBJECTS_SQL_COMMENT.toLowerCase())) { + linesIterator.remove(); } } } - // Write new DROP DATABASE lines for every Nuxeo-managed database. List replacementLines = new ArrayList(); + // Add back the comment elided above + replacementLines.add(DROP_OBJECTS_SQL_COMMENT); + // Add new DROP DATABASE lines for every Nuxeo-managed database. for (String dbName : dbsCheckedOrCreated) { - replacementLines.add("DROP DATABASE IF EXISTS " + dbName); + replacementLines.add(String.format(DROP_DATABASE_IF_EXISTS_SQL_CMD, dbName)); } - // Append all (non-DROP DATABASE) existing lines from that file, if any. + replacementLines.add(System.getProperty("line.separator")); + // Now append all existing lines from that file, except for + // any lines that were elided above. if (lines != null && ! lines.isEmpty()) { replacementLines.addAll(lines); } @@ -922,7 +947,7 @@ public class ServiceMain { logger.warn(msg); } else { // Note: Exceptions are written only to the console, not thrown, by - // this method in the common-api package. + // the writeFileFromLines() method in the common-api package. FileTools.writeFileFromLines(dbInitializationScriptFilePath, replacementLines); } } catch (Exception e) { diff --git a/src/main/resources/db/postgresql/init_nuxeo_db.sql b/src/main/resources/db/postgresql/init_nuxeo_db.sql index 21eddbdbb..5bb820ff9 100644 --- a/src/main/resources/db/postgresql/init_nuxeo_db.sql +++ b/src/main/resources/db/postgresql/init_nuxeo_db.sql @@ -23,5 +23,4 @@ DROP USER IF EXISTS @DB_READER_USER@; -- GRANT SELECT ON ALL TABLES IN SCHEMA public TO reader; -- This must be run by hand, after the system has already started up, --- so that it gives access to all the tables created on init. - +-- so that it gives access to all the tables created on init. \ No newline at end of file