Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#224] Support custom data migration template #232

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ You can override this setting in `config/initializers/data_migrate.rb`
```ruby
DataMigrate.configure do |config|
config.data_migrations_path = 'db/awesomepath/'
config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
config.db_configuration = {
'host' => '127.0.0.1',
'database' => 'awesome_database',
Expand Down
3 changes: 3 additions & 0 deletions lib/data_migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
end

module DataMigrate
def self.root
File.dirname(__FILE__)
end
end
11 changes: 10 additions & 1 deletion lib/data_migrate/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ def config
end

class Config
attr_accessor :data_migrations_path, :db_configuration, :spec_name
attr_accessor :data_migrations_path, :data_template_path, :db_configuration, :spec_name

DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb"

def initialize
@data_migrations_path = "db/data/"
@data_template_path = DEFAULT_DATA_TEMPLATE_PATH
@db_configuration = nil
@spec_name = nil
end

def data_template_path=(value)
@data_template_path = value.tap do |path|
raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performing validation on the setter seems a bit unorthodox, alternatively this can be performed from within the data migration generator i.e. https://github.com/ilyakatz/data-migrate/pull/232/files#diff-a4575045875fb9b1c551bd2d20696bd405d5c84ed26c2c773a895ce660c392ceR30

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the idea of doing validation here so that the error is raised on startup and devs will know right away that something is wrong. so looks good to me

end
end
end
end
17 changes: 15 additions & 2 deletions lib/generators/data_migrate.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
require 'rails/generators/named_base'

module DataMigrate
module Generators
class DataMigrationGenerator < Rails::Generators::NamedBase #:nodoc:
def self.source_root
@_data_migrate_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
class << self
def source_root
build_data_migrate_source_root
end

private

def build_data_migrate_source_root
if DataMigrate.config.data_template_path == DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
else
File.expand_path(File.dirname(DataMigrate.config.data_template_path))
end
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/generators/data_migration/data_migration_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DataMigrationGenerator < Rails::Generators::NamedBase

def create_data_migration
set_local_assigns!
migration_template "data_migration.rb", data_migrations_file_path
migration_template template_path, data_migrations_file_path
end

protected
Expand All @@ -26,6 +26,10 @@ def set_local_assigns!
end
end

def template_path
DataMigrate.config.data_template_path
end

def migration_base_class_name
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
end
Expand Down
45 changes: 42 additions & 3 deletions spec/data_migrate/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
require "spec_helper"

describe DataMigrate::Config do

it "sets default data_migrations_path path", :no_override do
expect(DataMigrate.config.data_migrations_path).to eq "db/data/"
end

it "sets default data_template_path path", :no_override do
expect(DataMigrate.config.data_template_path).to eq DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
end

describe "data migration path configured" do
subject { DataMigrate.config.data_migrations_path }
before do
@before = DataMigrate.config.data_migrations_path
DataMigrate.configure do |config|
Expand All @@ -20,8 +24,43 @@
end
end

it do
expect(DataMigrate.config.data_migrations_path).to eq "db/awesome/"
it "equals the custom data migration path" do
is_expected.to eq "db/awesome/"
end
end

describe "data template path configured" do
subject { DataMigrate.config.data_template_path }

before do
@before = DataMigrate.config.data_template_path
DataMigrate.configure do |config|
config.data_template_path = data_template_path
end
end

let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end

after do
DataMigrate.configure do |config|
config.data_template_path = @before
end
end

it "equals the custom data template path" do
is_expected.to eq data_template_path
end

context "when path does not exist" do
subject { DataMigrate.config.data_template_path = invalid_path }

let(:invalid_path) { "lib/awesome/templates/data_migration.rb" }

it "checks that file exists on setting config var" do
expect { subject }.to raise_error { ArgumentError.new("File not found: '#{data_template_path}'") }
end
end
end
end
53 changes: 47 additions & 6 deletions spec/generators/data_migration/data_migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
require 'generators/data_migration/data_migration_generator'

describe DataMigrate::Generators::DataMigrationGenerator do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator }
subject { DataMigrate::Generators::DataMigrationGenerator }

describe :next_migration_number do
it "next migration" do
Timecop.freeze("2016-12-03 22:15:26 -0800") do
Expand All @@ -19,14 +20,18 @@
end

describe :migration_base_class_name do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
subject { generator.send(:migration_base_class_name) }

let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }

it "returns the correct base class name" do
expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
end
end

describe :create_data_migration do
let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }

let(:data_migrations_file_path) { 'abc/my_migration.rb' }

context 'when custom data migrations path has a trailing slash' do
Expand All @@ -35,7 +40,7 @@
end

it 'returns correct file path' do
expect(subject).to receive(:migration_template).with(
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)

Expand All @@ -49,12 +54,48 @@
end

it 'returns correct file path' do
expect(subject).to receive(:migration_template).with(
is_expected.to receive(:migration_template).with(
'data_migration.rb', data_migrations_file_path
)

subject.create_data_migration
end
end
end

describe ".source_root" do
subject { described_class.source_root }

let(:default_source_root) do
File.expand_path(
File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
)
end

it { is_expected.to eq default_source_root }

context "when DateMigrate.config.data_template_path is set" do
before do
@before = DataMigrate.config.data_template_path
DataMigrate.configure do |config|
config.data_template_path = data_template_path
end
end

let(:data_template_path) do
File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
end
let(:expected_source_root) { File.dirname(data_template_path) }

after do
DataMigrate.configure do |config|
config.data_template_path = @before
end
end

it "reads directory from config data template path" do
is_expected.to eq expected_source_root
end
end
end
end