dbmule 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +12 -0
- data/Gemfile.lock +83 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +200 -0
- data/Rakefile +41 -0
- data/VERSION +1 -0
- data/dbmule.gemspec +62 -0
- data/lib/mule_migrations.rb +19 -0
- data/lib/mule_migrations/configurator.rb +85 -0
- data/lib/mule_migrations/generator.rb +83 -0
- data/lib/mule_migrations/minimal_railtie_config.rb +10 -0
- data/lib/mule_migrations/tasks.rb +35 -0
- data/lib/mule_migrations/tasks/connection.rake +7 -0
- data/lib/mule_migrations/tasks/environment.rake +3 -0
- data/lib/mule_migrations/tasks/mule.rake +331 -0
- data/lib/tasks/mule_migrations.rb +2 -0
- data/vendor/migration_helpers/MIT-LICENSE +20 -0
- data/vendor/migration_helpers/README.markdown +92 -0
- data/vendor/migration_helpers/init.rb +4 -0
- data/vendor/migration_helpers/lib/migration_helper.rb +51 -0
- metadata +115 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionpack (3.2.11)
|
5
|
+
activemodel (= 3.2.11)
|
6
|
+
activesupport (= 3.2.11)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
erubis (~> 2.7.0)
|
9
|
+
journey (~> 1.0.4)
|
10
|
+
rack (~> 1.4.0)
|
11
|
+
rack-cache (~> 1.2)
|
12
|
+
rack-test (~> 0.6.1)
|
13
|
+
sprockets (~> 2.2.1)
|
14
|
+
activemodel (3.2.11)
|
15
|
+
activesupport (= 3.2.11)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
activerecord (3.2.11)
|
18
|
+
activemodel (= 3.2.11)
|
19
|
+
activesupport (= 3.2.11)
|
20
|
+
arel (~> 3.0.2)
|
21
|
+
tzinfo (~> 0.3.29)
|
22
|
+
activesupport (3.2.11)
|
23
|
+
i18n (~> 0.6)
|
24
|
+
multi_json (~> 1.0)
|
25
|
+
arel (3.0.2)
|
26
|
+
builder (3.0.4)
|
27
|
+
diff-lcs (1.1.3)
|
28
|
+
erubis (2.7.0)
|
29
|
+
git (1.2.5)
|
30
|
+
hike (1.2.1)
|
31
|
+
i18n (0.6.1)
|
32
|
+
jeweler (1.6.4)
|
33
|
+
bundler (~> 1.0)
|
34
|
+
git (>= 1.2.5)
|
35
|
+
rake
|
36
|
+
journey (1.0.4)
|
37
|
+
json (1.7.6)
|
38
|
+
multi_json (1.5.0)
|
39
|
+
rack (1.4.4)
|
40
|
+
rack-cache (1.2)
|
41
|
+
rack (>= 0.4)
|
42
|
+
rack-ssl (1.3.2)
|
43
|
+
rack
|
44
|
+
rack-test (0.6.2)
|
45
|
+
rack (>= 1.0)
|
46
|
+
railties (3.2.11)
|
47
|
+
actionpack (= 3.2.11)
|
48
|
+
activesupport (= 3.2.11)
|
49
|
+
rack-ssl (~> 1.3.2)
|
50
|
+
rake (>= 0.8.7)
|
51
|
+
rdoc (~> 3.4)
|
52
|
+
thor (>= 0.14.6, < 2.0)
|
53
|
+
rake (0.9.2.2)
|
54
|
+
rdoc (3.12)
|
55
|
+
json (~> 1.4)
|
56
|
+
rspec (2.7.0)
|
57
|
+
rspec-core (~> 2.7.0)
|
58
|
+
rspec-expectations (~> 2.7.0)
|
59
|
+
rspec-mocks (~> 2.7.0)
|
60
|
+
rspec-core (2.7.1)
|
61
|
+
rspec-expectations (2.7.0)
|
62
|
+
diff-lcs (~> 1.1.2)
|
63
|
+
rspec-mocks (2.7.0)
|
64
|
+
sprockets (2.2.2)
|
65
|
+
hike (~> 1.2)
|
66
|
+
multi_json (~> 1.0)
|
67
|
+
rack (~> 1.0)
|
68
|
+
tilt (~> 1.1, != 1.3.0)
|
69
|
+
sqlite3 (1.3.6)
|
70
|
+
thor (0.17.0)
|
71
|
+
tilt (1.3.3)
|
72
|
+
tzinfo (0.3.35)
|
73
|
+
|
74
|
+
PLATFORMS
|
75
|
+
ruby
|
76
|
+
|
77
|
+
DEPENDENCIES
|
78
|
+
activerecord (~> 3.2.11)
|
79
|
+
jeweler
|
80
|
+
railties (~> 3.2.11)
|
81
|
+
rake
|
82
|
+
rspec (~> 2)
|
83
|
+
sqlite3
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2011 Todd Huss
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,200 @@
|
|
1
|
+
Rails migrations in non-Rails (and non Ruby) projects.
|
2
|
+
|
3
|
+
WHAT'S NEW
|
4
|
+
==========
|
5
|
+
In the 2.x release we have moved to using Rails 3 migrations instead of maintaining our own migration related code. Just about anything you can do with Rails 3 migrations you can now do with [Standalone Migrations](https://github.com/thuss/standalone-migrations) too! This removed 95% of the code we have to maintain. Big thanks to [Ricardo Valeriano](http://ricardovaleriano.com/) and [Michael Grosser](http://pragmatig.wordpress.com) for undertaking this major rewrite!
|
6
|
+
|
7
|
+
CONTRIBUTE
|
8
|
+
==========
|
9
|
+
[Standalone Migrations](https://github.com/thuss/standalone-migrations) relies on the contributions of the open-source community! To submit a fix or an enhancement fork the repository, checkout the *develop* branch, make your changes, add your name to the *Contributors* section in README.markdown, and send us a pull request! If you're active and do good work we'll add you as a collaborator!
|
10
|
+
|
11
|
+
USAGE
|
12
|
+
=====
|
13
|
+
Install Ruby, RubyGems and a ruby-database driver (e.g. `gem install mysql`) then:
|
14
|
+
|
15
|
+
$ gem install standalone_migrations
|
16
|
+
|
17
|
+
Add to `Rakefile` in your projects base directory:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'standalone_migrations'
|
21
|
+
StandaloneMigrations::Tasks.load_tasks
|
22
|
+
```
|
23
|
+
|
24
|
+
Add database configuration to `db/config.yml` in your projects base directory e.g.:
|
25
|
+
|
26
|
+
development:
|
27
|
+
adapter: sqlite3
|
28
|
+
database: db/development.sqlite3
|
29
|
+
pool: 5
|
30
|
+
timeout: 5000
|
31
|
+
|
32
|
+
production:
|
33
|
+
adapter: mysql
|
34
|
+
encoding: utf8
|
35
|
+
reconnect: false
|
36
|
+
database: somedatabase_dev
|
37
|
+
pool: 5
|
38
|
+
username: root
|
39
|
+
password:
|
40
|
+
socket: /var/run/mysqld/mysqld.sock
|
41
|
+
|
42
|
+
test: &test
|
43
|
+
adapter: sqlite3
|
44
|
+
database: db/test.sqlite3
|
45
|
+
pool: 5
|
46
|
+
timeout: 5000
|
47
|
+
|
48
|
+
### To create a new database migration:
|
49
|
+
|
50
|
+
rake db:new_migration name=foo_bar_migration
|
51
|
+
edit db/migrate/20081220234130_foo_bar_migration.rb
|
52
|
+
|
53
|
+
#### If you really want to, you can just execute raw SQL:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
def up
|
57
|
+
execute "insert into foo values (123,'something');"
|
58
|
+
end
|
59
|
+
|
60
|
+
def down
|
61
|
+
execute "delete from foo where field='something';"
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### To apply your newest migration:
|
66
|
+
|
67
|
+
rake db:migrate
|
68
|
+
|
69
|
+
### To migrate to a specific version (for example to rollback)
|
70
|
+
|
71
|
+
rake db:migrate VERSION=20081220234130
|
72
|
+
|
73
|
+
### To migrate a specific database (for example your "testing" database)
|
74
|
+
|
75
|
+
rake db:migrate DB=test ... or ...
|
76
|
+
rake db:migrate RAILS_ENV=test
|
77
|
+
|
78
|
+
### To execute a specific up/down of one single migration
|
79
|
+
|
80
|
+
rake db:migrate:up VERSION=20081220234130
|
81
|
+
|
82
|
+
### To revert your last migration
|
83
|
+
|
84
|
+
rake db:rollback
|
85
|
+
|
86
|
+
### To revert your last 3 migrations
|
87
|
+
|
88
|
+
rake db:rollback STEP=3
|
89
|
+
|
90
|
+
### Custom configuration
|
91
|
+
|
92
|
+
By default, Standalone Migrations will assume there exists a "db/"
|
93
|
+
directory in your project. But if for some reason you need a specific
|
94
|
+
directory structure to work with, you can use a configuration file
|
95
|
+
named .standalone_migrations in the root of your project containing
|
96
|
+
the following:
|
97
|
+
|
98
|
+
```yaml
|
99
|
+
db:
|
100
|
+
seeds: db/seeds.rb
|
101
|
+
migrate: db/migrate
|
102
|
+
schema: db/schema.rb
|
103
|
+
config:
|
104
|
+
database: db/config.yml
|
105
|
+
```
|
106
|
+
|
107
|
+
These are the configurable options available. You can omit any of
|
108
|
+
the keys and Standalone Migrations will assume the default values.
|
109
|
+
|
110
|
+
#### Changing environment config in runtime
|
111
|
+
|
112
|
+
If you are using Heroku or have to create or change your connection
|
113
|
+
configuration based on runtime aspects (maybe environment variables),
|
114
|
+
you can use the `StandaloneMigrations::Configurator.environments_config`
|
115
|
+
method. Check the usage example:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
require 'tasks/standalone_migrations'
|
119
|
+
|
120
|
+
StandaloneMigrations::Configurator.environments_config do |env|
|
121
|
+
|
122
|
+
env.on "production" do
|
123
|
+
|
124
|
+
if (ENV['DATABASE_URL'])
|
125
|
+
db = URI.parse(ENV['DATABASE_URL'])
|
126
|
+
return {
|
127
|
+
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
|
128
|
+
:host => db.host,
|
129
|
+
:username => db.user,
|
130
|
+
:password => db.password,
|
131
|
+
:database => db.path[1..-1],
|
132
|
+
:encoding => 'utf8'
|
133
|
+
}
|
134
|
+
end
|
135
|
+
|
136
|
+
nil
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
You have to put this anywhere on your `Rakefile`. If you want to
|
143
|
+
change some configuration, call the #on method on the object
|
144
|
+
received as argument in your block passed to ::environments_config
|
145
|
+
method call. The #on method receives the key to the configuration
|
146
|
+
that you want to change within the block. The block should return
|
147
|
+
your new configuration hash or nil if you want the configuration
|
148
|
+
to stay the same.
|
149
|
+
|
150
|
+
Your logic to decide the new configuration need to access some data
|
151
|
+
in your current configuration? Then you should receive the configuration
|
152
|
+
in your block, like this:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
require 'tasks/standalone_migrations'
|
156
|
+
|
157
|
+
StandaloneMigrations::Configurator.environments_config do |env|
|
158
|
+
|
159
|
+
env.on "my_custom_config" do |current_custom_config|
|
160
|
+
p current_custom_config
|
161
|
+
# => the values on your current "my_custom_config" environment
|
162
|
+
nil
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
#### Exporting Generated SQL
|
169
|
+
|
170
|
+
If instead of the database-agnostic `schema.rb` file you'd like to
|
171
|
+
save the database-specific SQL generated by the migrations, simply
|
172
|
+
add this to your `Rakefile`.
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
require 'tasks/standalone_migrations'
|
176
|
+
ActiveRecord::Base.schema_format = :sql
|
177
|
+
```
|
178
|
+
|
179
|
+
You should see a `db/structure.sql` file the next time you run a
|
180
|
+
migration.
|
181
|
+
|
182
|
+
Contributors
|
183
|
+
============
|
184
|
+
- [Todd Huss](http://gabrito.com/)
|
185
|
+
- [Michael Grosser](http://pragmatig.wordpress.com)
|
186
|
+
- [Ricardo Valeriano](http://ricardovaleriano.com/)
|
187
|
+
- [Two Bit Labs](http://twobitlabs.com/)
|
188
|
+
- [ClassMonkeys](http://www.classmonkeys.com/)
|
189
|
+
- [Windandtides](http://windandtides.com/)
|
190
|
+
- [Eric Lindvall](http://bitmonkey.net)
|
191
|
+
- [Steve Hodgkiss](http://stevehodgkiss.com/)
|
192
|
+
- [Rich Meyers](https://github.com/richmeyers)
|
193
|
+
- [Wes Bailey](http://exposinggotchas.blogspot.com/)
|
194
|
+
- [Robert J. Berger](http://blog.ibd.com/)
|
195
|
+
- [Federico Builes](http://mheroin.com/)
|
196
|
+
- [Gazler](http://blog.gazler.com/)
|
197
|
+
- [Yuu Yamashita](https://github.com/yyuu)
|
198
|
+
- [Koen Punt](http://www.koen.pt/)
|
199
|
+
|
200
|
+
This work is originally based on [Lincoln Stoll's blog post](http://lstoll.net/2008/04/stand-alone-activerecord-migrations/) and [David Welton's post](http://journal.dedasys.com/2007/01/28/using-migrations-outside-of-rails).
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
task :default do
|
5
|
+
sh "rspec spec"
|
6
|
+
end
|
7
|
+
|
8
|
+
task :all do
|
9
|
+
sh "AR='~>3.0.0' bundle update activerecord && bundle exec rake"
|
10
|
+
sh "AR='~>3.1.0.rc4' bundle update activerecord && bundle exec rake"
|
11
|
+
end
|
12
|
+
|
13
|
+
task :specs => ["specs:nodb"]
|
14
|
+
namespace :specs do
|
15
|
+
require 'rspec/core/rake_task'
|
16
|
+
|
17
|
+
desc "only specs that don't use database connection"
|
18
|
+
RSpec::Core::RakeTask.new "nodb" do |t|
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "run alls sepcs including those which uses database"
|
22
|
+
task :all => [:default, :nodb]
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
rescue LoadError => e
|
28
|
+
$stderr.puts "Jeweler, or one of its dependencies, is not available:"
|
29
|
+
$stderr.puts "#{e.class}: #{e.message}"
|
30
|
+
$stderr.puts "Install it with: sudo gem install jeweler"
|
31
|
+
else
|
32
|
+
Jeweler::Tasks.new do |gem|
|
33
|
+
gem.name = 'dbmule'
|
34
|
+
gem.summary = "Mule is a database migration tool based upon a stand-alone version of Rails Migrations. It is customized to be used in environments with multiple existing databases and to use SQL scripts by default instead of Migrations DSL."
|
35
|
+
gem.email = "willstepp@gmail.com"
|
36
|
+
gem.homepage = "http://github.com/willstepp/dbmule"
|
37
|
+
gem.authors = ["Daniel Stepp"]
|
38
|
+
end
|
39
|
+
|
40
|
+
Jeweler::GemcutterTasks.new
|
41
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/dbmule.gemspec
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "dbmule"
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Daniel Stepp"]
|
12
|
+
s.date = "2013-01-28"
|
13
|
+
s.email = "willstepp@gmail.com"
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.markdown"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
"Gemfile",
|
19
|
+
"Gemfile.lock",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.markdown",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"dbmule.gemspec",
|
25
|
+
"lib/mule_migrations.rb",
|
26
|
+
"lib/mule_migrations/configurator.rb",
|
27
|
+
"lib/mule_migrations/generator.rb",
|
28
|
+
"lib/mule_migrations/minimal_railtie_config.rb",
|
29
|
+
"lib/mule_migrations/tasks.rb",
|
30
|
+
"lib/mule_migrations/tasks/connection.rake",
|
31
|
+
"lib/mule_migrations/tasks/environment.rake",
|
32
|
+
"lib/mule_migrations/tasks/mule.rake",
|
33
|
+
"lib/tasks/mule_migrations.rb",
|
34
|
+
"vendor/migration_helpers/MIT-LICENSE",
|
35
|
+
"vendor/migration_helpers/README.markdown",
|
36
|
+
"vendor/migration_helpers/init.rb",
|
37
|
+
"vendor/migration_helpers/lib/migration_helper.rb"
|
38
|
+
]
|
39
|
+
s.homepage = "http://github.com/willstepp/dbmule"
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = "1.8.25"
|
42
|
+
s.summary = "Mule is a database migration tool based upon a stand-alone version of Rails Migrations. It is customized to be used in environments with multiple existing databases and to use SQL scripts by default instead of Migrations DSL."
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<rake>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3.2.11"])
|
50
|
+
s.add_runtime_dependency(%q<railties>, ["~> 3.2.11"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
53
|
+
s.add_dependency(%q<activerecord>, ["~> 3.2.11"])
|
54
|
+
s.add_dependency(%q<railties>, ["~> 3.2.11"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
58
|
+
s.add_dependency(%q<activerecord>, ["~> 3.2.11"])
|
59
|
+
s.add_dependency(%q<railties>, ["~> 3.2.11"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
lib_path = File.expand_path("../", __FILE__)
|
2
|
+
$:.unshift lib_path unless $:.include?(lib_path)
|
3
|
+
|
4
|
+
require "rubygems"
|
5
|
+
require "rails"
|
6
|
+
require "active_record"
|
7
|
+
|
8
|
+
require "mule_migrations/configurator"
|
9
|
+
require "mule_migrations/generator"
|
10
|
+
|
11
|
+
railtie_app_path = "#{lib_path}/mule_migrations/minimal_railtie_config"
|
12
|
+
APP_PATH = File.expand_path(railtie_app_path, __FILE__)
|
13
|
+
|
14
|
+
require "mule_migrations/minimal_railtie_config"
|
15
|
+
require "mule_migrations/tasks"
|
16
|
+
|
17
|
+
if !ENV["RAILS_ENV"]
|
18
|
+
ENV["RAILS_ENV"] = ENV["DB"] || ENV["RACK_ENV"] || Rails.env || "development"
|
19
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
module MuleMigrations
|
4
|
+
|
5
|
+
class InternalConfigurationsProxy
|
6
|
+
|
7
|
+
def initialize(configurations)
|
8
|
+
@configurations = configurations
|
9
|
+
end
|
10
|
+
|
11
|
+
def on(config_key)
|
12
|
+
if @configurations[config_key] && block_given?
|
13
|
+
@configurations[config_key] = yield(@configurations[config_key]) || @configurations[config_key]
|
14
|
+
end
|
15
|
+
@configurations[config_key]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
class Configurator
|
21
|
+
def self.load_configurations
|
22
|
+
@mule_configs ||= Configurator.new.config
|
23
|
+
@environments_config ||= YAML.load(ERB.new(File.read(@mule_configs)).result).with_indifferent_access
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.environments_config
|
27
|
+
proxy = InternalConfigurationsProxy.new(load_configurations)
|
28
|
+
yield(proxy) if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(options = {})
|
32
|
+
|
33
|
+
defaults = {
|
34
|
+
:config => "#{ENV["DB_NAME"]}db/config.yml",
|
35
|
+
:migrate_dir => "#{ENV["DB_NAME"]}db/migrate",
|
36
|
+
:seeds => "#{ENV["DB_NAME"]}db/seeds.rb",
|
37
|
+
:schema => "#{ENV["DB_NAME"]}db/schema.rb"
|
38
|
+
}
|
39
|
+
@options = load_from_file(defaults.dup) || defaults.merge(options)
|
40
|
+
ENV['SCHEMA'] = File.expand_path(schema)
|
41
|
+
end
|
42
|
+
|
43
|
+
def config
|
44
|
+
@options[:config]
|
45
|
+
end
|
46
|
+
|
47
|
+
def migrate_dir
|
48
|
+
@options[:migrate_dir]
|
49
|
+
end
|
50
|
+
|
51
|
+
def seeds
|
52
|
+
@options[:seeds]
|
53
|
+
end
|
54
|
+
|
55
|
+
def schema
|
56
|
+
@options[:schema]
|
57
|
+
end
|
58
|
+
|
59
|
+
def config_for_all
|
60
|
+
Configurator.load_configurations.dup
|
61
|
+
end
|
62
|
+
|
63
|
+
def config_for(environment)
|
64
|
+
config_for_all[environment]
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def configuration_file
|
70
|
+
".mule_migrations"
|
71
|
+
end
|
72
|
+
|
73
|
+
def load_from_file(defaults)
|
74
|
+
return nil unless File.exists? configuration_file
|
75
|
+
config = YAML.load( IO.read(configuration_file) )
|
76
|
+
{
|
77
|
+
:config => config["config"] ? config["config"]["database"] : defaults[:config],
|
78
|
+
:migrate_dir => config["db"] ? config["db"]["migrate"] : defaults[:migrate_dir],
|
79
|
+
:seeds => config["db"] ? config["db"]["seeds"] : defaults[:seeds],
|
80
|
+
:schema => config["db"] ? config["db"]["schema"] : defaults[:schema]
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# these generators are backed by rails' generators
|
2
|
+
require "rails/generators"
|
3
|
+
|
4
|
+
module MuleMigrations
|
5
|
+
|
6
|
+
class Generator
|
7
|
+
def self.migration(name, db, type, options="")
|
8
|
+
generator_params = [name] + options.split(" ")
|
9
|
+
|
10
|
+
migration_path = Rails.root.join(db, "db/migrate")
|
11
|
+
|
12
|
+
old_files = Dir.glob(File.join(migration_path, "*"))
|
13
|
+
|
14
|
+
Rails::Generators.invoke "active_record:migration", generator_params,
|
15
|
+
:destination_root => Rails.root.join(db)
|
16
|
+
new_files = Dir.glob(File.join(migration_path, "*"))
|
17
|
+
|
18
|
+
new_migration_file = get_new_migration_file(old_files, new_files)
|
19
|
+
|
20
|
+
if new_migration_file and type == 'sql'
|
21
|
+
filename = File.basename(new_migration_file, ".rb")
|
22
|
+
|
23
|
+
sql_scripts = create_sql_scripts(db, filename)
|
24
|
+
|
25
|
+
#update migration file contents with sql execution code
|
26
|
+
nmf = File.open(new_migration_file, "r") {|f| f.read}
|
27
|
+
updated_nmf = inject_sql_execution_code_into_migration(nmf, filename, sql_scripts)
|
28
|
+
File.open(new_migration_file, 'w') {|f| f.write(updated_nmf)}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.get_new_migration_file(old_files, new_files)
|
33
|
+
nf = []
|
34
|
+
new_files.each do |f|
|
35
|
+
if !old_files.include? f
|
36
|
+
nf << f
|
37
|
+
end
|
38
|
+
end
|
39
|
+
nf.count > 0 ? nf.first : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def self.create_sql_scripts(db, filename)
|
45
|
+
sql_path = Rails.root.join(db, "db/sql")
|
46
|
+
up_script = File.join(sql_path, "#{filename}_up.sql")
|
47
|
+
down_script = File.join(sql_path, "#{filename}_down.sql")
|
48
|
+
|
49
|
+
File.open(up_script, 'w') {|f| f.write("--SQL up code for migration (#{filename}) goes here")}
|
50
|
+
puts ""
|
51
|
+
puts "Mule created: #{filename}_up.sql"
|
52
|
+
File.open(down_script, 'w') {|f| f.write("--SQL down code for migration (#{filename}) goes here")}
|
53
|
+
puts "Mule created: #{filename}_down.sql"
|
54
|
+
|
55
|
+
puts "Mule put the scripts here: #{File.join(db, "db/sql")}"
|
56
|
+
puts ""
|
57
|
+
|
58
|
+
{ :up => up_script, :down => down_script }
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.inject_sql_execution_code_into_migration(nmf, filename, sql_scripts)
|
62
|
+
|
63
|
+
ci = nmf.index('class')+5
|
64
|
+
lti = nmf.index('<')-1
|
65
|
+
length = (lti - ci)
|
66
|
+
classname = nmf.slice(ci, length).strip
|
67
|
+
|
68
|
+
<<-eos
|
69
|
+
class #{classname} < ActiveRecord::Migration
|
70
|
+
def up
|
71
|
+
sql = File.open('#{sql_scripts[:up]}', 'r') {|f| f.read}
|
72
|
+
execute(sql)
|
73
|
+
end
|
74
|
+
|
75
|
+
def down
|
76
|
+
sql = File.open('#{sql_scripts[:down]}', 'r') {|f| f.read}
|
77
|
+
execute(sql)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
eos
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module MuleMigrations
|
2
|
+
class MuleMigrations::MinimalRailtieConfig < Rails::Application
|
3
|
+
config.generators.options[:rails] = {:orm => :active_record}
|
4
|
+
|
5
|
+
config.generators.options[:active_record] = {
|
6
|
+
:migration => true,
|
7
|
+
:timestamps => true
|
8
|
+
}
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module MuleMigrations
|
2
|
+
class Tasks
|
3
|
+
class << self
|
4
|
+
def configure
|
5
|
+
Deprecations.new.call
|
6
|
+
config_database_file = Configurator.new.config
|
7
|
+
|
8
|
+
paths = Rails.application.config.paths
|
9
|
+
paths.add "config/database", :with => config_database_file
|
10
|
+
end
|
11
|
+
|
12
|
+
def load_tasks
|
13
|
+
configure
|
14
|
+
|
15
|
+
load "active_record/railties/databases.rake"
|
16
|
+
MinimalRailtieConfig.load_tasks
|
17
|
+
%w(
|
18
|
+
connection
|
19
|
+
environment
|
20
|
+
mule
|
21
|
+
).each do
|
22
|
+
|task| load "mule_migrations/tasks/#{task}.rake"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Tasks::Deprecations
|
29
|
+
def call
|
30
|
+
if File.directory?('db/migrations')
|
31
|
+
puts "DEPRECATED move your migrations into db/migrate"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,331 @@
|
|
1
|
+
require File.expand_path("../../../mule_migrations", __FILE__)
|
2
|
+
require 'fileutils'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
#these tasks reference rails migration tasks located here:
|
6
|
+
#https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake
|
7
|
+
|
8
|
+
namespace :mule do
|
9
|
+
|
10
|
+
task :new_project, :db do |t, args|
|
11
|
+
|
12
|
+
def config_template(db)
|
13
|
+
<<-eos
|
14
|
+
development:
|
15
|
+
adapter: postgresql
|
16
|
+
encoding: unicode
|
17
|
+
database: #{db}_development
|
18
|
+
pool: 5
|
19
|
+
username:
|
20
|
+
password:
|
21
|
+
host:
|
22
|
+
|
23
|
+
test:
|
24
|
+
adapter: postgresql
|
25
|
+
encoding: unicode
|
26
|
+
database: #{db}_test
|
27
|
+
pool: 5
|
28
|
+
username:
|
29
|
+
password:
|
30
|
+
host:
|
31
|
+
|
32
|
+
production:
|
33
|
+
adapter: postgresql
|
34
|
+
encoding: unicode
|
35
|
+
database: #{db}_production
|
36
|
+
pool: 5
|
37
|
+
username:
|
38
|
+
password:
|
39
|
+
host:
|
40
|
+
eos
|
41
|
+
end
|
42
|
+
|
43
|
+
def seeds_template(db)
|
44
|
+
<<-eos
|
45
|
+
#run all sql scripts from #{db}/db/seeds
|
46
|
+
connection = ActiveRecord::Base.connection();
|
47
|
+
sql_seed_files = Dir.glob(Rails.root.join("#{db}/db/seeds", "*.sql"))
|
48
|
+
sql_seed_files.each do |ssf|
|
49
|
+
sql = File.open(ssf, "r") {|f| f.read}
|
50
|
+
connection.execute(sql)
|
51
|
+
end
|
52
|
+
eos
|
53
|
+
end
|
54
|
+
|
55
|
+
db = args[:db] || ENV['db']
|
56
|
+
unless db
|
57
|
+
puts ""
|
58
|
+
puts "Mule Error: must provide name of database to generate project for."
|
59
|
+
puts "example: rake #{t.name} db=my_cool_database"
|
60
|
+
puts ""
|
61
|
+
abort
|
62
|
+
end
|
63
|
+
|
64
|
+
project_dir = Rails.root.join(db)
|
65
|
+
|
66
|
+
exists = File.exists? project_dir
|
67
|
+
if !exists
|
68
|
+
puts ""
|
69
|
+
puts "Mule started creating database project for #{db}"
|
70
|
+
|
71
|
+
FileUtils.mkdir(project_dir)
|
72
|
+
puts "Mule created directory: #{db}"
|
73
|
+
db_dir = File.join(project_dir, "db")
|
74
|
+
FileUtils.mkdir(db_dir)
|
75
|
+
puts "Mule created directory: #{File.join(db, "db")}"
|
76
|
+
migrate_dir = File.join(db_dir, "migrate")
|
77
|
+
FileUtils.mkdir(migrate_dir)
|
78
|
+
puts "Mule created directory: #{File.join(db, "db", "migrate")}"
|
79
|
+
sql_dir = File.join(db_dir, "sql")
|
80
|
+
FileUtils.mkdir(sql_dir)
|
81
|
+
puts "Mule created directory: #{File.join(db, "db", "sql")}"
|
82
|
+
seeds_dir = File.join(db_dir, "seeds")
|
83
|
+
FileUtils.mkdir(seeds_dir)
|
84
|
+
puts "Mule created directory: #{File.join(db, "db", "seeds")}"
|
85
|
+
config_file = File.join(db_dir, "config.yml")
|
86
|
+
File.open(config_file, 'w') {|f| f.write(config_template(db))}
|
87
|
+
puts "Mule created file: #{File.join(db, "db", "config.yml")}"
|
88
|
+
seeds_file = File.join(db_dir, "seeds.rb")
|
89
|
+
File.open(seeds_file, 'w') {|f| f.write(seeds_template(db))}
|
90
|
+
puts "Mule created file: #{File.join(db, "db", "seeds.rb")}"
|
91
|
+
|
92
|
+
puts "Mule finished creating database project for #{db}"
|
93
|
+
puts ""
|
94
|
+
else
|
95
|
+
puts ""
|
96
|
+
puts "This database project already exists. Please choose another name."
|
97
|
+
puts ""
|
98
|
+
abort
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
task :configure_existing_database, :db do |t, args|
|
103
|
+
db = args[:db] || ENV['db']
|
104
|
+
|
105
|
+
unless db
|
106
|
+
puts ""
|
107
|
+
puts "Mule Error: must provide name of database"
|
108
|
+
puts "Example: rake #{current_task.name} db=my_cool_database"
|
109
|
+
puts ""
|
110
|
+
abort
|
111
|
+
end
|
112
|
+
|
113
|
+
puts ""
|
114
|
+
puts "Mule configuring migrations on existing database"
|
115
|
+
|
116
|
+
#create backup file of existing database schema
|
117
|
+
begin
|
118
|
+
Rake::Task["mule:structure:dump"].invoke(db)
|
119
|
+
rescue => e
|
120
|
+
#the above raises an exception in this scenario becuase it expects a
|
121
|
+
#schema_migrations table to exist, which it does not yet since there are
|
122
|
+
#no migrations, but the dump completes successfully anyway, so we just
|
123
|
+
#catch it here
|
124
|
+
end
|
125
|
+
|
126
|
+
schema = Rails.root.join(db, "db/structure_#{ENV['RAILS_ENV']}.sql")
|
127
|
+
|
128
|
+
#run mule:new_migration init_from_existing_schema
|
129
|
+
migration_path = Rails.root.join(db, "db/migrate")
|
130
|
+
old_files = Dir.glob(File.join(migration_path, "*"))
|
131
|
+
Rake::Task["mule:new_migration"].invoke("init_from_existing_db", db)
|
132
|
+
new_files = Dir.glob(File.join(migration_path, "*"))
|
133
|
+
|
134
|
+
new_migration_file = MuleMigrations::Generator.get_new_migration_file(old_files, new_files)
|
135
|
+
filename = File.basename(new_migration_file, ".rb")
|
136
|
+
|
137
|
+
#run the new empty migration in order to insert an initial version into database
|
138
|
+
Rake::Task["mule:migrate"].invoke(db)
|
139
|
+
|
140
|
+
#copy schema dump into generated sql up file
|
141
|
+
s = File.open(schema, "r") {|f| f.read}
|
142
|
+
File.open(Rails.root.join(db, "db/sql/#{filename}_up.sql"), 'w') {|f| f.write(s)}
|
143
|
+
|
144
|
+
puts ""
|
145
|
+
puts "Mule finished"
|
146
|
+
puts ""
|
147
|
+
end
|
148
|
+
|
149
|
+
task :new_migration, :name, :db, :type, :options do |t, args|
|
150
|
+
name = args[:name] || ENV['name']
|
151
|
+
db = args[:db] || ENV['db']
|
152
|
+
|
153
|
+
type = args[:type] || ENV['type']
|
154
|
+
if type.nil? then type = "sql" end
|
155
|
+
|
156
|
+
options = args[:options] || ENV['options']
|
157
|
+
|
158
|
+
unless db
|
159
|
+
puts ""
|
160
|
+
puts "Mule Error: must provide name of database to generate migration for."
|
161
|
+
puts "Example: rake #{t.name} db=my_cool_database name=add_field_to_form"
|
162
|
+
puts ""
|
163
|
+
abort
|
164
|
+
end
|
165
|
+
|
166
|
+
unless name
|
167
|
+
puts ""
|
168
|
+
puts "Mule Error: must provide name of migration to generate."
|
169
|
+
puts "Example: rake #{t.name} name=add_field_to_form"
|
170
|
+
puts ""
|
171
|
+
abort
|
172
|
+
end
|
173
|
+
|
174
|
+
set_rails_config_for(db)
|
175
|
+
|
176
|
+
if options
|
177
|
+
MuleMigrations::Generator.migration name, db, type, options.gsub('/', ' ')
|
178
|
+
else
|
179
|
+
MuleMigrations::Generator.migration name, db, type
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
task :migrate, :db do |t, args|
|
184
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate")
|
185
|
+
end
|
186
|
+
|
187
|
+
namespace :migrate do
|
188
|
+
task :up, :db do |t, args|
|
189
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate:up")
|
190
|
+
end
|
191
|
+
|
192
|
+
task :down, :db, :confirm do |t, args|
|
193
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
194
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
195
|
+
end
|
196
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate:down")
|
197
|
+
end
|
198
|
+
|
199
|
+
task :redo, :db do |t, args|
|
200
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate:redo")
|
201
|
+
end
|
202
|
+
|
203
|
+
task :reset, :db, :confirm do |t, args|
|
204
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
205
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
206
|
+
end
|
207
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate:reset")
|
208
|
+
end
|
209
|
+
|
210
|
+
task :status, :db do |t, args|
|
211
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:migrate:status")
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
task :rollback, :db, :confirm do |t, args|
|
217
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
218
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
219
|
+
end
|
220
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:rollback")
|
221
|
+
end
|
222
|
+
|
223
|
+
task :create, :db do |t, args|
|
224
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:create")
|
225
|
+
end
|
226
|
+
|
227
|
+
task :drop, :db, :confirm do |t, args|
|
228
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
229
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
230
|
+
end
|
231
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:drop")
|
232
|
+
end
|
233
|
+
|
234
|
+
task :reset, :db, :confirm do |t, args|
|
235
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
236
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
237
|
+
end
|
238
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:reset")
|
239
|
+
end
|
240
|
+
|
241
|
+
task :redo, :db do |t, args|
|
242
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:redo")
|
243
|
+
end
|
244
|
+
|
245
|
+
namespace :schema do
|
246
|
+
task :dump, :db do |t, args|
|
247
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:schema:dump")
|
248
|
+
end
|
249
|
+
|
250
|
+
task :load, :db do |t, args|
|
251
|
+
invoke_task_for(args[:db] || ENV['db'], t, "db:schema:load")
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
namespace :structure do
|
256
|
+
task :dump, :db do |t, args|
|
257
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:structure:dump")
|
258
|
+
end
|
259
|
+
|
260
|
+
task :load, :db do |t, args|
|
261
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:structure:load")
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
task :drop, :db, :confirm do |t, args|
|
266
|
+
if ENV['RAILS_ENV'] and ENV['RAILS_ENV'].include? "production"
|
267
|
+
ensure_confirmation(t, args[:confirm] || ENV['confirm'], args[:db] || ENV['db'])
|
268
|
+
end
|
269
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:drop")
|
270
|
+
end
|
271
|
+
|
272
|
+
task :setup, :db do |t, args|
|
273
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:setup")
|
274
|
+
end
|
275
|
+
|
276
|
+
task :seed, :db do |t, args|
|
277
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:seed")
|
278
|
+
end
|
279
|
+
|
280
|
+
namespace :fixtures do
|
281
|
+
task :load, :db do |t, args|
|
282
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:fixtures:load")
|
283
|
+
end
|
284
|
+
|
285
|
+
task :identify, :db do |t, args|
|
286
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:fixtures:identify")
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
task :version, :db do |t, args|
|
291
|
+
invoke_task_for(db = args[:db] || ENV['db'], t, "db:version")
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
def invoke_task_for(db, current_task, task_to_invoke)
|
296
|
+
unless db
|
297
|
+
puts ""
|
298
|
+
puts "Mule Error: must provide name of database"
|
299
|
+
puts "Example: rake #{current_task.name} db=my_cool_database"
|
300
|
+
puts ""
|
301
|
+
abort
|
302
|
+
end
|
303
|
+
|
304
|
+
set_rails_config_for(db)
|
305
|
+
Rake::Task[task_to_invoke].invoke
|
306
|
+
end
|
307
|
+
|
308
|
+
def set_rails_config_for(db)
|
309
|
+
paths = Rails.application.config.paths
|
310
|
+
paths.add "config/database", :with => File.join(db, "db/config.yml")
|
311
|
+
paths.add "db/migrate", :with => File.join(db, "db/migrate")
|
312
|
+
paths.add "db/seeds", :with => File.join(db, "db/seeds.rb")
|
313
|
+
paths.add "db/schema", :with => File.join(db, "db/schema.rb")
|
314
|
+
ENV['DB_STRUCTURE'] = File.join(db, "db/structure_#{ENV['RAILS_ENV']}.sql")
|
315
|
+
|
316
|
+
#for use in configurator initialize
|
317
|
+
ENV["DB_NAME"] = db + "/"
|
318
|
+
end
|
319
|
+
|
320
|
+
def ensure_confirmation(current_task, confirm, db)
|
321
|
+
if !confirm.nil? and !db.nil?
|
322
|
+
if confirm.downcase == db.downcase
|
323
|
+
return true
|
324
|
+
end
|
325
|
+
end
|
326
|
+
puts ""
|
327
|
+
puts "Mule Error: destructive operations on the production database requires a confirmation argument"
|
328
|
+
puts "Usage: rake #{current_task.name} confirm=<database_name> ...other arguments"
|
329
|
+
puts ""
|
330
|
+
abort
|
331
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Jesús García Sáez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,92 @@
|
|
1
|
+
|
2
|
+
DESCRIPTION
|
3
|
+
===========
|
4
|
+
|
5
|
+
Helpers for migrations of ActiveRecord for dealing with foreign keys and primary keys.
|
6
|
+
|
7
|
+
FEATURES
|
8
|
+
========
|
9
|
+
|
10
|
+
* **foreign keys**
|
11
|
+
* foreign_key(table, field, referenced_table, referenced_field, on_cascade)
|
12
|
+
* drop_foreign_key(table, field)
|
13
|
+
* **primary keys**
|
14
|
+
* primary_key(table, field)
|
15
|
+
|
16
|
+
Examples
|
17
|
+
========
|
18
|
+
|
19
|
+
Typical use:
|
20
|
+
|
21
|
+
def self.up
|
22
|
+
create_table :profiles do |t|
|
23
|
+
t.string :first_name
|
24
|
+
t.string :last_name
|
25
|
+
t.string :email
|
26
|
+
t.boolean :is_disabled
|
27
|
+
end
|
28
|
+
create_table :users do |t|
|
29
|
+
t.string :login
|
30
|
+
t.string :crypted_password
|
31
|
+
t.string :salt
|
32
|
+
t.integer :profile_id
|
33
|
+
end
|
34
|
+
|
35
|
+
foreign_key :users, :profile_id, :profiles
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.down
|
39
|
+
drop_foreign_key :users, :profile_id
|
40
|
+
drop_table :users
|
41
|
+
drop_table :profiles
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
Also, if we don't defined a common :id (exactly it's rails who define it), we should create a primary key:
|
46
|
+
|
47
|
+
def self.up
|
48
|
+
create_table :foo, :id => false do |t|
|
49
|
+
t.string :foo, :bar
|
50
|
+
end
|
51
|
+
|
52
|
+
primary_key :foo, [ :foo, :bar ]
|
53
|
+
end
|
54
|
+
|
55
|
+
In the parameter where a field is required (like the second parameter in *primary_key*) you can specified and symbol (or string) or an array of symbols (or strings).
|
56
|
+
|
57
|
+
|
58
|
+
REQUIREMENTS
|
59
|
+
============
|
60
|
+
|
61
|
+
* It's been tested with Mysql adapter and Jdbcmysql adapter
|
62
|
+
|
63
|
+
INSTALL
|
64
|
+
=======
|
65
|
+
|
66
|
+
* script/plugin install git://github.com/blaxter/migration_helpers.git
|
67
|
+
|
68
|
+
LICENSE
|
69
|
+
=======
|
70
|
+
|
71
|
+
(The MIT License)
|
72
|
+
|
73
|
+
Copyright (c) 2008 Jesús García Sáez <jgarcia@warp.es>
|
74
|
+
|
75
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
76
|
+
a copy of this software and associated documentation files (the
|
77
|
+
'Software'), to deal in the Software without restriction, including
|
78
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
79
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
80
|
+
permit persons to whom the Software is furnished to do so, subject to
|
81
|
+
the following conditions:
|
82
|
+
|
83
|
+
The above copyright notice and this permission notice shall be
|
84
|
+
included in all copies or substantial portions of the Software.
|
85
|
+
|
86
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
87
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
88
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
89
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
90
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
91
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
92
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module MigrationConstraintHelpers
|
2
|
+
|
3
|
+
# Creates a foreign key from +table+.+field+ against referenced_table.referenced_field
|
4
|
+
#
|
5
|
+
# table: The tablename
|
6
|
+
# field: A field of the table
|
7
|
+
# referenced_table: The table which contains the field referenced
|
8
|
+
# referenced_field: The field (which should be part of the primary key) of the referenced table
|
9
|
+
# cascade: delete & update on cascade?
|
10
|
+
def foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true)
|
11
|
+
execute "ALTER TABLE #{table} ADD CONSTRAINT #{constraint_name(table, field)}
|
12
|
+
FOREIGN KEY #{constraint_name(table, field)} (#{field_list(field)})
|
13
|
+
REFERENCES #{referenced_table}(#{field_list(referenced_field)})
|
14
|
+
#{(cascade ? 'ON DELETE CASCADE ON UPDATE CASCADE' : '')}"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Drops a foreign key from +table+.+field+ that has been created before with
|
18
|
+
# foreign_key method
|
19
|
+
#
|
20
|
+
# table: The table name
|
21
|
+
# field: A field (or array of fields) of the table
|
22
|
+
def drop_foreign_key(table, field)
|
23
|
+
execute "ALTER TABLE #{table} DROP FOREIGN KEY #{constraint_name(table, field)}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Creates a primary key for +table+, which right now HAS NOT primary key defined
|
27
|
+
#
|
28
|
+
# table: The table name
|
29
|
+
# field: A field (or array of fields) of the table that will be part of the primary key
|
30
|
+
def primary_key(table, field)
|
31
|
+
execute "ALTER TABLE #{table} ADD PRIMARY KEY(#{field_list(field)})"
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
# Creates a constraint name for table and field given as parameters
|
37
|
+
#
|
38
|
+
# table: The table name
|
39
|
+
# field: A field of the table
|
40
|
+
def constraint_name(table, field)
|
41
|
+
"fk_#{table}_#{field_list_name(field)}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def field_list(fields)
|
45
|
+
fields.is_a?(Array) ? fields.join(',') : fields
|
46
|
+
end
|
47
|
+
|
48
|
+
def field_list_name(fields)
|
49
|
+
fields.is_a?(Array) ? fields.join('_') : fields
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dbmule
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Stepp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.2.11
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.2.11
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: railties
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 3.2.11
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.2.11
|
62
|
+
description:
|
63
|
+
email: willstepp@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- README.markdown
|
68
|
+
files:
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- MIT-LICENSE
|
72
|
+
- README.markdown
|
73
|
+
- Rakefile
|
74
|
+
- VERSION
|
75
|
+
- dbmule.gemspec
|
76
|
+
- lib/mule_migrations.rb
|
77
|
+
- lib/mule_migrations/configurator.rb
|
78
|
+
- lib/mule_migrations/generator.rb
|
79
|
+
- lib/mule_migrations/minimal_railtie_config.rb
|
80
|
+
- lib/mule_migrations/tasks.rb
|
81
|
+
- lib/mule_migrations/tasks/connection.rake
|
82
|
+
- lib/mule_migrations/tasks/environment.rake
|
83
|
+
- lib/mule_migrations/tasks/mule.rake
|
84
|
+
- lib/tasks/mule_migrations.rb
|
85
|
+
- vendor/migration_helpers/MIT-LICENSE
|
86
|
+
- vendor/migration_helpers/README.markdown
|
87
|
+
- vendor/migration_helpers/init.rb
|
88
|
+
- vendor/migration_helpers/lib/migration_helper.rb
|
89
|
+
homepage: http://github.com/willstepp/dbmule
|
90
|
+
licenses: []
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.25
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Mule is a database migration tool based upon a stand-alone version of Rails
|
113
|
+
Migrations. It is customized to be used in environments with multiple existing databases
|
114
|
+
and to use SQL scripts by default instead of Migrations DSL.
|
115
|
+
test_files: []
|