You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 21, 2023. It is now read-only.
Since I had a production application depending on this gem I wrote a large migration to move
from it to the new official flipper-active_record adapter gem. Posted here to help anyone who might face a similar struggle. This is a fully reversible migration.
I'm assuming you used the following original migration for flipper-activerecord:
The two migrations are similar aside from the following changes necessary to be compatible with flipper-active_record:
flipper_features table uses key string over name string to identify the feature
flipper_gates table uses feature_key string over flipper_feature_id integer to reference the related flipper_features record
flipper_gates table uses key string over name string to identify the gate
there is no foreign key associating flipper_features and flipper_gates
Since I'm assuming that — like me — you had both existing flipper_features and flipper_gates records in production which you could not afford to wipe, here's the migration:
classMigrateToFlipperActiveRecord < ActiveRecord::Migration# Create Migration classes to avoid relying on models that may not# exist in the future. See:# http://blog.testdouble.com/posts/2014-11-04-healthy-migration-habitsclassMigrationFeature < ActiveRecord::Baseself.table_name="flipper_features"endclassMigrationGate < ActiveRecord::Baseself.table_name="flipper_gates"enddefup# Add new columns and indicesadd_column:flipper_features,:key,:string# Migrate existing data to avoid errors when enabling null: falseMigrationFeature.all.find_eachdo |feature|
feature.update_attribute(:key,feature.name)endchange_column_null:flipper_features,:key,falseadd_column:flipper_gates,:key,:stringadd_column:flipper_gates,:feature_key,:string# Migrate existing data to avoid errors when enabling null: falseMigrationGate.all.find_eachdo |gate|
feature=MigrationFeature.find_by(id: gate.flipper_feature_id)gate.update(key: gate.name,feature_key: feature.key)endchange_column_null:flipper_gates,:key,falsechange_column_null:flipper_gates,:feature_key,falseadd_index:flipper_features,:key,unique: trueadd_index:flipper_gates,[:feature_key,:key],unique: true# Renove old columns (and indices automatically)remove_column:flipper_features,:namechange_table:flipper_gatesdo |t|
t.remove:flipper_feature_idt.remove:nameendenddefdown# Add old columns and indicesadd_column:flipper_features,:name,:string# Migrate existing data to avoid errors when enabling null: falseMigrationFeature.all.find_eachdo |feature|
feature.update_attribute(:name,feature.key)endchange_column_null:flipper_features,:name,falseadd_index:flipper_features,:name,unique: trueadd_column:flipper_gates,:flipper_feature_id,:integeradd_column:flipper_gates,:name,:string# Migrate existing data to avoid errors when enabling null: falseMigrationGate.all.find_eachdo |gate|
feature=MigrationFeature.find_by(key: gate.feature_key)gate.update(flipper_feature_id: feature.id,name: gate.key)endchange_column_null:flipper_gates,:flipper_feature_id,falsechange_column_null:flipper_gates,:name,falseadd_foreign_key:flipper_gates,:flipper_features,on_delete: :cascadeadd_index:flipper_gates,[:flipper_feature_id,:name],unique: true# Remove new columns (and indices automatically)remove_column:flipper_features,:keychange_table:flipper_gatesdo |t|
t.remove:feature_keyt.remove:keyendendend
I've run this migration in both directions and checked state so it should be safe but I highly recommend testing in your development and staging environments before running it in production.
The text was updated successfully, but these errors were encountered:
Since I had a production application depending on this gem I wrote a large migration to move
from it to the new official flipper-active_record adapter gem. Posted here to help anyone who might face a similar struggle. This is a fully reversible migration.
I'm assuming you used the following original migration for flipper-activerecord:
Again, I'm assuming you are moving to
flipper-active_record
which at the time of this writing uses the following migration:The two migrations are similar aside from the following changes necessary to be compatible with
flipper-active_record
:flipper_features
table useskey
string overname
string to identify the featureflipper_gates
table usesfeature_key
string overflipper_feature_id
integer to reference the relatedflipper_features
recordflipper_gates
table useskey
string overname
string to identify the gateflipper_features
andflipper_gates
Since I'm assuming that — like me — you had both existing
flipper_features
andflipper_gates
records in production which you could not afford to wipe, here's the migration:I've run this migration in both directions and checked state so it should be safe but I highly recommend testing in your development and staging environments before running it in production.
The text was updated successfully, but these errors were encountered: