multi_ar 4.1.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/lib/multi_ar/database.rb +2 -2
- data/lib/multi_ar/interface.rb +34 -10
- data/lib/multi_ar/rake/migration_generator.rb +1 -3
- data/lib/multi_ar/rake/tasks.rb +60 -8
- data/lib/multi_ar/version.rb +1 -1
- data/lib/multi_ar.rb +67 -18
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e703260ff0896138cefe607320db98e5c020bbe0233117a518e833ae5afb0408
|
4
|
+
data.tar.gz: 2a2d097579ffb99fd8735b4fe5c53aef110d5edabc47761375b0edcbc4f59174
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c8c633fbdc59cd7d7bf4e01429956a602776f26bb726d2b26ef55985786c98e8cebfd960040dd540e5255d4405a227b805a5ed20859c10723d29cbaf81095d0
|
7
|
+
data.tar.gz: 665b59f332f12c9c6ee82629fb07a7e06fb3e56dc3425433c1e6801672ad1db8b178465450c446168a4e6f8ad639f4717ccf620efa2c6a7383f5338c5cc8cd87
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/multi_ar/database.rb
CHANGED
@@ -31,8 +31,8 @@ module MultiAR
|
|
31
31
|
|
32
32
|
# @return real connection name, nil in case connection is not available
|
33
33
|
def self.connection_name base_name
|
34
|
-
raise "#{base_name} is not in databases configuration variable." unless MultiAR
|
35
|
-
return nil unless MultiAR
|
34
|
+
raise "#{base_name} is not in databases configuration variable." unless MultiAR::databases.include? base_name
|
35
|
+
return nil unless MultiAR::databases.include? base_name
|
36
36
|
"#{base_name}_#{MultiAR.app.environment}"
|
37
37
|
end
|
38
38
|
|
data/lib/multi_ar/interface.rb
CHANGED
@@ -20,8 +20,7 @@ module MultiAR
|
|
20
20
|
# - dry # boolean
|
21
21
|
# - environment # `true` or `String`
|
22
22
|
# - verbose # boolean
|
23
|
-
# - databases #
|
24
|
-
# - migration_dir # String
|
23
|
+
# - databases # false, `Array` or `Hash`.
|
25
24
|
#
|
26
25
|
# If value is `true`, an option will be added to CLI interface. If the value is something else, the option will be populated by this value instead.
|
27
26
|
#
|
@@ -79,7 +78,7 @@ module MultiAR
|
|
79
78
|
# TODO: not implemented currently, do we really need this?
|
80
79
|
#p.opt "list_databases", "Lists databases that contains migrations in the gem", type: :flag
|
81
80
|
# TODO: should we do migration_dirs here too instead?
|
82
|
-
p.opt "migration_dir", "The directory where migrations for databases are read from", type: :string, default: "db/migrate"
|
81
|
+
#p.opt "migration_dir", "The directory where migrations for databases are read from", type: :string, default: "db/migrate"
|
83
82
|
p.opt "task", "Rake task to execute", short: "t", type: :string
|
84
83
|
p.opt "tasks", "List available Rake tasks", short: "T", type: :flag
|
85
84
|
end
|
@@ -136,14 +135,31 @@ module MultiAR
|
|
136
135
|
def init_multi_ar
|
137
136
|
opts = {}
|
138
137
|
opts[:db_config] = @opts["db_config"] unless @opts["db_config"].nil?
|
139
|
-
opts[:migration_dirs] = [ @opts["migration_dir"] ]
|
138
|
+
#opts[:migration_dirs] = [ @opts["migration_dir"] ]
|
140
139
|
opts[:config] = @opts["config"] unless @opts["config"].nil?
|
141
|
-
opts[:databases] = @opts["databases"]
|
140
|
+
opts[:databases] = parse_databases_input(@opts["databases"])
|
142
141
|
opts[:environment] = @opts["environment"]
|
143
142
|
opts[:verbose] = @opts["verbose"]
|
143
|
+
opts[:migration_framework] = @migration_framework
|
144
144
|
@multi_ar = MultiAR.new opts
|
145
145
|
end
|
146
146
|
|
147
|
+
def parse_databases_input databases
|
148
|
+
raise "You did not give proper databases. Please see --help for instructions." unless databases.respond_to? :each
|
149
|
+
|
150
|
+
out = {}
|
151
|
+
databases.each do |database|
|
152
|
+
if database.include? ":"
|
153
|
+
splitted = database.split(":")
|
154
|
+
out[splitted[0]] = splitted[1]
|
155
|
+
else
|
156
|
+
out[database] = "db/migrate/#{database}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
out
|
161
|
+
end
|
162
|
+
|
147
163
|
# @note This method will always quit the application or raise another exception for errors. Catch SystemExit if that’s not good for you.
|
148
164
|
def bootstrap opts
|
149
165
|
raise "--databases must be given when bootstrapping." unless opts["databases"]
|
@@ -171,7 +187,12 @@ module MultiAR
|
|
171
187
|
|
172
188
|
def bootstrap_db_config opts
|
173
189
|
str = ""
|
174
|
-
opts["databases"]
|
190
|
+
databases = parse_databases_input(opts["databases"])
|
191
|
+
databases.each do |db, migration_path|
|
192
|
+
# This is a bit misleading place to create the migration dir, but it needs to be done somewhere.
|
193
|
+
FileUtils.mkdir_p(migration_path)
|
194
|
+
|
195
|
+
# Create the config file
|
175
196
|
[ "development", "production", "test"].each do |env|
|
176
197
|
full_name = "#{db}_#{env}"
|
177
198
|
str << <<-EOS.gsub(/^ {10}/, "")
|
@@ -241,11 +262,14 @@ module MultiAR
|
|
241
262
|
def bootstrap_config opts
|
242
263
|
settings_file = "config/settings.yaml"
|
243
264
|
return if File.exist? settings_file
|
244
|
-
str =
|
245
|
-
databases:
|
265
|
+
str = <<~EOF
|
266
|
+
databases:
|
246
267
|
EOF
|
247
|
-
opts["databases"]
|
248
|
-
|
268
|
+
databases = parse_databases_input(opts["databases"])
|
269
|
+
databases.each do |database, path|
|
270
|
+
str << "-\n"
|
271
|
+
str << " database: #{database}\n"
|
272
|
+
str << " migration_path: #{path}\n"
|
249
273
|
end
|
250
274
|
|
251
275
|
File.open settings_file, "w" do |f|
|
@@ -10,9 +10,7 @@ module ActiveRecordMigrations
|
|
10
10
|
source_root ::ActiveRecord::Generators::MigrationGenerator.source_root
|
11
11
|
|
12
12
|
def db_migrate_path
|
13
|
-
|
14
|
-
db_dir = ::ActiveRecord::Tasks::DatabaseTasks.sub_db_dir
|
15
|
-
"#{dir}/#{db_dir}"
|
13
|
+
databases = MultiAR::MultiAR::databases.first[1]
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|
data/lib/multi_ar/rake/tasks.rb
CHANGED
@@ -78,7 +78,9 @@ module Rake
|
|
78
78
|
name = args[:name] || ENV["name"]
|
79
79
|
options = args[:options] || ENV["options"]
|
80
80
|
|
81
|
-
|
81
|
+
if MultiAR::MultiAR::databases.size != 1
|
82
|
+
raise "You need to specify exactly one database for migration generation. See --databases. Given databases: #{MultiAR::MultiAR::databases.inspect}"
|
83
|
+
end
|
82
84
|
|
83
85
|
unless name
|
84
86
|
generator = Rails::Generators.find_by_namespace "migration"
|
@@ -100,7 +102,7 @@ module Rake
|
|
100
102
|
multiple_databases_task "migrate", "db" do |database_name|
|
101
103
|
establish_connection database_name
|
102
104
|
|
103
|
-
context = ActiveRecord::MigrationContext.new
|
105
|
+
context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
104
106
|
context.migrate
|
105
107
|
|
106
108
|
#MultiAR::MultiAR.migration_dirs.each do |dir|
|
@@ -118,11 +120,59 @@ module Rake
|
|
118
120
|
multiple_databases_task "rollback", "db" do |database_name|
|
119
121
|
establish_connection database_name
|
120
122
|
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
123
|
+
|
124
|
+
context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
125
|
+
context.rollback step
|
126
|
+
end
|
127
|
+
|
128
|
+
multiple_databases_task "forward", "db" do |database_name|
|
129
|
+
establish_connection database_name
|
130
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
131
|
+
|
132
|
+
context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
133
|
+
context.forward step
|
134
|
+
end
|
135
|
+
|
136
|
+
multiple_databases_task "down", "db:migrate" do |database_name|
|
137
|
+
establish_connection database_name
|
138
|
+
raise "db:down is used to go back to certain version. Use db:rollback if you want to go back n migrations." unless ENV["VERSION"]
|
139
|
+
version = ENV["VERSION"]
|
140
|
+
|
141
|
+
context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
142
|
+
context.down version
|
143
|
+
end
|
144
|
+
|
145
|
+
multiple_databases_task "up", "db:migrate" do |database_name|
|
146
|
+
establish_connection database_name
|
147
|
+
raise "db:up is used to go to certain version. Use db:forward if you want to go up n migrations." unless ENV["VERSION"]
|
148
|
+
version = ENV["VERSION"]
|
149
|
+
|
150
|
+
context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
151
|
+
context.up version
|
152
|
+
end
|
153
|
+
|
154
|
+
multiple_databases_task "status", "db:migrate" do |database_name|
|
155
|
+
establish_connection database_name
|
156
|
+
|
157
|
+
unless ActiveRecord::SchemaMigration.table_exists?
|
158
|
+
abort "Schema migrations table does not exist yet."
|
125
159
|
end
|
160
|
+
|
161
|
+
#raise "db:up is used to go to certain version. Use db:forward if you want to go up n migrations." unless ENV["VERSION"]
|
162
|
+
#version = ENV["VERSION"]
|
163
|
+
|
164
|
+
#context = ActiveRecord::MigrationContext.new(MultiAR::MultiAR.databases[database_name])
|
165
|
+
#context.up version
|
166
|
+
|
167
|
+
# output
|
168
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
169
|
+
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
170
|
+
puts "-" * 50
|
171
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
172
|
+
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
173
|
+
end
|
174
|
+
puts
|
175
|
+
|
126
176
|
end
|
127
177
|
|
128
178
|
multiple_databases_task "drop", "db" do |database_name|
|
@@ -146,14 +196,16 @@ module Rake
|
|
146
196
|
name = (namespace && name) || "#{namespace}:#{name_without_namespace}"
|
147
197
|
old_comment = rename_task name, namespace
|
148
198
|
|
199
|
+
databases = MultiAR::MultiAR::databases
|
200
|
+
|
149
201
|
DSL.desc "Runs task #{name} for all selected databases"
|
150
202
|
DSL.task name.to_sym do
|
151
|
-
databases.each do |database_name|
|
203
|
+
databases.each do |database_name, migration_path|
|
152
204
|
::Rake::Task["#{name}:#{database_name}"].invoke
|
153
205
|
end
|
154
206
|
end
|
155
207
|
|
156
|
-
databases.each do |database_name|
|
208
|
+
databases.each do |database_name, migration_path|
|
157
209
|
DSL.desc old_comment
|
158
210
|
DSL.task :"#{name}:#{database_name}" do
|
159
211
|
yield database_name
|
data/lib/multi_ar/version.rb
CHANGED
data/lib/multi_ar.rb
CHANGED
@@ -12,13 +12,12 @@ module MultiAR
|
|
12
12
|
# Must be initialized before most actions works, that relies on MultiAR#app for getting configuration.
|
13
13
|
class MultiAR
|
14
14
|
|
15
|
-
attr_reader :databases
|
16
15
|
attr_reader :db_config
|
17
16
|
attr_reader :environment
|
18
17
|
|
19
18
|
# @api private
|
20
19
|
# This will always be overridden, when MultiAR is initialized. Don’t try to do any funny logic with this.
|
21
|
-
|
20
|
+
@@__databases = {}
|
22
21
|
|
23
22
|
class << self
|
24
23
|
# Instance of MultiAR::MultiAR, automatically assigned by MultiAR::MultiAR#new.
|
@@ -28,7 +27,7 @@ module MultiAR
|
|
28
27
|
|
29
28
|
# @param databases array of available databases
|
30
29
|
# @todo config file is overriding parameters passed here... I think it should be other way around, but need more custom logic for that :/
|
31
|
-
def initialize databases:, environment: "development", config: "config/settings.yaml", db_config: "config/database.yaml",
|
30
|
+
def initialize databases:, environment: "development", config: "config/settings.yaml", db_config: "config/database.yaml", verbose: false, migration_framework: true
|
32
31
|
|
33
32
|
# first load config
|
34
33
|
if not config.nil? and File.exist? config
|
@@ -36,6 +35,13 @@ module MultiAR
|
|
36
35
|
config = Psych.load_file config
|
37
36
|
b = binding
|
38
37
|
config.each do |key, value|
|
38
|
+
if key == "databases"
|
39
|
+
out = {}
|
40
|
+
value.each do |database|
|
41
|
+
out[database["database"]] = database["migration_path"]
|
42
|
+
end
|
43
|
+
value = out
|
44
|
+
end
|
39
45
|
b.local_variable_set key.to_sym, value
|
40
46
|
end
|
41
47
|
end
|
@@ -44,23 +50,26 @@ module MultiAR
|
|
44
50
|
raise "#{db_config} is not valid path to a file. Try specifying --db-config <path> or configuring it in the configuration file." if db_config.nil? or !File.exist?(db_config)
|
45
51
|
raise "databases is not responding to :each. Try passing passing --databases <database> or configuring it in the configuration file." unless databases.respond_to? :each
|
46
52
|
|
47
|
-
|
53
|
+
parse_databases_input databases
|
54
|
+
|
55
|
+
#@databases = databases
|
48
56
|
@db_config = db_config
|
49
57
|
@environment = environment
|
58
|
+
@migration_framework = migration_framework
|
50
59
|
#@@migration_dirs = migration_dirs unless migration_dirs.empty? # This takes care of that it will only be overridden if there is any given values, making default configs work
|
51
|
-
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = migration_dirs
|
60
|
+
#ActiveRecord::Tasks::DatabaseTasks.migrations_paths = migration_dirs
|
52
61
|
|
53
62
|
Database.initialize db_config: db_config
|
54
63
|
|
55
|
-
ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir }
|
56
|
-
ActiveRecord::Tasks::DatabaseTasks.sub_db_dir = databases.first # TODO: I don’t think this is how it should work
|
64
|
+
#ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir }
|
65
|
+
#ActiveRecord::Tasks::DatabaseTasks.sub_db_dir = databases.first # TODO: I don’t think this is how it should work
|
57
66
|
|
58
67
|
@rake = ::Rake::Application.new
|
59
68
|
::Rake.application = @rake
|
60
69
|
@rake.init
|
61
70
|
::Rake::TaskManager.record_task_metadata = true
|
62
71
|
|
63
|
-
Rake::Tasks.databases = databases
|
72
|
+
#Rake::Tasks.databases = databases
|
64
73
|
Rake::Tasks.environment = environment
|
65
74
|
Rake::Tasks.define
|
66
75
|
|
@@ -69,19 +78,39 @@ module MultiAR
|
|
69
78
|
MultiAR.app = self
|
70
79
|
end
|
71
80
|
|
81
|
+
# A helper method to add migrations for multiple databases that reside in same location.
|
82
|
+
#
|
83
|
+
# Expects the given directory contain subdirectories that contain the actual migrations.
|
84
|
+
# For example, `db/migrate/_db_name_`, where `db/migrate` is dir given as an argument to this
|
85
|
+
# method and `_db_name_` is name of the database.
|
86
|
+
def self.add_migration_dir dir
|
87
|
+
raise "Directory #{dir} does not exist." unless Dir.exist? dir
|
88
|
+
|
89
|
+
Dir.chdir dir do
|
90
|
+
dbs = Dir.glob "*/"
|
91
|
+
dbs.each do |database|
|
92
|
+
add_database database, "#{dir}/#{database.chop}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
72
97
|
# Array of paths to directories where migrations resides.
|
73
|
-
# @see
|
98
|
+
# @see add_database
|
74
99
|
def self.migration_dirs
|
75
100
|
ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
76
101
|
end
|
77
102
|
|
103
|
+
def self.migration_dir_for database_name
|
104
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
105
|
+
end
|
106
|
+
|
78
107
|
# Outputs contents if verbose flag has been passed.
|
79
108
|
def self.verb str
|
80
109
|
return unless @@verbose
|
81
110
|
puts str
|
82
111
|
end
|
83
112
|
|
84
|
-
# Add a
|
113
|
+
# Add a database and its migration path. For standard Rails setup, this would be "customdbname", “db/migrate”.
|
85
114
|
#
|
86
115
|
# The directory structure of how MultiAR uses the path is a bit different from traditional way: for each
|
87
116
|
# database, there is directory inside the migration dir.
|
@@ -90,9 +119,12 @@ module MultiAR
|
|
90
119
|
# migrations would be looked from path “my/migration/dir/messy_database”.
|
91
120
|
#
|
92
121
|
# @note often you want to add full path to this dir, `__dir__` is useful for this.
|
93
|
-
def self.
|
94
|
-
|
95
|
-
|
122
|
+
def self.add_database database_name, migration_path
|
123
|
+
if @migration_framework
|
124
|
+
raise "Migration dir #{migration_path} does not exist." unless Dir.exist? migration_path
|
125
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths << migration_path
|
126
|
+
end
|
127
|
+
@@__databases[database_name] = migration_path
|
96
128
|
end
|
97
129
|
|
98
130
|
# @todo this shows rake in start of the command, we want to show multi_ar instead.
|
@@ -107,9 +139,26 @@ module MultiAR
|
|
107
139
|
def rake_task task_name
|
108
140
|
@rake.invoke_task task_name
|
109
141
|
end
|
110
|
-
end
|
111
|
-
end
|
112
142
|
|
113
|
-
|
114
|
-
|
115
|
-
|
143
|
+
def self.databases
|
144
|
+
@@__databases
|
145
|
+
end
|
146
|
+
|
147
|
+
private
|
148
|
+
|
149
|
+
def parse_databases_input dbs
|
150
|
+
if dbs.kind_of? Array
|
151
|
+
dbs.each do |database|
|
152
|
+
::MultiAR::MultiAR::add_database database, "db/migrate/#{database}"
|
153
|
+
end
|
154
|
+
return
|
155
|
+
end
|
156
|
+
|
157
|
+
raise "input databases needs to be either Hash or Array" unless dbs.kind_of? Hash
|
158
|
+
|
159
|
+
dbs.each do |database, migration_path|
|
160
|
+
::MultiAR::MultiAR::add_database database, migration_path
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_ar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samu Voutilainen
|
@@ -29,7 +29,7 @@ cert_chain:
|
|
29
29
|
w7Q+3AEK9ifd9ywHO2Ott0JdJEadxhU4N0cI6bQg+uJiSYmdBK0vEJN4lhNg1w37
|
30
30
|
C6hgCMQL3/D3kq732F4KGypqykYwx1wRDGHLxDTBHY26sy/TXfHcj4zPuKA=
|
31
31
|
-----END CERTIFICATE-----
|
32
|
-
date: 2018-
|
32
|
+
date: 2018-07-04 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: trollop
|
@@ -195,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
195
|
version: '0'
|
196
196
|
requirements: []
|
197
197
|
rubyforge_project:
|
198
|
-
rubygems_version: 2.
|
198
|
+
rubygems_version: 2.7.3
|
199
199
|
signing_key:
|
200
200
|
specification_version: 4
|
201
201
|
summary: Multi database feature set for ActiveRecord
|
metadata.gz.sig
CHANGED
Binary file
|