-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdata_migration_generator.rb
More file actions
71 lines (57 loc) · 2.12 KB
/
data_migration_generator.rb
File metadata and controls
71 lines (57 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "generators/data_migrate"
require "rails/generators"
require "rails/generators/active_record/migration"
require "rails/generators/migration"
require "data_migrate/config"
module DataMigrate
module Generators
class DataMigrationGenerator < Rails::Generators::NamedBase
namespace "data_migration"
include ActiveRecord::Generators::Migration
argument :attributes, type: :array, default: [], banner: "field:type field:type"
def create_data_migration
set_local_assigns!
migration_template template_path, data_migrations_file_path
create_data_migration_test
end
protected
def create_data_migration_test
return unless DataMigrate.config.test_support_enabled
case DataMigrate::Helpers::InferTestSuiteType.new.call
when :rspec
template "data_migration_spec.rb", data_migrations_spec_file_path
when :minitest
template "data_migration_test.rb", data_migrations_test_file_path
end
end
def data_migrations_test_file_path
File.join(Rails.root, 'test', data_migrations_path, "#{migration_file_name}_test.rb")
end
def data_migrations_spec_file_path
File.join(Rails.root, 'spec', data_migrations_path, "#{migration_file_name}_spec.rb")
end
def set_local_assigns!
if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
@migration_action = $1
@table_name = $2.pluralize
end
end
def template_path
DataMigrate.config.data_template_path
end
def migration_base_class_name
"ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
end
def data_migrations_file_path
File.join(data_migrations_path, "#{file_name}.rb")
end
def data_migrations_file_path_with_version
File.join(data_migrations_path, "#{migration_number}_#{migration_file_name}.rb")
end
# Use the first path in the data_migrations_path as the target directory
def data_migrations_path
Array.wrap(DataMigrate.config.data_migrations_path).first
end
end
end
end