multi-database-9000 0.1.0 → 0.2.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: 2b615a4d064746274e944f5bd1c41720eb88e416
4
- data.tar.gz: 3d988a77248229b87e719feee2fd930abd3f4867
3
+ metadata.gz: 053dcf36754207e2afd03e12dbbc523acefe665d
4
+ data.tar.gz: 964c14eda0b97b3a2acb60cfe8751be735b98915
5
5
  SHA512:
6
- metadata.gz: 0bd12c0da8d04f705809843bcaf6db87380dab761ec3301964f7483755c0c0f8f5a4c5b3545995cc4001296d3aa180ffede8fd393f20e89f83df44f8ecafc3d0
7
- data.tar.gz: 7a1b040ea4ff78b2e9a1e1c41c0269a81f3a72c7e83e201af8bd3ba464c74e84083e3f1a199d1a4c8688158f1ae0005e2108a3decdbd837e95c857b0154be904
6
+ metadata.gz: 5610ed20bb066f1163bbc8adface34b51b8160017c4f6b6cdd2212c8a2f9bf68e20dd34d8490f293e3e183165f40673eba671d272e3c52843a09c9cd94d2ef9f
7
+ data.tar.gz: b66a494376a8443ae0f54b87471cac4190000f720fcf06c91eea25e9d4f317d752c790c807415e9fe8d8c6f5c2e661d7988ee9036b8f2433b4a091722aa4fe52
data/README.md CHANGED
@@ -32,20 +32,25 @@ and production connections defined in database.yml. Also that there are one or m
32
32
 
33
33
  ## Usage
34
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.
35
+ ### Database Connections
36
+ Create a new set of connections for your database in database.yml, in the form of "*database_name*_test",
37
+ "*database_name*_production" etc.
37
38
 
38
- Create a new directory under db/ called <database_name>_migrate to hold migrations for the database.
39
+ Create a new directory under db/ called *database_name*_migrate to hold migrations for the database.
39
40
 
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
41
+ ### The Multi Migration command
42
+ Create a new migration with "rails generate multi_migration *migration* *database_name*". The *database_name* is 'default' for your default or original database, otherwise it is the name of the database you wish to migrate to e.g. "rails generate multi_migration AddTitleToWidgets widgets".
42
43
 
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.
44
+ If you wish to use multi-database-9000 in a single database app, the command 'rails generate migration CreateWidgetsTable' will still work.
45
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.
46
+ ### Rake Tasks
47
+ "rake db:create" will attempt to create all databases for all connections.
48
+ "rake db:create DATABASE=widgets" will only create databases for connections with "widget" in their name.
48
49
 
50
+ Similarly, "rake db:schema:load" and "rake db:schema:dump" will load or dump schemas for all connections to all databases.
51
+ "rake db:schema:load DATABASE=widget" will only load the schema for databases in connections with "widget" in their name.
52
+
53
+ ### Database Schema
49
54
  In order for test database schemas to be kept updated, add the following line to your `test_helper` or `spec_helper` files:
50
55
 
51
56
  MultiDatabase9000.maintain_all_test_schemas!
