Skip to content

Commit e623079

Browse files
committed
networkDesign: change DB data type of fitness from numeric to double
The `decimal` knex data type converts by default to a `numeric(8,2)` in postgres, effectively making maxing the value to 6 digits with 2 decimals. We use `double` instead to support any floating point value for the fitness score, as it can be quite high for large simulations, for example, with many thousands OD trip pairs, the value can be in the millions or tens of millions. This converts to `double precision` in postgres.
1 parent 42c6326 commit e623079

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2026, Polytechnique Montreal and contributors
3+
*
4+
* This file is licensed under the MIT License.
5+
* License text available at https://opensource.org/licenses/MIT
6+
*/
7+
import { Knex } from 'knex';
8+
9+
const tableName = 'tr_jobs_network_design_results';
10+
const simulationMethodTableName = 'tr_jobs_network_design_simulation_results';
11+
12+
/**
13+
* Update the fitness score type from decimal to float to avoid large
14+
* fitnesses scores causing database errors.
15+
* @param knex
16+
* @returns
17+
*/
18+
export async function up(knex: Knex): Promise<unknown> {
19+
await knex.schema.alterTable(simulationMethodTableName, (table: Knex.TableBuilder) => {
20+
table.double('fitness_score').notNullable().alter();
21+
});
22+
return knex.schema.alterTable(tableName, (table: Knex.TableBuilder) => {
23+
table.double('total_fitness').notNullable().alter();
24+
});
25+
}
26+
27+
export async function down(knex: Knex): Promise<unknown> {
28+
await knex.schema.alterTable(simulationMethodTableName, (table: Knex.TableBuilder) => {
29+
table.decimal('fitness_score').notNullable().alter();
30+
});
31+
return knex.schema.alterTable(tableName, (table: Knex.TableBuilder) => {
32+
table.decimal('total_fitness').notNullable().alter();
33+
});
34+
}

0 commit comments

Comments
 (0)