Skip to content

Commit dfe9cf3

Browse files
authored
Merge pull request #790 from flippercloud/mysql-fix
ActiveRecord adapter: Fix migration error for MySQL
2 parents fa51aa5 + f1e2223 commit dfe9cf3

File tree

12 files changed

+414
-184
lines changed

12 files changed

+414
-184
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ jobs:
1313
--health-interval 10s
1414
--health-timeout 5s
1515
--health-retries 5
16+
postgres:
17+
image: postgres:13
18+
ports:
19+
- 5432:5432
20+
env:
21+
POSTGRES_USER: postgres
22+
POSTGRES_PASSWORD: postgres
23+
options: >-
24+
--health-cmd pg_isready
25+
--health-interval 10s
26+
--health-timeout 5s
27+
--health-retries 5
1628
strategy:
1729
matrix:
1830
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
@@ -41,7 +53,13 @@ jobs:
4153
REDIS_URL: redis://localhost:6379/0
4254
CI: true
4355
RAILS_VERSION: ${{ matrix.rails }}
56+
MYSQL_USER: root
57+
MYSQL_PASSWORD: root
58+
POSTGRES_USER: postgres
59+
POSTGRES_PASSWORD: postgres
4460
steps:
61+
- name: Set up MySQL
62+
run: sudo /etc/init.d/mysql start
4563
- name: Setup memcached
4664
uses: KeisukeYamashita/memcached-actions@v1
4765
- name: Start MongoDB

Changelog.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ All notable changes to this project will be documented in this file.
1212
undefined local variable or method `default_strict_value' for Flipper::Engine:Class
1313
```
1414

15+
### Additions/Changes
16+
17+
- Added `$ rails generate flipper:update` to generate necessary schema migrations (https://github.com/flippercloud/flipper/pull/787)
18+
1519
## v1.1.1
1620

1721
### Bug Fixes
@@ -40,12 +44,9 @@ All notable changes to this project will be documented in this file.
4044
```
4145
- Handle deprecation of Rack::File in Rack 3.1 (https://github.com/flippercloud/flipper/pull/773).
4246
- Human readable actor names in flipper-ui (https://github.com/flippercloud/flipper/pull/737).
43-
- Expressions are now available and considered "alpha". They are not yet documented, but you can see examples in [examples/expressions.rb](examples/expressions.rb). Those using the `flipper-active_record` adapter will want to migrate the database so it can store JSON expressions (https://github.com/flippercloud/flipper/pull/692)
47+
- Expressions are now available and considered "alpha". They are not yet documented, but you can see examples in [examples/expressions.rb](examples/expressions.rb). Those using the `flipper-active_record` adapter will want to [migrate the database](See https://github.com/flippercloud/flipper/issues/557) so it can store JSON expressions (https://github.com/flippercloud/flipper/pull/692)
4448
```
45-
$ rails generate migration change_flipper_gates_value_to_text
46-
```
47-
```ruby
48-
change_column :flipper_gates, :value, :text
49+
$ rails generate flipper:update && rails db:migrate
4950
```
5051
- Allow head requests to api (https://github.com/flippercloud/flipper/pull/759)
5152
- Cloud Telemetry alpha (https://github.com/flippercloud/flipper/pull/775).

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ gem 'benchmark-ips'
2525
gem 'stackprof-webnav'
2626
gem 'flamegraph'
2727
gem 'climate_control'
28+
gem 'mysql2'
29+
gem 'pg'
2830

2931
group(:guard) do
3032
gem 'guard'

lib/flipper/adapters/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Gate < Model
3434

3535
VALUE_TO_TEXT_WARNING = <<-EOS
3636
Your database needs migrated to use the latest Flipper features.
37-
See https://github.com/flippercloud/flipper/issues/557
37+
Run `rails generate flipper:update` and `rails db:migrate`.
3838
EOS
3939

4040
# Public: Initialize a new ActiveRecord adapter instance.

lib/generators/flipper/templates/migration.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %>
2-
def self.up
2+
def up
33
create_table :flipper_features do |t|
44
t.string :key, null: false
55
t.timestamps null: false
@@ -12,10 +12,10 @@ class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %>
1212
t.text :value
1313
t.timestamps null: false
1414
end
15-
add_index :flipper_gates, [:feature_key, :key, :value], unique: true
15+
add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 }
1616
end
1717

18-
def self.down
18+
def down
1919
drop_table :flipper_gates
2020
drop_table :flipper_features
2121
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %>
2+
def up
3+
create_table :flipper_features do |t|
4+
t.string :key, null: false
5+
t.timestamps null: false
6+
end
7+
add_index :flipper_features, :key, unique: true
8+
9+
create_table :flipper_gates do |t|
10+
t.string :feature_key, null: false
11+
t.string :key, null: false
12+
t.string :value
13+
t.timestamps null: false
14+
end
15+
add_index :flipper_gates, [:feature_key, :key, :value], unique: true
16+
end
17+
18+
def down
19+
drop_table :flipper_gates
20+
drop_table :flipper_features
21+
end
22+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class ChangeFlipperGatesValueToText < ActiveRecord::Migration<%= migration_version %>
4+
def up
5+
# Ensure this incremental update migration is idempotent
6+
return unless connection.column_exists? :flipper_gates, :value, :string
7+
8+
if index_exists? :flipper_gates, [:feature_key, :key, :value]
9+
remove_index :flipper_gates, [:feature_key, :key, :value]
10+
end
11+
change_column :flipper_gates, :value, :text
12+
add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 }
13+
end
14+
15+
def down
16+
change_column :flipper_gates, :value, :string
17+
end
18+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails/generators'
4+
require 'rails/generators/active_record'
5+
6+
module Flipper
7+
module Generators
8+
#
9+
# Rails generator used for updating Flipper in a Rails application.
10+
# Run it with +bin/rails g flipper:update+ in your console.
11+
#
12+
class UpdateGenerator < Rails::Generators::Base
13+
include ActiveRecord::Generators::Migration
14+
15+
TEMPLATES = File.join(File.dirname(__FILE__), 'templates/update')
16+
source_paths << TEMPLATES
17+
18+
# Generates incremental migration files unless they already exist.
19+
# All migrations should be idempotent e.g. +add_index+ is guarded with +if_index_exists?+
20+
def update_migration_files
21+
migration_templates = Dir.children(File.join(TEMPLATES, 'migrations')).sort
22+
migration_templates.each do |template_file|
23+
destination_file = template_file.match(/^\d*_(.*\.rb)/)[1] # 01_create_flipper_tables.rb.erb => create_flipper_tables.rb
24+
migration_template "migrations/#{template_file}", File.join(db_migrate_path, destination_file), skip: true
25+
end
26+
end
27+
28+
private
29+
30+
def migration_version
31+
"[#{ActiveRecord::VERSION::STRING.to_f}]"
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)