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

When adding a column with a non-null default the suggested migration does not work #63

Closed
Florent2 opened this issue Mar 14, 2019 · 1 comment
Labels

Comments

@Florent2
Copy link

PostgreSQL version 9.5.13, Ruby 2.3.8 and Rails 4.2.11.

When running this migration

class AddIsAdminToUsers < ActiveRecord::Migration
  def change
    add_column :users, :is_admin, :boolean, default: false, null: false
  end
end

I get this from strong_migrations:

== 20190314162335 AddIsAdminToUsers: migrating - Shard: master ================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

=== Dangerous operation detected #strong_migrations ===

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 AddIsAdminToUsers < ActiveRecord::Migration
  def up
    add_column :users, :is_admin, :boolean, null: false
    change_column_default :users, :is_admin, false
  end

  def down
    remove_column :users, :is_admin
  end
end

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

class BackfillAddIsAdminToUsers < ActiveRecord::Migration
  disable_ddl_transaction!

  def change
    User.find_in_batches do |records|
      User.where(id: records.map(&:id)).update_all is_admin: false
    end
  end
end

But I create those suggested migrations and run them, the first one fails with the error:

== 20190314162335 AddIsAdminToUsers: migrating - Shard: master ================
-- add_column(:users, :is_admin, :boolean, {:null=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::NotNullViolation: ERROR:  column "is_admin" contains null values
: ALTER TABLE "users" ADD "is_admin" boolean NOT NULL

This is because of null: false in the suggested line

add_column :users, :is_admin, :boolean, null: false

Should instead this line be the following instead?

add_column :users, :is_admin, :boolean, null: true

and an additional third migration should be suggested to set the NOT NULL constraint?

change_column_null :users, :is_admin, false
@Florent2 Florent2 changed the title When adding a column with a non-null default the suggestion migration does not work When adding a column with a non-null default the suggested migration does not work Mar 14, 2019
@ankane
Copy link
Owner

ankane commented Mar 14, 2019

Hey @Florent2, thanks for reporting 👍 Yeah, the null option should be removed from the first migration if it's false (since true is the default) and a third migration would be needed to change the null value. Feel free to submit a PR, or I'll get around to it when I have some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants