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

Cannot rollback migrations that use add_index with PostgreSQL expressions instead of column name #43331

Closed
oliverguenther opened this issue Sep 29, 2021 · 0 comments · Fixed by #43333

Comments

@oliverguenther
Copy link
Contributor

Steps to reproduce

rails new cannot_rollback --database=postgresql
cd cannot_rollback
rails g migration CreateJsonTable

Open the generated migration, paste this migration into it

class CreateJsonTable < ActiveRecord::Migration[6.1]
  def change
    create_table :json_tables do |t|
      t.jsonb :data
      t.timestamps
    end

    add_index :json_tables,
              "(data->'foo')",
              using: :gin,
              name: 'index_json_tables_data_foo'
  end
end

Then run rails db:create db:migrate which runs through

Trying to roll back that index:

rails db:rollback

results in

== 20210929121223 CreateJsonTable: reverting ==================================
-- remove_index(:json_tables, "(data->'foo')", {:using=>:gin, :name=>"index_json_tables_data_foo"})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

No indexes found on json_tables with the options provided.
bin/rails:5:in `<top (required)>'
bin/spring:10:in `block in <top (required)>'
bin/spring:7:in `tap'
bin/spring:7:in `<top (required)>'

Expected behavior

The migration can be rolled back, especially since an explicit index name has been provded.

Actual behavior

As the expression is provided as column_name , https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L1433 does not allow the index to be removed by name.

The check in this line then fails to match the index https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L1447

What reasons would there be for an index not to be removable by its name if one is explictly given in the add_index definition? I'll gladly submit a PR that will skip this check if a name has been given: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L1446-L1448

System configuration

Rails version:
tested with current version 6.1

Ruby version:
irrelevant, but tested with 2.7.x

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

Successfully merging a pull request may close this issue.

2 participants