Skip to content

Redo proofing cost migrations as strong migrations#4004

Merged
zachmargolis merged 1 commit intomasterfrom
margolis-proofing-cost-strong-migration
Aug 3, 2020
Merged

Redo proofing cost migrations as strong migrations#4004
zachmargolis merged 1 commit intomasterfrom
margolis-proofing-cost-strong-migration

Conversation

@zachmargolis
Copy link
Copy Markdown
Contributor

The migration I previously added failed when deployed in prod because adding a column with a default value causes a table rewrite. Here's literally what the gem told me to do:

          Adding a column with a non-null default causes the entire table to be rewritten.
          Instead, add the column without a default value, then change the default.
          
          class AddAcuantResultToProofingCosts < ActiveRecord::Migration[5.2]
            def up
              add_column :proofing_costs, :acuant_result_count, :integer
              change_column_default :proofing_costs, :acuant_result_count, 0
            end
          
            def down
              remove_column :proofing_costs, :acuant_result_count
            end
          end
          
          Then backfill the existing rows in the Rails console or a separate migration with disable_ddl_transaction!.
          
          class BackfillAddAcuantResultToProofingCosts < ActiveRecord::Migration[5.2]
            disable_ddl_transaction!
          
            def up
              ProofingCost.unscoped.in_batches do |relation| 
                relation.update_all acuant_result_count: 0
                sleep(0.01)
              end
            end
          end

@@ -1,5 +0,0 @@
class AddAcuantResultToProofingCosts < ActiveRecord::Migration[5.2]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Normally I think it's bad to delete migrations? But since this failed to run in production, I think it's safe. Some local devs might be messed up if they already have the column

Copy link
Copy Markdown
Contributor

@mitchellhenke mitchellhenke left a comment

Choose a reason for hiding this comment

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

👍🏼

@zachmargolis zachmargolis merged commit 3f99b8b into master Aug 3, 2020
@zachmargolis zachmargolis deleted the margolis-proofing-cost-strong-migration branch August 3, 2020 21:35
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.

2 participants