-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix #459: add a test to reproduce the issue * fixes #459: reduce timestamptz precision across the database
- Loading branch information
1 parent
59b0467
commit 3bd2c80
Showing
2 changed files
with
76 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 ODK Central Developers | ||
// See the NOTICE file at the top-level directory of this distribution and at | ||
// https://github.com/getodk/central-backend/blob/master/NOTICE. | ||
// This file is part of ODK Central. It is subject to the license terms in | ||
// the LICENSE file found in the top-level directory of this distribution and at | ||
// https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
// including this file, may be copied, modified, propagated, or distributed | ||
// except according to the terms contained in the LICENSE file. | ||
|
||
// Issue: #cb459 - `gt` filter for submissionDate is not working as expected because of tz precision | ||
// Root cause: Default timestamptz precision in postgres is microseconds and node/js has just milliseconds | ||
// Solution: Let's change precision to milliseconds in database, since there is no value in having higher precision | ||
// in the database when application can't use/handle it + typical usage of ODK Central doesn't demand higher | ||
// precision. | ||
|
||
const getTimestampColumns = (db) => db.raw(` | ||
SELECT | ||
table_name, column_name, is_nullable | ||
FROM | ||
information_schema.columns | ||
WHERE | ||
table_schema = 'public' | ||
AND udt_name = 'timestamptz'`) | ||
.then(data => data.rows); | ||
|
||
const changePrecision = (db, columnMeta, precision) => db.schema.alterTable(columnMeta.table_name, (table) => { | ||
if (columnMeta.is_nullable === 'YES') { | ||
table.dateTime(columnMeta.column_name, { useTz: true, precision }).alter(); | ||
} else { | ||
table.dateTime(columnMeta.column_name, { useTz: true, precision }).notNullable().alter(); | ||
} | ||
}); | ||
|
||
const up = async (db) => { | ||
const columns = await getTimestampColumns(db); | ||
|
||
await Promise.all(columns.map(c => changePrecision(db, c, 3))); | ||
}; | ||
|
||
const down = async (db) => { | ||
const columns = await getTimestampColumns(db); | ||
|
||
await Promise.all(columns.map(c => changePrecision(db, c, 6))); | ||
}; | ||
|
||
module.exports = { up, down }; |
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