Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parent publishedVersionId if when deleting FeedVersion #528

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ public void delete() {
fs.lastFetched = null;
Persistence.feedSources.replace(fs.id, fs);
}
ensurePublishedVersionIdIsUnset(fs);

feedStore.deleteFeed(id);
// Delete feed version tables in GTFS database
GTFS.delete(this.namespace, DataManager.GTFS_DATA_SOURCE);
Expand All @@ -499,7 +501,7 @@ public void delete() {
Persistence.deployments.getMongoCollection().updateMany(eq("projectId", this.parentFeedSource().projectId),
pull("feedVersionIds", this.id));
Persistence.feedVersions.removeById(this.id);
this.parentFeedSource().renumberFeedVersions();
fs.renumberFeedVersions();

// recalculate feed expiration notifications in case the latest version has changed
Scheduler.scheduleExpirationNotifications(fs);
Expand All @@ -510,6 +512,16 @@ public void delete() {
}
}

/**
* If this feed version is referenced in the parent feed source by publishedVersionId,
* ensure that the field is set to null.
*/
private void ensurePublishedVersionIdIsUnset(FeedSource fs) {
if (this.namespace != null && this.namespace.equals(fs.publishedVersionId)) {
Persistence.feedSources.updateField(fs.id, "publishedVersionId", null);
}
}

/**
* Assign characteristics from a new GTFS file to the feed version. This should generally be called directly after
* constructing the feed version (assuming the GTFS file is available). Characteristics set from file include:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.stream.Stream;

import static com.conveyal.datatools.TestUtils.createFeedVersionFromGtfsZip;
import static com.conveyal.datatools.manager.DataManager.GTFS_DATA_SOURCE;
import static com.mongodb.client.model.Filters.eq;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -106,4 +103,45 @@ void canDetectBlockingErrorTypesForPublishing(NewGTFSErrorType errorType) throws

assertThat(feedVersion1.hasBlockingIssuesForPublishing(), equalTo(true));
}

/**
* {@link FeedSource::publishedVersionId} should be unset if it references a feed version being deleted.
*/
@Test
void shouldDeletePublishedVersionIdWhenDeletingVersion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is an OS thing, but the feed versions are not deleted after the test has complete from Mongo:
Could not drop feed for namespace published_namespace - ERROR: schema "published_namespace" does not exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I suppose the error occurs because the respective namespaces are never created in PostgreSQL, so it fails to delete them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments and cleanup code in 70a01b7.

final String NAMESPACE = "published_namespace";

FeedSource storedFeedSource = Persistence.feedSources.getOneFiltered(eq("projectId", project.id));
String feedSourceId = storedFeedSource.id;

// Create a feed version linked to the feed source.
FeedVersion feedVersion1 = new FeedVersion(feedSource);
feedVersion1.namespace = NAMESPACE;
feedVersion1.feedSourceId = feedSourceId;
Persistence.feedVersions.create(feedVersion1);

// Other feed version linked to the feed source, without effect to the published version.
FeedVersion feedVersion2 = new FeedVersion(feedSource);
feedVersion2.namespace = "other_namespace";
feedVersion2.feedSourceId = feedSourceId;
Persistence.feedVersions.create(feedVersion2);

// Set publishedVersionId of FeedSource as the namespace of the feed version, per FeedVersionController logic.
Persistence.feedSources.updateField(feedSourceId, "publishedVersionId", NAMESPACE);

// publishedVersionId should be set at this point.
assertThat(getPubVersionId(feedSourceId), equalTo(NAMESPACE));

// Deleting feedVersion2 should not touch publishedVersionId.
feedVersion2.delete();
assertThat(getPubVersionId(feedSourceId), equalTo(NAMESPACE));

// Deleting feedVersion1 should unset publishedVersionId.
feedVersion1.delete();
assertThat(getPubVersionId(feedSourceId), equalTo(null));
}

String getPubVersionId(String feedSourceId) {
return Persistence.feedSources.getById(feedSourceId).publishedVersionId;
}
}