multi_ar 1.1.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/multi_ar/database.rb +3 -4
- data/lib/multi_ar/interface.rb +63 -24
- data/lib/multi_ar/rake/migration_generator.rb +1 -1
- data/lib/multi_ar/rake/tasks.rb +16 -7
- data/lib/multi_ar/version.rb +1 -1
- data/lib/multi_ar.rb +3 -2
- data.tar.gz.sig +0 -0
- metadata +44 -30
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a68d8b3b4fdd989b89ab5aa655fd6ac2fec91c5
|
4
|
+
data.tar.gz: 29520a7ac32bf608d7c358f16c9a20708c672dd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a61a6393a304c3686985ff31371489437a7b17115eb16e1b9661e7f13281487399a6e2c8ea06ef992b027ba65ce93a7e896f563079ac1d89707b6befd75e7bf
|
7
|
+
data.tar.gz: 563de82ff9c3661a85ca339c177edb8ed1028d042cc111f68e4f955aee4a431e77ab6a64f1926ad5d45ad796608ede7b2da0d431974b2a14b8ab83cee09588a1
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/multi_ar/database.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
|
2
2
|
# TODO: we don’t want to unconditionally load Rails as that gives too many unnecessary dependencies,
|
3
3
|
# but if we need it for something, it should be conditional dep through dep-gem or just used if present
|
4
|
-
require "rails"
|
5
|
-
require "rails/application"
|
4
|
+
#require "rails"
|
5
|
+
#require "rails/application"
|
6
6
|
|
7
7
|
require "active_record"
|
8
8
|
require "active_record/tasks/database_tasks"
|
@@ -20,13 +20,12 @@ module MultiAR
|
|
20
20
|
def self.initialize db_config: "config/database.yaml", migration_dir: "db/migrate"
|
21
21
|
@@initialized ||= false
|
22
22
|
return if @@initialized == true
|
23
|
-
Class.new(Rails::Application) unless Rails.application
|
23
|
+
#Class.new(Rails::Application) unless Rails.application
|
24
24
|
raise "The database configuration file was not found. You can be pass path to it with db_config attribute. Current path: #{db_config}" unless File.exist? db_config
|
25
25
|
db_config_data = YAML.load(ERB.new(File.read db_config).result)
|
26
26
|
include ActiveRecord::Tasks
|
27
27
|
|
28
28
|
ActiveRecord::Base.configurations = ::ActiveRecord::Tasks::DatabaseTasks.database_configuration = db_config_data
|
29
|
-
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [ migration_dir ] # this is only used by db:new_migration rake task. # TODO: because of that, maybe we want to handle this differently?
|
30
29
|
@@initialized = true
|
31
30
|
end
|
32
31
|
|
data/lib/multi_ar/interface.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require "fileutils"
|
3
|
+
require "pathname"
|
3
4
|
require "trollop"
|
4
5
|
|
5
6
|
require_relative "../multi_ar"
|
@@ -19,7 +20,7 @@ module MultiAR
|
|
19
20
|
# - dry # boolean
|
20
21
|
# - environment # `true` or `String`
|
21
22
|
# - verbose # boolean
|
22
|
-
# - databases # `
|
23
|
+
# - databases # `false` or `Array`, by default asks for databases
|
23
24
|
# - migration_dir # String
|
24
25
|
#
|
25
26
|
# 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.
|
@@ -55,15 +56,18 @@ module MultiAR
|
|
55
56
|
@run_by_default = false
|
56
57
|
end
|
57
58
|
|
59
|
+
DEFAULT_DB_CONFIG = "config/database.yaml"
|
60
|
+
|
58
61
|
# @note Consumes ARGV, create copy of it if you need it for something.
|
59
62
|
# @todo hardcode shorthands
|
60
63
|
def cli
|
61
64
|
p = Trollop::Parser.new
|
62
65
|
p.version @version if @version
|
63
66
|
p.banner @description if @description
|
64
|
-
p.opt "init", "Create stub environment with configuration and database.yaml"
|
65
|
-
|
66
|
-
p.opt "
|
67
|
+
p.opt "init", "Create stub environment with configuration and database.yaml. " +
|
68
|
+
"For current dir, use “.”.", type: :string
|
69
|
+
p.opt "databases", "List of databases to perform operations", type: :strings if(@options["databases"] != false && !@options["databases"].respond_to?(:each))
|
70
|
+
p.opt "db_config", "Path to database config file", type: :string, default: DEFAULT_DB_CONFIG if @options["db_config"] == true
|
67
71
|
p.opt "config", "Path to MultiAR framework config file", type: :string, default: "config/settings.yaml" if @options["config"] == true
|
68
72
|
p.opt "dry", "Run the program without doing anything. Useful for debugging with -v", type: :flag if @options["dry"] == true
|
69
73
|
p.opt "environment", "The environment to use. Corresponds to database config name " +
|
@@ -130,35 +134,37 @@ module MultiAR
|
|
130
134
|
private
|
131
135
|
|
132
136
|
def init_multi_ar
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
137
|
+
opts = {}
|
138
|
+
opts[:db_config] = @opts["db_config"] unless opts["db_config"].nil?
|
139
|
+
opts[:migration_dirs] = [ @opts["migration_dir"] ]
|
140
|
+
opts[:config] = @opts["config"] unless @opts["config"].nil?
|
141
|
+
opts[:databases] = @opts["databases"]
|
142
|
+
opts[:environment] = @opts["environment"]
|
143
|
+
@multi_ar = MultiAR.new opts
|
138
144
|
end
|
139
145
|
|
140
146
|
# @note This method will always quit the application or raise another exception for errors. Catch SystemExit if that’s not good for you.
|
141
147
|
def bootstrap opts
|
142
148
|
raise "--databases must be given when bootstrapping." unless opts["databases"]
|
143
|
-
raise "#{opts["init"]} already exists" if File.exist? opts["init"]
|
149
|
+
#raise "#{opts["init"]} already exists" if File.exist? opts["init"] # We don’t want to actually check it, init is more like “add missing configuration”.
|
144
150
|
|
145
151
|
config_dir = "config"
|
146
152
|
database_config = "database.yaml"
|
147
153
|
|
148
|
-
FileUtils.mkdir opts["init"]
|
154
|
+
FileUtils.mkdir opts["init"] unless Dir.exist? opts["init"]
|
149
155
|
Dir.chdir opts["init"] do
|
150
|
-
File.write "README", bootstrap_readme(opts)
|
151
|
-
|
156
|
+
File.write "README.md", bootstrap_readme(opts) unless File.exist?("README.md")
|
157
|
+
bootstrap_gemfile
|
152
158
|
|
153
|
-
FileUtils.mkdir config_dir
|
159
|
+
FileUtils.mkdir config_dir unless Dir.exist? config_dir
|
154
160
|
Dir.chdir config_dir do
|
155
|
-
File.write database_config, bootstrap_db_config(opts)
|
161
|
+
File.write database_config, bootstrap_db_config(opts) unless File.exist?(database_config)
|
156
162
|
end
|
157
163
|
|
158
|
-
|
164
|
+
bootstrap_config opts
|
159
165
|
end
|
160
166
|
|
161
|
-
puts "#{opts["init"]} has been initialized. You can now run your program at the directory."
|
167
|
+
puts "Project at dir #{opts["init"]} has been initialized. You can now run your program at the directory."
|
162
168
|
exit 0
|
163
169
|
end
|
164
170
|
|
@@ -170,7 +176,7 @@ module MultiAR
|
|
170
176
|
str << <<-EOS.gsub(/^ {10}/, "")
|
171
177
|
#{full_name}:
|
172
178
|
adapter: sqlite3
|
173
|
-
database: db/#{full_name}
|
179
|
+
database: db/#{full_name}.sqlite3
|
174
180
|
pool: 5
|
175
181
|
timeout: 5000
|
176
182
|
|
@@ -182,10 +188,11 @@ module MultiAR
|
|
182
188
|
end
|
183
189
|
|
184
190
|
def bootstrap_gemfile
|
191
|
+
|
185
192
|
str = <<-EOS.gsub(/^ {6}/, "")
|
186
193
|
source "https://rubygems.org/"
|
187
194
|
|
188
|
-
gem "
|
195
|
+
gem "multi_ar"
|
189
196
|
EOS
|
190
197
|
|
191
198
|
@dependencies.each do |dep, version|
|
@@ -195,14 +202,24 @@ module MultiAR
|
|
195
202
|
str << line
|
196
203
|
end
|
197
204
|
|
198
|
-
|
205
|
+
if File.exist?("Gemfile")
|
206
|
+
puts "NOTE: you should add following to your Gemfile:"
|
207
|
+
puts
|
208
|
+
puts str
|
209
|
+
return
|
210
|
+
end
|
211
|
+
|
212
|
+
File.write "Gemfile", str
|
213
|
+
|
214
|
+
# For convenience, we’ll run the bundler for the user in case we created the Gemfile.
|
215
|
+
run_bundler
|
199
216
|
end
|
200
217
|
|
201
218
|
def bootstrap_readme opts
|
202
219
|
str = <<-EOS.gsub(/^ {6}/, "")
|
203
220
|
## #{script_name}
|
204
221
|
|
205
|
-
This is scaffolded runtime directory for project #{
|
222
|
+
This is scaffolded runtime directory for project #{project_name}, created by command #{script_name} --init #{opts["init"]}.
|
206
223
|
|
207
224
|
Purpose of this scaffold is to ease usage of this #{script_name}, by providing sample configuration and ready
|
208
225
|
bundle just ready for running. Default configuration is for SQLite3, but any databases supported
|
@@ -212,17 +229,39 @@ module MultiAR
|
|
212
229
|
|
213
230
|
bundle exec #{script_name}
|
214
231
|
|
215
|
-
#{script_name} is powered by ActiveRecord migration framework MultiAR-#{VERSION}. More information
|
216
|
-
at its
|
232
|
+
#{script_name} is powered by ActiveRecord migration framework MultiAR-#{VERSION}. More information about MultiAR
|
233
|
+
can be found at [its home page](https://github.com/Smarre/multi_ar)
|
234
|
+
|
235
|
+
More information bundler can be found [at its homepage](http://bundler.io)
|
217
236
|
EOS
|
218
237
|
end
|
219
238
|
|
239
|
+
# Writes simple config file for some defaults we can assume from input
|
240
|
+
def bootstrap_config opts
|
241
|
+
settings_file = "config/settings.yaml"
|
242
|
+
return if File.exist? settings_file
|
243
|
+
str = <<-EOF
|
244
|
+
databases:
|
245
|
+
EOF
|
246
|
+
opts["databases"].each do |database|
|
247
|
+
str << " - #{database}"
|
248
|
+
end
|
249
|
+
|
250
|
+
File.open settings_file, "w" do |f|
|
251
|
+
f.write str
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
220
255
|
def run_bundler
|
221
256
|
puts `bundle install`
|
222
257
|
end
|
223
258
|
|
224
259
|
def script_name
|
225
|
-
@script_name ||= File.basename($0)
|
260
|
+
@script_name ||= File.basename($0).gsub("_", "\\_")
|
261
|
+
end
|
262
|
+
|
263
|
+
def project_name
|
264
|
+
Pathname.new(Dir.pwd).basename
|
226
265
|
end
|
227
266
|
end
|
228
267
|
end
|
@@ -12,7 +12,7 @@ module ActiveRecordMigrations
|
|
12
12
|
def create_migration_file
|
13
13
|
set_local_assigns!
|
14
14
|
validate_file_name!
|
15
|
-
dir = ::ActiveRecord::Tasks::DatabaseTasks.
|
15
|
+
dir = ::ActiveRecord::Tasks::DatabaseTasks.migration_dir
|
16
16
|
db_dir = ::ActiveRecord::Tasks::DatabaseTasks.sub_db_dir
|
17
17
|
migration_template @migration_template, "#{dir}/#{db_dir}/#{file_name}.rb"
|
18
18
|
end
|
data/lib/multi_ar/rake/tasks.rb
CHANGED
@@ -1,20 +1,24 @@
|
|
1
1
|
|
2
|
-
# TODO: we don’t want to unconditionally load Rails as that gives too many unnecessary dependencies,
|
3
|
-
# but if we need it for something, it should be conditional dep through dep-gem or just used if present
|
4
|
-
require "rails"
|
5
|
-
require "rails/generators"
|
6
|
-
|
7
2
|
require "active_record"
|
8
3
|
require "active_record/tasks/database_tasks"
|
9
4
|
|
10
|
-
require_relative "migration_generator"
|
11
|
-
|
12
5
|
# Optionally support migration_comments
|
13
6
|
begin
|
14
7
|
require "migration_comments"
|
15
8
|
rescue LoadError
|
16
9
|
end
|
17
10
|
|
11
|
+
# In case there is no Rails available, let’s do simple class
|
12
|
+
begin
|
13
|
+
require "rails"
|
14
|
+
rescue LoadError
|
15
|
+
class Rails
|
16
|
+
def self.root
|
17
|
+
nil # TODO, we want MultiAR::root like thing somewhere
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
18
22
|
# @api private
|
19
23
|
module Rake
|
20
24
|
|
@@ -65,6 +69,11 @@ module Rake
|
|
65
69
|
|
66
70
|
DSL.desc "Creates a new migration file with the specified name"
|
67
71
|
DSL.task :new_migration, :name, :options do |t, args|
|
72
|
+
# Only migration generator requires Rails generators
|
73
|
+
require "rails"
|
74
|
+
require "rails/generators"
|
75
|
+
require_relative "migration_generator"
|
76
|
+
|
68
77
|
name = args[:name] || ENV["name"]
|
69
78
|
options = args[:options] || ENV["options"]
|
70
79
|
|
data/lib/multi_ar/version.rb
CHANGED
data/lib/multi_ar.rb
CHANGED
@@ -29,7 +29,7 @@ module MultiAR
|
|
29
29
|
|
30
30
|
# @param databases array of available databases
|
31
31
|
# @todo config file is overriding parameters passed here... I think it should be other way around, but need more custom logic for that :/
|
32
|
-
def initialize databases:, environment: "development", config: "config/
|
32
|
+
def initialize databases:, environment: "development", config: "config/settings.yaml", db_config: "config/database.yaml", migration_dirs: []
|
33
33
|
|
34
34
|
# first load config
|
35
35
|
if not config.nil? and File.exist? config
|
@@ -52,8 +52,9 @@ module MultiAR
|
|
52
52
|
|
53
53
|
Database.initialize db_config: db_config
|
54
54
|
|
55
|
-
ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir }
|
55
|
+
ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir, :migration_dir }
|
56
56
|
ActiveRecord::Tasks::DatabaseTasks.sub_db_dir = databases.first # TODO: I don’t think this is how it should work
|
57
|
+
ActiveRecord::Tasks::DatabaseTasks.migration_dir = migration_dirs.first # TODO: I don’t think this is how it should work
|
57
58
|
|
58
59
|
@rake = ::Rake::Application.new
|
59
60
|
::Rake.application = @rake
|
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: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samu Voutilainen
|
@@ -29,7 +29,7 @@ cert_chain:
|
|
29
29
|
Ws73WZ+F+DOnfopIMUaeYUzKyUG+59p17ls//xzTouiLjcC9ee5ZaC9e+TzYsp4t
|
30
30
|
ofnwRrqEHVW+zRWs/VIwm70F2Jnl5eSc3MjyhpWIC0eGFXtBY1XYCrHeCdQ=
|
31
31
|
-----END CERTIFICATE-----
|
32
|
-
date:
|
32
|
+
date: 2016-01-18 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: trollop
|
@@ -60,98 +60,112 @@ dependencies:
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '4'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: rake
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '4'
|
68
|
+
version: '10.4'
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 10.4.2
|
69
72
|
type: :runtime
|
70
73
|
prerelease: false
|
71
74
|
version_requirements: !ruby/object:Gem::Requirement
|
72
75
|
requirements:
|
73
76
|
- - "~>"
|
74
77
|
- !ruby/object:Gem::Version
|
75
|
-
version: '4'
|
78
|
+
version: '10.4'
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 10.4.2
|
76
82
|
- !ruby/object:Gem::Dependency
|
77
|
-
name:
|
83
|
+
name: safe_attributes
|
78
84
|
requirement: !ruby/object:Gem::Requirement
|
79
85
|
requirements:
|
80
86
|
- - "~>"
|
81
87
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
88
|
+
version: '1.0'
|
83
89
|
type: :runtime
|
84
90
|
prerelease: false
|
85
91
|
version_requirements: !ruby/object:Gem::Requirement
|
86
92
|
requirements:
|
87
93
|
- - "~>"
|
88
94
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
95
|
+
version: '1.0'
|
90
96
|
- !ruby/object:Gem::Dependency
|
91
|
-
name:
|
97
|
+
name: redcarpet
|
92
98
|
requirement: !ruby/object:Gem::Requirement
|
93
99
|
requirements:
|
94
100
|
- - "~>"
|
95
101
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
97
|
-
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
version: 10.4.2
|
100
|
-
type: :runtime
|
102
|
+
version: '3.3'
|
103
|
+
type: :development
|
101
104
|
prerelease: false
|
102
105
|
version_requirements: !ruby/object:Gem::Requirement
|
103
106
|
requirements:
|
104
107
|
- - "~>"
|
105
108
|
- !ruby/object:Gem::Version
|
106
|
-
version: '
|
107
|
-
|
109
|
+
version: '3.3'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: github-markup
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
108
115
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
116
|
+
version: '1.4'
|
117
|
+
type: :development
|
118
|
+
prerelease: false
|
119
|
+
version_requirements: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '1.4'
|
110
124
|
- !ruby/object:Gem::Dependency
|
111
|
-
name:
|
125
|
+
name: cucumber
|
112
126
|
requirement: !ruby/object:Gem::Requirement
|
113
127
|
requirements:
|
114
128
|
- - "~>"
|
115
129
|
- !ruby/object:Gem::Version
|
116
|
-
version: '1
|
117
|
-
type: :
|
130
|
+
version: '2.1'
|
131
|
+
type: :development
|
118
132
|
prerelease: false
|
119
133
|
version_requirements: !ruby/object:Gem::Requirement
|
120
134
|
requirements:
|
121
135
|
- - "~>"
|
122
136
|
- !ruby/object:Gem::Version
|
123
|
-
version: '1
|
137
|
+
version: '2.1'
|
124
138
|
- !ruby/object:Gem::Dependency
|
125
|
-
name:
|
139
|
+
name: sqlite3
|
126
140
|
requirement: !ruby/object:Gem::Requirement
|
127
141
|
requirements:
|
128
142
|
- - "~>"
|
129
143
|
- !ruby/object:Gem::Version
|
130
|
-
version: '
|
144
|
+
version: '1.3'
|
131
145
|
type: :development
|
132
146
|
prerelease: false
|
133
147
|
version_requirements: !ruby/object:Gem::Requirement
|
134
148
|
requirements:
|
135
149
|
- - "~>"
|
136
150
|
- !ruby/object:Gem::Version
|
137
|
-
version: '
|
151
|
+
version: '1.3'
|
138
152
|
- !ruby/object:Gem::Dependency
|
139
|
-
name:
|
153
|
+
name: rspec
|
140
154
|
requirement: !ruby/object:Gem::Requirement
|
141
155
|
requirements:
|
142
156
|
- - "~>"
|
143
157
|
- !ruby/object:Gem::Version
|
144
|
-
version: '
|
158
|
+
version: '3.4'
|
145
159
|
type: :development
|
146
160
|
prerelease: false
|
147
161
|
version_requirements: !ruby/object:Gem::Requirement
|
148
162
|
requirements:
|
149
163
|
- - "~>"
|
150
164
|
- !ruby/object:Gem::Version
|
151
|
-
version: '
|
165
|
+
version: '3.4'
|
152
166
|
description: |-
|
153
|
-
|
154
|
-
|
167
|
+
Core library for multi database support in any Ruby project for ActiveRecord 4.
|
168
|
+
Migrations are supported by optional gem multi_ar_migrations.
|
155
169
|
email: smar@smar.fi
|
156
170
|
executables:
|
157
171
|
- multi_ar
|
@@ -190,6 +204,6 @@ rubyforge_project:
|
|
190
204
|
rubygems_version: 2.4.8
|
191
205
|
signing_key:
|
192
206
|
specification_version: 4
|
193
|
-
summary: Multi database
|
207
|
+
summary: Multi database feature set for ActiveRecord
|
194
208
|
test_files: []
|
195
209
|
has_rdoc:
|
metadata.gz.sig
CHANGED
Binary file
|