ejhayes_standalone_migrations 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +113 -0
- data/LICENSE +20 -0
- data/README.markdown +265 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/example/.gitignore +5 -0
- data/example/Rakefile +9 -0
- data/example/db/config.yml +15 -0
- data/example/db/migrate/20120930053225_create_table_awesome_migration.rb +7 -0
- data/lib/standalone_migrations.rb +20 -0
- data/lib/standalone_migrations/callbacks.rb +13 -0
- data/lib/standalone_migrations/configurator.rb +88 -0
- data/lib/standalone_migrations/generator.rb +11 -0
- data/lib/standalone_migrations/minimal_railtie_config.rb +10 -0
- data/lib/standalone_migrations/tasks.rb +36 -0
- data/lib/standalone_migrations/tasks/connection.rake +8 -0
- data/lib/standalone_migrations/tasks/db/new_migration.rake +19 -0
- data/lib/standalone_migrations/tasks/environment.rake +3 -0
- data/lib/tasks/standalone_migrations.rb +10 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/standalone_migrations/callbacks_spec.rb +48 -0
- data/spec/standalone_migrations/configurator_spec.rb +263 -0
- data/spec/standalone_migrations_spec.rb +331 -0
- data/standalone_migrations.gemspec +74 -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 +129 -0
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionpack (3.2.13)
|
5
|
+
activemodel (= 3.2.13)
|
6
|
+
activesupport (= 3.2.13)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
erubis (~> 2.7.0)
|
9
|
+
journey (~> 1.0.4)
|
10
|
+
rack (~> 1.4.5)
|
11
|
+
rack-cache (~> 1.2)
|
12
|
+
rack-test (~> 0.6.1)
|
13
|
+
sprockets (~> 2.2.1)
|
14
|
+
activemodel (3.2.13)
|
15
|
+
activesupport (= 3.2.13)
|
16
|
+
builder (~> 3.0.0)
|
17
|
+
activerecord (3.2.13)
|
18
|
+
activemodel (= 3.2.13)
|
19
|
+
activesupport (= 3.2.13)
|
20
|
+
arel (~> 3.0.2)
|
21
|
+
tzinfo (~> 0.3.29)
|
22
|
+
activesupport (3.2.13)
|
23
|
+
i18n (= 0.6.1)
|
24
|
+
multi_json (~> 1.0)
|
25
|
+
addressable (2.3.6)
|
26
|
+
arel (3.0.2)
|
27
|
+
builder (3.0.4)
|
28
|
+
diff-lcs (1.1.3)
|
29
|
+
erubis (2.7.0)
|
30
|
+
faraday (0.8.9)
|
31
|
+
multipart-post (~> 1.2.0)
|
32
|
+
git (1.2.6)
|
33
|
+
github_api (0.10.2)
|
34
|
+
addressable
|
35
|
+
faraday (~> 0.8.7)
|
36
|
+
hashie (>= 1.2)
|
37
|
+
multi_json (~> 1.4)
|
38
|
+
nokogiri (~> 1.6.0)
|
39
|
+
oauth2
|
40
|
+
hashie (2.1.1)
|
41
|
+
highline (1.6.21)
|
42
|
+
hike (1.2.1)
|
43
|
+
i18n (0.6.1)
|
44
|
+
jeweler (2.0.1)
|
45
|
+
builder
|
46
|
+
bundler (>= 1.0)
|
47
|
+
git (>= 1.2.5)
|
48
|
+
github_api
|
49
|
+
highline (>= 1.6.15)
|
50
|
+
nokogiri (>= 1.5.10)
|
51
|
+
rake
|
52
|
+
rdoc
|
53
|
+
journey (1.0.4)
|
54
|
+
json (1.7.7)
|
55
|
+
jwt (0.1.11)
|
56
|
+
multi_json (>= 1.5)
|
57
|
+
mini_portile (0.5.3)
|
58
|
+
multi_json (1.7.1)
|
59
|
+
multi_xml (0.5.5)
|
60
|
+
multipart-post (1.2.0)
|
61
|
+
nokogiri (1.6.1)
|
62
|
+
mini_portile (~> 0.5.0)
|
63
|
+
oauth2 (0.9.3)
|
64
|
+
faraday (>= 0.8, < 0.10)
|
65
|
+
jwt (~> 0.1.8)
|
66
|
+
multi_json (~> 1.3)
|
67
|
+
multi_xml (~> 0.5)
|
68
|
+
rack (~> 1.2)
|
69
|
+
rack (1.4.5)
|
70
|
+
rack-cache (1.2)
|
71
|
+
rack (>= 0.4)
|
72
|
+
rack-ssl (1.3.3)
|
73
|
+
rack
|
74
|
+
rack-test (0.6.2)
|
75
|
+
rack (>= 1.0)
|
76
|
+
railties (3.2.13)
|
77
|
+
actionpack (= 3.2.13)
|
78
|
+
activesupport (= 3.2.13)
|
79
|
+
rack-ssl (~> 1.3.2)
|
80
|
+
rake (>= 0.8.7)
|
81
|
+
rdoc (~> 3.4)
|
82
|
+
thor (>= 0.14.6, < 2.0)
|
83
|
+
rake (10.2.2)
|
84
|
+
rdoc (3.12.2)
|
85
|
+
json (~> 1.4)
|
86
|
+
rspec (2.7.0)
|
87
|
+
rspec-core (~> 2.7.0)
|
88
|
+
rspec-expectations (~> 2.7.0)
|
89
|
+
rspec-mocks (~> 2.7.0)
|
90
|
+
rspec-core (2.7.1)
|
91
|
+
rspec-expectations (2.7.0)
|
92
|
+
diff-lcs (~> 1.1.2)
|
93
|
+
rspec-mocks (2.7.0)
|
94
|
+
sprockets (2.2.2)
|
95
|
+
hike (~> 1.2)
|
96
|
+
multi_json (~> 1.0)
|
97
|
+
rack (~> 1.0)
|
98
|
+
tilt (~> 1.1, != 1.3.0)
|
99
|
+
sqlite3 (1.3.6)
|
100
|
+
thor (0.17.0)
|
101
|
+
tilt (1.3.6)
|
102
|
+
tzinfo (0.3.37)
|
103
|
+
|
104
|
+
PLATFORMS
|
105
|
+
ruby
|
106
|
+
|
107
|
+
DEPENDENCIES
|
108
|
+
activerecord (~> 3.2)
|
109
|
+
jeweler
|
110
|
+
railties (~> 3.2)
|
111
|
+
rake (~> 10.0)
|
112
|
+
rspec (~> 2)
|
113
|
+
sqlite3
|
data/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,265 @@
|
|
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
|
+
### on_loaded callbacks
|
111
|
+
|
112
|
+
If you would like to use an external library such as [foreigner](https://github.com/matthuhiggins/foreigner) with standalone migrations, you can add the following to your `Rakefile`:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
require 'foreigner'
|
116
|
+
|
117
|
+
StandaloneMigrations.on_load do
|
118
|
+
Foreigner.load
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
###Multiple database support
|
123
|
+
|
124
|
+
####Structure
|
125
|
+
|
126
|
+
Create a custom configuration file for each database and name them `.database_name.standalone_migrations`. The same conditions apply as described under Custom Configuration, however you are most likely want to specify all options to avoid conflicts and errors.
|
127
|
+
|
128
|
+
An example set up would look like this:
|
129
|
+
|
130
|
+
```
|
131
|
+
app/
|
132
|
+
|-- db/
|
133
|
+
| |-- migrate/
|
134
|
+
| | |-- db1/
|
135
|
+
| | | |-- 001_migration.rb
|
136
|
+
| | |
|
137
|
+
| | |-- db2/
|
138
|
+
| | |-- 001_migration.rb
|
139
|
+
| |
|
140
|
+
| |-- config_db1.yml
|
141
|
+
| |-- config_db2.yml
|
142
|
+
| |-- seeds_db1.rb
|
143
|
+
| |-- seeds_db2.rb
|
144
|
+
| |-- schema_db1.rb
|
145
|
+
| |-- schema_db2.rb
|
146
|
+
|
|
147
|
+
|-- .db1.standalone_migrations
|
148
|
+
|-- .db2.standalone_migrations
|
149
|
+
```
|
150
|
+
Sample config file:
|
151
|
+
|
152
|
+
```yaml
|
153
|
+
db:
|
154
|
+
seeds: db/seeds_db1.rb
|
155
|
+
migrate: db/migrate/db1
|
156
|
+
schema: db/schema_db1.rb
|
157
|
+
config:
|
158
|
+
database: db/config_db1.yml
|
159
|
+
```
|
160
|
+
Of course you can achieve a different layout by simply editing the paths.
|
161
|
+
|
162
|
+
#####Running
|
163
|
+
|
164
|
+
You can run the Rake tasks on a particular database by passing the `DATABASE` environment variable to it:
|
165
|
+
|
166
|
+
$ rake DATABASE=db1 db:version
|
167
|
+
|
168
|
+
Combined with the environment selector:
|
169
|
+
|
170
|
+
$ rake DATABASE=db2 DB=production db:migrate
|
171
|
+
|
172
|
+
#### Changing environment config in runtime
|
173
|
+
|
174
|
+
If you are using Heroku or have to create or change your connection
|
175
|
+
configuration based on runtime aspects (maybe environment variables),
|
176
|
+
you can use the `StandaloneMigrations::Configurator.environments_config`
|
177
|
+
method. Check the usage example:
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
require 'tasks/standalone_migrations'
|
181
|
+
|
182
|
+
StandaloneMigrations::Configurator.environments_config do |env|
|
183
|
+
|
184
|
+
env.on "production" do
|
185
|
+
|
186
|
+
if (ENV['DATABASE_URL'])
|
187
|
+
db = URI.parse(ENV['DATABASE_URL'])
|
188
|
+
return {
|
189
|
+
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
|
190
|
+
:host => db.host,
|
191
|
+
:username => db.user,
|
192
|
+
:password => db.password,
|
193
|
+
:database => db.path[1..-1],
|
194
|
+
:encoding => 'utf8'
|
195
|
+
}
|
196
|
+
end
|
197
|
+
|
198
|
+
nil
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
You have to put this anywhere on your `Rakefile`. If you want to
|
205
|
+
change some configuration, call the #on method on the object
|
206
|
+
received as argument in your block passed to ::environments_config
|
207
|
+
method call. The #on method receives the key to the configuration
|
208
|
+
that you want to change within the block. The block should return
|
209
|
+
your new configuration hash or nil if you want the configuration
|
210
|
+
to stay the same.
|
211
|
+
|
212
|
+
Your logic to decide the new configuration need to access some data
|
213
|
+
in your current configuration? Then you should receive the configuration
|
214
|
+
in your block, like this:
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
require 'tasks/standalone_migrations'
|
218
|
+
|
219
|
+
StandaloneMigrations::Configurator.environments_config do |env|
|
220
|
+
|
221
|
+
env.on "my_custom_config" do |current_custom_config|
|
222
|
+
p current_custom_config
|
223
|
+
# => the values on your current "my_custom_config" environment
|
224
|
+
nil
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
#### Exporting Generated SQL
|
231
|
+
|
232
|
+
If instead of the database-agnostic `schema.rb` file you'd like to
|
233
|
+
save the database-specific SQL generated by the migrations, simply
|
234
|
+
add this to your `Rakefile`.
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
require 'tasks/standalone_migrations'
|
238
|
+
ActiveRecord::Base.schema_format = :sql
|
239
|
+
```
|
240
|
+
|
241
|
+
You should see a `db/structure.sql` file the next time you run a
|
242
|
+
migration.
|
243
|
+
|
244
|
+
Contributors
|
245
|
+
============
|
246
|
+
- [Todd Huss](http://gabrito.com/)
|
247
|
+
- [Michael Grosser](http://pragmatig.wordpress.com)
|
248
|
+
- [Ricardo Valeriano](http://ricardovaleriano.com/)
|
249
|
+
- [Two Bit Labs](http://twobitlabs.com/)
|
250
|
+
- [ClassMonkeys](http://www.classmonkeys.com/)
|
251
|
+
- [Windandtides](http://windandtides.com/)
|
252
|
+
- [Eric Lindvall](http://bitmonkey.net)
|
253
|
+
- [Steve Hodgkiss](http://stevehodgkiss.com/)
|
254
|
+
- [Rich Meyers](https://github.com/richmeyers)
|
255
|
+
- [Wes Bailey](http://exposinggotchas.blogspot.com/)
|
256
|
+
- [Robert J. Berger](http://blog.ibd.com/)
|
257
|
+
- [Federico Builes](http://mheroin.com/)
|
258
|
+
- [Gazler](http://blog.gazler.com/)
|
259
|
+
- [Yuu Yamashita](https://github.com/yyuu)
|
260
|
+
- [Koen Punt](http://www.koen.pt/)
|
261
|
+
- [Parker Moore](http://www.parkermoore.de/)
|
262
|
+
- [Marcell Jusztin](http://www.morcmarc.com)
|
263
|
+
- [Eric Hayes](http://ejhay.es)
|
264
|
+
|
265
|
+
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,52 @@
|
|
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
|
+
t.pattern = "spec/standalone_migrations/**/*_spec.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "run all specs for travis"
|
23
|
+
RSpec::Core::RakeTask.new "travis" do |t|
|
24
|
+
t.pattern = "spec/**/*_spec.rb"
|
25
|
+
t.rspec_opts = "--tag ~@travis_error"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "run all specs including those which uses database"
|
29
|
+
task :all => [:default, :nodb]
|
30
|
+
end
|
31
|
+
|
32
|
+
# rake install -> install gem locally (for tests)
|
33
|
+
# rake release -> push to github and release to gemcutter
|
34
|
+
# rake version:bump:patch -> increase version and add a git-tag
|
35
|
+
begin
|
36
|
+
require 'jeweler'
|
37
|
+
rescue LoadError => e
|
38
|
+
$stderr.puts "Jeweler, or one of its dependencies, is not available:"
|
39
|
+
$stderr.puts "#{e.class}: #{e.message}"
|
40
|
+
$stderr.puts "Install it with: sudo gem install jeweler"
|
41
|
+
else
|
42
|
+
Jeweler::Tasks.new do |gem|
|
43
|
+
gem.name = 'ejhayes_standalone_migrations'
|
44
|
+
gem.summary = "A thin wrapper to use Rails Migrations in non Rails projects"
|
45
|
+
gem.email = "thuss@gabrito.com"
|
46
|
+
gem.homepage = "http://github.com/thuss/standalone-migrations"
|
47
|
+
gem.authors = ["Todd Huss", "Michael Grosser"]
|
48
|
+
gem.license = "MIT"
|
49
|
+
end
|
50
|
+
|
51
|
+
Jeweler::GemcutterTasks.new
|
52
|
+
end
|