multi-database-9000 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +5 -0
- data/features/schema_tasks.feature +38 -0
- data/features/step_definitions/schema_steps.rb +24 -0
- data/lib/active_record_extensions/schema.rb +19 -0
- data/lib/multi-database-9000.rb +3 -1
- data/lib/multi-database-9000/multi-database-9000.rb +44 -1
- data/lib/multi-database-9000/version.rb +1 -1
- data/multi-db-dummy/Gemfile.lock +1 -1
- data/single-db-dummy/Gemfile.lock +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4aff0b568745cf031959322115ac1463cfb2be74
|
4
|
+
data.tar.gz: d887a6c2f4b7b980590b34768431561659fedf5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92dab60e53e359f0e57a8bc76defa34871324d563eaf7702100129cdb19048e3fa491d2563cca323783ce6c921d769ca1dd449a6c187446937ae3efd910c8782
|
7
|
+
data.tar.gz: 58b04be81ed4f4c5e19386f99cc333c574174f3e986d140c720fe05af6dc11453afef3bfca9b92458624c88edb0962dc9586ffa2ba64bee3b0bfba7a67ef342b
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
@@ -2,6 +2,7 @@ Feature: rake schema tasks should work for all databases in the app
|
|
2
2
|
|
3
3
|
rake db:schema:load will load all the database schemas for the current environment
|
4
4
|
rake db:schema:load DATABASE=widgets will load the schema for the widgets database
|
5
|
+
the schema_migrations table for each database should be correctly updated with migrations run on that database only
|
5
6
|
|
6
7
|
Background:
|
7
8
|
Given empty databases have been created for the app
|
@@ -108,3 +109,40 @@ Feature: rake schema tasks should work for all databases in the app
|
|
108
109
|
And I should see the created 'accounts' table in the 'users' 'production' database
|
109
110
|
And I should see the "title", "text" and "author" columns in the "posts" table in the "default" "production" database
|
110
111
|
And I should see the "expense", "user_id" and "total" columns in the "accounts" table in the "users" "production" database
|
112
|
+
|
113
|
+
Scenario: checking the schema_migrations table after running rake db:schema:load
|
114
|
+
Given a multi database app and a schema file with:
|
115
|
+
"""
|
116
|
+
ActiveRecord::Schema.define(version: 20151010142141) do
|
117
|
+
|
118
|
+
create_table "posts", force: :cascade do |t|
|
119
|
+
t.string "title"
|
120
|
+
t.integer "text"
|
121
|
+
t.string "author"
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
"""
|
126
|
+
And a users_schema file with:
|
127
|
+
"""
|
128
|
+
ActiveRecord::Schema.define(version: 20151010141234) do
|
129
|
+
|
130
|
+
create_table "accounts", force: :cascade do |t|
|
131
|
+
t.string "expense"
|
132
|
+
t.integer "user_id"
|
133
|
+
t.string "total"
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
"""
|
138
|
+
And the following migrations have already been run:
|
139
|
+
| Migration | database|
|
140
|
+
| 20151008140000_create_posts_table.rb | default |
|
141
|
+
| 20151009140000_update_posts_table.rb | default |
|
142
|
+
| 20151010142141_change_posts_table.rb | default |
|
143
|
+
| 20151008220000_create_accounts_table.rb | users |
|
144
|
+
| 20151009220000_update_accounts_table.rb | users |
|
145
|
+
| 20151010141234_change_gadgets_table.rb | users |
|
146
|
+
When I run `bundle exec rake db:schema:load` in the multi database app
|
147
|
+
Then the schema_migrations table for the "default" database should only contain version numbers from default database migrations
|
148
|
+
And the schema_migrations table for the "users" database should only contain version numbers from users database migrations
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'pry'
|
2
|
+
|
1
3
|
Given(/^a single database app and a schema file with:$/) do |schema_content|
|
2
4
|
write_file "../../single-db-dummy/db/schema.rb", schema_content
|
3
5
|
end
|
@@ -8,4 +10,26 @@ end
|
|
8
10
|
|
9
11
|
Given(/^a (\w+) file with:$/) do |schema_name, schema_content|
|
10
12
|
write_file "../../multi-db-dummy/db/#{schema_name}.rb", schema_content
|
13
|
+
end
|
14
|
+
|
15
|
+
Given(/^the following migrations have already been run:$/) do |table|
|
16
|
+
@version_numbers = Hash.new {|hash, missing_key| hash[missing_key] = []}
|
17
|
+
table.hashes.each{|migration| @version_numbers[migration["database"]] << migration["Migration"].match(/\d{14}/).to_s}
|
18
|
+
table.hashes.each do |migration|
|
19
|
+
write_multi_db_migration_for migration["database"], migration["Migration"]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Then(/^the schema_migrations table for the "([^"]*)" database should only contain version numbers from (?:\w+) database migrations$/) do |database|
|
24
|
+
if database == "default"
|
25
|
+
database_file = "development.sqlite3"
|
26
|
+
else
|
27
|
+
database_file = "#{database}_development.sqlite3"
|
28
|
+
end
|
29
|
+
SQLite3::Database.new("multi-db-dummy/db/#{database_file}") do |db|
|
30
|
+
versions_query = db.execute("SELECT version FROM schema_migrations ORDER BY version ASC")
|
31
|
+
puts versions_query
|
32
|
+
expect(versions_query.count).to eql @version_numbers[database].count
|
33
|
+
expect(versions_query.flatten).to match_array @version_numbers[database]
|
34
|
+
end
|
11
35
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module MultiDatabase9000
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
module Schema
|
4
|
+
def define(info, &block) # :nodoc:
|
5
|
+
instance_eval(&block)
|
6
|
+
|
7
|
+
unless info[:version].blank?
|
8
|
+
initialize_schema_migrations_table
|
9
|
+
database_match = caller[1].match(/\/(\w+)_schema/)
|
10
|
+
if database_match.present?
|
11
|
+
connection.assume_migrated_upto_version(info[:version], MultiDatabase9000.migration_path_for(database_match.captures.first))
|
12
|
+
else
|
13
|
+
connection.assume_migrated_upto_version(info[:version], migrations_paths)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/multi-database-9000.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "multi-database-9000/version"
|
2
2
|
require "multi-database-9000/multi-database-9000"
|
3
|
+
require "active_record_extensions/schema"
|
3
4
|
|
4
5
|
module MultiDatabase9000
|
5
6
|
class Railtie < Rails::Railtie
|
@@ -7,5 +8,6 @@ module MultiDatabase9000
|
|
7
8
|
load "multi-database-9000/tasks/multi-database-9000_tasks.rake"
|
8
9
|
end
|
9
10
|
end
|
10
|
-
|
11
11
|
end
|
12
|
+
|
13
|
+
ActiveRecord::Schema.prepend MultiDatabase9000::ActiveRecordExtensions::Schema
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module MultiDatabase9000
|
2
|
-
def self.maintain_all_test_schemas!
|
2
|
+
def self.maintain_all_test_schemas!
|
3
3
|
# Roundrip to Rake to allow plugins to hook into database initialization.
|
4
4
|
FileUtils.cd Rails.root do
|
5
5
|
current_config = ActiveRecord::Base.connection_config
|
@@ -9,4 +9,47 @@ module MultiDatabase9000
|
|
9
9
|
ActiveRecord::Base.establish_connection(current_config)
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
def self.migration_path_for(database)
|
14
|
+
return nil unless database.present?
|
15
|
+
return ["db/#{database}_migrate"]
|
16
|
+
end
|
17
|
+
|
18
|
+
def database_connections(database: nil, rails_envs: nil)
|
19
|
+
connections = connections_for_environment(rails_envs)
|
20
|
+
if database.present?
|
21
|
+
connections.keep_if {|key, _| key.match Regexp.new(database)}
|
22
|
+
end
|
23
|
+
return connections
|
24
|
+
end
|
25
|
+
|
26
|
+
def connections_for_environment(rails_envs)
|
27
|
+
rails_envs = Array(rails_envs)
|
28
|
+
matcher = ->(key, value){rails_envs.any?{|env| key.match Regexp.new(env)}}
|
29
|
+
return ActiveRecord::Base.configurations.keep_if &matcher
|
30
|
+
end
|
31
|
+
|
32
|
+
def database_name(connection_key)
|
33
|
+
if connection_key.match /\w+_\w+/
|
34
|
+
return connection_key.split('_').first
|
35
|
+
else
|
36
|
+
return "default"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def migration_directory(connection_key)
|
41
|
+
if ['test','development','staging','cucumber','production'].include? connection_key
|
42
|
+
return "db/migrate/"
|
43
|
+
else
|
44
|
+
return "db/#{database_name(connection_key)}_migrate/"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def schema_file_name(connection_key)
|
49
|
+
if ['test','development','staging','cucumber','production'].include? connection_key
|
50
|
+
return "schema.rb"
|
51
|
+
else
|
52
|
+
return "#{database_name(connection_key)}_schema.rb"
|
53
|
+
end
|
54
|
+
end
|
12
55
|
end
|
data/multi-db-dummy/Gemfile.lock
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi-database-9000
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Weston
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-11-
|
13
|
+
date: 2015-11-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -132,6 +132,7 @@ extensions: []
|
|
132
132
|
extra_rdoc_files: []
|
133
133
|
files:
|
134
134
|
- ".gitignore"
|
135
|
+
- ".travis.yml"
|
135
136
|
- Gemfile
|
136
137
|
- LICENSE.txt
|
137
138
|
- README.md
|
@@ -150,6 +151,7 @@ files:
|
|
150
151
|
- features/support/env.rb
|
151
152
|
- features/test_app.feature
|
152
153
|
- features/test_migrations.feature
|
154
|
+
- lib/active_record_extensions/schema.rb
|
153
155
|
- lib/generators/multi_migration_generator.rb
|
154
156
|
- lib/generators/templates/migration.rb
|
155
157
|
- lib/multi-database-9000.rb
|