flippercloud / flipper

🐬 Beautiful, performant feature flags for Ruby.
https://www.flippercloud.io/docs
MIT License
3.7k stars 413 forks source link

Error when running migrations with Flipper and Strong Migrations #801

Closed jadekstewart3 closed 10 months ago

jadekstewart3 commented 10 months ago

Description

I'm encountering an issue when running migrations with Flipper and Strong Migrations gems in my application. The specific error I'm facing is related to CREATE INDEX CONCURRENTLY not being able to run inside a transaction block.

Error:

Your database needs migrated to use the latest Flipper features.
        Run  rails generate flipper:update and rails db:migrate.
== 20231218222029 ChangeFlipperGatesValueToText: migrating ====================
-- index_exists?(:flipper_gates, [:feature_key, :key, :value])
   -> 0.0051s
-- remove_index(:flipper_gates, [:feature_key, :key, :value])
   -> 0.0052s
-- change_column(:flipper_gates, :value, :text)
   -> 0.0010s
-- add_index(:flipper_gates, [:feature_key, :key, :value], {:unique=>true, :length=>{:value=>255}, :algorithm=>:concurrently})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

PG::ActiveSqlTransaction: ERROR:  CREATE INDEX CONCURRENTLY cannot run inside a transaction block

Generated after running rails db:migrate

Environment:

Ruby version: 3.2.2 Rails version: 7.0.8 Flipper gem version: 1.1.2 Strong Migrations gem version: 1.6.4 Database: PostgreSQL

bkeepers commented 10 months ago

@jadekstewart3 sorry you're running into an issue. We love the strong migrations gem and use it as well.

A couple ideas to solve it:

  1. Use disable_ddl_transaction! to disable the transaction.
    class ChangeFlipperGatesValueToText < ActiveRecord::Migration[7.0]
    disable_ddl_transaction!
    # …
    end
  2. I don't think dropping and re-creating the index is actually necessary on Postgres (we didn't do it anyway on flippercloud.io, but added it because mysql couldn't handle the change in column type). So you can remove the remove_index and add_index lines entirely and should be fine.

Let me know if neither of those solve it for you and we'll reopen this issue.