From 124f6b722e0a9311027f8724dc23904f06080e67 Mon Sep 17 00:00:00 2001 From: Evangelos Chaniotakis Date: Wed, 10 Apr 2019 10:48:39 -0700 Subject: [PATCH] export timestamps to SQL for migration (#319) --- .../net/es/oscars/nsi/svc/NsiService.java | 2 +- .../java/net/es/oscars/resv/ent/EventLog.java | 1 - .../net/es/oscars/resv/svc/LogService.java | 2 +- migration/config/application.properties | 4 +- migration/config/nsi-filter.json | 3 + migration/config/nsi-peerings.json | 3 + migration/sql/commands.sh | 10 +++ migration/sql/migrate_timestamps_schema.sql | 25 ++++++ .../main/java/net/es/oscars/MigrationApp.java | 7 +- .../es/oscars/timefix/TimestampExporter.java | 82 +++++++++++++++++++ 10 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 migration/config/nsi-filter.json create mode 100644 migration/config/nsi-peerings.json create mode 100644 migration/sql/commands.sh create mode 100644 migration/sql/migrate_timestamps_schema.sql create mode 100644 migration/src/main/java/net/es/oscars/timefix/TimestampExporter.java diff --git a/backend/src/main/java/net/es/oscars/nsi/svc/NsiService.java b/backend/src/main/java/net/es/oscars/nsi/svc/NsiService.java index ece252800..beff8d103 100644 --- a/backend/src/main/java/net/es/oscars/nsi/svc/NsiService.java +++ b/backend/src/main/java/net/es/oscars/nsi/svc/NsiService.java @@ -81,7 +81,7 @@ public class NsiService { @Value("${nsi.provider-nsa}") private String providerNsa; - @Value("${nsi.strict-policing}") + @Value("${nsi.strict-policing:true}") private boolean strictPolicing; @Value("#{'${nsi.allowed-requesters}'.split(',')}") diff --git a/backend/src/main/java/net/es/oscars/resv/ent/EventLog.java b/backend/src/main/java/net/es/oscars/resv/ent/EventLog.java index 0eb4fa971..01f5ac90f 100644 --- a/backend/src/main/java/net/es/oscars/resv/ent/EventLog.java +++ b/backend/src/main/java/net/es/oscars/resv/ent/EventLog.java @@ -38,7 +38,6 @@ public EventLog(@JsonProperty("connectionId") @NonNull String connectionId, @NonNull private Instant created; - @NonNull private Instant archived; @OneToMany(cascade = CascadeType.ALL) diff --git a/backend/src/main/java/net/es/oscars/resv/svc/LogService.java b/backend/src/main/java/net/es/oscars/resv/svc/LogService.java index 155cfad1e..6acf080c1 100644 --- a/backend/src/main/java/net/es/oscars/resv/svc/LogService.java +++ b/backend/src/main/java/net/es/oscars/resv/svc/LogService.java @@ -25,7 +25,7 @@ public class LogService { public void logEvent(String connectionId, Event ev) { EventLog eventLog = logRepo.findByConnectionId(connectionId) .orElseGet(() -> EventLog.builder() - .archived(Instant.MAX) + .archived(null) .created(Instant.now()) .connectionId(connectionId) .events(new ArrayList<>()) diff --git a/migration/config/application.properties b/migration/config/application.properties index 3f9752dcd..24c169ca8 100644 --- a/migration/config/application.properties +++ b/migration/config/application.properties @@ -37,8 +37,8 @@ nsi.allowed-requesters=urn:ogf:network:surfnet.nl:1990:nsa:nsi-requester nsi.nsa-name=Nsa Name nsi.nsa-contact=Evangelos Chaniotakis,haniotak@es.net nsi.nsa-location=37.876,-122.253 - -nsi.peerings=nada +nsi.peerings=./config/nsi-peerings.json +nsi.filter=./config/nsi-filter.json slack.enable=false slack.channel=oscars-playground diff --git a/migration/config/nsi-filter.json b/migration/config/nsi-filter.json new file mode 100644 index 000000000..df44077d1 --- /dev/null +++ b/migration/config/nsi-filter.json @@ -0,0 +1,3 @@ +[ + "foo" +] \ No newline at end of file diff --git a/migration/config/nsi-peerings.json b/migration/config/nsi-peerings.json new file mode 100644 index 000000000..41b42e677 --- /dev/null +++ b/migration/config/nsi-peerings.json @@ -0,0 +1,3 @@ +[ + +] diff --git a/migration/sql/commands.sh b/migration/sql/commands.sh new file mode 100644 index 000000000..fcf73ae6e --- /dev/null +++ b/migration/sql/commands.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + + +dump_cmd = "pg_dump -f oscars.sql.bak -C -d oscars_backend" + +restore_command = "psql < oscars.sql.bak" + +drop_everything = "dropdb oscars_backend" + +import = "psql -d oscars_backend -f timestamps.sql" \ No newline at end of file diff --git a/migration/sql/migrate_timestamps_schema.sql b/migration/sql/migrate_timestamps_schema.sql new file mode 100644 index 000000000..914b83eb8 --- /dev/null +++ b/migration/sql/migrate_timestamps_schema.sql @@ -0,0 +1,25 @@ +alter table held drop column expiration; +alter table held add column expiration timestamp; + +alter table router_command_history drop column date; +alter table router_command_history add column date timestamp; + +alter table version drop column updated; +alter table version add column updated timestamp; + + +alter table schedule drop column beginning; +alter table schedule drop column ending; + +alter table schedule add column beginning timestamp; +alter table schedule add column ending timestamp; + +alter table event_log drop column created; +alter table event_log drop column archived; + +alter table event_log add column created timestamp; +alter table event_log add column archived timestamp; + +alter table event drop column at; +alter table event add column at timestamp; + diff --git a/migration/src/main/java/net/es/oscars/MigrationApp.java b/migration/src/main/java/net/es/oscars/MigrationApp.java index 9fda0c0a0..571358697 100644 --- a/migration/src/main/java/net/es/oscars/MigrationApp.java +++ b/migration/src/main/java/net/es/oscars/MigrationApp.java @@ -2,6 +2,7 @@ import lombok.extern.slf4j.Slf4j; import net.es.oscars.migration.MigrationEngine; +import net.es.oscars.timefix.TimestampExporter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; @@ -19,9 +20,13 @@ public static void main(String[] args) { @Autowired private MigrationEngine engine; + @Autowired + private TimestampExporter exporter; + @Override public void run(String... strings) throws Exception { - this.engine.runEngine(); +// this.engine.runEngine(); + exporter.export(); System.exit(0); } diff --git a/migration/src/main/java/net/es/oscars/timefix/TimestampExporter.java b/migration/src/main/java/net/es/oscars/timefix/TimestampExporter.java new file mode 100644 index 000000000..a882d4c50 --- /dev/null +++ b/migration/src/main/java/net/es/oscars/timefix/TimestampExporter.java @@ -0,0 +1,82 @@ +package net.es.oscars.timefix; + +import net.es.oscars.pss.ent.RouterCommandHistory; +import net.es.oscars.resv.db.CommandHistoryRepository; +import net.es.oscars.resv.db.LogRepository; +import net.es.oscars.resv.db.ScheduleRepository; +import net.es.oscars.resv.ent.Event; +import net.es.oscars.resv.ent.EventLog; +import net.es.oscars.resv.ent.Schedule; +import net.es.oscars.topo.db.VersionRepository; +import net.es.oscars.topo.ent.Version; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.io.*; +import java.time.Instant; +import java.util.List; + +@Component +public class TimestampExporter { + @Autowired + private CommandHistoryRepository historyRepo; + @Autowired + private VersionRepository versionRepo; + + @Autowired + private ScheduleRepository schedRepo; + + + @Autowired + private LogRepository logRepo; + + private String i2p(Instant when) { + if (when.equals(Instant.MAX)) { + return "null"; + } + return "to_timestamp("+when.getEpochSecond()+")"; + } + + @Transactional + public void export() throws FileNotFoundException { + PrintWriter out = new PrintWriter("timestamps.sql"); + + List rch = historyRepo.findAll(); + + for (RouterCommandHistory rc : rch) { + String sql = "UPDATE router_command_history SET date = "+i2p(rc.getDate())+" WHERE id = "+rc.getId()+";"; + out.println(sql); + } + + List versions = versionRepo.findAll(); + + for (Version v : versions) { + String sql = "UPDATE version SET updated = "+i2p(v.getUpdated())+" WHERE id = "+v.getId()+";"; + out.println(sql); + } + + + List schedules = schedRepo.findAll(); + + for (Schedule s : schedules) { + String sql = "UPDATE schedule SET beginning = "+i2p(s.getBeginning())+", ending="+i2p(s.getEnding())+" WHERE id = "+s.getId()+";"; + out.println(sql); + } + + + List logs = logRepo.findAll(); + + for (EventLog el : logs) { + String sql = "UPDATE event_log SET archived = "+i2p(el.getArchived())+", created = "+i2p(el.getCreated())+" WHERE id = "+el.getId()+";"; + out.println(sql); + + for (Event event : el.getEvents()) { + String evsql = "UPDATE event SET at = "+i2p(event.getAt())+" WHERE id = "+event.getId()+";"; + out.println(evsql); + } + } + out.close(); + } + +} \ No newline at end of file