From 9c7a9edeccc7717900fcc65ef6247f5f6ec901a6 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Sun, 23 Jul 2017 18:57:28 -0400 Subject: [PATCH] Properly generate migrations for Rails 5.0+ Requires a migration version number to be specified in the constant. --- .../doorkeeper/migration_generator.rb | 12 +++++++- .../doorkeeper/templates/migration.rb | 2 +- spec/generators/migration_generator_spec.rb | 29 +++++++++++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/generators/doorkeeper/migration_generator.rb b/lib/generators/doorkeeper/migration_generator.rb index 820e8d156..53ad21be2 100644 --- a/lib/generators/doorkeeper/migration_generator.rb +++ b/lib/generators/doorkeeper/migration_generator.rb @@ -6,10 +6,20 @@ class Doorkeeper::MigrationGenerator < ::Rails::Generators::Base desc 'Installs Doorkeeper migration file.' def install - migration_template 'migration.rb', 'db/migrate/create_doorkeeper_tables.rb' + migration_template( + 'migration.rb', + 'db/migrate/create_doorkeeper_tables.rb', + migration_version: migration_version + ) end def self.next_migration_number(dirname) ActiveRecord::Generators::Base.next_migration_number(dirname) end + + def migration_version + if Rails.version >= "5.0.0" + "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + end + end end diff --git a/lib/generators/doorkeeper/templates/migration.rb b/lib/generators/doorkeeper/templates/migration.rb index 8cf99b349..5a2bd6a52 100644 --- a/lib/generators/doorkeeper/templates/migration.rb +++ b/lib/generators/doorkeeper/templates/migration.rb @@ -1,4 +1,4 @@ -class CreateDoorkeeperTables < ActiveRecord::Migration +class CreateDoorkeeperTables < ActiveRecord::Migration<%= migration_version %> def change create_table :oauth_applications do |t| t.string :name, null: false diff --git a/spec/generators/migration_generator_spec.rb b/spec/generators/migration_generator_spec.rb index b4ae20853..d39d226c6 100644 --- a/spec/generators/migration_generator_spec.rb +++ b/spec/generators/migration_generator_spec.rb @@ -10,11 +10,34 @@ describe 'after running the generator' do before :each do prepare_destination - run_generator end - it 'creates a migration' do - assert_migration 'db/migrate/create_doorkeeper_tables.rb' + context 'pre Rails 5.0.0' do + it 'creates a migration with no version specifier' do + expect(Rails).to receive(:version).at_least(:once).and_return '4.2.0' + stub_const("Rails::VERSION::MAJOR", 4) + stub_const("Rails::VERSION::MINOR", 2) + + run_generator + + assert_migration 'db/migrate/create_doorkeeper_tables.rb' do |migration| + assert migration.include?("ActiveRecord::Migration\n") + end + end + end + + context 'post Rails 5.0.0' do + it 'creates a migration with a version specifier' do + expect(Rails).to receive(:version).at_least(:once).and_return '5.0.0' + stub_const("Rails::VERSION::MAJOR", 5) + stub_const("Rails::VERSION::MINOR", 0) + + run_generator + + assert_migration 'db/migrate/create_doorkeeper_tables.rb' do |migration| + assert migration.include?("ActiveRecord::Migration[5.0]\n") + end + end end end end