data_migrate 8.4.2 → 8.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cc2467bde93f842786d6a5bb5077e8a1ef595a80e5868a03ae960552a232faa
4
- data.tar.gz: 452420d3616deaaa6776511bb6cabbe765f92106d19e67b017fafb717ca0d760
3
+ metadata.gz: c0ed214d2b242e9802276616e52abc79dc40081b413f6c38cdcbc5414d5897e9
4
+ data.tar.gz: f4045cba836825fa34a0da775d38999ea472f2fb8624537e8ffc216f01204e85
5
5
  SHA512:
6
- metadata.gz: aa097fbb8c8560e888984965d6ce10e9b09fcda1031c18aeccbe9e25b46db7baa3c9821e9ea18468a2df24aba7cb94412fc3fb15aa88e2a110f71018697db323
7
- data.tar.gz: f9f1bc42960a703223edd39c22285e6dfa0c541bf723152992da61b24a689c1b90e30dee0d2902b15a489d1606b681eb03b4b6d6723fccf932745b20d8f71f72
6
+ metadata.gz: c98becc1b27f1afc98a8e45c4c81ea35195a992f2ff7433d8891b542cc05063dde7ffd1655ba521e14e700e477acc23345b15a893e2a900c508fc0b72c69302c
7
+ data.tar.gz: 04560cc96cf6696b032b7a9d6892d70b772801b62910899ea2c360d7a17b98e8ab58bcd535e9a1fbdd40aa07c8633e92f5f52db8f9a6340c00788919eb5acf5b
@@ -3,7 +3,7 @@ name: Ruby Gem
3
3
  on:
4
4
  push:
5
5
  branches:
6
- - master
6
+ - main
7
7
 
8
8
  jobs:
9
9
  build:
