Skip to content

Commit

Permalink
improved logging; moved utiltiy methods to util class
Browse files Browse the repository at this point in the history
  • Loading branch information
isapir committed Jul 20, 2018
1 parent 94f6127 commit 9de9774
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 115 deletions.
29 changes: 10 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.twentyonesolutions</groupId>
<artifactId>Migrate2Postgres</artifactId>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.5</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -72,28 +72,19 @@
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.twentyonesolutions.m2pg.PgMigrator</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>net.twentyonesolutions.m2pg.PgMigrator</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
!-->
</plugins>
</build>

Expand Down
100 changes: 50 additions & 50 deletions src/main/java/net/twentyonesolutions/m2pg/PgMigrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.sql.SQLException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -25,13 +23,12 @@
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;

public class PgMigrator {

public final static String DISCL = "Migrate2Postgres Copyright (C) 2018 Igal Sapir\n"
public final static String DISCL = getProductName() + " Copyright (C) 2018 Igal Sapir\n"
+ "This program comes with ABSOLUTELY NO WARRANTY;\n"
+ "This is free software, and you are welcome to redistribute it\n"
+ "under certain conditions;\n"
Expand Down Expand Up @@ -107,6 +104,33 @@ public static void doDml(Schema schema, String filename) throws IOException, SQL

long tc = System.currentTimeMillis();

List<String> queries = null;
String logentry;
boolean completed = false;
Path path = Paths.get(filename);
Util.log(path, getBanner());

queries = (List<String>)schema.config.dml.getOrDefault("execute.before_all", Collections.EMPTY_LIST);
if (!queries.isEmpty()){

logentry = "-- executing queries execute.before_all";
System.out.println("\n" + logentry);
Util.log(path, logentry);

StringBuilder log = new StringBuilder(1024);
completed = Util.executeQueries(queries, log, schema.config);
Util.log(path, log.toString());

if (!completed){
Util.log(path, "!!!ABORTING!!!");
System.exit(1);
}

logentry = "-- completed queries execute.before_all";
System.out.println("\n" + logentry);
Util.log(path, logentry);
}

int numThreads = 1;

Object arg = schema.config.dml.get("threads");
Expand Down Expand Up @@ -147,37 +171,12 @@ else if (arg instanceof String){
executorService.execute(task);
}

Path path = Paths.get(filename);
log(path, getBanner());

List<String> queries = null;
String logentry;

queries = (List<String>)schema.config.dml.getOrDefault("execute.before_all", Collections.EMPTY_LIST);
if (!queries.isEmpty()){

logentry = "-- executing queries execute.before_all";
System.out.println("\n" + logentry);
log(path, logentry);

try {
StringBuilder log = new StringBuilder(1024);
schema.executeQueries(queries, log, null);
log(path, log);
}
catch (SQLException e) {
e.printStackTrace();
log(path, e);
throw (e);
}
}

tasks
.parallelStream()
.forEach(task -> {
try {
String entry = task.get() + "\n\n";
log(path, entry);
Util.log(path, entry);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
Expand All @@ -194,50 +193,51 @@ else if (arg instanceof String){

logentry = "-- executing queries execute.after_all";
System.out.println("\n" + logentry);
log(path, logentry);

try {
StringBuilder log = new StringBuilder(1024);
schema.executeQueries(queries, log, null);
log(path, log);
}
catch (SQLException e) {
e.printStackTrace();
log(path, e);
throw (e);
Util.log(path, logentry);

StringBuilder log = new StringBuilder(1024);
completed = Util.executeQueries(queries, log, schema.config);
Util.log(path, log.toString());
if (!completed){
Util.log(path, "!!!ABORTING!!!");
System.exit(1);
}
}

tc = System.currentTimeMillis() - tc;

logentry = String.format("-- %tT Completed in %.3f seconds\n", System.currentTimeMillis(), tc / 1000.0);
log(path, logentry);
Util.log(path, logentry);

System.out.println("\n" + logentry);
System.out.println("See log and recommended actions at " + path);
}


static void log(Path path, CharSequence logentry) throws IOException {
public static String getProductName() {

String result = PgMigrator.class.getPackage().getImplementationTitle();

Files.write(path, Collections.singleton(logentry), StandardCharsets.UTF_8, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
return result != null ? result : "Migrate2Postgres";
}


static void log(Path path, Exception ex) throws IOException {
public static String getProductVersion() {

String logentry = Arrays.stream(ex.getStackTrace())
.map(StackTraceElement::toString)
.collect(Collectors.joining("\n"));
String result = PgMigrator.class.getPackage().getImplementationVersion();

log(path, logentry);
return result != null ? result : "";
}


public static String getBanner(){

return "/**\n"
+ "\tScripted by Migrate2Postgres on "
+ "\tScripted by "
+ getProductName()
+ " "
+ getProductVersion()
+ " on "
+ ZonedDateTime.now().format(RFC_1123_DATE_TIME)
+ "\n\n\t"
+ DISCL.replace("\n", "\n\t")
Expand Down
49 changes: 3 additions & 46 deletions src/main/java/net/twentyonesolutions/m2pg/Schema.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package net.twentyonesolutions.m2pg;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.JDBCType;
import java.sql.PreparedStatement;
Expand Down Expand Up @@ -103,45 +100,6 @@ public Schema(Config config) throws Exception {
}


public void executeQueries(List<String> queries, StringBuilder log, Connection conTgt) throws IOException, SQLException {

Config config = this.config;

long tc = System.currentTimeMillis();

Statement statTgt = null;

if (conTgt == null)
conTgt = config.connect(config.target);

statTgt = conTgt.createStatement();
statTgt.execute("BEGIN TRANSACTION;");

for (String script : queries){

String logentry = "\n -- Executing: " + script + "\n";

System.out.println(logentry);
log.append(logentry);

String sql = script.trim();
if (sql.toLowerCase().endsWith(".sql")){
Path path = Paths.get(sql);
sql = Files.lines(path)
.collect(Collectors.joining());

logentry = "\n/**\n" + sql + "\n*/\n";
System.out.println(logentry);
log.append(logentry);
}

statTgt.execute(sql);
}

statTgt.execute("COMMIT;");
}


public String copyTable(String tableName, IProgress progress) throws IOException {

StringBuilder log = new StringBuilder(1024);
Expand Down Expand Up @@ -307,7 +265,7 @@ public String copyTable(String tableName, IProgress progress) throws IOException
.append(". ");

if (config.dml.get("execute.recommended").toString().toLowerCase().equals("all")){
executeQueries(Arrays.asList(sqlRecommended), log, conTgt);
Util.executeQueries(Arrays.asList(sqlRecommended), log, conTgt);
}
else {
log.append("Recommended:\n\t")
Expand Down Expand Up @@ -346,9 +304,8 @@ public String copyTable(String tableName, IProgress progress) throws IOException

tc = System.currentTimeMillis() - tc;

// System.out.printf("\n%tT Copied table %s %,d/%,d in %.3f seconds\n", System.currentTimeMillis(), tableName, copied, rowCount, tc / 1000.0);

log.append(String.format(" /* copied %,d/%,d records in %.3f seconds **/\n", copied, rowCount, tc / 1000.0));
if (rowCount > 0)
log.append(String.format(" /* copied %,d / %,d records in %.3f seconds **/\n", copied, rowCount, tc / 1000.0));

return log.toString();
}
Expand Down
Loading

0 comments on commit 9de9774

Please sign in to comment.