-
Notifications
You must be signed in to change notification settings - Fork 15
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
schedules: Use numeric ids for schedules tables primary keys #1086
Merged
greenscientist
merged 4 commits into
chairemobilite:main
from
tahini:dbSchedulesNumericId
Dec 17, 2024
+596
−303
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8ce1d5e
db: Add a `transaction` option to `exists` and `read` functions
tahini 0168a73
schedules in db: Deprecate the `create` and `update` db functions
tahini 38e352a
default db: Support ID parameter either string or number
tahini e622a2b
schedules: Use numeric ids for schedules tables primary keys
tahini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 130 additions & 90 deletions
220
packages/transition-backend/src/models/db/__tests__/TransitSchedule.db.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...nsition-backend/src/models/db/migrations/20241003145700_useNumericIdsInSchedulesTables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2024, Polytechnique Montreal and contributors | ||
* | ||
* This file is licensed under the MIT License. | ||
* License text available at https://opensource.org/licenses/MIT | ||
*/ | ||
import { Knex } from 'knex'; | ||
|
||
const scheduleTblName = 'tr_transit_schedules'; | ||
const schedulePeriodTblName = 'tr_transit_schedule_periods'; | ||
const scheduleTripTblName = 'tr_transit_schedule_trips'; | ||
|
||
/** | ||
* This migration changes the primary key column of the 3 tables describing | ||
* schedules to be a numeric auto-increment ID. It is not necessary to have a | ||
* uuid and numeric auto-increment IDs will allow more performant duplication | ||
* operations, among other things. | ||
* | ||
* We keep the uuid columns, but it will not be used as primary or foreign keys | ||
* anymore, just for json2capnp conversion. | ||
* | ||
* After this migration, the currently named *_id columns in the 3 schedule | ||
* tables will now be integers instead of uuid, the trip does not force a link | ||
* to the schedule anymore as we have this link through the period. | ||
* | ||
* @param knex The database configuration object | ||
* @returns | ||
*/ | ||
export async function up(knex: Knex): Promise<unknown> { | ||
// Rename the current uuid columns, and drop the foreign key constraints for now | ||
await knex.schema.alterTable(scheduleTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('id', 'uuid'); | ||
}); | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('id', 'uuid'); | ||
table.renameColumn('schedule_id', 'schedule_uuid'); | ||
table.dropForeign('schedule_id'); | ||
}); | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('id', 'uuid'); | ||
table.renameColumn('schedule_id', 'schedule_uuid'); | ||
table.renameColumn('schedule_period_id', 'schedule_period_uuid'); | ||
table.dropForeign('schedule_id'); | ||
table.dropForeign('schedule_period_id'); | ||
}); | ||
|
||
// Drop all primary keys from table, to better recreate them | ||
await knex.schema.alterTable(scheduleTblName, (table: Knex.TableBuilder) => { | ||
table.dropPrimary(); | ||
}); | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.dropPrimary(); | ||
}); | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.dropPrimary(); | ||
}); | ||
|
||
// Add the new auto-incrementing primary key id columns and columns for | ||
// foreign key with nullable values | ||
await knex.schema.alterTable(scheduleTblName, (table: Knex.TableBuilder) => { | ||
table.increments(); | ||
}); | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.increments(); | ||
table.integer('schedule_id').nullable(); | ||
}); | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.increments(); | ||
table.integer('schedule_period_id').nullable(); | ||
}); | ||
|
||
// Set the values for the foreign key columns | ||
await knex.raw(` | ||
UPDATE ${schedulePeriodTblName} sp | ||
SET schedule_id = s.id | ||
FROM ${scheduleTblName} s | ||
WHERE sp.schedule_uuid = s.uuid; | ||
`); | ||
await knex.raw(` | ||
UPDATE ${scheduleTripTblName} st | ||
SET schedule_period_id = sp.id | ||
FROM ${schedulePeriodTblName} sp | ||
WHERE st.schedule_period_uuid = sp.uuid; | ||
`); | ||
|
||
// Drop the previous foreign uuid key columns, make the new foreign key | ||
// columns not nullable and add foreign key constraints | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.dropColumns('schedule_uuid', 'schedule_period_uuid'); | ||
table.integer('schedule_period_id').notNullable().index().alter(); | ||
table.foreign('schedule_period_id').references(`${schedulePeriodTblName}.id`).onDelete('CASCADE'); | ||
}); | ||
return await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.dropColumns('schedule_uuid'); | ||
table.integer('schedule_id').notNullable().index().alter(); | ||
table.foreign('schedule_id').references(`${scheduleTblName}.id`).onDelete('CASCADE'); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<unknown> { | ||
// Create the uuid foreign key columns in tables | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.uuid('schedule_uuid').nullable(); | ||
}); | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.uuid('schedule_uuid').nullable(); | ||
table.uuid('schedule_period_uuid').nullable(); | ||
}); | ||
|
||
// Set the values of the uuid columns from current id columns | ||
await knex.raw(` | ||
UPDATE ${schedulePeriodTblName} sp | ||
SET schedule_uuid = s.uuid | ||
FROM ${scheduleTblName} s | ||
WHERE sp.schedule_id = s.id; | ||
`); | ||
await knex.raw(` | ||
UPDATE ${scheduleTripTblName} st | ||
SET schedule_period_uuid = sp.uuid, schedule_uuid = s.uuid | ||
FROM ${schedulePeriodTblName} sp | ||
INNER JOIN ${scheduleTblName} s ON sp.schedule_id = s.id | ||
WHERE st.schedule_period_id = sp.id; | ||
`); | ||
|
||
// Delete the id columns, set uuids as primary keys and make foreign key columns not nullable and indexed | ||
await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.dropColumns('id', 'schedule_period_id'); | ||
table.uuid('uuid').primary().alter(); | ||
table.uuid('schedule_period_uuid').notNullable().index().alter(); | ||
table.uuid('schedule_uuid').notNullable().index().alter(); | ||
}); | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.dropColumns('id', 'schedule_id'); | ||
table.uuid('uuid').primary().alter(); | ||
table.uuid('schedule_uuid').notNullable().index().alter(); | ||
}); | ||
await knex.schema.alterTable(scheduleTblName, (table: Knex.TableBuilder) => { | ||
table.dropColumn('id'); | ||
table.uuid('uuid').primary().alter(); | ||
}); | ||
|
||
// Revert name change and add foreign key constraints | ||
await knex.schema.alterTable(scheduleTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('uuid', 'id'); | ||
}); | ||
await knex.schema.alterTable(schedulePeriodTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('uuid', 'id'); | ||
table.renameColumn('schedule_uuid', 'schedule_id'); | ||
table.foreign('schedule_id').references('tr_transit_schedules.id').onDelete('CASCADE'); | ||
}); | ||
return await knex.schema.alterTable(scheduleTripTblName, (table: Knex.TableBuilder) => { | ||
table.renameColumn('uuid', 'id'); | ||
table.renameColumn('schedule_uuid', 'schedule_id'); | ||
table.renameColumn('schedule_period_uuid', 'schedule_period_id'); | ||
table.foreign('schedule_id').references('tr_transit_schedules.id').onDelete('CASCADE'); | ||
table.foreign('schedule_period_id').references('tr_transit_schedule_periods.id').onDelete('CASCADE'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 127 additions & 60 deletions
187
packages/transition-backend/src/models/db/transitSchedules.db.queries.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 53 additions & 73 deletions
126
packages/transition-backend/src/services/gtfsImport/__tests__/ScheduleImporter.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unchanged files with check annotations Beta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PreferencesModelWithIdAndData extends PreferencesModel { | ||
id: string; | ||
data: { [key: string]: any }; | ||
} | ||
const prefChangeEvent = 'change'; | ||
} | ||
// Not implemented for Preferences, we should never delete the preferences object. | ||
public delete(_socket: any): Promise<any> { | ||
Check warning on line 247 in packages/chaire-lib-common/src/config/Preferences.ts
|
||
return new Promise((resolve) => { | ||
resolve(null); | ||
}); | ||
} | ||
// TODO: type this: | ||
public get(path: string, defaultValue: unknown = undefined): any { | ||
return super.get(path, defaultValue); | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Hexadecimal strings of the various colors that should be available */ | ||
colors: string[]; | ||
}; | ||
[key: string]: any; | ||
} | ||
// TODO: Type more fields |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
} & AdditionalConfig; | ||
// Initialize default configuration | ||
const projectConfig: ProjectConfiguration<any> = { | ||
mapDefaultCenter: { lon: -73.6131, lat: 45.5041 }, | ||
separateAdminLoginPage: false, | ||
projectShortname: 'default', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataSource extends ObjectWithHistory<DataSourceAttributes> { | ||
protected static displayName = 'DataSource'; | ||
private _collectionManager: any; | ||
constructor(attributes = {}, isNew: boolean, collectionManager?) { | ||
super(attributes, isNew); | ||
return this.attributes.name || this.attributes.shortname || this.getId(); | ||
} | ||
get collectionManager(): any { | ||
// TODO: test or use dependency injection | ||
return this._collectionManager; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Property in keyof TEvent[EventArgsKey]]: TEvent[EventArgsKey][Property]; | ||
}) => void | ||
): void; | ||
emit(event: string | Event, ...args: any[]): void; | ||
emitProgress(progressName: string, completeRatio: number): void; | ||
once(event: string | Event, callback: (data: any) => void): void; | ||
on(event: string | Event, callback: (...data: any) => void): void; | ||
addListener(event: string | Event, callback: (...data: any) => void): void; | ||
removeListener(event: string | Event, callback: (...data: any) => void): void; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment saying that we now accept both uuids (string) or id (integer)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jsdoc was updated, also with the new options parameters