-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
update migration to stop excessive consumption of computation #2521
Conversation
WalkthroughThe changes involve a database migration script for the Changes
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts (1)
Line range hint
1-17
: Summary of review and next stepsThe migration file makes a positive change by converting the
affiliateRevShare
column from string to decimal, which aligns with the PR objective of reducing computational consumption. However, there are several important points to address:
- Implement data preservation and conversion logic in both
up
anddown
migrations to prevent data loss.- Consider adding an index on the
affiliateRevShare
column if it's frequently used in queries.- If possible, specify the precision for the decimal column instead of using variable precision.
- Verify the impact of these changes by analyzing existing data and query patterns.
Please update the migration file addressing these points. Once the changes are made, we can proceed with further testing to ensure the migration performs as expected without any data loss or performance issues.
As a general guideline for future migrations, consider creating a checklist or template that includes steps for data preservation, performance considerations, and rollback strategies. This can help ensure that all migrations are thoroughly planned and executed with minimal risk.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (2)
indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts (2)
15-16
:⚠️ Potential issueImprove data handling in down migration
The
down
function correctly reverts the column type change, but it has similar issues to theup
function:
- Dropping and re-adding the column will lead to data loss.
- There's no data conversion logic to handle existing decimal values.
Consider modifying the down migration to preserve and convert existing data:
export async function down(knex: Knex): Promise<void> { return knex.schema.alterTable('fills', (table) => { // Add a new column table.string('affiliateRevShare_old').nullable(); // Update the new column with converted data await knex.raw(` UPDATE fills SET affiliateRevShare_old = CAST(affiliateRevShare AS VARCHAR) `); // Drop the old column table.dropColumn('affiliateRevShare'); // Rename the new column table.renameColumn('affiliateRevShare_old', 'affiliateRevShare'); // Set the column to not nullable with a default value table.string('affiliateRevShare').notNullable().defaultTo('0').alter(); }); }This approach ensures data preservation and proper type conversion during rollback.
To verify the impact of this change, we can check for any existing data in the
fills
table:#!/bin/bash # Check for existing data in the fills table psql -c "SELECT COUNT(*) FROM fills WHERE affiliateRevShare IS NOT NULL AND affiliateRevShare != 0;"If this query returns a count greater than 0, we should definitely implement the data preservation strategy in the down migration as well.
Line range hint
1-17
: Alignment with PR objectives and further optimization suggestionsThe change from string to decimal for
affiliateRevShare
aligns well with the PR objective of reducing "excessive consumption of computation". This data type change should improve query performance and reduce storage requirements.However, to fully realize these benefits and ensure data integrity:
- Implement the data preservation and conversion logic as suggested in the previous comments.
- Consider adding an index on the
affiliateRevShare
column if it's frequently used in queries:export async function up(knex: Knex): Promise<void> { // ... (previous migration steps) // Add an index on affiliateRevShare await knex.schema.alterTable('fills', (table) => { table.index('affiliateRevShare'); }); } export async function down(knex: Knex): Promise<void> { // Drop the index before altering the column await knex.schema.alterTable('fills', (table) => { table.dropIndex('affiliateRevShare'); }); // ... (previous down migration steps) }
- If
affiliateRevShare
values have a known precision, consider specifying it instead of using variable precision:table.decimal('affiliateRevShare', 15, 5).notNullable().defaultTo(0);This can further optimize storage and computation.
To assess the potential performance impact, we can check the current usage of the
affiliateRevShare
column:#!/bin/bash # Check for queries using affiliateRevShare psql -c "SELECT query, calls, total_time, mean_time FROM pg_stat_statements WHERE query ILIKE '%affiliateRevShare%' ORDER BY total_time DESC LIMIT 5;"This will help determine if adding an index would be beneficial.
table.dropColumn('affiliateRevShare'); | ||
table.decimal('affiliateRevShare', null).notNullable().defaultTo(0); |
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.
💡 Codebase verification
Implement data conversion to preserve existing affiliateRevShare
values
The migration alters the affiliateRevShare
column from string to decimal without handling existing data. Given the extensive usage of affiliateRevShare
across the codebase, this change risks data loss. It is essential to incorporate data conversion logic to ensure existing values are accurately transformed and preserved during the migration.
export async function up(knex: Knex): Promise<void> {
return knex.schema.alterTable('fills', async (table) => {
// Add a new column with the desired type
table.decimal('affiliateRevShare_new', null).nullable();
// Convert and migrate existing data
await knex.raw(`
UPDATE fills
SET affiliateRevShare_new = CAST(affiliateRevShare AS DECIMAL)
`);
// Drop the old column
table.dropColumn('affiliateRevShare');
// Rename the new column to maintain consistency
table.renameColumn('affiliateRevShare_new', 'affiliateRevShare');
// Set the new column to not nullable with a default value
table.decimal('affiliateRevShare', null).notNullable().defaultTo(0).alter();
});
}
🔗 Analysis chain
Consider data preservation and conversion in the migration
The change from string to decimal for affiliateRevShare
is a good step towards improving computational efficiency. However, there are some concerns:
- Dropping and re-adding the column will lead to data loss for existing records.
- There's no data conversion logic to handle existing string values.
Consider modifying the migration to preserve and convert existing data:
export async function up(knex: Knex): Promise<void> {
return knex.schema.alterTable('fills', (table) => {
// Add a new column
table.decimal('affiliateRevShare_new', null).nullable();
// Update the new column with converted data
await knex.raw(`
UPDATE fills
SET affiliateRevShare_new = CAST(affiliateRevShare AS DECIMAL)
`);
// Drop the old column
table.dropColumn('affiliateRevShare');
// Rename the new column
table.renameColumn('affiliateRevShare_new', 'affiliateRevShare');
// Set the column to not nullable with a default value
table.decimal('affiliateRevShare', null).notNullable().defaultTo(0).alter();
});
}
This approach ensures data preservation and proper type conversion.
To verify the impact of this change, we can check for any existing data in the fills
table:
If this query returns a count greater than 0, we should definitely implement the data preservation strategy.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check for existing data in the fills table
psql -c "SELECT COUNT(*) FROM fills WHERE affiliateRevShare IS NOT NULL AND affiliateRevShare != '0';"
Length of output: 266
Script:
#!/bin/bash
# Search the entire codebase for usages of 'affiliateRevShare'
rg 'affiliateRevShare' --context 2
Length of output: 67579
80d16c6
to
f2d3072
Compare
f2d3072
to
a3df5f8
Compare
(cherry picked from commit b829b28)
Changelist
[Describe or list the changes made in this PR]
Test Plan
[Describe how this PR was tested (if applicable)]
Author/Reviewer Checklist
state-breaking
label.indexer-postgres-breaking
label.PrepareProposal
orProcessProposal
, manually add the labelproposal-breaking
.feature:[feature-name]
.backport/[branch-name]
.refactor
,chore
,bug
.Summary by CodeRabbit
New Features
affiliateRevShare
column in thefills
table to improve data accuracy by changing its type to decimal.Bug Fixes
affiliateRevShare
column is not nullable and has a default value of 0 for better data integrity.