Inherit from the new versioned migration class

e.g. `ActiveRecord::Migration[5.1]`

Fixes #949

Following the example of the devise gem, use erb. In addition, add
the `.erb` file extension to fix syntax highlighting in editor.

Deletes create_versions_spec.rb because the file is no longer valid
ruby so those tests will have to be re-written. We can either
generate a valid ruby file and continue to use similar assertions,
or we can convert those features to use erb, which will have the
benefit of cleaner generated files.
This commit is contained in:
Jared Beck 2017-04-10 11:10:15 -04:00
parent 9cdfd64812
commit f92cfbc23d
8 changed files with 25 additions and 55 deletions

View File

@ -15,7 +15,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Fixed
- None
- [#949](https://github.com/airblade/paper_trail/issues/949) - Inherit from the
new versioned migration class, e.g. `ActiveRecord::Migration[5.1]`
## 7.0.0 (2017-04-01)

View File

@ -50,7 +50,18 @@ module PaperTrail
if self.class.migration_exists?(migration_dir, template)
::Kernel.warn "Migration already exists: #{template}"
else
migration_template "#{template}.rb", "db/migrate/#{template}.rb"
migration_template(
"#{template}.rb.erb",
"db/migrate/#{template}.rb",
migration_version: migration_version
)
end
end
def migration_version
major = ActiveRecord::VERSION::MAJOR
if major >= 5
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
end
end
end

View File

@ -1,6 +1,6 @@
# This migration creates the `versions` table, the only schema PT requires.
# All other migrations PT provides are optional.
class CreateVersions < ActiveRecord::Migration
class CreateVersions < ActiveRecord::Migration<%= migration_version %>
# Class names of MySQL adapters.
# - `MysqlAdapter` - Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
# - `Mysql2Adapter` - Used by `mysql2` gem.

View File

@ -15,12 +15,21 @@ RSpec.describe PaperTrail::InstallGenerator, type: :generator do
end
it "generates a migration for creating the 'versions' table" do
expected_parent_class = lambda {
old_school = "ActiveRecord::Migration"
ar_version = ActiveRecord::VERSION
if ar_version::MAJOR >= 5
format("%s[%d.%d]", old_school, ar_version::MAJOR, ar_version::MINOR)
else
old_school
end
}.call
expect(destination_root).to(
have_structure {
directory("db") {
directory("migrate") {
migration("create_versions") {
contains "class CreateVersions"
contains("class CreateVersions < " + expected_parent_class)
contains "def change"
contains "create_table :versions"
}

View File

@ -1,51 +0,0 @@
require "rails_helper"
require "generators/paper_trail/templates/create_versions"
RSpec.describe CreateVersions do
describe "#change", verify_stubs: false do
let(:migration) { described_class.new }
before do
allow(migration).to receive(:add_index)
allow(migration).to receive(:create_table)
end
it "creates the versions table" do
migration.change
expect(migration).to have_received(:create_table) do |arg1|
expect(arg1).to eq(:versions)
end
end
case ENV["DB"]
when "mysql"
it "uses InnoDB engine" do
migration.change
expect(migration).to have_received(:create_table) do |_, arg2|
expect(arg2[:options]).to match(/ENGINE=InnoDB/)
end
end
it "uses utf8mb4 character set" do
migration.change
expect(migration).to have_received(:create_table) do |_, arg2|
expect(arg2[:options]).to match(/DEFAULT CHARSET=utf8mb4/)
end
end
it "uses utf8mb4_col collation" do
migration.change
expect(migration).to have_received(:create_table) do |_, arg2|
expect(arg2[:options]).to match(/COLLATE=utf8mb4_general_ci/)
end
end
else
it "passes an empty options hash to create_table" do
migration.change
expect(migration).to have_received(:create_table) do |_, arg2|
expect(arg2).to eq({})
end
end
end
end
end