data/Changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 8.5.0
4
+
5
+ Allow custom templates [bazay](https://github.com/bazay)
6
+
3
7
  ## 8.4.0
4
8
 
5
9
  Avoid Globally Accessible Functions for All Rake Tasks [berniechiu](https://github.com/berniechiu)
data/README.md CHANGED
@@ -108,6 +108,7 @@ You can override this setting in `config/initializers/data_migrate.rb`
108
108
  ```ruby
109
109
  DataMigrate.configure do |config|
110
110
  config.data_migrations_path = 'db/awesomepath/'
111
+ config.data_template_path = Rails.root.join("lib", "awesomepath", "custom_data_migration.rb")
111
112
  config.db_configuration = {
112
113
  'host' => '127.0.0.1',
113
114
  'database' => 'awesome_database',
@@ -12,12 +12,21 @@ module DataMigrate
12
12
  end
13
13
 
14
14
  class Config
15
- attr_accessor :data_migrations_path, :db_configuration, :spec_name
15
+ attr_accessor :data_migrations_path, :data_template_path, :db_configuration, :spec_name
16
+
17
+ DEFAULT_DATA_TEMPLATE_PATH = "data_migration.rb"
16
18
 
17
19
  def initialize
18
20
  @data_migrations_path = "db/data/"
21
+ @data_template_path = DEFAULT_DATA_TEMPLATE_PATH
19
22
  @db_configuration = nil
20
23
  @spec_name = nil
21
24
  end
25
+
26
+ def data_template_path=(value)
27
+ @data_template_path = value.tap do |path|
28
+ raise ArgumentError, "File not found: '#{path}'" unless path == DEFAULT_DATA_TEMPLATE_PATH || File.exists?(path)
29
+ end
30
+ end
22
31
  end
23
32
  end
@@ -1,3 +1,3 @@
1
1
  module DataMigrate
2
- VERSION = "8.4.2".freeze
2
+ VERSION = "8.5.0".freeze
3
3
  end
data/lib/data_migrate.rb CHANGED
@@ -19,4 +19,7 @@ else
19
19
  end
20
20
 
21
21
  module DataMigrate
22
+ def self.root
23
+ File.dirname(__FILE__)
24
+ end
22
25
  end
@@ -1,9 +1,22 @@
1
1
  require 'rails/generators/named_base'
2
+
2
3
  module DataMigrate
3
4
  module Generators
4
5
  class DataMigrationGenerator < Rails::Generators::NamedBase #:nodoc:
5
- def self.source_root
6
- @_data_migrate_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
6
+ class << self
7
+ def source_root
8
+ build_data_migrate_source_root
9
+ end
10
+
11
+ private
12
+
13
+ def build_data_migrate_source_root
14
+ if DataMigrate.config.data_template_path == DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
15
+ File.expand_path(File.join(File.dirname(__FILE__), generator_name, 'templates'))
16
+ else
17
+ File.expand_path(File.dirname(DataMigrate.config.data_template_path))
18
+ end
19
+ end
7
20
  end
8
21
  end
9
22
  end
@@ -14,7 +14,7 @@ module DataMigrate
14
14
 
15
15
  def create_data_migration
16
16
  set_local_assigns!
17
- migration_template "data_migration.rb", data_migrations_file_path
17
+ migration_template template_path, data_migrations_file_path
18
18
  end
19
19
 
20
20
  protected
@@ -26,6 +26,10 @@ module DataMigrate
26
26
  end
27
27
  end
28
28
 
29
+ def template_path
30
+ DataMigrate.config.data_template_path
31
+ end
32
+
29
33
  def migration_base_class_name
30
34
  "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
31
35
  end
@@ -1,12 +1,16 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe DataMigrate::Config do
4
-
5
4
  it "sets default data_migrations_path path", :no_override do
6
5
  expect(DataMigrate.config.data_migrations_path).to eq "db/data/"
7
6
  end
8
7
 
8
+ it "sets default data_template_path path", :no_override do
9
+ expect(DataMigrate.config.data_template_path).to eq DataMigrate::Config::DEFAULT_DATA_TEMPLATE_PATH
10
+ end
11
+
9
12
  describe "data migration path configured" do
13
+ subject { DataMigrate.config.data_migrations_path }
10
14
  before do
11
15
  @before = DataMigrate.config.data_migrations_path
12
16
  DataMigrate.configure do |config|
@@ -20,8 +24,43 @@ describe DataMigrate::Config do
20
24
  end
21
25
  end
22
26
 
23
- it do
24
- expect(DataMigrate.config.data_migrations_path).to eq "db/awesome/"
27
+ it "equals the custom data migration path" do
28
+ is_expected.to eq "db/awesome/"
29
+ end
30
+ end
31
+
32
+ describe "data template path configured" do
33
+ subject { DataMigrate.config.data_template_path }
34
+
35
+ before do
36
+ @before = DataMigrate.config.data_template_path
37
+ DataMigrate.configure do |config|
38
+ config.data_template_path = data_template_path
39
+ end
40
+ end
41
+
42
+ let(:data_template_path) do
43
+ File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
44
+ end
45
+
46
+ after do
47
+ DataMigrate.configure do |config|
48
+ config.data_template_path = @before
49
+ end
50
+ end
51
+
52
+ it "equals the custom data template path" do
53
+ is_expected.to eq data_template_path
54
+ end
55
+
56
+ context "when path does not exist" do
57
+ subject { DataMigrate.config.data_template_path = invalid_path }
58
+
59
+ let(:invalid_path) { "lib/awesome/templates/data_migration.rb" }
60
+
61
+ it "checks that file exists on setting config var" do
62
+ expect { subject }.to raise_error { ArgumentError.new("File not found: '#{data_template_path}'") }
63
+ end
25
64
  end
26
65
  end
27
66
  end
@@ -4,7 +4,8 @@ require 'rails/generators/migration'
4
4
  require 'generators/data_migration/data_migration_generator'
5
5
 
6
6
  describe DataMigrate::Generators::DataMigrationGenerator do
7
- let(:subject) { DataMigrate::Generators::DataMigrationGenerator }
7
+ subject { DataMigrate::Generators::DataMigrationGenerator }
8
+
8
9
  describe :next_migration_number do
9
10
  it "next migration" do
10
11
  Timecop.freeze("2016-12-03 22:15:26 -0800") do
@@ -19,14 +20,18 @@ describe DataMigrate::Generators::DataMigrationGenerator do
19
20
  end
20
21
 
21
22
  describe :migration_base_class_name do
22
- let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
23
+ subject { generator.send(:migration_base_class_name) }
24
+
25
+ let(:generator) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
26
+
23
27
  it "returns the correct base class name" do
24
- expect(subject.send(:migration_base_class_name)).to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
28
+ is_expected.to eq("ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]")
25
29
  end
26
30
  end
27
31
 
28
32
  describe :create_data_migration do
29
- let(:subject) { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
33
+ subject { DataMigrate::Generators::DataMigrationGenerator.new(['my_migration']) }
34
+
30
35
  let(:data_migrations_file_path) { 'abc/my_migration.rb' }
31
36
 
32
37
  context 'when custom data migrations path has a trailing slash' do
@@ -35,7 +40,7 @@ describe DataMigrate::Generators::DataMigrationGenerator do
35
40
  end
36
41
 
37
42
  it 'returns correct file path' do
38
- expect(subject).to receive(:migration_template).with(
43
+ is_expected.to receive(:migration_template).with(
39
44
  'data_migration.rb', data_migrations_file_path
40
45
  )
41
46
 
@@ -49,7 +54,7 @@ describe DataMigrate::Generators::DataMigrationGenerator do
49
54
  end
50
55
 
51
56
  it 'returns correct file path' do
52
- expect(subject).to receive(:migration_template).with(
57
+ is_expected.to receive(:migration_template).with(
53
58
  'data_migration.rb', data_migrations_file_path
54
59
  )
55
60
 
@@ -57,4 +62,40 @@ describe DataMigrate::Generators::DataMigrationGenerator do
57
62
  end
58
63
  end
59
64
  end
65
+
66
+ describe ".source_root" do
67
+ subject { described_class.source_root }
68
+
69
+ let(:default_source_root) do
70
+ File.expand_path(
71
+ File.dirname(File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb"))
72
+ )
73
+ end
74
+
75
+ it { is_expected.to eq default_source_root }
76
+
77
+ context "when DateMigrate.config.data_template_path is set" do
78
+ before do
79
+ @before = DataMigrate.config.data_template_path
80
+ DataMigrate.configure do |config|
81
+ config.data_template_path = data_template_path
82
+ end
83
+ end
84
+
85
+ let(:data_template_path) do
86
+ File.join(DataMigrate.root, "generators", "data_migration", "templates", "data_migration.rb")
87
+ end
88
+ let(:expected_source_root) { File.dirname(data_template_path) }
89
+
90
+ after do
91
+ DataMigrate.configure do |config|
92
+ config.data_template_path = @before
93
+ end
94
+ end
95
+
96
+ it "reads directory from config data template path" do
97
+ is_expected.to eq expected_source_root
98
+ end
99
+ end
100
+ end
60
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_migrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.4.2
4
+ version: 8.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew J Vargo
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2022-12-14 00:00:00.000000000 Z
13
+ date: 2023-01-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord