multi-database-migrations 0.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multi-database-migrations.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 jystewart, chrisrohr, sinsoku
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,58 @@
1
+ # Multi::Database::Migrations
2
+
3
+ A plugin to make it easier to host migrations for multiple databases in one rails app. NOTE: This has been upgraded to work with Rails 3.0.x.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'multi-database-migrations'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install multi-database-migrations
18
+
19
+ ## CONCEPT
20
+
21
+ Rather than mixing all the migrations in one folder and relying on the specific migration to define which database it works with, we have separate folders for each database. Supposing you had a normal database--myapp\_development--and a legacy database--legacy\_development--we'd have something like:
22
+
23
+ db/migrate
24
+ db/migrate/myapp
25
+ db/migrate/legacy
26
+
27
+ and each database's migrations sit in the relevant folder.
28
+
29
+ ### USAGE
30
+
31
+ With the plugin installed we have:
32
+
33
+ rails g multi_migration MigrationName DBName ....
34
+
35
+ which should accept all the options of a normal migration generation, and
36
+
37
+ rake db:multi:migrate DATABASE=xxxx
38
+
39
+ normally we would expect the database name to match rails' conventions, eg:
40
+
41
+ rake db:multi:migrate DATABASE=myapp
42
+
43
+ would look for a database configuration of myapp\_development. But if you specify a full database name, we will look for a configuration with a matching database name.
44
+
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ ### CREDIT
55
+
56
+ * This plugin was originally created by James Stewart - http://jystewart.net/process/
57
+ * Converted to Rails 3 by Chris Rohr http://www.nearinfinity.com/blogs/chris_rohr/
58
+ * Converted to gem by sinsoku https://github.com/sinsoku/
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Generator that creates migrations for multiple databases to run independently.
3
+
4
+ Example:
5
+ rails generate multi_migration NAME DBNAME [field:type field:type] [options]
6
+
7
+ This will create:
8
+ db/migrate/DBNAME/NAME
@@ -0,0 +1,34 @@
1
+ class MultiMigrationGenerator < Rails::Generators::NamedBase
2
+ include Rails::Generators::Migration
3
+
4
+ argument :database_name, :type => :string, :required => true, :banner => 'DBNAME'
5
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
6
+
7
+ hook_for :orm, :required => true
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def self.next_migration_number(dirname)
12
+ next_migration_number = current_migration_number(dirname) + 1
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
15
+ else
16
+ "%.3d" % next_migration_number
17
+ end
18
+ end
19
+
20
+ def create_migration_file
21
+ set_local_assigns!
22
+ migration_template "migration.rb", "db/migrate/#{database_name.downcase}/#{file_name}.rb"
23
+ end
24
+
25
+ protected
26
+ attr_reader :migration_action
27
+
28
+ def set_local_assigns!
29
+ if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
30
+ @migration_action = $1
31
+ @table_name = $2.pluralize
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ <%- if migration_action == 'add' -%>
3
+ def change
4
+ <% attributes.each do |attribute| -%>
5
+ add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %>
6
+ <%- end -%>
7
+ end
8
+ <%- else -%>
9
+ def up
10
+ <% attributes.each do |attribute| -%>
11
+ <%- if migration_action -%>
12
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end %>
13
+ <%- end -%>
14
+ <%- end -%>
15
+ end
16
+
17
+ def down
18
+ <% attributes.reverse.each do |attribute| -%>
19
+ <%- if migration_action -%>
20
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end %>
21
+ <%- end -%>
22
+ <%- end -%>
23
+ end
24
+ <%- end -%>
25
+ end
@@ -0,0 +1,13 @@
1
+ require "multi-database-migrations/version"
2
+
3
+ module Multi
4
+ module Database
5
+ module Migrations
6
+ class Railtie < Rails::Railtie
7
+ rake_tasks do
8
+ load "multi-database-migrations/tasks/multi_database_migrations_tasks.rake"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,84 @@
1
+ module MultiMigrations
2
+ def self.make_connection(db_name = nil)
3
+ raise "DATABASE is required" unless ENV['DATABASE']
4
+ connection_key = self.identify_configuration
5
+ raise "VALID DATABASE is required" unless connection_key
6
+ ActiveRecord::Base.establish_connection(connection_key)
7
+ end
8
+
9
+ def self.identify_configuration
10
+ if ActiveRecord::Base.configurations.has_key?("#{ENV['DATABASE']}_#{Rails.env}")
11
+ return "#{ENV['DATABASE']}_#{Rails.env}"
12
+ else
13
+ match = ActiveRecord::Base.configurations.find { |config| config[1]['database'] == ENV['DATABASE'] }
14
+ return match[0] unless match.nil?
15
+ end
16
+ end
17
+ end
18
+
19
+ namespace :db do
20
+ namespace :multi do
21
+ desc "Migrate through the scripts in db/migrate/<dbname>/ Target specific version with VERSION=x. Turn off output with VERBOSE=false."
22
+ task :migrate => :environment do
23
+ MultiMigrations.make_connection(ENV['DATABASE'])
24
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
25
+ ActiveRecord::Migrator.migrate("db/migrate/#{ENV['DATABASE']}", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
26
+ Rake::Task["db:multi:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
27
+ end
28
+
29
+ namespace :migrate do
30
+ desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
31
+ task :redo => [ 'db:multi:rollback', 'db:multi:migrate' ]
32
+
33
+ # TODO: Implement db:multi:drop, db:multi:create
34
+ #desc 'Resets your database using your migrations for the current environment'
35
+ #task :reset => ["db:multi:drop", "db:multi:create", "db:multi:migrate"]
36
+
37
+ desc 'Runs the "up" for a given migration VERSION.'
38
+ task :up => :environment do
39
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
40
+ raise "VERSION is required" unless version
41
+ MultiMigrations.make_connection(ENV['DATABASE'])
42
+ ActiveRecord::Migrator.run(:up, "db/migrate/#{ENV['DATABASE']}", version)
43
+ Rake::Task["db:multi:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
44
+ end
45
+
46
+ desc 'Runs the "down" for a given migration VERSION.'
47
+ task :down => :environment do
48
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
49
+ raise "VERSION is required" unless version
50
+ MultiMigrations.make_connection(ENV['DATABASE'])
51
+ ActiveRecord::Migrator.run(:down, "db/migrate/#{ENV['DATABASE']}", version)
52
+ Rake::Task["db:multi:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
53
+ end
54
+ end
55
+
56
+ desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
57
+ task :rollback => :environment do
58
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
59
+ MultiMigrations.make_connection(ENV['DATABASE'])
60
+ ActiveRecord::Migrator.rollback("db/migrate/#{ENV['DATABASE']}", step)
61
+ Rake::Task["db:multi:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
62
+ end
63
+
64
+
65
+ namespace :schema do
66
+ desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
67
+ task :dump => :environment do
68
+ MultiMigrations.make_connection(ENV['DATABASE'])
69
+ require 'active_record/schema_dumper'
70
+ File.open(ENV['SCHEMA'] || "db/schema_#{ENV["DATABASE"]}.rb", "w:utf-8") do |file|
71
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
72
+ end
73
+ end
74
+
75
+ desc "Load a schema.rb file into the database"
76
+ task :load => :environment do
77
+ MultiMigrations.make_connection(ENV['DATABASE'])
78
+ file = ENV['SCHEMA'] || "db/schema_#{ENV['DATABASE']}.rb"
79
+ load(file)
80
+ end
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ module Multi
2
+ module Database
3
+ module Migrations
4
+ VERSION = "0.0.1.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/multi-database-migrations/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["jystewart", "chrisrohr", "sinsoku"]
6
+ gem.email = ["jys@ketlai.co.uk", "crohr@nearinfinity.com", "sinsoku.listy@gmail.com"]
7
+ gem.description = %q{A plugin to make it easier to host migrations for multiple databases in one rails app.}
8
+ gem.summary = %q{multi-datbase-migrations}
9
+ gem.homepage = "https://github.com/chrisrohr/multi-datbase-migrations"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "multi-database-migrations"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Multi::Database::Migrations::VERSION
17
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+ require 'generators/multi_migration/multi_migration_generator'
3
+
4
+ class MultiMigrationGeneratorTest < Rails::Generators::TestCase
5
+ destination "tmp"
6
+ setup :prepare_destination
7
+ tests ::MultiMigrationGenerator
8
+
9
+ context "MultiMigrationGenerator" do
10
+ should "create the migrations in the specific database folders" do
11
+ run_generator %w(create_table_in_test_db_1 test_db1 --orm active_record)
12
+ assert_migration "db/migrate/test_db1/create_table_in_test_db_1"
13
+
14
+ run_generator %w(create_table_in_test_db_2 test_db2 --orm active_record)
15
+ assert_migration "db/migrate/test_db2/create_table_in_test_db_2"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ # require 'bundler'
6
+ # Bundler.setup
7
+
8
+ ENV['RAILS_ENV'] = 'test'
9
+
10
+ $:.unshift File.dirname(__FILE__)
11
+
12
+ require "rails"
13
+ require "active_record"
14
+ require "rails/test_help"
15
+ require "rails/generators"
16
+ require "rails/generators/rails/migration/migration_generator"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi-database-migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - jystewart
9
+ - chrisrohr
10
+ - sinsoku
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2012-07-31 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description: A plugin to make it easier to host migrations for multiple databases
17
+ in one rails app.
18
+ email:
19
+ - jys@ketlai.co.uk
20
+ - crohr@nearinfinity.com
21
+ - sinsoku.listy@gmail.com
22
+ executables: []
23
+ extensions: []
24
+ extra_rdoc_files: []
25
+ files:
26
+ - .gitignore
27
+ - Gemfile
28
+ - LICENSE
29
+ - README.md
30
+ - Rakefile
31
+ - lib/generators/multi_migration/USAGE
32
+ - lib/generators/multi_migration/multi_migration_generator.rb
33
+ - lib/generators/multi_migration/templates/migration.rb
34
+ - lib/multi-database-migrations.rb
35
+ - lib/multi-database-migrations/tasks/multi_database_migrations_tasks.rake
36
+ - lib/multi-database-migrations/version.rb
37
+ - multi-database-migrations.gemspec
38
+ - test/multi_migration_generator_test.rb
39
+ - test/test_helper.rb
40
+ homepage: https://github.com/chrisrohr/multi-datbase-migrations
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: multi-datbase-migrations
64
+ test_files:
65
+ - test/multi_migration_generator_test.rb
66
+ - test/test_helper.rb