multi-database-9000 0.2.2 → 0.3.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
  SHA1:
3
- metadata.gz: c1e52410b94cd1ebbf0ba4bff0d6454877eb3089
4
- data.tar.gz: c8acc0fbde4701940948b4cfde04da7c46a17c99
3
+ metadata.gz: 33e7b9669dcf781d0f6b56a0d2f51726024e66fd
4
+ data.tar.gz: d7c68fc744690834858eb9c9147fc8d9fcfdcd19
5
5
  SHA512:
6
- metadata.gz: 386f8b2b90cda2dffe2775e08c512317459a1661e6e2ad77609f2fe21555fa27936e8510dce32bd5a76ae56c00a3d8fdd3ef700541463c4f6e27e7aa0a107b8b
7
- data.tar.gz: 819a923c069d2d92e2596a8cacf8e21fd050914f503d5872dd885c2422c885c4a8ba5ae0d055b32c4a5e9c66a08ea3bcb72f62a4ce2521bd5dd270e01e0e9846
6
+ metadata.gz: b71aba44cf23dcc5eb977df8ed37e3b39707e1b588194c22a80c51107a0544f0429dcf80743ca69d58dc5ef919896ed5571b8220102deeb0bfa9dca3dcc0a8cb
7
+ data.tar.gz: f94f1891d8add9af32e25aab7857ea8df2b7ff0fc7631cc5d6802cee6ee6280d0f88ac3ad4aec0a25c0118e0781afb4cdecb7c4397edd52f990224965bd483ac
@@ -57,16 +57,15 @@ Feature: Migrations run for all databases in the app
57
57
  And the file "../../multi-db-dummy/db/schema.rb" should not exist
58
58
  And the version in the users schema file should be updated
59
59
 
60
- Scenario: Specifying the database and the environment variables to migrate to
60
+ Scenario: Specifying the database and the Rails environment to migrate to
61
61
  Given I set the environment variables to:
62
62
  | variable | value |
63
63
  | RAILS_ENV | production |
64
64
  And there is a migration for the widgets database in a multi database app
65
65
  When I run `bundle exec rake db:migrate DATABASE=widgets` in a multi database app
66
- Then the file "../../multi-db-dummy/db/widgets_production.sqlite3" should exist
67
- And the file "../../multi-db-dummy/db/production.sqlite3" should not exist
68
- And the file "../../multi-db-dummy/db/users_production.sqlite3" should not exist
69
-
66
+ Then I should see the created 'gadgets' table in the 'widgets' 'production' database
67
+ And I should see the "doobry", "wotsit" and "thingy" columns in the "gadgets" table in the "widgets" "production" database
68
+
70
69
  Scenario: User runs rake db:migrate:status in a single database app
71
70
  Given I have created and run a migration with the name "20151010142141_create_users_table.rb", in a single database app
72
71
  And I have created but not run a migration with the name "20151010151000_add_nickname_to_users.rb", in a single database app
@@ -0,0 +1,110 @@
1
+ Feature: rake schema tasks should work for all databases in the app
2
+
3
+ rake db:schema:load will load all the database schemas for the current environment
4
+ rake db:schema:load DATABASE=widgets will load the schema for the widgets database
5
+
6
+ Background:
7
+ Given empty databases have been created for the app
8
+
9
+ Scenario: User loads the schema in a single database app
10
+ Given a single database app and a schema file with:
11
+ """
12
+ ActiveRecord::Schema.define(version: 20151010145432) do
13
+
14
+ create_table "users", force: :cascade do |t|
15
+ t.string "name"
16
+ t.integer "age"
17
+ t.string "email"
18
+ end
19
+
20
+ end
21
+ """
22
+ When I run `bundle exec rake db:schema:load` in a single database app
23
+ Then I should see the created users table
24
+ And I should see the correct columns in the users table
25
+
26
+ Scenario: User loads the schema in a multi database app
27
+ Given a multi database app and a schema file with:
28
+ """
29
+ ActiveRecord::Schema.define(version: 20151010142141) do
30
+
31
+ create_table "posts", force: :cascade do |t|
32
+ t.string "title"
33
+ t.integer "text"
34
+ t.string "author"
35
+ end
36
+
37
+ end
38
+ """
39
+ And a users_schema file with:
40
+ """
41
+ ActiveRecord::Schema.define(version: 20151010141234) do
42
+
43
+ create_table "accounts", force: :cascade do |t|
44
+ t.string "expense"
45
+ t.integer "user_id"
46
+ t.string "total"
47
+ end
48
+
49
+ end
50
+ """
51
+ And a widgets_schema file with:
52
+ """
53
+ ActiveRecord::Schema.define(version: 20151010141234) do
54
+
55
+ create_table "accounts", force: :cascade do |t|
56
+ t.string "doobry"
57
+ t.integer "wotsit"
58
+ t.string "thingy"
59
+ end
60
+
61
+ end
62
+ """
63
+ When I run `bundle exec rake db:schema:load` in the multi database app
64
+ Then I should see the created 'posts' table in the 'default' database
65
+ And I should see the created 'accounts' table in the 'users' database
66
+ And I should see the "title", "text" and "author" columns in the "posts" table in the "default" database
67
+ And I should see the "expense", "user_id" and "total" columns in the "accounts" table in the "users" database
68
+
69
+ Scenario: loading the schema in the production environment
70
+ Given a multi database app and a schema file with:
71
+ """
72
+ ActiveRecord::Schema.define(version: 20151010142141) do
73
+
74
+ create_table "posts", force: :cascade do |t|
75
+ t.string "title"
76
+ t.integer "text"
77
+ t.string "author"
78
+ end
79
+
80
+ end
81
+ """
82
+ And a users_schema file with:
83
+ """
84
+ ActiveRecord::Schema.define(version: 20151010141234) do
85
+
86
+ create_table "accounts", force: :cascade do |t|
87
+ t.string "expense"
88
+ t.integer "user_id"
89
+ t.string "total"
90
+ end
91
+
92
+ end
93
+ """
94
+ And a widgets_schema file with:
95
+ """
96
+ ActiveRecord::Schema.define(version: 20151010141234) do
97
+
98
+ create_table "accounts", force: :cascade do |t|
99
+ t.string "doobry"
100
+ t.integer "wotsit"
101
+ t.string "thingy"
102
+ end
103
+
104
+ end
105
+ """
106
+ When I run `bundle exec rake db:schema:load RAILS_ENV=production` in the multi database app
107
+ Then I should see the created 'posts' table in the 'default' 'production' database
108
+ And I should see the created 'accounts' table in the 'users' 'production' database
109
+ And I should see the "title", "text" and "author" columns in the "posts" table in the "default" "production" database
110
+ And I should see the "expense", "user_id" and "total" columns in the "accounts" table in the "users" "production" database
@@ -57,22 +57,16 @@ Then(/^I should see the created posts table in the default database$/) do
57
57
  table_exists? :database => "development.sqlite3", :table => "posts"
58
58
  end
59
59
 
60
- Then(/^I should see the created '([^']*)' table in the '([^']*)' database$/) do |table, database|
61
- if database == "default"
62
- database_file_name = "development"
63
- else
64
- database_file_name = "#{database}_development"
65
- end
66
- table_exists? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table
60
+ # the (?:'([^']*)' )? capture group specifies an optional group
61
+ # e.g. "I should see the created users table in the default database" will match
62
+ # e.g. "I should see the created users table in the default production database" will also match
63
+ # (and "production" will be captured in the third parameter)
64
+ Then(/^I should see the created '([^']*)' table in the '([^']*)' (?:'([^']*)' )?database$/) do |table, database, environment|
65
+ table_exists? :app => "multi-db-dummy", :database => "#{database_file_name database, environment}.sqlite3", :table => table
67
66
  end
68
67
 
69
- Then(/^I should see the "([^"]*)", "([^"]*)" and "([^"]*)" columns in the "([^"]*)" table in the "([^"]*)" database$/) do |column1, column2, column3, table, database|
70
- if database == "default"
71
- database_file_name = "development"
72
- else
73
- database_file_name = "#{database}_development"
74
- end
75
- columns_exist? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table, :columns => [column1, column2, column3]
68
+ Then(/^I should see the "([^"]*)", "([^"]*)" and "([^"]*)" columns in the "([^"]*)" table in the "([^"]*)" (?:"([^"]*)" )?database$/) do |column1, column2, column3, table, database, environment|
69
+ columns_exist? :app => "multi-db-dummy", :database => "#{database_file_name database, environment}.sqlite3", :table => table, :columns => [column1, column2, column3]
76
70
  end
77
71
 
78
72
  When(/^I run a migration with the timestamp "([^"]*)" in a single database app$/) do |timestamp|
@@ -144,6 +138,15 @@ end
144
138
 
145
139
  # Helpers
146
140
 
141
+ def database_file_name(database, environment)
142
+ env = environment || "development"
143
+ if database == "default"
144
+ return env
145
+ else
146
+ return "#{database}_#{env}"
147
+ end
148
+ end
149
+
147
150
  def write_multi_db_migration_for(database, migration_name)
148
151
  migration_database = database == "default" ? "migrate" : "#{database}_migrate"
