Guest User

Untitled

a guest
Apr 6th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /**
  2. * USE AT YOUR OWN RISK - BACKUP THE DATABASE FIRST!
  3. * 1) Download the additional report ZIP files from the Reporting Module
  4. * 2) Place them in a separate directory
  5. * 3) Place this file in the same directory
  6. * 4) Copy the directory to the Postgres server
  7. * 5) Change the information under the START CHANGE THIS heading
  8. * 6) Run jjs from Java 8 or later from same directory:
  9. * jjs -scripting installreports.js
  10. */
  11.  
  12. //Declare Java stuff
  13. var FilesSystem = Java.type("java.nio.file.FileSystem");
  14. var FileSystems = Java.type("java.nio.file.FileSystems")
  15. var Files = Java.type("java.nio.file.Files")
  16. var Path = Java.type("java.nio.file.Path")
  17. var Paths = Java.type("java.nio.file.Paths")
  18. var URI = Java.type("java.net.URI")
  19. var HashMap = Java.type("java.util.HashMap")
  20. var FileVisitOption = Java.type("java.nio.file.FileVisitOption")
  21. var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption")
  22. var emptyMap = new HashMap()
  23.  
  24. var JavaStringArray = Java.type("java.lang.String[]")
  25. var emptyStringArray = new JavaStringArray(0)
  26. //End declare Java stuff
  27.  
  28.  
  29. /**
  30. * START CHANGE THIS
  31. */
  32. var pgHostname = "localhost"
  33. var pgPort = "5432"
  34. var pgDbName = "igops"
  35. var pgUsername = "postgres"
  36. var pgPassword = "12345"
  37. var psqlPath = "C:\\netiq\\idm\\apps\\postgres\\bin\\psql.exe"
  38. var workingDir = Paths.get("C:\\Users\\svciga\\Downloads\\ReportingAdditional")
  39. /**
  40. * END CHANGE THIS
  41. */
  42.  
  43. //Run psql without asking for password
  44. $ENV.PGPASSWORD = pgPassword
  45.  
  46.  
  47.  
  48. /**
  49. * 1) Get all files whose filename ends with .zip
  50. * 2) From each ZIP file find the file whose filename starts with "pg-" and ends with ".sql"
  51. * 3) Extract the file
  52. * 4) Call psql to apply the changes to the Postgres database
  53. */
  54. var findZipFiles = Files.walk(workingDir)
  55. try {
  56.  
  57. findZipFiles.filter(function(zip) {
  58. return zip.getFileName().toString().endsWith(".zip") } ).forEach(function(file) {
  59. var toUri = file.toUri()
  60. var uri = URI.create("jar:" + toUri.toString())
  61. var zipFileSystem = FileSystems.newFileSystem(uri, emptyMap)
  62.  
  63. print("Processing file: " + file.toString())
  64.  
  65. var zipStream = Files.walk(zipFileSystem.getPath("/", emptyStringArray))
  66.  
  67. zipStream.filter(function(file) {
  68.  
  69. return file.getFileName() != null &&
  70. file.getFileName().toString().startsWith("pg-") &&
  71. file.getFileName().toString().endsWith(".sql")
  72.  
  73. }).forEach(function(sqlFile) {
  74. print(sqlFile)
  75. var destination = Paths.get(workingDir.toString(), sqlFile.getFileName())
  76. Files.copy(sqlFile, destination, StandardCopyOption.REPLACE_EXISTING)
  77. runpsql(destination.toString())
  78. })
  79.  
  80. })
  81.  
  82.  
  83. } catch(e) {
  84. print(e)
  85. }
  86.  
  87. function runpsql(file) {
  88. $EXEC("${psqlPath} -U ${pgUsername} -d ${pgDbName} -f ${file} -h ${pgHostname} -p ${pgPort}")
  89. }
Add Comment
Please, Sign In to add comment