standalone_migrations 1.0.2 → 1.0.3
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.
- data/Gemfile +1 -0
- data/README.markdown +25 -6
- data/Rakefile +11 -0
- data/VERSION +1 -1
- data/lib/standalone_migrations/configurator.rb +48 -0
- data/lib/tasks/standalone_migrations.rb +22 -5
- data/spec/spec_helper.rb +3 -0
- data/spec/standalone_migrations/configurator_spec.rb +114 -0
- data/spec/standalone_migrations_spec.rb +18 -13
- data/standalone_migrations.gemspec +5 -2
- metadata +9 -6
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Rails migrations in non-Rails (and non Ruby) projects.
|
1
|
+
Rails migrations in non-Rails (and non Ruby) projects.
|
2
2
|
|
3
3
|
WHAT'S NEW
|
4
4
|
==========
|
@@ -70,7 +70,7 @@ The general form is:
|
|
70
70
|
rake db:generate model="model_name" fields="type:column_name0 type:column_name1 ... type:column_namen"
|
71
71
|
|
72
72
|
You can have as many fields as you would like.
|
73
|
-
|
73
|
+
|
74
74
|
An example to create a Person table with 3 columns (and it will automatically add the t.timestamps line)
|
75
75
|
|
76
76
|
rake db:generate model="Person" fields="string:first_name string:last_name integer:age"
|
@@ -82,7 +82,7 @@ This will create a migration in db/migrate/
|
|
82
82
|
create_table :Person do |t|
|
83
83
|
t.string :first_name
|
84
84
|
t.string :last_name
|
85
|
-
t.integer :age
|
85
|
+
t.integer :age
|
86
86
|
t.timestamps
|
87
87
|
end
|
88
88
|
end
|
@@ -108,14 +108,32 @@ This will create a migration in db/migrate/
|
|
108
108
|
### To execute a specific up/down of one single migration
|
109
109
|
|
110
110
|
rake db:migrate:up VERSION=20081220234130
|
111
|
-
|
111
|
+
|
112
112
|
### To revert your last migration
|
113
113
|
|
114
114
|
rake db:rollback
|
115
115
|
|
116
116
|
### To revert your last 3 migrations
|
117
117
|
|
118
|
-
rake db:rollback STEP=3
|
118
|
+
rake db:rollback STEP=3
|
119
|
+
|
120
|
+
### Custom configuration
|
121
|
+
|
122
|
+
By default, Standalone Migrations will assume there exists a "db/"
|
123
|
+
directory in your project. But if for some reason you need a specific
|
124
|
+
directory structure to work with, you can use a configuration file
|
125
|
+
named .standalone_migrations in the root of your project containing
|
126
|
+
the following:
|
127
|
+
|
128
|
+
db:
|
129
|
+
seeds: db/seeds.rb
|
130
|
+
migrate: db/migrate
|
131
|
+
schema: db/schema.rb
|
132
|
+
config:
|
133
|
+
database: db/config.yml
|
134
|
+
|
135
|
+
These are the configurable options available. You can omit any of
|
136
|
+
the keys and Standalone Migrations will assume the default values.
|
119
137
|
|
120
138
|
Contributors
|
121
139
|
============
|
@@ -127,6 +145,7 @@ Contributors
|
|
127
145
|
- [Rich Meyers](https://github.com/richmeyers)
|
128
146
|
- [Wes Bailey](http://exposinggotchas.blogspot.com/)
|
129
147
|
- [Robert J. Berger](http://blog.ibd.com/)
|
130
|
-
- [Federico Builes](http://mheroin.com)
|
148
|
+
- [Federico Builes](http://mheroin.com/)
|
149
|
+
- [Ricardo Valeriano](http://ricardovaleriano.com/)
|
131
150
|
|
132
151
|
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
CHANGED
@@ -7,6 +7,17 @@ task :all do
|
|
7
7
|
sh "AR='~>3.1.0.rc4' bundle update activerecord && bundle exec rake"
|
8
8
|
end
|
9
9
|
|
10
|
+
task :specs => ["specs:nodb"]
|
11
|
+
namespace :specs do
|
12
|
+
require 'rspec/core/rake_task'
|
13
|
+
RSpec::Core::RakeTask.new "nodb" do |t|
|
14
|
+
t.pattern = "spec/standalone_migrations/**/*_spec.rb"
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "run alls sepcs including those which uses database"
|
18
|
+
task :all => [:default, :nodb]
|
19
|
+
end
|
20
|
+
|
10
21
|
# rake install -> install gem locally (for tests)
|
11
22
|
# rake release -> push to github and release to gemcutter
|
12
23
|
# rake version:bump:patch -> increase version and add a git-tag
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.3
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module StandaloneMigrations
|
2
|
+
class Configurator
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
defaults = {
|
6
|
+
:config => "db/config.yml",
|
7
|
+
:migrate_dir => "db/migrate",
|
8
|
+
:seeds => "db/seeds.rb",
|
9
|
+
:schema => "db/schema.rb"
|
10
|
+
}
|
11
|
+
@options = load_from_file || defaults.merge(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def config
|
15
|
+
@options[:config]
|
16
|
+
end
|
17
|
+
|
18
|
+
def migrate_dir
|
19
|
+
@options[:migrate_dir]
|
20
|
+
end
|
21
|
+
|
22
|
+
def seeds
|
23
|
+
@options[:seeds]
|
24
|
+
end
|
25
|
+
|
26
|
+
def schema
|
27
|
+
@options[:schema]
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def configuration_file
|
33
|
+
".standalone_migrations"
|
34
|
+
end
|
35
|
+
|
36
|
+
def load_from_file
|
37
|
+
return nil unless File.exists? configuration_file
|
38
|
+
config = YAML.load( IO.read(configuration_file) )
|
39
|
+
{
|
40
|
+
:config => config[:config][:database],
|
41
|
+
:migrate_dir => config[:db][:migrate],
|
42
|
+
:seeds => config[:db][:seeds],
|
43
|
+
:schema => config[:db][:schema]
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -1,14 +1,17 @@
|
|
1
1
|
require 'active_support/all'
|
2
2
|
require 'active_record'
|
3
3
|
require 'pathname'
|
4
|
+
require 'standalone_migrations/configurator'
|
4
5
|
|
5
6
|
# earlier versions used migrations from db/migrations, so warn users about the change
|
6
7
|
if File.directory?('db/migrations')
|
7
8
|
puts "DEPRECATED move your migrations into db/migrate"
|
8
9
|
end
|
9
10
|
|
11
|
+
configurator = StandaloneMigrations::Configurator.new
|
12
|
+
|
10
13
|
DB_CONFIG = YAML.load(
|
11
|
-
ERB.new(File.read(
|
14
|
+
ERB.new(File.read(configurator.config)).result
|
12
15
|
).with_indifferent_access
|
13
16
|
|
14
17
|
module Rails
|
@@ -26,7 +29,13 @@ module Rails
|
|
26
29
|
s = "fake_app"
|
27
30
|
|
28
31
|
def s.paths
|
29
|
-
|
32
|
+
configurator = StandaloneMigrations::Configurator.new
|
33
|
+
|
34
|
+
{
|
35
|
+
"db/migrate" => [configurator.migrate_dir],
|
36
|
+
"db/seeds.rb" => [configurator.seeds],
|
37
|
+
"db/schema.rb" => [configurator.schema]
|
38
|
+
}
|
30
39
|
end
|
31
40
|
|
32
41
|
def s.config
|
@@ -36,8 +45,11 @@ module Rails
|
|
36
45
|
end
|
37
46
|
s
|
38
47
|
end
|
48
|
+
|
49
|
+
def s.load_seed; end # no-op, needed for db:reset
|
39
50
|
s
|
40
51
|
end
|
52
|
+
|
41
53
|
end
|
42
54
|
|
43
55
|
task(:rails_env){}
|
@@ -68,8 +80,13 @@ class #{class_name migration} < ActiveRecord::Migration
|
|
68
80
|
end
|
69
81
|
end
|
70
82
|
eof
|
71
|
-
|
72
|
-
|
83
|
+
filename = migration.underscore
|
84
|
+
create_file file_name(filename), file_contents
|
85
|
+
puts "Created migration #{file_name filename}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def configurator
|
89
|
+
StandaloneMigrations::Configurator.new
|
73
90
|
end
|
74
91
|
|
75
92
|
def create_file file, contents
|
@@ -79,7 +96,7 @@ eof
|
|
79
96
|
end
|
80
97
|
|
81
98
|
def file_name migration
|
82
|
-
File.join
|
99
|
+
File.join configurator.migrate_dir, "#{Time.now.utc.strftime '%Y%m%d%H%M%S'}_#{migration}.rb"
|
83
100
|
end
|
84
101
|
|
85
102
|
def class_name str
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module StandaloneMigrations
|
5
|
+
describe Configurator, "which allows define custom dirs and files to work with your migrations" do
|
6
|
+
|
7
|
+
context "default values when .standalone_configurations is missing" do
|
8
|
+
|
9
|
+
let(:configurator) do
|
10
|
+
Configurator.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "use config/database.yml" do
|
14
|
+
configurator.config.should == 'db/config.yml'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "use db/migrate dir" do
|
18
|
+
configurator.migrate_dir.should == 'db/migrate'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "use db/seeds.rb" do
|
22
|
+
configurator.seeds.should == "db/seeds.rb"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "use db/schema.rb" do
|
26
|
+
configurator.schema.should == "db/schema.rb"
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context "passing configurations as a parameter" do
|
32
|
+
|
33
|
+
let(:args) do
|
34
|
+
{
|
35
|
+
:config => "custom/config/database.yml",
|
36
|
+
:migrate_dir => "custom/db/migrate",
|
37
|
+
:seeds => "custom/db/seeds.rb",
|
38
|
+
:schema => "custom/db/schema.rb"
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:configurator) do
|
43
|
+
Configurator.new args
|
44
|
+
end
|
45
|
+
|
46
|
+
it "use custom config" do
|
47
|
+
configurator.config.should == args[:config]
|
48
|
+
end
|
49
|
+
|
50
|
+
it "use custom migrate dir" do
|
51
|
+
configurator.migrate_dir.should == args[:migrate_dir]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "use custom seeds" do
|
55
|
+
configurator.seeds.should == args[:seeds]
|
56
|
+
end
|
57
|
+
|
58
|
+
it "use custom schema" do
|
59
|
+
configurator.schema.should == args[:schema]
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
context "using a .standalone_migrations file with configurations" do
|
65
|
+
|
66
|
+
before(:all) do
|
67
|
+
@original_dir = Dir.pwd
|
68
|
+
Dir.chdir File.expand_path("../", __FILE__)
|
69
|
+
end
|
70
|
+
|
71
|
+
let(:yaml_hash) do
|
72
|
+
{
|
73
|
+
:db => {
|
74
|
+
:seeds => "file/db/seeds.rb",
|
75
|
+
:migrate => "file/db/migrate",
|
76
|
+
:schema => "file/db/schema.rb"
|
77
|
+
},
|
78
|
+
:config => {
|
79
|
+
:database => "file/config/database.yml"
|
80
|
+
}
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
let(:configurator) do
|
85
|
+
file = ".standalone_migrations"
|
86
|
+
File.open(file, "w") { |file| file.write(yaml_hash.to_yaml) }
|
87
|
+
|
88
|
+
Configurator.new
|
89
|
+
end
|
90
|
+
|
91
|
+
it "use custom config from file" do
|
92
|
+
configurator.config.should == yaml_hash[:config][:database]
|
93
|
+
end
|
94
|
+
|
95
|
+
it "use custom migrate dir from file" do
|
96
|
+
configurator.migrate_dir.should == yaml_hash[:db][:migrate]
|
97
|
+
end
|
98
|
+
|
99
|
+
it "use custom seeds from file" do
|
100
|
+
configurator.seeds.should == yaml_hash[:db][:seeds]
|
101
|
+
end
|
102
|
+
|
103
|
+
it "use custom schema from file" do
|
104
|
+
configurator.schema.should == yaml_hash[:db][:schema]
|
105
|
+
end
|
106
|
+
|
107
|
+
after(:all) do
|
108
|
+
File.delete ".standalone_migrations"
|
109
|
+
Dir.chdir @original_dir
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -110,6 +110,11 @@ test:
|
|
110
110
|
run("rake db:new_migration name=test_abc").should =~ %r{Created migration db/migrate/\d+_test_abc\.rb}
|
111
111
|
run("ls db/migrate").should =~ /^\d+_test_abc.rb$/
|
112
112
|
end
|
113
|
+
|
114
|
+
it "generates a new migration with the name converted to the Rails migration format" do
|
115
|
+
run("rake db:new_migration name=MyNiceModel").should =~ %r{Created migration db/migrate/\d+_my_nice_model\.rb}
|
116
|
+
run("ls db/migrate").should =~ /^\d+_my_nice_model.rb$/
|
117
|
+
end
|
113
118
|
end
|
114
119
|
|
115
120
|
describe 'db:version' do
|
@@ -169,30 +174,30 @@ test:
|
|
169
174
|
lambda{ run("rake db:migrate:up") }.should raise_error(/VERSION/)
|
170
175
|
end
|
171
176
|
end
|
172
|
-
|
177
|
+
|
173
178
|
describe 'db:rollback' do
|
174
179
|
it "does nothing when no migrations have been run" do
|
175
180
|
run("rake db:version").should =~ /version: 0/
|
176
181
|
run("rake db:rollback").should == ''
|
177
|
-
run("rake db:version").should =~ /version: 0/
|
182
|
+
run("rake db:version").should =~ /version: 0/
|
178
183
|
end
|
179
|
-
|
180
|
-
it "rolls back the last migration if one has been applied" do
|
181
|
-
write_multiple_migrations
|
182
|
-
run("rake db:migrate")
|
184
|
+
|
185
|
+
it "rolls back the last migration if one has been applied" do
|
186
|
+
write_multiple_migrations
|
187
|
+
run("rake db:migrate")
|
183
188
|
run("rake db:version").should =~ /version: 20100509095816/
|
184
189
|
run("rake db:rollback").should =~ /revert/
|
185
190
|
run("rake db:version").should =~ /version: 20100509095815/
|
186
191
|
end
|
187
|
-
|
192
|
+
|
188
193
|
it "rolls back multiple migrations if the STEP argument is given" do
|
189
|
-
write_multiple_migrations
|
190
|
-
run("rake db:migrate")
|
194
|
+
write_multiple_migrations
|
195
|
+
run("rake db:migrate")
|
191
196
|
run("rake db:version").should =~ /version: 20100509095816/
|
192
197
|
run("rake db:rollback STEP=2") =~ /revert/
|
193
198
|
run("rake db:version").should =~ /version: 0/
|
194
199
|
end
|
195
|
-
end
|
200
|
+
end
|
196
201
|
|
197
202
|
describe 'schema:dump' do
|
198
203
|
it "dumps the schema" do
|
@@ -248,7 +253,7 @@ test:
|
|
248
253
|
run('rake db:test:purge')
|
249
254
|
end
|
250
255
|
end
|
251
|
-
|
256
|
+
|
252
257
|
describe 'db:migrate when environment is specified' do
|
253
258
|
it "runs when using the DB environment variable" do
|
254
259
|
make_migration('yyy')
|
@@ -256,9 +261,9 @@ test:
|
|
256
261
|
run('rake db:version DB=test').should_not =~ /version: 0/
|
257
262
|
run('rake db:version').should =~ /version: 0/
|
258
263
|
end
|
259
|
-
|
264
|
+
|
260
265
|
it "should error on an invalid database" do
|
261
266
|
lambda{ run("rake db:create DB=nonexistent")}.should raise_error(/rake aborted/)
|
262
|
-
end
|
267
|
+
end
|
263
268
|
end
|
264
269
|
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "standalone_migrations"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Todd Huss", "Michael Grosser"]
|
12
|
-
s.date = "2011-
|
12
|
+
s.date = "2011-11-01"
|
13
13
|
s.email = "thuss@gabrito.com"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.markdown"
|
@@ -21,7 +21,10 @@ Gem::Specification.new do |s|
|
|
21
21
|
"README.markdown",
|
22
22
|
"Rakefile",
|
23
23
|
"VERSION",
|
24
|
+
"lib/standalone_migrations/configurator.rb",
|
24
25
|
"lib/tasks/standalone_migrations.rb",
|
26
|
+
"spec/spec_helper.rb",
|
27
|
+
"spec/standalone_migrations/configurator_spec.rb",
|
25
28
|
"spec/standalone_migrations_spec.rb",
|
26
29
|
"standalone_migrations.gemspec",
|
27
30
|
"vendor/migration_helpers/MIT-LICENSE",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: standalone_migrations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-01 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
17
|
-
requirement: &
|
17
|
+
requirement: &70305689446500 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70305689446500
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: activerecord
|
28
|
-
requirement: &
|
28
|
+
requirement: &70305689445960 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: '3'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70305689445960
|
37
37
|
description:
|
38
38
|
email: thuss@gabrito.com
|
39
39
|
executables: []
|
@@ -47,7 +47,10 @@ files:
|
|
47
47
|
- README.markdown
|
48
48
|
- Rakefile
|
49
49
|
- VERSION
|
50
|
+
- lib/standalone_migrations/configurator.rb
|
50
51
|
- lib/tasks/standalone_migrations.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- spec/standalone_migrations/configurator_spec.rb
|
51
54
|
- spec/standalone_migrations_spec.rb
|
52
55
|
- standalone_migrations.gemspec
|
53
56
|
- vendor/migration_helpers/MIT-LICENSE
|