]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-6336: DROP DATABASE commands are now written to SQL script that initializes...
authorAron Roberts <aron@socrates.berkeley.edu>
Tue, 18 Mar 2014 22:00:08 +0000 (15:00 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Tue, 18 Mar 2014 22:00:08 +0000 (15:00 -0700)
services/common-api/src/main/java/org/collectionspace/services/common/api/FileTools.java
services/common/src/main/java/org/collectionspace/services/common/ServiceMain.java
src/main/resources/db/postgresql/init_nuxeo_db.sql

index 61d71b6dc7f39ace1aebec11a2cfbc636e7c0d47..b7d65957ec99693088cd4e36aede3eec790c61e5 100644 (file)
@@ -31,10 +31,10 @@ package org.collectionspace.services.common.api;
 import java.io.*;\r
 import java.nio.charset.Charset;\r
 import java.nio.file.Files;\r
-import java.nio.file.OpenOption;\r
 import java.nio.file.Path;\r
 import java.nio.file.Paths;\r
 import java.nio.file.StandardOpenOption;\r
+import java.util.ArrayList;\r
 import java.util.List;\r
 import java.util.UUID;\r
 \r
@@ -50,7 +50,7 @@ public class FileTools {
     \r
     public static String DEFAULT_ENCODING = "";\r
     public static String UTF8_ENCODING = "UTF-8";\r
-    public static Charset UTF8_CHARSET = Charset.forName(UTF8_ENCODING);\r
+    public static Charset UTF8_CHARSET = java.nio.charset.StandardCharsets.UTF_8;\r
     public static boolean FORCE_CREATE_PARENT_DIRS = true;\r
     private static String JAVA_TEMP_DIR_PROPERTY = "java.io.tmpdir";\r
 \r
@@ -178,7 +178,7 @@ public class FileTools {
     }\r
     \r
     public static List<String> readFileAsLines(String filePath) {\r
-        List<String> lines = null;\r
+        List<String> lines = new ArrayList<String>();\r
         try {\r
             Path path = Paths.get(filePath);\r
             lines = Files.readAllLines(path, UTF8_CHARSET);\r
@@ -192,7 +192,7 @@ public class FileTools {
     public static void writeFileFromLines(String filePath, Iterable<? extends CharSequence> lines) {\r
         try {\r
             Path path = Paths.get(filePath);\r
-            Files.write(path, lines, UTF8_CHARSET, StandardOpenOption.WRITE);\r
+            Files.write(path, lines, UTF8_CHARSET, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);\r
         } catch (Exception e) {\r
             System.out.println("ERROR: " + e);\r
         }\r
index 54c1632f975f722ceed12294d71d4043974f4d17..c6d0b8e2c95325750297e8b5ddf22b3b4cbb27a0 100644 (file)
@@ -79,6 +79,11 @@ public class ServiceMain {
             String.format(COMPONENT_EXTENSION_XPATH, "repository");\r
     private static final String REPOSITORIES_EXTENSION_POINT_XPATH =\r
             String.format(COMPONENT_EXTENSION_XPATH, "repositories");\r
+    \r
+    private static final String DROP_DATABASE_SQL_CMD = "DROP DATABASE";\r
+    private static final String DROP_DATABASE_IF_EXISTS_SQL_CMD = DROP_DATABASE_SQL_CMD + " IF EXISTS %s";\r
+    private static final String DROP_OBJECTS_SQL_COMMENT = "-- drop all the objects before dropping roles";\r
+\r
             \r
     private ServiceMain() {\r
        //empty\r
@@ -884,6 +889,14 @@ public class ServiceMain {
         return repoConfigDoc;\r
     }\r
 \r
+    /**\r
+     * Update the current copy of the Nuxeo databases initialization script file,\r
+     * by removing all existing DROP DATABASE commands, then adding a DROP DATABASE\r
+     * command for each current database, at the top of that file.\r
+     * \r
+     * @param dbInitializationScriptFilePath\r
+     * @param dbsCheckedOrCreated \r
+     */\r
     private void updateInitializationScript(String dbInitializationScriptFilePath,\r
             HashSet<String> dbsCheckedOrCreated) {\r
         // Get the current copy of the Nuxeo databases initialization script file,\r
@@ -898,21 +911,33 @@ public class ServiceMain {
                 logger.warn(msg);\r
             } else {\r
                 // Note: Exceptions are written only to the console, not thrown, by\r
-                // this method in the common-api package.\r
+                // the readFileAsLines() method in the common-api package.\r
                 lines = FileTools.readFileAsLines(dbInitializationScriptFilePath);\r
-                for (String line : lines) {\r
-                    // Elide current DROP DATABASE statements\r
-                    if (line.toLowerCase().contains("DROP DATABASE".toLowerCase())) {\r
-                        continue;\r
+                Iterator<String> linesIterator = lines.iterator();\r
+                String currentLine;\r
+                while (linesIterator.hasNext()) {\r
+                    currentLine = linesIterator.next();\r
+                    // Elide all existing DROP DATABASE statements.\r
+                    if (currentLine.toLowerCase().contains(DROP_DATABASE_SQL_CMD.toLowerCase())) {\r
+                        linesIterator.remove();\r
+                    }\r
+                    // Elide a comment pertaining to the existing\r
+                    // DROP DATABASE statements.\r
+                    if (currentLine.toLowerCase().contains(DROP_OBJECTS_SQL_COMMENT.toLowerCase())) {\r
+                        linesIterator.remove();\r
                     }\r
                 }\r
             }\r
-            // Write new DROP DATABASE lines for every Nuxeo-managed database.\r
             List<String> replacementLines = new ArrayList<String>();\r
+            // Add back the comment elided above\r
+            replacementLines.add(DROP_OBJECTS_SQL_COMMENT);\r
+            // Add new DROP DATABASE lines for every Nuxeo-managed database.\r
             for (String dbName : dbsCheckedOrCreated) {\r
-              replacementLines.add("DROP DATABASE IF EXISTS " + dbName);\r
+              replacementLines.add(String.format(DROP_DATABASE_IF_EXISTS_SQL_CMD, dbName));\r
             }\r
-            // Append all (non-DROP DATABASE) existing lines from that file, if any.\r
+            replacementLines.add(System.getProperty("line.separator"));\r
+            // Now append all existing lines from that file, except for\r
+            // any lines that were elided above.\r
             if (lines != null && ! lines.isEmpty()) {\r
                 replacementLines.addAll(lines);\r
             }\r
@@ -922,7 +947,7 @@ public class ServiceMain {
                 logger.warn(msg);\r
             } else {\r
                 // Note: Exceptions are written only to the console, not thrown, by\r
-                // this method in the common-api package.\r
+                // the writeFileFromLines() method in the common-api package.\r
                 FileTools.writeFileFromLines(dbInitializationScriptFilePath, replacementLines);\r
             }\r
         } catch (Exception e) {\r
index 21eddbdbb387cbbb80655835e234e435cf7d9740..5bb820ff9f1853a778fa94e8775550e54b006f61 100644 (file)
@@ -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