@@ -0,0 +1,27 @@
1
+ Feature: Running rails generate commands produces migration files in the correct database migration folders
2
+
3
+ 'rails generate migration CreateXxxxxTable' should create a migration file in the 'migrate' folder in a single database app
4
+ 'rails generate multi_migration CreateXxxxxTable default' should create a migration file in the 'migrate' folder in a multi-database app
5
+ 'rails generate multi_migration CreateXxxxxTable users' should create a migration file in the 'users_migrate' folder in a multi-database app
6
+ 'rails generate multi_migration CreateXxxxxTable widgets' should create a migration file in the 'widgets_migrate' folder in a multi-database app
7
+
8
+ Background:
9
+ Given empty databases have been created for the app
10
+
11
+ Scenario: Running 'rails generate migration' in a single database app
12
+ Given There is no db/migrate folder before a migration is generated in a single database app
13
+ And I run `rails generate migration CreateFoolsTable` in a single database app
14
+ Then I should see the db/migrate folder in a single database app
15
+ And I should see a migration file in the db/migrate folder in a single database app
16
+
17
+ Scenario: Running "rails generate multi_migration <database_name>" in a multi database app
18
+ Given There are no migration folders before a migration is generated in a multi database app
19
+ And I run `rails generate multi_migration CreateFoolsTable default` in a multi database app
20
+ Then I should see the db/migrate folder for the default database
21
+ And I should see a migration file in the db/migrate folder in a multi database app
22
+ And I run `rails generate multi_migration CreateFoolsTable users` in a multi database app
23
+ Then I should see the db/users_migrate folder for the default database
24
+ And I should see a migration file in the db/users_migrate folder in a multi database app
25
+ And I run `rails generate multi_migration CreateFoolsTable widgets` in a multi database app
26
+ Then I should see the db/widgets_migrate folder for the default database
27
+ And I should see a migration file in the db/widgets_migrate folder in a multi database app
@@ -0,0 +1,48 @@
1
+
2
+ Given(/^There is no db\/migrate folder before a migration is generated in a single database app$/) do
3
+ expect('../../single-db-dummy/db/migrate').not_to be_an_existing_directory
4
+ end
5
+
6
+ Then(/^I should see the db\/migrate folder in a single database app$/) do
7
+ expect('../../single-db-dummy/db/migrate').to be_an_existing_directory
8
+ end
9
+
10
+
11
+ Then(/^I should see a migration file in the db\/migrate folder in a single database app$/) do
12
+ expect('../../single-db-dummy/db/migrate').not_to be_empty
13
+ expect(Dir.entries("single-db-dummy/db/migrate").last).to include "_create_fools_table.rb"
14
+ end
15
+
16
+
17
+ Given(/^There are no migration folders before a migration is generated in a multi database app$/) do
18
+ expect('../../multi-db-dummy/db/migrate').not_to be_an_existing_directory
19
+ expect('../../multi-db-dummy/db/users_migrate').not_to be_an_existing_directory
20
+ expect('../../multi-db-dummy/db/widgets_migrate').not_to be_an_existing_directory
21
+ end
22
+
23
+ Then(/^I should see the db\/migrate folder for the default database$/) do
24
+ expect('../../multi-db-dummy/db/migrate').to be_an_existing_directory
25
+ end
26
+
27
+ Then(/^I should see a migration file in the db\/migrate folder in a multi database app$/) do
28
+ expect('../../multi-db-dummy/db/migrate').not_to be_empty
29
+ expect(Dir.entries("multi-db-dummy/db/migrate").last).to include "_create_fools_table.rb"
30
+ end
31
+
32
+ Then(/^I should see the db\/users_migrate folder for the default database$/) do
33
+ expect('../../multi-db-dummy/db/users_migrate').to be_an_existing_directory
34
+ end
35
+
36
+ Then(/^I should see a migration file in the db\/users_migrate folder in a multi database app$/) do
37
+ expect('../../multi-db-dummy/db/users_migrate').not_to be_empty
38
+ expect(Dir.entries("multi-db-dummy/db/users_migrate").last).to include "_create_fools_table.rb"
39
+ end
40
+
41
+ Then(/^I should see the db\/widgets_migrate folder for the default database$/) do
42
+ expect('../../multi-db-dummy/db/widgets_migrate').to be_an_existing_directory
43
+ end
44
+
45
+ Then(/^I should see a migration file in the db\/widgets_migrate folder in a multi database app$/) do
46
+ expect('../../multi-db-dummy/db/widgets_migrate').not_to be_empty
47
+ expect(Dir.entries("multi-db-dummy/db/widgets_migrate").last).to include "_create_fools_table.rb"
48
+ end
@@ -0,0 +1,44 @@
1
+ require 'pry'
2
+ class MultiMigrationGenerator < Rails::Generators::NamedBase
3
+ include Rails::Generators::Migration
4
+
5
+ argument :database_name, :type => :string, :required => true, :banner => 'DBNAME'
6
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
+
8
+ hook_for :orm, :required => true
9
+
10
+ source_root File.expand_path('../templates', __FILE__)
11
+
12
+ def self.next_migration_number(dirname)
13
+ next_migration_number = current_migration_number(dirname) + 1
14
+ if ActiveRecord::Base.timestamped_migrations
15
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
16
+ else
17
+ "%.3d" % next_migration_number
18
+ end
19
+ end
20
+
21
+ def create_migration_file
22
+ set_local_assigns!
23
+ migration_template "migration.rb", migration_directory
24
+ end
25
+
26
+ def migration_directory
27
+ if database_name == "default"
28
+ "db/migrate/#{file_name}.rb"
29
+ else
30
+ "db/#{database_name.downcase}_migrate/#{file_name}.rb"
31
+ end
32
+ end
33
+
34
+ protected
35
+ attr_reader :migration_action
36
+
37
+ def set_local_assigns!
38
+ if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
39
+ @migration_action = $1
40
+ @table_name = $2.pluralize
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,26 @@
1
+
2
+ class <%= migration_class_name %> < ActiveRecord::Migration
3
+ <%- if migration_action == 'add' -%>
4
+ def change
5
+ <% attributes.each do |attribute| -%>
6
+ add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %>
7
+ <%- end -%>
8
+ end
9
+ <%- else -%>
10
+ def up
11
+ <% attributes.each do |attribute| -%>
12
+ <%- if migration_action -%>
13
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %>
14
+ <%- end -%>
15
+ <%- end -%>
16
+ end
17
+
18
+ def down
19
+ <% attributes.reverse.each do |attribute| -%>
20
+ <%- if migration_action -%>
21
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %>
22
+ <%- end -%>
23
+ <%- end -%>
24
+ end
25
+ <%- end -%>
26
+ end
@@ -1,3 +1,3 @@
1
1
  module MultiDatabase9000
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,4 +7,5 @@ module MultiDatabase9000
7
7
  load "multi-database-9000/tasks/multi-database-9000_tasks.rake"
