-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Align charts.updatedAt and chart_configs.updatedAt (#3962)
* 🐛 Align charts.updatedAt and chart_configs.updatedAt * ✨ ensure chart and config updatedAt timestamps are in sync --------- Co-authored-by: sophiamersmann <[email protected]>
- Loading branch information
1 parent
496aeba
commit 3d0e20b
Showing
6 changed files
with
128 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class AlignUpdatedAtOfCharts1726480222201 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
UPDATE chart_configs cf | ||
INNER JOIN charts c ON c.configId = cf.id | ||
SET cf.updatedAt = LEAST(cf.updatedAt, c.updatedAt) | ||
WHERE c.updatedAt IS NOT NULL; | ||
`) | ||
|
||
await queryRunner.query(` | ||
ALTER TABLE charts | ||
MODIFY updatedAt datetime DEFAULT NULL; | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE chart_configs | ||
MODIFY updatedAt datetime DEFAULT NULL; | ||
`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
ALTER TABLE charts | ||
MODIFY updatedAt datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP; | ||
`) | ||
await queryRunner.query(` | ||
ALTER TABLE chart_configs | ||
MODIFY updatedAt datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP; | ||
`) | ||
|
||
// We can't automatically revert the data changes | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ import { | |
UsersTableName, | ||
DbInsertChartConfig, | ||
} from "@ourworldindata/types" | ||
import { cleanTestDb, sleep } from "./testHelpers.js" | ||
import { cleanTestDb } from "./testHelpers.js" | ||
|
||
let knexInstance: Knex<any, unknown[]> | undefined = undefined | ||
|
||
|
@@ -62,7 +62,7 @@ test("it can query a user created in fixture via TypeORM", async () => { | |
expect(user.email).toBe("[email protected]") | ||
}) | ||
|
||
test("timestamps are automatically created and updated", async () => { | ||
test("createdAt timestamp is automatically created", async () => { | ||
await knexReadWriteTransaction( | ||
async (trx) => { | ||
const user = await knexInstance! | ||
|
@@ -95,29 +95,6 @@ test("timestamps are automatically created and updated", async () => { | |
if (created) { | ||
expect(created.createdAt).not.toBeNull() | ||
expect(created.updatedAt).toBeNull() | ||
await sleep(1000, undefined) | ||
await trx | ||
.table(ChartsTableName) | ||
.where({ id: chartId }) | ||
.update({ isIndexable: true }) | ||
const updated = await knexRawFirst<DbPlainChart>( | ||
trx, | ||
"select * from charts where id = ?", | ||
[chartId] | ||
) | ||
expect(updated).not.toBeNull() | ||
if (updated) { | ||
expect(updated.createdAt).not.toBeNull() | ||
expect(updated.updatedAt).not.toBeNull() | ||
expect( | ||
updated.updatedAt!.getTime() - | ||
updated.createdAt.getTime() | ||
).toBeGreaterThan(800) | ||
expect( | ||
updated.updatedAt!.getTime() - | ||
updated.createdAt.getTime() | ||
).toBeLessThanOrEqual(2000) | ||
} | ||
} | ||
}, | ||
TransactionCloseMode.KeepOpen, | ||
|