Skip to content

Commit 6f99f85

Browse files
committed
fix(convert-snapshots): check for existing snapshot in mongo during import
1 parent 495db81 commit 6f99f85

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/main/java/com/conveyal/datatools/editor/jobs/ConvertEditorMapDBToSQL.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.conveyal.datatools.editor.models.transit.TripPatternStop;
1212
import com.conveyal.datatools.manager.DataManager;
1313
import com.conveyal.datatools.manager.models.FeedSource;
14+
import com.conveyal.datatools.manager.models.Snapshot;
1415
import com.conveyal.datatools.manager.persistence.Persistence;
1516
import com.conveyal.gtfs.GTFSFeed;
1617
import com.conveyal.gtfs.loader.FeedLoadResult;
@@ -26,8 +27,12 @@
2627
import java.sql.JDBCType;
2728
import java.sql.PreparedStatement;
2829
import java.sql.SQLException;
30+
import java.util.Iterator;
31+
import java.util.List;
2932

3033
import static com.conveyal.gtfs.loader.DateField.GTFS_DATE_FORMATTER;
34+
import static com.mongodb.client.model.Filters.and;
35+
import static com.mongodb.client.model.Filters.eq;
3136

3237
public class ConvertEditorMapDBToSQL extends MonitorableJob {
3338
private final String feedId;
@@ -53,6 +58,18 @@ public void jobLogic() {
5358
LOG.warn("Not converting snapshot. Feed source Id {} does not exist in application data", feedId);
5459
return;
5560
}
61+
Snapshot matchingSnapshot = Persistence.snapshots.getOneFiltered(
62+
and(
63+
eq("version", versionNumber),
64+
eq(Snapshot.FEED_SOURCE_REF, feedId)
65+
),
66+
null
67+
);
68+
boolean snapshotExists = true;
69+
if (matchingSnapshot == null) {
70+
snapshotExists = false;
71+
matchingSnapshot = new Snapshot("Imported", feedId, "mapdb_editor");
72+
}
5673
FeedTx feedTx;
5774
// FIXME: This needs to share a connection with the snapshotter.
5875
// Create connection for each snapshot
@@ -74,19 +91,19 @@ public void jobLogic() {
7491
LOG.info("Converting {}.{} to SQL", feedId, versionNumber);
7592
// Convert mapdb to SQL
7693
FeedLoadResult convertFeedResult = convertFeed(feedId, versionNumber, feedTx);
77-
// Create manager snapshot for storing in feed source.
78-
com.conveyal.datatools.manager.models.Snapshot managerSnapshot =
79-
new com.conveyal.datatools.manager.models.Snapshot(
80-
feedId, versionNumber != null ? versionNumber : 0, "mapdb_editor", convertFeedResult);
81-
// managerSnapshot.dateCreated =
82-
LOG.info("Storing snapshot {}", managerSnapshot.id);
83-
Persistence.snapshots.create(managerSnapshot);
94+
// Update manager snapshot with result details.
95+
matchingSnapshot.snapshotOf = "mapdb_editor";
96+
matchingSnapshot.namespace = convertFeedResult.uniqueIdentifier;
97+
matchingSnapshot.feedLoadResult = convertFeedResult;
98+
LOG.info("Storing snapshot {}", matchingSnapshot.id);
99+
if (snapshotExists) Persistence.snapshots.replace(matchingSnapshot.id, matchingSnapshot);
100+
else Persistence.snapshots.create(matchingSnapshot);
84101
if (setEditorBuffer) {
85102
// If there is no version, that indicates that this was from the editor buffer for that feedId.
86103
// Make this snapshot the editor namespace buffer.
87-
LOG.info("Updating active snapshot to {}", managerSnapshot.id);
104+
LOG.info("Updating active snapshot to {}", matchingSnapshot.id);
88105
FeedSource updatedFeedSource = Persistence.feedSources.updateField(
89-
feedSource.id, "editorNamespace", managerSnapshot.namespace);
106+
feedSource.id, "editorNamespace", matchingSnapshot.namespace);
90107
LOG.info("Editor namespace: {}", updatedFeedSource.editorNamespace);
91108
}
92109
connection.commit();

0 commit comments

Comments
 (0)