Skip to content

Commit

Permalink
export timestamps to SQL for migration (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
haniotak authored Apr 10, 2019
1 parent d7f74f8 commit 124f6b7
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(',')}")
Expand Down
1 change: 0 additions & 1 deletion backend/src/main/java/net/es/oscars/resv/ent/EventLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public EventLog(@JsonProperty("connectionId") @NonNull String connectionId,
@NonNull
private Instant created;

@NonNull
private Instant archived;

@OneToMany(cascade = CascadeType.ALL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>())
Expand Down
4 changes: 2 additions & 2 deletions migration/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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,[email protected]
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
Expand Down
3 changes: 3 additions & 0 deletions migration/config/nsi-filter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"foo"
]
3 changes: 3 additions & 0 deletions migration/config/nsi-peerings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[

]
10 changes: 10 additions & 0 deletions migration/sql/commands.sh
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions migration/sql/migrate_timestamps_schema.sql
Original file line number Diff line number Diff line change
@@ -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;

7 changes: 6 additions & 1 deletion migration/src/main/java/net/es/oscars/MigrationApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RouterCommandHistory> 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<Version> versions = versionRepo.findAll();

for (Version v : versions) {
String sql = "UPDATE version SET updated = "+i2p(v.getUpdated())+" WHERE id = "+v.getId()+";";
out.println(sql);
}


List<Schedule> 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<EventLog> 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();
}

}

0 comments on commit 124f6b7

Please sign in to comment.