8
8
  end
9
9
  end
10
+
10
11
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- multi-database-9000 (0.0.1)
4
+ multi-database-9000 (0.1.0)
5
5
  rails (>= 4.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- multi-database-9000 (0.0.1)
4
+ multi-database-9000 (0.1.0)
5
5
  rails (>= 4.0)
6
6
 
7
7
  GEM
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.1.0
4
+ version: 0.2.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-10-14 00:00:00.000000000 Z
13
+ date: 2015-10-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -139,13 +139,17 @@ files:
139
139
  - features/creating_databases.feature
140
140
  - features/development_migrations.feature
141
141
  - features/production_migrations.feature
142
+ - features/running_generators.feature
142
143
  - features/step_definitions/development_migrations_steps.rb
143
144
  - features/step_definitions/production_migration_steps.rb
145
+ - features/step_definitions/running_generators_steps.rb
144
146
  - features/step_definitions/sqlite_database_steps.rb
145
147
  - features/step_definitions/test_migration_steps.rb
146
148
  - features/support/env.rb
147
149
  - features/test_app.feature
148
150
  - features/test_migrations.feature
151
+ - lib/generators/multi_migration_generator.rb
152
+ - lib/generators/templates/migration.rb
149
153
  - lib/multi-database-9000.rb
150
154
  - lib/multi-database-9000/multi-database-9000.rb
151
155
  - lib/multi-database-9000/tasks/multi-database-9000_tasks.rake
@@ -277,7 +281,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
277
281
  version: '0'
278
282
  requirements: []
279
283
  rubyforge_project:
280
- rubygems_version: 2.4.5
284
+ rubygems_version: 2.4.6
281
285
  signing_key:
282
286
  specification_version: 4
283
287
  summary: Enables Rails apps with multiple databases to handle migrations and rake
@@ -286,8 +290,10 @@ test_files:
286
290
  - features/creating_databases.feature
287
291
  - features/development_migrations.feature
288
292
  - features/production_migrations.feature
293
+ - features/running_generators.feature
289
294
  - features/step_definitions/development_migrations_steps.rb
290
295
  - features/step_definitions/production_migration_steps.rb
296
+ - features/step_definitions/running_generators_steps.rb
291
297
  - features/step_definitions/sqlite_database_steps.rb
292
298
  - features/step_definitions/test_migration_steps.rb
293
299
  - features/support/env.rb