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

Migrations failing on 'well' designed tables #5763

Open
pacMakaveli opened this issue Feb 10, 2025 · 0 comments
Open

Migrations failing on 'well' designed tables #5763

pacMakaveli opened this issue Feb 10, 2025 · 0 comments

Comments

@pacMakaveli
Copy link

I've been using Devise for years and today, for

Image

my migration failed because, well, I designed my table properly..

== TIMESTAMP AddDeviseToUsers: migrating =================================
-- change_table(:users)
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:7:in 'block in AddDeviseToUsers.up'
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:5:in 'AddDeviseToUsers.up'

Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists (ActiveRecord::StatementInvalid)
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:7:in 'block in AddDeviseToUsers.up'
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:5:in 'AddDeviseToUsers.up'

Caused by:
PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists (PG::DuplicateColumn)
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:7:in 'block in AddDeviseToUsers.up'
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:5:in 'AddDeviseToUsers.up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

and

== TIMESTAMP AddDeviseToUsers: migrating =================================
-- change_table(:users)
   -> 0.0444s
-- add_index(:users, :email, {unique: true})
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

PG::DuplicateTable: ERROR:  relation "index_users_on_email" already exists
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:39:in 'AddDeviseToUsers.up'

Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR:  relation "index_users_on_email" already exists (ActiveRecord::StatementInvalid)
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:39:in 'AddDeviseToUsers.up'

Caused by:
PG::DuplicateTable: ERROR:  relation "index_users_on_email" already exists (PG::DuplicateTable)
/path/to/project/db/migrate/TIMESTAMP_add_devise_to_users.rb:39:in 'AddDeviseToUsers.up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

It's not that big of a thing as the fix is quite trivial, but I feel like this shouldn't happen in the first place.

My proposed fix would be to add some checks in: lib/generators/active_record/templates/migration_existing.rb

add_index :<%= table_name %>, :email, unique: true, if_not_exists: true

and

<% attributes.each do |attribute| -%>
  t.<%= attribute.type %> :<%= attribute.name %> unless t.column_exists?(attribute)
<% end -%>

Happy to sort a PR for this if we're all in agreement.

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

No branches or pull requests

1 participant