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

AddServiceNameToActiveStorageBlobs migration Error #3204

Closed
chenmiaowei opened this issue May 24, 2021 · 2 comments
Closed

AddServiceNameToActiveStorageBlobs migration Error #3204

chenmiaowei opened this issue May 24, 2021 · 2 comments

Comments

@chenmiaowei
Copy link

StandardError: An error has occurred, this and all later migrations canceled:

=== Dangerous operation detected #strong_migrations ===

Adding a column with a non-null default blocks reads and writes while the entire table is rewritten.
Instead, add the column without a default value, then change the default.

class AddServiceNameToActiveStorageBlobs < ActiveRecord::Migration[6.1]
  def up
    add_column :active_storage_blobs, :service_name, :string
    change_column_default :active_storage_blobs, :service_name, :local
  end

  def down
    remove_column :active_storage_blobs, :service_name
  end
end

Then backfill the existing rows in the Rails console or a separate migration with disable_ddl_transaction!.

class BackfillAddServiceNameToActiveStorageBlobs < ActiveRecord::Migration[6.1]
  disable_ddl_transaction!

  def up
    ActiveStorageBlob.unscoped.in_batches do |relation| 
      relation.update_all service_name: :local
      sleep(0.01)
    end
  end
end

Then add the NOT NULL constraint in separate migrations.
@tomhughes
Copy link
Member

What version of postgres are you using?

@tomhughes
Copy link
Member

If you're seeing this then I believe you are still using postgres 9 in which case please update to postgres 10 or later.

If you're still seeing it with postgres 10 or later then please let us know and we can reopen the ticket.

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

No branches or pull requests

2 participants