Skip to content

Commit

Permalink
Merge pull request #160 from Wilhansen/patch-1
Browse files Browse the repository at this point in the history
Fix error when loading GTFS with a subdirectory
  • Loading branch information
Landon Reed authored Dec 13, 2018
2 parents b31e716 + c9dfda5 commit 40366ed
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 162 deletions.
43 changes: 35 additions & 8 deletions src/main/java/com/conveyal/gtfs/loader/JdbcGtfsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,20 @@ public FeedLoadResult loadTables () {
this.tablePrefix = randomIdString();
result.filename = gtfsFilePath;
result.uniqueIdentifier = tablePrefix;
registerFeed(gtfsFile);
// Include the dot separator in the table prefix.
// This allows everything to work even when there's no prefix.
this.tablePrefix += ".";
this.errorStorage = new SQLErrorStorage(connection, tablePrefix, true);

//The order of the following four lines should not be changed because the schema needs to be in place
//before the error storage can be constructed, which in turn needs to exist in case any errors are
//encountered during the loading process.
{
createSchema(connection, tablePrefix);
//the SQLErrorStorage constructor expects the tablePrefix to contain the dot separator.
this.errorStorage = new SQLErrorStorage(connection, tablePrefix + ".", true);
//registerFeed accesses this.tablePrefix which shouldn't contain the dot separator.
registerFeed(gtfsFile);
// Include the dot separator in the table prefix from this point onwards.
// This allows everything to work even when there's no prefix.
this.tablePrefix += ".";
}
this.referenceTracker = new ReferenceTracker(errorStorage);
// Load each table in turn, saving some summary information about what happened during each table load
result.agency = load(Table.AGENCY);
Expand Down Expand Up @@ -164,6 +173,27 @@ public FeedLoadResult loadTables () {
}
return result;
}

/**
* Creates a schema/namespace in the database.
* This does *not* setup any other tables or enter the schema name in a registry (@see #registerFeed).
*
* @param connection Connection to the database to create the schema on.
* @param schemaName Name of the schema (i.e. table prefix). Should not include the dot suffix.
*/
private static void createSchema (Connection connection, String schemaName) {
try {
Statement statement = connection.createStatement();
// FIXME do the following only on databases that support schemas.
// SQLite does not support them. Is there any advantage of schemas over flat tables?
statement.execute("create schema " + schemaName);
connection.commit();
LOG.info("Created new feed schema: {}", statement);
} catch (Exception ex) {
LOG.error("Exception while registering new feed namespace in feeds table: {}", ex.getMessage());
DbUtils.closeQuietly(connection);
}
}

/**
* Add a line to the list of loaded feeds showing that this feed has been loaded.
Expand Down Expand Up @@ -204,9 +234,6 @@ private void registerFeed (File gtfsFile) {
// TODO try to get the feed_id and feed_version out of the feed_info table
// statement.execute("select * from feed_info");

// FIXME do the following only on databases that support schemas.
// SQLite does not support them. Is there any advantage of schemas over flat tables?
statement.execute("create schema " + tablePrefix);
// current_timestamp seems to be the only standard way to get the current time across all common databases.
// Record total load processing time?
statement.execute("create table if not exists feeds (namespace varchar primary key, md5 varchar, " +
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/conveyal/gtfs/GTFSFeedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void setUpClass() {
//executed only once, before the first test
simpleGtfsZipFileName = null;
try {
simpleGtfsZipFileName = TestUtils.zipFolderFiles("fake-agency");
simpleGtfsZipFileName = TestUtils.zipFolderFiles("fake-agency", true);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -190,7 +190,7 @@ public void canGetAgencyTimeZoneForStop() {
public void canGetInterpolatedTimes() throws GTFSFeed.FirstAndLastStopsDoNotHaveTimes, IOException {
String tripId = "a30277f8-e50a-4a85-9141-b1e0da9d429d";

String gtfsZipFileName = TestUtils.zipFolderFiles("fake-agency-interpolated-stop-times");
String gtfsZipFileName = TestUtils.zipFolderFiles("fake-agency-interpolated-stop-times", true);

GTFSFeed feed = GTFSFeed.fromFile(gtfsZipFileName);
Iterable<StopTime> stopTimes = feed.getInterpolatedStopTimesForTrip(tripId);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/conveyal/gtfs/GTFSMainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void setUpClass() {
//executed only once, before the first test
simpleGtfsZipFileName = null;
try {
simpleGtfsZipFileName = TestUtils.zipFolderFiles("fake-agency");
simpleGtfsZipFileName = TestUtils.zipFolderFiles("fake-agency", true);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit 40366ed

Please sign in to comment.