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

update migration to stop excessive consumption of computation #2521

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

affanv14
Copy link
Contributor

@affanv14 affanv14 commented Oct 18, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Updated the affiliateRevShare column in the fills table to improve data accuracy by changing its type to decimal.
  • Bug Fixes

    • Ensured that the affiliateRevShare column is not nullable and has a default value of 0 for better data integrity.

@affanv14 affanv14 requested a review from a team as a code owner October 18, 2024 22:57
Copy link
Contributor

coderabbitai bot commented Oct 18, 2024

Walkthrough

The changes involve a database migration script for the fills table in a PostgreSQL database. The migration modifies the affiliateRevShare column, changing its data type from string to decimal in the up function, ensuring it is non-nullable with a default value of 0. In the down function, the column is reverted to a string type, also non-nullable, with a default value of '0'. This process facilitates the type adjustment during migrations.

Changes

File Change Summary
indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts Modified affiliateRevShare column in fills table: changed from string to decimal in up function, reverted to string in down function.

Poem

In the database, a change so bright,
From string to decimal, a wondrous sight!
Affiliate shares, now precise and clear,
With defaults set, there's nothing to fear.
Hopping through migrations, we dance with glee,
A smoother flow for all, oh joy, oh me! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 80d16c6 and a3df5f8.

📒 Files selected for processing (1)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240910101410_change_fills_affiliaterevshare_type.ts

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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 steps

The 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:

  1. Implement data preservation and conversion logic in both up and down migrations to prevent data loss.
  2. Consider adding an index on the affiliateRevShare column if it's frequently used in queries.
  3. If possible, specify the precision for the decimal column instead of using variable precision.
  4. 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

📥 Commits

Files that changed from the base of the PR and between 8a866a5 and 80d16c6.

📒 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 issue

Improve data handling in down migration

The down function correctly reverts the column type change, but it has similar issues to the up function:

  1. Dropping and re-adding the column will lead to data loss.
  2. 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 suggestions

The 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:

  1. Implement the data preservation and conversion logic as suggested in the previous comments.
  2. 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)
}
  1. 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.

Comment on lines 8 to 11
table.dropColumn('affiliateRevShare');
table.decimal('affiliateRevShare', null).notNullable().defaultTo(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

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:

  1. Dropping and re-adding the column will lead to data loss for existing records.
  2. 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

@affanv14 affanv14 merged commit b829b28 into main Oct 18, 2024
12 checks passed
@affanv14 affanv14 deleted the affan/update-migration branch October 18, 2024 23:26
mergify bot pushed a commit that referenced this pull request Oct 18, 2024
affanv14 added a commit that referenced this pull request Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants