[openstreetmap/openstreetmap-website] AddServiceNameToActiveStorageBlobs migration Error (#3204)

chenmiaowei notifications at github.com
Mon May 24 13:56:34 UTC 2021


```
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.
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/issues/3204
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20210524/c14a0fa9/attachment.htm>


More information about the rails-dev mailing list