Skip to content
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

Add automatic row timestamps for Accounts and Groups tables #1674

Merged
merged 7 commits into from
Jun 20, 2024

Conversation

hectorgomezv
Copy link
Member

@hectorgomezv hectorgomezv commented Jun 19, 2024

Summary

This PR adds createdAt and updatedAt timestamps to Accounts and Groups tables.

  • createdAt is set to the current timestamp when a new row is created and it's not meant to be modified.
  • updatedAt is set to the current timestamp when a new row is created and the idea is to modify it automatically using a trigger.

The goal is to enhance these database tables' maintainability and auditing while improving debugging.

Changes

  • Add Row as an entity meant to be extended by any persistence entity (id, created_at, updated_at are enforced).
  • Add createdAt and updatedAt timestamps to Accounts and Groups tables.
  • Add a Postgres function/trigger to automatically set updatedAt on each row modification.
  • Modifies PostgresDatabaseMigrator.test to make it generic, as the caller may need to specify the types returned to access their properties in the tests.
  • Add a test to check the behavior.

@hectorgomezv hectorgomezv self-assigned this Jun 19, 2024
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9577490268

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 49.394%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 8.42%
Totals Coverage Status
Change from base Build 9563691273: -0.01%
Covered Lines: 3971
Relevant Lines: 6466

💛 - Coveralls

@hectorgomezv hectorgomezv force-pushed the add-accounts-table-timestamps branch from d75e2af to fc56d7c Compare June 19, 2024 07:05
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9577639417

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 49.394%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 8.42%
Totals Coverage Status
Change from base Build 9563691273: -0.01%
Covered Lines: 3971
Relevant Lines: 6466

💛 - Coveralls

@hectorgomezv hectorgomezv force-pushed the add-accounts-table-timestamps branch from fc56d7c to a22412f Compare June 19, 2024 12:49
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9582299562

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 34 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.03%) to 49.263%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 8.42%
src/routes/transactions/entities/swaps/twap-order-info.entity.ts 4 64.91%
src/routes/transactions/helpers/swap-order.helper.ts 4 32.43%
src/routes/transactions/mappers/common/twap-order.mapper.ts 25 31.58%
Totals Coverage Status
Change from base Build 9582121482: 0.03%
Covered Lines: 4068
Relevant Lines: 6660

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9583178940

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 34 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.8%) to 50.077%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 56.84%
src/routes/transactions/entities/swaps/twap-order-info.entity.ts 4 64.91%
src/routes/transactions/helpers/swap-order.helper.ts 4 32.43%
src/routes/transactions/mappers/common/twap-order.mapper.ts 25 31.58%
Totals Coverage Status
Change from base Build 9582121482: 0.8%
Covered Lines: 4119
Relevant Lines: 6660

💛 - Coveralls

@hectorgomezv hectorgomezv marked this pull request as ready for review June 19, 2024 13:54
@hectorgomezv hectorgomezv requested a review from a team as a code owner June 19, 2024 13:54
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9583280310

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 34 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.8%) to 50.033%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 56.84%
src/routes/transactions/entities/swaps/twap-order-info.entity.ts 4 64.91%
src/routes/transactions/helpers/swap-order.helper.ts 4 32.43%
src/routes/transactions/mappers/common/twap-order.mapper.ts 25 31.58%
Totals Coverage Status
Change from base Build 9582121482: 0.8%
Covered Lines: 4117
Relevant Lines: 6660

💛 - Coveralls

migrations/00001_accounts/index.sql Outdated Show resolved Hide resolved
migrations/__tests__/00001_accounts.spec.ts Outdated Show resolved Hide resolved

await sql.end();
it('should add and update row timestamps', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: let's add another test that then ensures the created_at doesn't change but updated_at does after as the after callback combines all queries in one transaction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test in b1a9b0f

@@ -175,7 +175,7 @@ export default () => ({
delegatesV2: process.env.FF_DELEGATES_V2?.toLowerCase() === 'true',
counterfactualBalances:
process.env.FF_COUNTERFACTUAL_BALANCES?.toLowerCase() === 'true',
accounts: process.env.FF_ACCOUNTS?.toLowerCase() === 'true',
accounts: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this an accidental commit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 😓 I've fixed it in b1a9b0f

src/datasources/accounts/entities/group.entity.ts Outdated Show resolved Hide resolved

export type Row = z.infer<typeof RowSchema>;

export const RowSchema = z.object({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: as we will be extending this for all tables, I would add some comments here as I've seen issues when using merge, e.g. outling that id is the primary key and caution should be taken when updating.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment in b1a9b0f

Please let me know if you want to rephrase it or add something else!

@@ -51,7 +56,8 @@ export class PostgresDatabaseMigrator {
}

/**
* @private migrates up to/allows for querying before/after migration to test it.
* Migrates up to/allows for querying before/after migration to test it.
* Uses generics to allow the caller to specify the return type of the before/after functions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

hectorgomezv and others added 4 commits June 19, 2024 16:08
Co-authored-by: Aaron Cook <aaron@safe.global>
Co-authored-by: Aaron Cook <aaron@safe.global>
Co-authored-by: Aaron Cook <aaron@safe.global>
@coveralls
Copy link

coveralls commented Jun 19, 2024

Pull Request Test Coverage Report for Build 9584039461

Details

  • 0 of 4 (0.0%) changed or added relevant lines in 1 file are covered.
  • 34 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.08%) to 49.307%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/db/postgres-database.migrator.ts 0 4 0.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/db/postgres-database.migrator.ts 1 8.42%
src/routes/transactions/entities/swaps/twap-order-info.entity.ts 4 64.91%
src/routes/transactions/helpers/swap-order.helper.ts 4 32.43%
src/routes/transactions/mappers/common/twap-order.mapper.ts 25 31.58%
Totals Coverage Status
Change from base Build 9582121482: 0.08%
Covered Lines: 4070
Relevant Lines: 6660

💛 - Coveralls

@hectorgomezv hectorgomezv merged commit da262d0 into main Jun 20, 2024
16 checks passed
@hectorgomezv hectorgomezv deleted the add-accounts-table-timestamps branch June 20, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants