multi-database-9000 0.1.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.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +59 -0
  6. data/Rakefile +2 -0
  7. data/features/creating_databases.feature +75 -0
  8. data/features/development_migrations.feature +94 -0
  9. data/features/production_migrations.feature +32 -0
  10. data/features/step_definitions/development_migrations_steps.rb +244 -0
  11. data/features/step_definitions/production_migration_steps.rb +28 -0
  12. data/features/step_definitions/sqlite_database_steps.rb +41 -0
  13. data/features/step_definitions/test_migration_steps.rb +84 -0
  14. data/features/support/env.rb +2 -0
  15. data/features/test_app.feature +6 -0
  16. data/features/test_migrations.feature +24 -0
  17. data/lib/multi-database-9000/multi-database-9000.rb +12 -0
  18. data/lib/multi-database-9000/tasks/multi-database-9000_tasks.rake +143 -0
  19. data/lib/multi-database-9000/version.rb +3 -0
  20. data/lib/multi-database-9000.rb +10 -0
  21. data/multi-database-9000.gemspec +29 -0
  22. data/multi-db-dummy/Gemfile +15 -0
  23. data/multi-db-dummy/Gemfile.lock +115 -0
  24. data/multi-db-dummy/README.rdoc +28 -0
  25. data/multi-db-dummy/Rakefile +6 -0
  26. data/multi-db-dummy/app/assets/images/.keep +0 -0
  27. data/multi-db-dummy/app/assets/stylesheets/application.css +15 -0
  28. data/multi-db-dummy/app/controllers/application_controller.rb +5 -0
  29. data/multi-db-dummy/app/controllers/concerns/.keep +0 -0
  30. data/multi-db-dummy/app/helpers/application_helper.rb +2 -0
  31. data/multi-db-dummy/app/mailers/.keep +0 -0
  32. data/multi-db-dummy/app/models/.keep +0 -0
  33. data/multi-db-dummy/app/models/concerns/.keep +0 -0
  34. data/multi-db-dummy/app/models/gadget.rb +3 -0
  35. data/multi-db-dummy/app/views/layouts/application.html.erb +13 -0
  36. data/multi-db-dummy/bin/bundle +3 -0
  37. data/multi-db-dummy/bin/rails +4 -0
  38. data/multi-db-dummy/bin/rake +4 -0
  39. data/multi-db-dummy/bin/setup +29 -0
  40. data/multi-db-dummy/config/application.rb +35 -0
  41. data/multi-db-dummy/config/boot.rb +3 -0
  42. data/multi-db-dummy/config/database.yml +49 -0
  43. data/multi-db-dummy/config/environment.rb +5 -0
  44. data/multi-db-dummy/config/environments/development.rb +28 -0
  45. data/multi-db-dummy/config/environments/production.rb +67 -0
  46. data/multi-db-dummy/config/environments/test.rb +42 -0
  47. data/multi-db-dummy/config/initializers/backtrace_silencers.rb +7 -0
  48. data/multi-db-dummy/config/initializers/cookies_serializer.rb +3 -0
  49. data/multi-db-dummy/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/multi-db-dummy/config/initializers/inflections.rb +16 -0
  51. data/multi-db-dummy/config/initializers/mime_types.rb +4 -0
  52. data/multi-db-dummy/config/initializers/session_store.rb +3 -0
  53. data/multi-db-dummy/config/initializers/wrap_parameters.rb +14 -0
  54. data/multi-db-dummy/config/locales/en.yml +23 -0
  55. data/multi-db-dummy/config/routes.rb +56 -0
  56. data/multi-db-dummy/config/secrets.yml +22 -0
  57. data/multi-db-dummy/config.ru +4 -0
  58. data/multi-db-dummy/lib/assets/.keep +0 -0
  59. data/multi-db-dummy/lib/tasks/.keep +0 -0
  60. data/multi-db-dummy/log/.keep +0 -0
  61. data/multi-db-dummy/public/404.html +67 -0
  62. data/multi-db-dummy/public/422.html +67 -0
  63. data/multi-db-dummy/public/500.html +66 -0
  64. data/multi-db-dummy/public/favicon.ico +0 -0
  65. data/multi-db-dummy/public/robots.txt +5 -0
  66. data/multi-db-dummy/test/controllers/.keep +0 -0
  67. data/multi-db-dummy/test/fixtures/.keep +0 -0
  68. data/multi-db-dummy/test/helpers/.keep +0 -0
  69. data/multi-db-dummy/test/integration/.keep +0 -0
  70. data/multi-db-dummy/test/mailers/.keep +0 -0
  71. data/multi-db-dummy/test/models/.keep +0 -0
  72. data/multi-db-dummy/test/models/gadget_test.rb +8 -0
  73. data/multi-db-dummy/test/test_helper.rb +12 -0
  74. data/multi-db-dummy/vendor/assets/stylesheets/.keep +0 -0
  75. data/single-db-dummy/Gemfile +16 -0
  76. data/single-db-dummy/Gemfile.lock +115 -0
  77. data/single-db-dummy/README.rdoc +28 -0
  78. data/single-db-dummy/Rakefile +6 -0
  79. data/single-db-dummy/app/assets/images/.keep +0 -0
  80. data/single-db-dummy/app/assets/stylesheets/application.css +15 -0
  81. data/single-db-dummy/app/controllers/application_controller.rb +5 -0
  82. data/single-db-dummy/app/controllers/concerns/.keep +0 -0
  83. data/single-db-dummy/app/helpers/application_helper.rb +2 -0
  84. data/single-db-dummy/app/mailers/.keep +0 -0
  85. data/single-db-dummy/app/models/.keep +0 -0
  86. data/single-db-dummy/app/models/concerns/.keep +0 -0
  87. data/single-db-dummy/app/models/user.rb +3 -0
  88. data/single-db-dummy/app/views/layouts/application.html.erb +13 -0
  89. data/single-db-dummy/bin/bundle +3 -0
  90. data/single-db-dummy/bin/rails +4 -0
  91. data/single-db-dummy/bin/rake +4 -0
  92. data/single-db-dummy/bin/setup +29 -0
  93. data/single-db-dummy/config/application.rb +35 -0
  94. data/single-db-dummy/config/boot.rb +3 -0
  95. data/single-db-dummy/config/database.yml +25 -0
  96. data/single-db-dummy/config/environment.rb +5 -0
  97. data/single-db-dummy/config/environments/development.rb +28 -0
  98. data/single-db-dummy/config/environments/production.rb +67 -0
  99. data/single-db-dummy/config/environments/test.rb +42 -0
  100. data/single-db-dummy/config/initializers/backtrace_silencers.rb +7 -0
  101. data/single-db-dummy/config/initializers/cookies_serializer.rb +3 -0
  102. data/single-db-dummy/config/initializers/filter_parameter_logging.rb +4 -0
  103. data/single-db-dummy/config/initializers/inflections.rb +16 -0
  104. data/single-db-dummy/config/initializers/mime_types.rb +4 -0
  105. data/single-db-dummy/config/initializers/session_store.rb +3 -0
  106. data/single-db-dummy/config/initializers/wrap_parameters.rb +14 -0
  107. data/single-db-dummy/config/locales/en.yml +23 -0
  108. data/single-db-dummy/config/routes.rb +56 -0
  109. data/single-db-dummy/config/secrets.yml +22 -0
  110. data/single-db-dummy/config.ru +4 -0
  111. data/single-db-dummy/lib/assets/.keep +0 -0
  112. data/single-db-dummy/lib/tasks/.keep +0 -0
  113. data/single-db-dummy/log/.keep +0 -0
  114. data/single-db-dummy/public/404.html +67 -0
  115. data/single-db-dummy/public/422.html +67 -0
  116. data/single-db-dummy/public/500.html +66 -0
  117. data/single-db-dummy/public/favicon.ico +0 -0
  118. data/single-db-dummy/public/robots.txt +5 -0
  119. data/single-db-dummy/test/controllers/.keep +0 -0
  120. data/single-db-dummy/test/fixtures/.keep +0 -0
  121. data/single-db-dummy/test/helpers/.keep +0 -0
  122. data/single-db-dummy/test/integration/.keep +0 -0
  123. data/single-db-dummy/test/mailers/.keep +0 -0
  124. data/single-db-dummy/test/models/.keep +0 -0
  125. data/single-db-dummy/test/models/user_test.rb +8 -0
  126. data/single-db-dummy/test/test_helper.rb +10 -0
  127. data/single-db-dummy/vendor/assets/stylesheets/.keep +0 -0
  128. metadata +295 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b615a4d064746274e944f5bd1c41720eb88e416
4
+ data.tar.gz: 3d988a77248229b87e719feee2fd930abd3f4867
5
+ SHA512:
6
+ metadata.gz: 0bd12c0da8d04f705809843bcaf6db87380dab761ec3301964f7483755c0c0f8f5a4c5b3545995cc4001296d3aa180ffede8fd393f20e89f83df44f8ecafc3d0
7
+ data.tar.gz: 7a1b040ea4ff78b2e9a1e1c41c0269a81f3a72c7e83e201af8bd3ba464c74e84083e3f1a199d1a4c8688158f1ae0005e2108a3decdbd837e95c857b0154be904
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /single-db-dummy/db
11
+ /single-db-dummy/log
12
+ /multi-db-dummy/db
13
+ /multi-db-dummy/log
14
+ *.bundle
15
+ *.so
16
+ *.o
17
+ *.a
18
+ mkmf.log
19
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi-database-9000.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 BCSG Ltd, Stefania Cardenas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # MultiDatabase9000
2
+
3
+ A plugin designed to help a Rails app work transparently with multiple databases, inspired by the multi-database-migrations
4
+ gem (which was immensely helpful when we started down this road). But in the process of updating that gem to work with Rails 4,
5
+ we decided that we preferred an API that allowed us to transparently use the Rails and Rake commands we already knew
6
+ rather than create new versions within a separate namespace.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'multi-database-9000'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install multi-database-9000
23
+
24
+ ## Concept
25
+
26
+ The gem assumes that your app has a "default" or "main" database that's configured in the standard test, development
27
+ and production connections defined in database.yml. Also that there are one or more extra databases configured with connection
28
+ names that start with a name for that database followed by the environment name (e.g. if you had a database called "widgets" you
29
+ would have connections defined for widgets_test, widgets_development and widgets_production). The name used in the connection
30
+ does not have to match the actual database name, but does need to be used consistently to refer to that database (e.g.
31
+ if I use the name "widgets" in database.yml I also need to use it when creating a directory for migrations).
32
+
33
+ ## Usage
34
+
35
+ Create a new set of connections for your database in database.yml, in the form of "<database_name>_test",
36
+ "<database_name>_production" etc.
37
+
38
+ Create a new directory under db/ called <database_name>_migrate to hold migrations for the database.
39
+
40
+ Create a new migration with rails g migration DATABASE=<database_name>. The DATABASE environment variable can be left
41
+ off if the target of the migration is your default database; alternatively multi-database-9000 also accepts DATABASE=default
42
+
43
+ rake db:create will attempt to create all databases for all connections. rake db:create DATABASE=widgets will only created
44
+ databases for connections with "widget" in their name.
45
+
46
+ Similarly, rake db:schema:load and rake:db:schema:dump will load or dump schemas for all connections to all databases.
47
+ rake db:schema:load DATABASE=widget will only load the schema for databases in connections with "widget" in their name.
48
+
49
+ In order for test database schemas to be kept updated, add the following line to your `test_helper` or `spec_helper` files:
50
+
51
+ MultiDatabase9000.maintain_all_test_schemas!
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it ( https://github.com/[my-github-username]/multi-database-9000/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,75 @@
1
+ Feature: Running rake db:create
2
+
3
+ rake db:create should create all the databases in a multi-database app
4
+ rake db:create should still work in a single database app
5
+ rake db:create DATABASE=users should create the users database only
6
+
7
+ Background:
8
+ Given no databases have been created
9
+
10
+ Scenario: creating the database in a single database app
11
+ When I run `bundle exec rake db:create` in a single database app
12
+ Then the file "../../single-db-dummy/db/development.sqlite3" should exist
13
+ And the file "../../single-db-dummy/db/test.sqlite3" should exist
14
+
15
+ Scenario: creating the database in a multi database app
16
+ When I run `bundle exec rake db:create` in a multi database app
17
+ Then the following files should exist:
18
+ |../../multi-db-dummy/db/development.sqlite3 |
19
+ |../../multi-db-dummy/db/users_development.sqlite3 |
20
+ |../../multi-db-dummy/db/widgets_development.sqlite3|
21
+ |../../multi-db-dummy/db/test.sqlite3 |
22
+ |../../multi-db-dummy/db/users_test.sqlite3 |
23
+ |../../multi-db-dummy/db/widgets_test.sqlite3 |
24
+ And the following files should not exist:
25
+ |../../multi-db-dummy/db/production.sqlite3 |
26
+ |../../multi-db-dummy/db/users_production.sqlite3 |
27
+ |../../multi-db-dummy/db/widgets_production.sqlite3 |
28
+
29
+ Scenario: specifying the database to create using the DATABASE environment variable
30
+ When I run `bundle exec rake db:create DATABASE=users` in a multi database app
31
+ Then the following files should exist:
32
+ |../../multi-db-dummy/db/users_development.sqlite3 |
33
+ |../../multi-db-dummy/db/users_test.sqlite3 |
34
+ And the following files should not exist:
35
+ |../../multi-db-dummy/db/development.sqlite3 |
36
+ |../../multi-db-dummy/db/widgets_development.sqlite3|
37
+ |../../multi-db-dummy/db/test.sqlite3 |
38
+ |../../multi-db-dummy/db/widgets_test.sqlite3 |
39
+ |../../multi-db-dummy/db/production.sqlite3 |
40
+ |../../multi-db-dummy/db/users_production.sqlite3 |
41
+ |../../multi-db-dummy/db/widgets_production.sqlite3 |
42
+
43
+ Scenario: specifying the Rails environment
44
+ Given I set the environment variables to:
45
+ | variable | value |
46
+ | RAILS_ENV | test |
47
+ When I run `bundle exec rake db:create` in a multi database app
48
+ Then the following files should exist:
49
+ |../../multi-db-dummy/db/users_test.sqlite3 |
50
+ |../../multi-db-dummy/db/test.sqlite3 |
51
+ |../../multi-db-dummy/db/widgets_test.sqlite3 |
52
+ And the following files should not exist:
53
+ |../../multi-db-dummy/db/users_development.sqlite3 |
54
+ |../../multi-db-dummy/db/development.sqlite3 |
55
+ |../../multi-db-dummy/db/widgets_development.sqlite3|
56
+ |../../multi-db-dummy/db/production.sqlite3 |
57
+ |../../multi-db-dummy/db/users_production.sqlite3 |
58
+ |../../multi-db-dummy/db/widgets_production.sqlite3 |
59
+
60
+ Scenario: specifying the database and the Rails environment
61
+ Given I set the environment variables to:
62
+ | variable | value |
63
+ | RAILS_ENV | production |
64
+ When I run `bundle exec rake db:create DATABASE=widgets` in a multi database app
65
+ Then the following files should exist:
66
+ |../../multi-db-dummy/db/widgets_production.sqlite3 |
67
+ And the following files should not exist:
68
+ |../../multi-db-dummy/db/users_test.sqlite3 |
69
+ |../../multi-db-dummy/db/test.sqlite3 |
70
+ |../../multi-db-dummy/db/widgets_test.sqlite3 |
71
+ |../../multi-db-dummy/db/users_development.sqlite3 |
72
+ |../../multi-db-dummy/db/development.sqlite3 |
73
+ |../../multi-db-dummy/db/widgets_development.sqlite3|
74
+ |../../multi-db-dummy/db/production.sqlite3 |
75
+ |../../multi-db-dummy/db/users_production.sqlite3 |
@@ -0,0 +1,94 @@
1
+ Feature: Migrations run for all databases in the app
2
+
3
+ rake db:migrate should still work in a single database app
4
+ rake db:migrate should migrate all the databases in a multi-database app
5
+ rake db:migrate DATABASE=users should migrate the users database only
6
+ rake db:migrate:status should list all migrations for all databases, ordered by timestamp.
7
+ The migrations list should include an extra column listing the database each migration is for.
8
+
9
+ Status Migration ID Database Migration Name
10
+ --------------------------------------------------
11
+ up 20151010141234 users Create accounts table
12
+ up 20151010142141 default Create posts table
13
+ up 20151010145432 widgets Create widgets table
14
+
15
+ Background:
16
+ Given empty databases have been created for the app
17
+
18
+ Scenario: User runs a migration in a single database app
19
+ When I create a database migration in a single database app
20
+ And It creates a users table with columns called 'name' and 'age' and 'email'
21
+ And I run `bundle exec rake db:migrate` in a single database app
22
+ Then I should see the created users table
23
+ And I should see the correct columns in the users table
24
+
25
+ Scenario: The schema is updated when migrations are run in a single database app
26
+ When I run a migration with the timestamp "20151010142141" in a single database app
27
+ Then the file "../../single-db-dummy/db/schema.rb" should exist
28
+ And the version in the schema file should be updated
29
+
30
+ Scenario: User runs a migration in a multi database app
31
+ When I create a database migration on the default database in a multi database app
32
+ And It creates a posts table with columns called 'title' and 'text' and 'author'
33
+ And I create another database migration on the users database in a multi database app
34
+ And It creates an accounts table with columns called 'expense' and 'user_id' and 'total'
35
+ And I create another database migration on the widgets database in a multi database app
36
+ And It creates a gadgets table with columns called 'doobry' and 'wotsit' and 'thingy'
37
+ And I run `bundle exec rake db:migrate` in a multi database app
38
+ Then I should see the created 'posts' table in the 'default' database
39
+ And I should see the created 'accounts' table in the 'users' database
40
+ And I should see the created 'gadgets' table in the 'widgets' database
41
+ And I should see the "title", "text" and "author" columns in the "posts" table in the "default" database
42
+ And I should see the "expense", "user_id" and "total" columns in the "accounts" table in the "users" database
43
+ And I should see the "doobry", "wotsit" and "thingy" columns in the "gadgets" table in the "widgets" database
44
+
45
+ Scenario: The schema is updated when migrations are run in a multi database app
46
+ When I run migrations with the following timestamps "20151010142141", "20151010141234" and "20151010145432" in a multi-database app
47
+ Then the file "../../multi-db-dummy/db/schema.rb" should exist
48
+ Then the file "../../multi-db-dummy/db/users_schema.rb" should exist
49
+ Then the file "../../multi-db-dummy/db/widgets_schema.rb" should exist
50
+ And the versions in the schema files should be updated
51
+
52
+ Scenario: Specifying the database to migrate using the DATABASE environment variable
53
+ Given There is a migration with the timestamp "20151010141234" for the users database
54
+ When I run `bundle exec rake db:migrate DATABASE=users` in a multi database app
55
+ Then the file "../../multi-db-dummy/db/users_schema.rb" should exist
56
+ And the file "../../multi-db-dummy/db/widgets_schema.rb" should not exist
57
+ And the file "../../multi-db-dummy/db/schema.rb" should not exist
58
+ And the version in the users schema file should be updated
59
+
60
+ Scenario: Specifying the database and the environment variables to migrate to
61
+ Given I set the environment variables to:
62
+ | variable | value |
63
+ | RAILS_ENV | production |
64
+ And there is a migration for the widgets database in a multi database app
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
+
70
+ Scenario: User runs rake db:migrate:status in a single database app
71
+ Given I have created and run a migration with the name "20151010142141_create_users_table.rb", in a single database app
72
+ And I have created but not run a migration with the name "20151010151000_add_nickname_to_users.rb", in a single database app
73
+ When I run `bundle exec rake db:migrate:status` in a single database app
74
+ Then the output should match /up\s+20151010142141\s+default\s+Create users table/
75
+ And the output should match /down\s+20151010151000\s+default\s+Add nickname to users/
76
+
77
+ Scenario: User runs rake db:migrate:status in a multi database app
78
+ Given I have created and run the following migrations in a multi database app:
79
+ | Migration | database|
80
+ | 20151010142141_create_posts_table.rb | default |
81
+ | 20151010141234_create_accounts_table.rb | users |
82
+ | 20151010145432_create_gadgets_table.rb | widgets |
83
+ And I have created and not run the following migrations:
84
+ | Migration | database|
85
+ | 20151010153000_add_address_to_accounts.rb | users |
86
+ | 20151010153500_add_serial_to_gadgets.rb | widgets |
87
+ When I run `bundle exec rake db:migrate:status` in a multi database app
88
+ Then the output should match /up\s+20151010142141\s+default\s+Create posts table/
89
+ Then the output should match /up\s+20151010141234\s+users\s+Create accounts table/
90
+ Then the output should match /up\s+20151010145432\s+widgets\s+Create gadgets table/
91
+ Then the output should match /down\s+20151010153000\s+users\s+Add address to accounts/
92
+ Then the output should match /down\s+20151010153500\s+widgets\s+Add serial to gadgets/
93
+
94
+
@@ -0,0 +1,32 @@
1
+ Feature: Migrations run for all databases in the app in the production environment
2
+
3
+ rake db:migrate should still work in a single database app
4
+ rake db:migrate should migrate all the databases in a multi-database app
5
+
6
+ Background:
7
+ Given I set the environment variables to:
8
+ | variable | value |
9
+ | RAILS_ENV | production |
10
+ And empty databases have been created for the app
11
+
12
+ Scenario: User runs a migration in a single database app in the production environment
13
+ When I create a database migration in a single database app
14
+ And It creates a users table with columns called 'name' and 'age' and 'email'
15
+ And I run `bundle exec rake db:migrate` in a single database app
16
+ Then I should see the created users table in the production environment
17
+ And I should see the correct columns in the users table in the production environment
18
+
19
+ Scenario: User runs a migration in a multi database app in the production environment
20
+ When I create a database migration on the default database in a multi database app
21
+ And It creates a posts table with columns called 'title' and 'text' and 'author'
22
+ And I create another database migration on the users database in a multi database app
23
+ And It creates an accounts table with columns called 'expense' and 'user_id' and 'total'
24
+ And I create another database migration on the widgets database in a multi database app
25
+ And It creates a gadgets table with columns called 'doobry' and 'wotsit' and 'thingy'
26
+ And I run `bundle exec rake db:migrate` in a multi database app
27
+ Then I should see the created "posts" table in the "default" database in the production environment
28
+ And I should see the created "accounts" table in the "users" database in the production environment
29
+ And I should see the created "gadgets" table in the "widgets" database in the production environment
30
+ And I should see the "title", "text" and "author" columns in the "posts" table in the "default" database in the production environment
31
+ And I should see the "expense", "user_id" and "total" columns in the "accounts" table in the "users" database in the production environment
32
+ And I should see the "doobry", "wotsit" and "thingy" columns in the "gadgets" table in the "widgets" database in the production environment
@@ -0,0 +1,244 @@
1
+ require 'sqlite3'
2
+ require 'pry'
3
+
4
+ Given(/^empty databases have been created for the app$/) do
5
+ clear_db_dir
6
+
7
+ cd "../../single-db-dummy" do
8
+ run_rake_db_create
9
+ end
10
+
11
+ cd "../../multi-db-dummy" do
12
+ run_rake_db_create
13
+ end
14
+ end
15
+
16
+
17
+ When(/^I create a database migration in a single database app$/) do
18
+ write_single_db_migration
19
+ end
20
+
21
+ When(/^It creates a users table with columns called 'name' and 'age' and 'email'$/) do
22
+ # This line is just describing what was created in the migration.
23
+ end
24
+
25
+ Then(/^I should see the created users table$/) do
26
+ table_exists? :app => "single-db-dummy", :database => "development.sqlite3", :table => "users"
27
+ end
28
+
29
+ Then(/^I should see the correct columns in the users table$/) do
30
+ columns_exist? :app => "single-db-dummy", :database => "development.sqlite3", :table => "users", :columns => ["name", "email", "age"]
31
+ end
32
+
33
+ When(/^I create a database migration on the default database in a multi database app$/) do
34
+ write_migration_for_default_db
35
+ end
36
+
37
+ When(/^It creates a posts table with columns called 'title' and 'text' and 'author'$/) do
38
+ # This line describes what was created in the migration
39
+ end
40
+
41
+ When(/^I create another database migration on the users database in a multi database app$/) do
42
+ write_migration_for_users_db
43
+ end
44
+
45
+ When(/^It creates an accounts table with columns called 'expense' and 'user_id' and 'total'$/) do
46
+ # This line shows which columns are created on the users database accounts table.
47
+ end
48
+
49
+ When(/^I create another database migration on the widgets database in a multi database app$/) do
50
+ write_migration_for_widgets_db
51
+ end
52
+
53
+ When(/^It creates a gadgets table with columns called 'doobry' and 'wotsit' and 'thingy'$/) do
54
+ # This line shows which columns are created on the widgets database gadgets table.
55
+ end
56
+
57
+ Then(/^I should see the created posts table in the default database$/) do
58
+ table_exists? :database => "development.sqlite3", :table => "posts"
59
+ end
60
+
61
+ Then(/^I should see the created '([^']*)' table in the '([^']*)' database$/) do |table, database|
62
+ if database == "default"
63
+ database_file_name = "development"
64
+ else
65
+ database_file_name = "#{database}_development"
66
+ end
67
+ table_exists? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table
68
+ end
69
+
70
+ Then(/^I should see the "([^"]*)", "([^"]*)" and "([^"]*)" columns in the "([^"]*)" table in the "([^"]*)" database$/) do |column1, column2, column3, table, database|
71
+ if database == "default"
72
+ database_file_name = "development"
73
+ else
74
+ database_file_name = "#{database}_development"
75
+ end
76
+ columns_exist? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table, :columns => [column1, column2, column3]
77
+ end
78
+
79
+ When(/^I run a migration with the timestamp "([^"]*)" in a single database app$/) do |timestamp|
80
+ @timestamp = timestamp
81
+ write_single_db_migration
82
+ run_task_in_single_db_app "bundle exec rake db:migrate"
83
+ end
84
+
85
+ Then(/^the version in the schema file should be updated$/) do
86
+ version = "version: #{@timestamp}"
87
+ expect(File.read "single-db-dummy/db/schema.rb").to match Regexp.new(version)
88
+ end
89
+
90
+ When(/^I run migrations with the following timestamps "([^"]*)", "([^"]*)" and "([^"]*)" in a multi\-database app$/) do |timestamp1, timestamp2, timestamp3|
91
+ @timestamps = [timestamp1, timestamp2, timestamp3]
92
+ write_migration_for_default_db
93
+ write_migration_for_users_db
94
+ write_migration_for_widgets_db
95
+ run_task_in_multi_db_app "bundle exec rake db:migrate"
96
+ end
97
+
98
+
99
+ Then(/^the versions in the schema files should be updated$/) do
100
+ version = "version: #{@timestamps[0]}"
101
+ expect(File.read "multi-db-dummy/db/schema.rb").to match Regexp.new(version)
102
+ version = "version: #{@timestamps[1]}"
103
+ expect(File.read "multi-db-dummy/db/users_schema.rb").to match Regexp.new(version)
104
+ version = "version: #{@timestamps[2]}"
105
+ expect(File.read "multi-db-dummy/db/widgets_schema.rb").to match Regexp.new(version)
106
+ end
107
+
108
+ Given(/^I have created and run a migration with the name "([^"]*)", in a single database app$/) do |up_migration|
109
+ @up_migration = up_migration
110
+ write_single_db_migration
111
+ run_task_in_single_db_app "bundle exec rake db:migrate"
112
+ end
113
+ Given(/^There is a migration with the timestamp "([^"]*)" for the users database$/) do |timestamp|
114
+ @timestamp = [timestamp]
115
+ write_migration_for_users_db
116
+ end
117
+
118
+ Given(/^I have created but not run a migration with the name "([^"]*)", in a single database app$/) do |down_migration|
119
+ @down_migration = down_migration
120
+ write_another_single_db_migration
121
+ end
122
+
123
+ Given(/^I have created and run the following migrations in a multi database app:$/) do |table|
124
+ write_migration_for_default_db
125
+ write_migration_for_users_db
126
+ write_migration_for_widgets_db
127
+ run_task_in_multi_db_app "bundle exec rake db:migrate"
128
+ end
129
+
130
+ Given(/^I have created and not run the following migrations:$/) do |table|
131
+ table.hashes.each do |row|
132
+ write_multi_db_migration_for row["database"], row["Migration"]
133
+ end
134
+ end
135
+
136
+
137
+ Then(/^the version in the users schema file should be updated$/) do
138
+ version = "version: #{@timestamp[0]}"
139
+ expect(File.read "multi-db-dummy/db/users_schema.rb").to match Regexp.new(version)
140
+ end
141
+
142
+ And(/^there is a migration for the widgets database in a multi database app$/) do
143
+ write_migration_for_widgets_db
144
+ end
145
+
146
+ # Helpers
147
+
148
+ def write_multi_db_migration_for(database, migration_name)
149
+ migration_database = database == "default" ? "migrate" : "#{database}_migrate"
150
+ migration_class = migration_name.match(/\d+_(\w+).rb/).captures.first.split('_').map(&:capitalize).join
151
+
152
+ migration = <<-MIGRATION_END
153
+ class #{migration_class} < ActiveRecord::Migration
154
+ def change
155
+ say "changed!"
156
+ end
157
+ end
158
+ MIGRATION_END
159
+
160
+ write_file "../../multi-db-dummy/db/#{migration_database}/#{migration_name}", migration
161
+ end
162
+
163
+ def run_rake_db_create
164
+ cmd = unescape_text("rake db:create")
165
+ cmd = extract_text(cmd) if !aruba.config.keep_ansi || aruba.config.remove_ansi_escape_sequences
166
+
167
+ run_simple(cmd, false)
168
+ end
169
+
170
+ def write_single_db_migration
171
+ migration = <<-MIGRATION_END
172
+ class CreateUsersTable < ActiveRecord::Migration
173
+ def change
174
+ create_table :users do |column|
175
+ column.string :name
176
+ column.integer :age
177
+ column.string :email
178
+ end
179
+ end
180
+ end
181
+ MIGRATION_END
182
+
183
+ write_file "../../single-db-dummy/db/migrate/20151010142141_" + "create_users_table.rb", migration
184
+ end
185
+
186
+ def write_another_single_db_migration
187
+ migration = <<-MIGRATION_END
188
+ class AddNicknameToUsers < ActiveRecord::Migration
189
+ def change
190
+ add_column :users, :nickname, :string
191
+ end
192
+ end
193
+ MIGRATION_END
194
+
195
+ write_file "../../single-db-dummy/db/migrate/20151010151000_" + "add_nickname_to_users.rb", migration
196
+ end
197
+
198
+ def write_migration_for_default_db
199
+ migration = <<-MIGRATION_END
200
+ class CreatePostsTable < ActiveRecord::Migration
201
+ def change
202
+ create_table :posts do |column|
203
+ column.string :title
204
+ column.integer :text
205
+ column.string :author
206
+ end
207
+ end
208
+ end
209
+ MIGRATION_END
210
+
211
+ write_file "../../multi-db-dummy/db/migrate/20151010142141_" + "create_posts_table.rb", migration
212
+ end
213
+
214
+ def write_migration_for_users_db
215
+ migration = <<-MIGRATION_END
216
+ class CreateAccountsTable < ActiveRecord::Migration
217
+ def change
218
+ create_table :accounts do |column|
219
+ column.string :expense
220
+ column.integer :user_id
221
+ column.string :total
222
+ end
223
+ end
224
+ end
225
+ MIGRATION_END
226
+
227
+ write_file "../../multi-db-dummy/db/users_migrate/20151010141234_" + "create_accounts_table.rb", migration
228
+ end
229
+
230
+ def write_migration_for_widgets_db
231
+ migration = <<-MIGRATION_END
232
+ class CreateGadgetsTable < ActiveRecord::Migration
233
+ def change
234
+ create_table :gadgets do |column|
235
+ column.string :doobry
236
+ column.integer :wotsit
237
+ column.string :thingy
238
+ end
239
+ end
240
+ end
241
+ MIGRATION_END
242
+
243
+ write_file "../../multi-db-dummy/db/widgets_migrate/20151010145432_" + "create_gadgets_table.rb", migration
244
+ end
@@ -0,0 +1,28 @@
1
+ require 'sqlite3'
2
+ require 'pry'
3
+
4
+ Then(/^I should see the created users table in the production environment$/) do
5
+ table_exists? :app => "single-db-dummy", :database => "production.sqlite3", :table => "users"
6
+ end
7
+
8
+ Then(/^I should see the correct columns in the users table in the production environment$/) do
9
+ columns_exist? :app => "single-db-dummy", :database => "production.sqlite3", :table => "users", :columns => ["name", "email", "age"]
10
+ end
11
+
12
+ Then(/^I should see the created "([^"]*)" table in the "([^"]*)" database in the production environment$/) do |table, database|
13
+ if database == "default"
14
+ database_file_name = "production"
15
+ else
16
+ database_file_name = "#{database}_production"
17
+ end
18
+ table_exists? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table
19
+ end
20
+
21
+ Then(/^I should see the "([^"]*)", "([^"]*)" and "([^"]*)" columns in the "([^"]*)" table in the "([^"]*)" database in the production environment$/) do |column1, column2, column3, table , database|
22
+ if database == "default"
23
+ database_file_name = "production"
24
+ else
25
+ database_file_name = "#{database}_production"
26
+ end
27
+ columns_exist? :app => "multi-db-dummy", :database => "#{database_file_name}.sqlite3", :table => table, :columns => [column1, column2, column3]
28
+ end
@@ -0,0 +1,41 @@
1
+
2
+ Given(/^no databases have been created$/) do
3
+ clear_db_dir
4
+ end
5
+
6
+ Given(/^I run `([^`]*)` in a single database app$/) do |command|
7
+ run_task_in_single_db_app(command)
8
+ end
9
+
10
+
11
+ Given(/^I run `([^`]*)` in a multi database app$/) do |command|
12
+ run_task_in_multi_db_app(command)
13
+ end
14
+
15
+
16
+ #helper
17
+
18
+ def run_task_in_single_db_app(command)
19
+ cmd = unescape_text(command)
20
+ cmd = extract_text(cmd) if !aruba.config.keep_ansi || aruba.config.remove_ansi_escape_sequences
21
+
22
+ cd "../../single-db-dummy" do
23
+ run_simple(cmd, false)
24
+ end
25
+ end
26
+
27
+ def run_task_in_multi_db_app(command)
28
+ cmd = unescape_text(command)
29
+ cmd = extract_text(cmd) if !aruba.config.keep_ansi || aruba.config.remove_ansi_escape_sequences
30
+
31
+ cd "../../multi-db-dummy" do
32
+ run_simple(cmd, false)
33
+ end
34
+ end
35
+
36
+ def clear_db_dir
37
+ remove("../../single-db-dummy/db", :force => true)
38
+ remove("../../multi-db-dummy/db", :force => true)
39
+ create_directory "../../single-db-dummy/db"
40
+ create_directory "../../multi-db-dummy/db"
41
+ end