149
152
  migration_class = migration_name.match(/\d+_(\w+).rb/).captures.first.split('_').map(&:capitalize).join
@@ -160,10 +163,12 @@ def write_multi_db_migration_for(database, migration_name)
160
163
  end
161
164
 
162
165
  def run_rake_db_create
163
- cmd = unescape_text("rake db:create")
164
- cmd = extract_text(cmd) if !aruba.config.keep_ansi || aruba.config.remove_ansi_escape_sequences
166
+ ["rake db:create", "rake db:create RAILS_ENV=production"].each do |command|
167
+ cmd = unescape_text(command)
168
+ cmd = extract_text(cmd) if !aruba.config.keep_ansi || aruba.config.remove_ansi_escape_sequences
165
169
 
166
- run_simple(cmd, false)
170
+ run_simple(cmd, false)
171
+ end
167
172
  end
168
173
 
169
174
  def write_single_db_migration
@@ -0,0 +1,11 @@
1
+ Given(/^a single database app and a schema file with:$/) do |schema_content|
2
+ write_file "../../single-db-dummy/db/schema.rb", schema_content
3
+ end
4
+
5
+ Given(/^a multi database app and a schema file with:$/) do |schema_content|
6
+ write_file "../../multi-db-dummy/db/schema.rb", schema_content
7
+ end
8
+
9
+ Given(/^a (\w+) file with:$/) do |schema_name, schema_content|
10
+ write_file "../../multi-db-dummy/db/#{schema_name}.rb", schema_content
11
+ end
@@ -3,12 +3,12 @@ Given(/^no databases have been created$/) do
3
3
  clear_db_dir
4
4
  end
5
5
 
6
- Given(/^I run `([^`]*)` in a single database app$/) do |command|
6
+ Given(/^I run `([^`]*)` in (?:a|the) single database app$/) do |command|
7
7
  run_task_in_single_db_app(command)
8
8
  end
9
9
 
10
10
 
11
- Given(/^I run `([^`]*)` in a multi database app$/) do |command|
11
+ Given(/^I run `([^`]*)` in (?:a|the) multi database app$/) do |command|
12
12
  run_task_in_multi_db_app(command)
13
13
  end
14
14
 
@@ -40,6 +40,7 @@ end
40
40
  Rake::Task['db:create'].clear
41
41
  Rake::Task['db:migrate'].clear
42
42
  Rake::Task['db:schema:dump'].clear
43
+ Rake::Task['db:schema:load'].clear
43
44
  Rake::Task['db:migrate:status'].clear
44
45
 
45
46
  Rake::Task["db:test:load_schema"].enhance do
@@ -138,5 +139,12 @@ db_namespace = namespace :db do
138
139
  end
139
140
  db_namespace['schema:dump'].reenable
140
141
  end
142
+
143
+ desc 'Load a schema.rb file into the database'
144
+ task :load => [:environment, :load_config] do
145
+ database_connections(:database => ENV["DATABASE"], :rails_envs => ENV["RAILS_ENV"] || "development").each do |connection_key, connection|
146
+ ActiveRecord::Tasks::DatabaseTasks.load_schema_for connection, :ruby, "db/#{schema_file_name(connection_key)}"
147
+ end
148
+ end
141
149
  end
142
150
  end
@@ -1,3 +1,3 @@
1
1
  module MultiDatabase9000
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.2
4
+ version: 0.3.0
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-03 00:00:00.000000000 Z
13
+ date: 2015-11-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -140,9 +140,11 @@ files:
140
140
  - features/development_migrations.feature
141
141
  - features/production_migrations.feature
142
142
  - features/running_generators.feature
143
+ - features/schema_tasks.feature
143
144
  - features/step_definitions/development_migrations_steps.rb
144
145
  - features/step_definitions/production_migration_steps.rb
145
146
  - features/step_definitions/running_generators_steps.rb
147
+ - features/step_definitions/schema_steps.rb
146
148
  - features/step_definitions/sqlite_database_steps.rb
147
149
  - features/step_definitions/test_migration_steps.rb
148
150
  - features/support/env.rb
@@ -291,9 +293,11 @@ test_files:
291
293
  - features/development_migrations.feature
292
294
  - features/production_migrations.feature
293
295
  - features/running_generators.feature
296
+ - features/schema_tasks.feature
294
297
  - features/step_definitions/development_migrations_steps.rb
295
298
  - features/step_definitions/production_migration_steps.rb
296
299
  - features/step_definitions/running_generators_steps.rb
300
+ - features/step_definitions/schema_steps.rb
297
301
  - features/step_definitions/sqlite_database_steps.rb
298
302
  - features/step_definitions/test_migration_steps.rb
299
303
  - features/support/env.rb