standalone_migrations 0.4.11 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +47 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +14 -35
- data/Rakefile +6 -15
- data/VERSION +1 -1
- data/lib/tasks/standalone_migrations.rb +50 -340
- data/spec/standalone_migrations_spec.rb +131 -269
- data/standalone_migrations.gemspec +8 -5
- metadata +11 -8
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.1.0.rc4)
|
5
|
+
activesupport (= 3.1.0.rc4)
|
6
|
+
bcrypt-ruby (~> 2.1.4)
|
7
|
+
builder (~> 3.0.0)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
activerecord (3.1.0.rc4)
|
10
|
+
activemodel (= 3.1.0.rc4)
|
11
|
+
activesupport (= 3.1.0.rc4)
|
12
|
+
arel (~> 2.1.1)
|
13
|
+
tzinfo (~> 0.3.27)
|
14
|
+
activesupport (3.1.0.rc4)
|
15
|
+
multi_json (~> 1.0)
|
16
|
+
arel (2.1.1)
|
17
|
+
bcrypt-ruby (2.1.4)
|
18
|
+
builder (3.0.0)
|
19
|
+
diff-lcs (1.1.2)
|
20
|
+
git (1.2.5)
|
21
|
+
i18n (0.6.0)
|
22
|
+
jeweler (1.6.2)
|
23
|
+
bundler (~> 1.0)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rake
|
26
|
+
multi_json (1.0.3)
|
27
|
+
rake (0.9.2)
|
28
|
+
rspec (2.6.0)
|
29
|
+
rspec-core (~> 2.6.0)
|
30
|
+
rspec-expectations (~> 2.6.0)
|
31
|
+
rspec-mocks (~> 2.6.0)
|
32
|
+
rspec-core (2.6.4)
|
33
|
+
rspec-expectations (2.6.0)
|
34
|
+
diff-lcs (~> 1.1.2)
|
35
|
+
rspec-mocks (2.6.0)
|
36
|
+
sqlite3 (1.3.3)
|
37
|
+
tzinfo (0.3.27)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
activerecord (>= 3)
|
44
|
+
jeweler
|
45
|
+
rake
|
46
|
+
rspec (~> 2)
|
47
|
+
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
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
Rails migrations in non-Rails (and non Ruby) projects.
|
2
2
|
|
3
|
+
WHAT'S NEW
|
4
|
+
==========
|
5
|
+
In the 1.0 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 [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
|
+
|
3
11
|
USAGE
|
4
12
|
=====
|
5
13
|
Install Ruby, RubyGems and a ruby-database driver (e.g. `gem install mysql`) then:
|
@@ -10,15 +18,6 @@ Add to `Rakefile` in your projects base directory:
|
|
10
18
|
|
11
19
|
begin
|
12
20
|
require 'tasks/standalone_migrations'
|
13
|
-
MigratorTasks.new do |t|
|
14
|
-
# t.migrations = "db/migrations"
|
15
|
-
# t.config = "db/config.yml"
|
16
|
-
# t.schema = "db/schema.rb"
|
17
|
-
# t.sub_namespace = "dbname"
|
18
|
-
# t.env = "DB"
|
19
|
-
# t.verbose = true
|
20
|
-
# t.log_level = Logger::ERROR
|
21
|
-
end
|
22
21
|
rescue LoadError => e
|
23
22
|
puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: #{e})"
|
24
23
|
end
|
@@ -50,7 +49,7 @@ Add database configuration to `db/config.yml` in your projects base directory e.
|
|
50
49
|
### To create a new database migration:
|
51
50
|
|
52
51
|
rake db:new_migration name=FooBarMigration
|
53
|
-
edit db/
|
52
|
+
edit db/migrate/20081220234130_foo_bar_migration.rb
|
54
53
|
|
55
54
|
... and fill in the up and down migrations [Cheatsheet](http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations).
|
56
55
|
|
@@ -76,7 +75,7 @@ An example to create a Person table with 3 columns (and it will automatically ad
|
|
76
75
|
|
77
76
|
rake db:generate model="Person" fields="string:first_name string:last_name integer:age"
|
78
77
|
|
79
|
-
This will create a migration in db/
|
78
|
+
This will create a migration in db/migrate/
|
80
79
|
|
81
80
|
class CreatePerson < ActiveRecord::Migration
|
82
81
|
def self.up
|
@@ -103,40 +102,20 @@ This will create a migration in db/migrations/
|
|
103
102
|
|
104
103
|
### To migrate a specific database (for example your "testing" database)
|
105
104
|
|
106
|
-
rake db:migrate DB=test
|
105
|
+
rake db:migrate DB=test ... or ...
|
106
|
+
rake db:migrate RAILS_ENV=test
|
107
107
|
|
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
|
119
|
-
|
120
|
-
## Sub-namespacing
|
121
|
-
|
122
|
-
When working with multiple databases in a single application it is convenient
|
123
|
-
to have separate sets of tasks for each database. This is accomplished with
|
124
|
-
sub_namespace parameter - for example, given the following declaration:
|
125
|
-
|
126
|
-
MigratorTasks.new do |t|
|
127
|
-
t.migrations = "db/migrate/widgets"
|
128
|
-
t.sub_namespace = "widgets"
|
129
|
-
...
|
130
|
-
end
|
131
|
-
|
132
|
-
The following tasks will be created:
|
133
|
-
|
134
|
-
db:widgets:new_migration
|
135
|
-
db:widgets:migrate
|
136
|
-
...
|
137
|
-
|
138
|
-
And migrations for this database would be created in db/migrate/widgets
|
139
|
-
subdirectory.
|
118
|
+
rake db:rollback STEP=3
|
140
119
|
|
141
120
|
Contributors
|
142
121
|
============
|
data/Rakefile
CHANGED
@@ -1,17 +1,10 @@
|
|
1
|
-
task :default
|
1
|
+
task :default do
|
2
|
+
sh "rspec spec"
|
3
|
+
end
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
$stderr.puts "RSpec 2, or one of its dependencies, is not available:"
|
7
|
-
$stderr.puts "#{e.class}: #{e.message}"
|
8
|
-
$stderr.puts "Install it with: sudo gem install rspec"
|
9
|
-
$stderr.puts "Test-related tasks will not be available."
|
10
|
-
$stderr.puts "If you have RSpec 1 installed you can try running the tests with:"
|
11
|
-
$stderr.puts " spec spec"
|
12
|
-
$stderr.puts "However, RSpec 1 is not officially supported."
|
13
|
-
else
|
14
|
-
RSpec::Core::RakeTask.new {|t| t.rspec_opts = ['--color']}
|
5
|
+
task :all do
|
6
|
+
sh "AR='~>3.0.0' bundle update activerecord && bundle exec rake"
|
7
|
+
sh "AR='~>3.1.0.rc4' bundle update activerecord && bundle exec rake"
|
15
8
|
end
|
16
9
|
|
17
10
|
# rake install -> install gem locally (for tests)
|
@@ -30,8 +23,6 @@ else
|
|
30
23
|
gem.email = "thuss@gabrito.com"
|
31
24
|
gem.homepage = "http://github.com/thuss/standalone-migrations"
|
32
25
|
gem.authors = ["Todd Huss", "Michael Grosser"]
|
33
|
-
gem.files += ["lib/tasks/*"]
|
34
|
-
%w[rake activerecord].each{|d| gem.add_dependency d}
|
35
26
|
end
|
36
27
|
|
37
28
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
@@ -1,350 +1,62 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'active_record'
|
3
|
+
require 'pathname'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attr_accessor :verbose, :log_level, :logger, :sub_namespace
|
10
|
-
attr_reader :migrations
|
5
|
+
# earlier versions used migrations from db/migrations, so warn users about the change
|
6
|
+
if File.directory?('db/migrations')
|
7
|
+
puts "DEPRECATED move your migrations into db/migrate"
|
8
|
+
end
|
11
9
|
|
12
|
-
|
13
|
-
@name = name
|
14
|
-
base = File.expand_path('.')
|
15
|
-
here = File.expand_path(File.dirname(File.dirname(File.dirname((__FILE__)))))
|
16
|
-
@base = base
|
17
|
-
@vendor = "#{here}/vendor"
|
18
|
-
@migrations = ["#{base}/db/migrations"]
|
19
|
-
@config = "#{base}/db/config.yml"
|
20
|
-
@schema = "#{base}/db/schema.rb"
|
21
|
-
@env = 'DB'
|
22
|
-
@verbose = true
|
23
|
-
@log_level = Logger::ERROR
|
24
|
-
yield self if block_given?
|
25
|
-
# Add to load_path every "lib/" directory in vendor
|
26
|
-
Dir["#{vendor}/**/lib"].each { |p| $LOAD_PATH << p }
|
27
|
-
define
|
28
|
-
end
|
10
|
+
DB_CONFIG = YAML.load_file('db/config.yml').with_indifferent_access
|
29
11
|
|
30
|
-
|
31
|
-
|
12
|
+
module Rails
|
13
|
+
def self.env
|
14
|
+
s = (ENV['RAILS_ENV'] || ENV['DB'] || 'development').dup # env is frozen -> dup
|
15
|
+
def s.development?; self == 'development';end
|
16
|
+
s
|
32
17
|
end
|
33
18
|
|
34
|
-
def
|
35
|
-
|
36
|
-
if sub_namespace
|
37
|
-
namespace sub_namespace do
|
38
|
-
define_tasks
|
39
|
-
end
|
40
|
-
else
|
41
|
-
define_tasks
|
42
|
-
end
|
43
|
-
end
|
19
|
+
def self.root
|
20
|
+
Pathname.new Dir.pwd
|
44
21
|
end
|
45
22
|
|
46
|
-
def
|
47
|
-
|
48
|
-
|
49
|
-
def ar_init(connect = true)
|
50
|
-
require 'active_record'
|
51
|
-
self.current_env = ENV[@env] || DefaultEnv
|
52
|
-
|
53
|
-
if @config.is_a?(Hash)
|
54
|
-
ActiveRecord::Base.configurations = @config
|
55
|
-
else
|
56
|
-
require 'erb'
|
57
|
-
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(@config)).result)
|
58
|
-
end
|
59
|
-
ActiveRecord::Base.establish_connection(current_env) if connect
|
60
|
-
if @logger
|
61
|
-
logger = @logger
|
62
|
-
else
|
63
|
-
logger = Logger.new($stderr)
|
64
|
-
logger.level = @log_level
|
65
|
-
end
|
66
|
-
ActiveRecord::Base.logger = logger
|
67
|
-
end
|
68
|
-
|
69
|
-
task :ar_init do
|
70
|
-
ar_init
|
71
|
-
end
|
72
|
-
|
73
|
-
desc "Migrate the database using the scripts in the migrations directory. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
|
74
|
-
task :migrate => :ar_init do
|
75
|
-
begin
|
76
|
-
require 'migration_helpers'
|
77
|
-
rescue LoadError
|
78
|
-
require "#{@vendor}/migration_helpers/init"
|
79
|
-
end
|
80
|
-
ActiveRecord::Migration.verbose = ENV['VERBOSE'] || @verbose
|
81
|
-
@migrations.each do |path|
|
82
|
-
ActiveRecord::Migrator.migrate(path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
83
|
-
end
|
84
|
-
Rake::Task["db:#{sub_namespace_with_separator}schema:dump"].execute
|
85
|
-
end
|
86
|
-
|
87
|
-
desc "Revert the last migration applied. Revert multiple migrations with STEP=x."
|
88
|
-
task :rollback => :ar_init do
|
89
|
-
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
90
|
-
@migrations.each do |path|
|
91
|
-
ActiveRecord::Migrator.rollback(path, step)
|
92
|
-
end
|
93
|
-
Rake::Task["db:#{sub_namespace_with_separator}schema:dump"].execute
|
94
|
-
end
|
95
|
-
|
96
|
-
desc "Retrieves the current schema version number"
|
97
|
-
task :version => :ar_init do
|
98
|
-
puts "Current version: #{ActiveRecord::Migrator.current_version}"
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
def create_database(config)
|
103
|
-
begin
|
104
|
-
if config['adapter'] =~ /sqlite/
|
105
|
-
if File.exist?(config['database'])
|
106
|
-
$stderr.puts "#{config['database']} already exists"
|
107
|
-
else
|
108
|
-
begin
|
109
|
-
# Create the SQLite database
|
110
|
-
ActiveRecord::Base.establish_connection(config)
|
111
|
-
ActiveRecord::Base.connection
|
112
|
-
rescue Exception => e
|
113
|
-
$stderr.puts e, *(e.backtrace)
|
114
|
-
$stderr.puts "Couldn't create database for #{config.inspect}"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
return # Skip the else clause of begin/rescue
|
118
|
-
else
|
119
|
-
ActiveRecord::Base.establish_connection(config)
|
120
|
-
ActiveRecord::Base.connection
|
121
|
-
end
|
122
|
-
rescue
|
123
|
-
case config['adapter']
|
124
|
-
when /mysql/
|
125
|
-
@charset = ENV['CHARSET'] || 'utf8'
|
126
|
-
@collation = ENV['COLLATION'] || 'utf8_unicode_ci'
|
127
|
-
creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
|
128
|
-
error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
|
129
|
-
access_denied_error = 1045
|
130
|
-
begin
|
131
|
-
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
132
|
-
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
|
133
|
-
ActiveRecord::Base.establish_connection(config)
|
134
|
-
rescue error_class => sqlerr
|
135
|
-
if sqlerr.errno == access_denied_error
|
136
|
-
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
|
137
|
-
root_password = $stdin.gets.strip
|
138
|
-
grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
|
139
|
-
"TO '#{config['username']}'@'localhost' " \
|
140
|
-
"IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
|
141
|
-
ActiveRecord::Base.establish_connection(config.merge(
|
142
|
-
'database' => nil, 'username' => 'root', 'password' => root_password))
|
143
|
-
ActiveRecord::Base.connection.create_database(config['database'], creation_options)
|
144
|
-
ActiveRecord::Base.connection.execute grant_statement
|
145
|
-
ActiveRecord::Base.establish_connection(config)
|
146
|
-
else
|
147
|
-
$stderr.puts sqlerr.error
|
148
|
-
$stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
|
149
|
-
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
|
150
|
-
end
|
151
|
-
end
|
152
|
-
when 'postgresql'
|
153
|
-
@encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
|
154
|
-
begin
|
155
|
-
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
156
|
-
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
|
157
|
-
ActiveRecord::Base.establish_connection(config)
|
158
|
-
rescue Exception => e
|
159
|
-
$stderr.puts e, *(e.backtrace)
|
160
|
-
$stderr.puts "Couldn't create database for #{config.inspect}"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
else
|
164
|
-
$stderr.puts "#{config['database']} already exists" unless config['adapter'] =~ /sqlite/
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
desc 'Create the database from config/database.yml for the current DB'
|
169
|
-
task :create do
|
170
|
-
ar_init(false)
|
171
|
-
config = ActiveRecord::Base.configurations[self.current_env]
|
172
|
-
raise ArgumentError, "#{self.current_env} database is not configured" unless config
|
173
|
-
create_database config
|
174
|
-
end
|
175
|
-
|
176
|
-
def drop_database(config)
|
177
|
-
case config['adapter']
|
178
|
-
when /mysql/
|
179
|
-
ActiveRecord::Base.establish_connection(config)
|
180
|
-
ActiveRecord::Base.connection.drop_database config['database']
|
181
|
-
when /^sqlite/
|
182
|
-
require 'pathname'
|
183
|
-
path = Pathname.new(config['database'])
|
184
|
-
file = path.absolute? ? path.to_s : File.join(@base, path)
|
185
|
-
FileUtils.rm(file)
|
186
|
-
when 'postgresql'
|
187
|
-
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
188
|
-
ActiveRecord::Base.connection.drop_database config['database']
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
desc 'Drops the database for the current DB'
|
193
|
-
task :drop => :ar_init do
|
194
|
-
config = ActiveRecord::Base.configurations[current_env]
|
195
|
-
drop_database(config)
|
196
|
-
end
|
197
|
-
|
198
|
-
namespace :migrate do
|
199
|
-
[:up, :down].each do |direction|
|
200
|
-
desc "Runs the '#{direction}' for a given migration VERSION."
|
201
|
-
task direction => :ar_init do
|
202
|
-
ActiveRecord::Migration.verbose = @verbose
|
203
|
-
version = ENV["VERSION"].to_i
|
204
|
-
raise "VERSION is required (must be a number)" if version == 0
|
205
|
-
migration_path = nil
|
206
|
-
if @migrations.length == 1
|
207
|
-
migration_path = @migrations.first
|
208
|
-
else
|
209
|
-
@migrations.each do |path|
|
210
|
-
Dir[File.join(path, '*.rb')].each do |file|
|
211
|
-
if File.basename(file).match(/^\d+/)[0] == version.to_s
|
212
|
-
migration_path = path
|
213
|
-
break
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
raise "Migration #{version} wasn't found on paths #{@migrations.join(', ')}" if migration_path.nil?
|
218
|
-
end
|
219
|
-
ActiveRecord::Migrator.run(direction, migration_path, version)
|
220
|
-
Rake::Task["db:#{sub_namespace_with_separator}schema:dump"].execute
|
221
|
-
end
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
desc "Raises an error if there are pending migrations"
|
226
|
-
task :abort_if_pending_migrations => :ar_init do
|
227
|
-
@migrations.each do |path|
|
228
|
-
pending_migrations = ActiveRecord::Migrator.new(:up, path).pending_migrations
|
229
|
-
|
230
|
-
if pending_migrations.any?
|
231
|
-
puts "You have #{pending_migrations.size} pending migrations:"
|
232
|
-
pending_migrations.each do |pending_migration|
|
233
|
-
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
|
234
|
-
end
|
235
|
-
abort %{Run "rake db:migrate" to update your database then try again.}
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
23
|
+
def self.application
|
24
|
+
s = "fake_app"
|
239
25
|
|
240
|
-
|
241
|
-
|
242
|
-
task :dump => :ar_init do
|
243
|
-
if schema_file = ENV['SCHEMA'] || @schema
|
244
|
-
require 'active_record/schema_dumper'
|
245
|
-
File.open(schema_file, "w") do |file|
|
246
|
-
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
247
|
-
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
|
-
desc "Load a ar_schema.rb file into the database"
|
252
|
-
task :load => :ar_init do
|
253
|
-
file = ENV['SCHEMA'] || @schema
|
254
|
-
load(file)
|
255
|
-
end
|
26
|
+
def s.paths
|
27
|
+
Dir.glob('db/*').inject({}){|hash,x|hash[x]=[x]; hash}
|
256
28
|
end
|
257
29
|
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
ActiveRecord::Schema.verbose = false
|
263
|
-
Rake::Task["db:#{sub_namespace_with_separator}schema:load"].invoke
|
30
|
+
def s.config
|
31
|
+
s = "fake_config"
|
32
|
+
def s.database_configuration
|
33
|
+
DB_CONFIG
|
264
34
|
end
|
265
|
-
|
266
|
-
desc "Empty the test database"
|
267
|
-
task :purge => "db:#{sub_namespace_with_separator}ar_init" do
|
268
|
-
config = ActiveRecord::Base.configurations['test']
|
269
|
-
case config["adapter"]
|
270
|
-
when *["mysql", "mysql2"]
|
271
|
-
ActiveRecord::Base.establish_connection(:test)
|
272
|
-
ActiveRecord::Base.connection.recreate_database(config["database"], config)
|
273
|
-
when "postgresql" #TODO i doubt this will work <-> methods are not defined
|
274
|
-
ActiveRecord::Base.clear_active_connections!
|
275
|
-
drop_database(config)
|
276
|
-
create_database(config)
|
277
|
-
when "sqlite", "sqlite3"
|
278
|
-
db_file = config["database"] || config["dbfile"]
|
279
|
-
File.delete(db_file) if File.exist?(db_file)
|
280
|
-
when "sqlserver"
|
281
|
-
drop_script = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/, '-')
|
282
|
-
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{drop_script}`
|
283
|
-
`osql -E -S #{config["host"]} -d #{config["database"]} -i db\\test_structure.sql`
|
284
|
-
when "oci", "oracle"
|
285
|
-
ActiveRecord::Base.establish_connection(:test)
|
286
|
-
ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
|
287
|
-
ActiveRecord::Base.connection.execute(ddl)
|
288
|
-
end
|
289
|
-
when "firebird"
|
290
|
-
ActiveRecord::Base.establish_connection(:test)
|
291
|
-
ActiveRecord::Base.connection.recreate_database!
|
292
|
-
else
|
293
|
-
raise "Task not supported by #{config["adapter"].inspect}"
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
desc 'Check for pending migrations and load the test schema'
|
298
|
-
task :prepare => ["db:#{sub_namespace_with_separator}abort_if_pending_migrations", "db:#{sub_namespace_with_separator}test:load"]
|
35
|
+
s
|
299
36
|
end
|
37
|
+
s
|
38
|
+
end
|
39
|
+
end
|
300
40
|
|
301
|
-
|
302
|
-
task :generate do
|
303
|
-
ts = Time.now.strftime '%Y%m%d%H%%M%S'
|
304
|
-
|
305
|
-
if ENV['model']
|
306
|
-
table_name = ENV['model']
|
307
|
-
else
|
308
|
-
print 'model name> '
|
309
|
-
table_name = $stdin.gets.strip
|
310
|
-
end
|
311
|
-
|
312
|
-
raise ArgumentError, 'must provide a name for the model to generate' if table_name.empty?
|
313
|
-
|
314
|
-
create_table_str = "create_table :#{table_name} do |t|"
|
315
|
-
|
316
|
-
fields = ENV['fields'] if ENV['fields']
|
317
|
-
|
318
|
-
columns = ENV.has_key?('fields') ? ENV['fields'].split.map { |v| "t.#{v.sub(/:/, ' :')}" }.join("\n#{' '*6}") : nil
|
319
|
-
|
320
|
-
create_table_str << "\n #{columns}" if columns
|
41
|
+
task(:rails_env){}
|
321
42
|
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
#{create_table_str}
|
326
|
-
t.timestamps
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
def self.down
|
331
|
-
drop_table :#{table_name}
|
332
|
-
end
|
43
|
+
task(:environment) do
|
44
|
+
ActiveRecord::Base.configurations = DB_CONFIG
|
45
|
+
ActiveRecord::Base.establish_connection DB_CONFIG[Rails.env]
|
333
46
|
end
|
334
|
-
MIGRATION
|
335
47
|
|
336
|
-
|
337
|
-
end
|
48
|
+
load 'active_record/railties/databases.rake'
|
338
49
|
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
50
|
+
namespace :db do
|
51
|
+
desc "Create a new migration"
|
52
|
+
task :new_migration do |t|
|
53
|
+
unless migration = ENV['name']
|
54
|
+
puts "Error: must provide name of migration to generate."
|
55
|
+
puts "For example: rake #{t.name} name=add_field_to_form"
|
56
|
+
abort
|
57
|
+
end
|
346
58
|
|
347
|
-
|
59
|
+
file_contents = <<eof
|
348
60
|
class #{class_name migration} < ActiveRecord::Migration
|
349
61
|
def self.up
|
350
62
|
end
|
@@ -354,23 +66,21 @@ class #{class_name migration} < ActiveRecord::Migration
|
|
354
66
|
end
|
355
67
|
end
|
356
68
|
eof
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
puts "Created migration #{file_name migration}"
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
def class_name str
|
365
|
-
str.split('_').map { |s| s.capitalize }.join
|
69
|
+
create_file file_name(migration), file_contents
|
70
|
+
puts "Created migration #{file_name migration}"
|
366
71
|
end
|
367
72
|
|
368
|
-
def create_file
|
73
|
+
def create_file file, contents
|
74
|
+
path = File.dirname(file)
|
369
75
|
FileUtils.mkdir_p path unless File.exists? path
|
370
76
|
File.open(file, 'w') { |f| f.write contents }
|
371
77
|
end
|
372
78
|
|
373
79
|
def file_name migration
|
374
|
-
File.join
|
80
|
+
File.join 'db/migrate', "#{Time.now.utc.strftime '%Y%m%d%H%M%S'}_#{migration}.rb"
|
81
|
+
end
|
82
|
+
|
83
|
+
def class_name str
|
84
|
+
str.split('_').map { |s| s.capitalize }.join
|
375
85
|
end
|
376
86
|
end
|
@@ -12,8 +12,8 @@ describe 'Standalone migrations' do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def migration(name)
|
15
|
-
m = `cd spec/tmp/db/
|
16
|
-
m ? "db/
|
15
|
+
m = `cd spec/tmp/db/migrate && ls`.split("\n").detect { |m| m =~ name }
|
16
|
+
m ? "db/migrate/#{m}" : m
|
17
17
|
end
|
18
18
|
|
19
19
|
def tmp_file(file)
|
@@ -21,16 +21,14 @@ describe 'Standalone migrations' do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def run(cmd)
|
24
|
-
`cd spec/tmp && #{cmd} 2>&1
|
25
|
-
|
26
|
-
|
27
|
-
def run_with_output(cmd)
|
28
|
-
`cd spec/tmp && #{cmd} 2>&1`
|
24
|
+
result = `cd spec/tmp && #{cmd} 2>&1`
|
25
|
+
raise result unless $?.success?
|
26
|
+
result
|
29
27
|
end
|
30
28
|
|
31
29
|
def make_migration(name, options={})
|
32
30
|
task_name = options[:task_name] || 'db:new_migration'
|
33
|
-
migration = run("rake #{task_name} name=#{name}").match(%r{db/
|
31
|
+
migration = run("rake #{task_name} name=#{name}").match(%r{db/migrate/\d+.*.rb})[0]
|
34
32
|
content = read(migration)
|
35
33
|
content.sub!(/def self.down.*?\send/m, "def self.down;puts 'DOWN-#{name}';end")
|
36
34
|
content.sub!(/def self.up.*?\send/m, "def self.up;puts 'UP-#{name}';end")
|
@@ -38,62 +36,39 @@ describe 'Standalone migrations' do
|
|
38
36
|
migration.match(/\d{14}/)[0]
|
39
37
|
end
|
40
38
|
|
41
|
-
def make_sub_namespaced_migration(namespace, name)
|
42
|
-
# specify complete task name here to avoid conditionals in make_migration
|
43
|
-
make_migration(name, :task_name => "db:#{namespace}:new_migration")
|
44
|
-
end
|
45
|
-
|
46
39
|
def write_rakefile(config=nil)
|
47
40
|
write 'Rakefile', <<-TXT
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
#{config}
|
54
|
-
end
|
55
|
-
rescue LoadError => e
|
56
|
-
puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: \#{e})"
|
57
|
-
end
|
58
|
-
TXT
|
59
|
-
end
|
60
|
-
|
61
|
-
def write_reversible_migration
|
62
|
-
write "db/migrations/20110703102233_create_tests.rb", <<-TXT
|
63
|
-
class CreateTests < ActiveRecord::Migration
|
64
|
-
def self.up
|
65
|
-
puts "UP-CreateTests"
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.down
|
69
|
-
puts "DOWN-CreateTests"
|
70
|
-
end
|
41
|
+
$LOAD_PATH.unshift '#{File.expand_path('lib')}'
|
42
|
+
begin
|
43
|
+
require 'tasks/standalone_migrations'
|
44
|
+
rescue LoadError => e
|
45
|
+
puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: \#{e})"
|
71
46
|
end
|
72
47
|
TXT
|
73
48
|
end
|
74
49
|
|
75
50
|
def write_multiple_migrations
|
76
51
|
write_rakefile %{t.migrations = "db/migrations", "db/migrations2"}
|
77
|
-
write "db/
|
52
|
+
write "db/migrate/20100509095815_create_tests.rb", <<-TXT
|
78
53
|
class CreateTests < ActiveRecord::Migration
|
79
|
-
def self.up
|
80
|
-
|
81
|
-
end
|
54
|
+
def self.up
|
55
|
+
puts "UP-CreateTests"
|
56
|
+
end
|
82
57
|
|
83
|
-
def self.down
|
84
|
-
|
85
|
-
end
|
58
|
+
def self.down
|
59
|
+
puts "DOWN-CreateTests"
|
60
|
+
end
|
86
61
|
end
|
87
62
|
TXT
|
88
|
-
write "db/
|
63
|
+
write "db/migrate/20100509095816_create_tests2.rb", <<-TXT
|
89
64
|
class CreateTests2 < ActiveRecord::Migration
|
90
|
-
def self.up
|
91
|
-
|
92
|
-
end
|
65
|
+
def self.up
|
66
|
+
puts "UP-CreateTests2"
|
67
|
+
end
|
93
68
|
|
94
|
-
def self.down
|
95
|
-
|
96
|
-
end
|
69
|
+
def self.down
|
70
|
+
puts "DOWN-CreateTests2"
|
71
|
+
end
|
97
72
|
end
|
98
73
|
TXT
|
99
74
|
end
|
@@ -103,65 +78,37 @@ end
|
|
103
78
|
`mkdir spec/tmp`
|
104
79
|
write_rakefile
|
105
80
|
write 'db/config.yml', <<-TXT
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
81
|
+
development:
|
82
|
+
adapter: sqlite3
|
83
|
+
database: db/development.sql
|
84
|
+
test:
|
85
|
+
adapter: sqlite3
|
86
|
+
database: db/test.sql
|
112
87
|
TXT
|
113
88
|
end
|
114
89
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
90
|
+
it "warns of deprecated folder structure" do
|
91
|
+
warning = /DEPRECATED.* db\/migrate/
|
92
|
+
run("rake db:create").should_not =~ warning
|
93
|
+
write('db/migrations/fooo.rb', 'xxx')
|
94
|
+
run("rake db:create").should =~ warning
|
120
95
|
end
|
121
96
|
|
122
|
-
describe 'db:create
|
123
|
-
it "should
|
124
|
-
|
97
|
+
describe 'db:create and drop' do
|
98
|
+
it "should create the database and drop the database that was created" do
|
99
|
+
run "rake db:create"
|
100
|
+
run "rake db:drop"
|
125
101
|
end
|
126
102
|
end
|
127
103
|
|
128
104
|
describe 'db:new_migration' do
|
129
|
-
|
130
|
-
|
131
|
-
run("rake db:new_migration").should_not =~ /SUCCESS/
|
132
|
-
end
|
133
|
-
|
134
|
-
it "generates a new migration with this name and timestamp" do
|
135
|
-
run("rake db:new_migration name=test_abc").should =~ %r{Created migration .*spec/tmp/db/migrations/\d+_test_abc\.rb}
|
136
|
-
run("ls db/migrations").should =~ /^\d+_test_abc.rb$/
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
context "multiple migration paths" do
|
141
|
-
before do
|
142
|
-
write_rakefile %{t.migrations = "db/migrations", "db/migrations2"}
|
143
|
-
end
|
144
|
-
it "chooses the first path" do
|
145
|
-
run("rake db:new_migration name=test_abc").should =~ %r{Created migration .*db/migrations/\d+_test_abc\.rb}
|
146
|
-
end
|
105
|
+
it "fails if i do not add a name" do
|
106
|
+
lambda{ run("rake db:new_migration") }.should raise_error(/name=/)
|
147
107
|
end
|
148
108
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
end
|
153
|
-
it "fails if i do not add a name" do
|
154
|
-
run("rake db:widgets:new_migration").should_not =~ /SUCCESS/
|
155
|
-
end
|
156
|
-
|
157
|
-
it "generates a new migration with this name and timestamp" do
|
158
|
-
run("rake db:widgets:new_migration name=test_widget").should =~ %r{Created migration .*spec/tmp/db/migrations/\d+_test_widget\.rb}
|
159
|
-
run("ls db/migrations").should =~ /^\d+_test_widget.rb$/
|
160
|
-
end
|
161
|
-
|
162
|
-
it 'does not create top level db:new_migration task' do
|
163
|
-
run('rake db:new_migration').should =~ /Don't know how to build task 'db:new_migration'/
|
164
|
-
end
|
109
|
+
it "generates a new migration with this name and timestamp" do
|
110
|
+
run("rake db:new_migration name=test_abc").should =~ %r{Created migration db/migrate/\d+_test_abc\.rb}
|
111
|
+
run("ls db/migrate").should =~ /^\d+_test_abc.rb$/
|
165
112
|
end
|
166
113
|
end
|
167
114
|
|
@@ -169,192 +116,88 @@ end
|
|
169
116
|
it "should start with a new database version" do
|
170
117
|
run("rake db:version").should =~ /Current version: 0/
|
171
118
|
end
|
119
|
+
|
120
|
+
it "should display the current version" do
|
121
|
+
run("rake db:new_migration name=test_abc")
|
122
|
+
run("rake --trace db:migrate")
|
123
|
+
run("rake db:version").should =~ /Current version: #{Time.now.year}/
|
124
|
+
end
|
172
125
|
end
|
173
126
|
|
174
127
|
describe 'db:migrate' do
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
end
|
128
|
+
it "does nothing when no migrations are present" do
|
129
|
+
run("rake db:migrate").should_not =~ /Migrating/
|
130
|
+
end
|
179
131
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
result.should =~ /SUCCESS/
|
184
|
-
result.should =~ /Migrating to Xxx \(#{Time.now.year}/
|
185
|
-
end
|
132
|
+
it "migrates if i add a migration" do
|
133
|
+
run("rake db:new_migration name=xxx")
|
134
|
+
run("rake db:migrate").should =~ /Xxx: Migrating/i
|
186
135
|
end
|
136
|
+
end
|
187
137
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
138
|
+
describe 'db:migrate:down' do
|
139
|
+
it "migrates down" do
|
140
|
+
make_migration('xxx')
|
141
|
+
sleep 1
|
142
|
+
version = make_migration('yyy')
|
143
|
+
run 'rake db:migrate'
|
144
|
+
|
145
|
+
result = run("rake db:migrate:down VERSION=#{version}")
|
146
|
+
result.should_not =~ /DOWN-xxx/
|
147
|
+
result.should =~ /DOWN-yyy/
|
197
148
|
end
|
198
149
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
run("rake db:widgets:new_migration name=new_widget")
|
205
|
-
result = run("rake db:widgets:migrate")
|
206
|
-
result.should =~ /SUCCESS/
|
207
|
-
result.should =~ /Migrating to NewWidget \(#{Time.now.year}/
|
208
|
-
end
|
150
|
+
it "fails without version" do
|
151
|
+
make_migration('yyy')
|
152
|
+
lambda{ run("rake db:migrate:down") }.should raise_error(/VERSION/)
|
153
|
+
end
|
154
|
+
end
|
209
155
|
|
210
|
-
|
211
|
-
|
212
|
-
|
156
|
+
describe 'db:migrate:up' do
|
157
|
+
it "migrates up" do
|
158
|
+
make_migration('xxx')
|
159
|
+
run 'rake db:migrate'
|
160
|
+
sleep 1
|
161
|
+
version = make_migration('yyy')
|
162
|
+
result = run("rake db:migrate:up VERSION=#{version}")
|
163
|
+
result.should_not =~ /UP-xxx/
|
164
|
+
result.should =~ /UP-yyy/
|
165
|
+
end
|
166
|
+
|
167
|
+
it "fails without version" do
|
168
|
+
make_migration('yyy')
|
169
|
+
lambda{ run("rake db:migrate:up") }.should raise_error(/VERSION/)
|
213
170
|
end
|
214
171
|
end
|
215
172
|
|
216
173
|
describe 'db:rollback' do
|
217
174
|
it "does nothing when no migrations have been run" do
|
218
|
-
run("rake db:
|
175
|
+
run("rake db:version").should =~ /version: 0/
|
176
|
+
run("rake db:rollback").should == ''
|
177
|
+
run("rake db:version").should =~ /version: 0/
|
219
178
|
end
|
220
179
|
|
221
|
-
it "rolls back the last migration if one has been applied" do
|
222
|
-
|
223
|
-
run("rake db:migrate")
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
result.should =~ /reverted/
|
180
|
+
it "rolls back the last migration if one has been applied" do
|
181
|
+
write_multiple_migrations
|
182
|
+
run("rake db:migrate")
|
183
|
+
run("rake db:version").should =~ /version: 20100509095816/
|
184
|
+
run("rake db:rollback").should =~ /revert/
|
185
|
+
run("rake db:version").should =~ /version: 20100509095815/
|
228
186
|
end
|
229
187
|
|
230
188
|
it "rolls back multiple migrations if the STEP argument is given" do
|
231
|
-
write_multiple_migrations
|
232
|
-
run("rake db:migrate")
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
result.should =~ /reverted/
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
describe 'db:migrate:down' do
|
241
|
-
context "single migration path" do
|
242
|
-
it "migrates down" do
|
243
|
-
make_migration('xxx')
|
244
|
-
sleep 1
|
245
|
-
version = make_migration('yyy')
|
246
|
-
run 'rake db:migrate'
|
247
|
-
|
248
|
-
result = run("rake db:migrate:down VERSION=#{version}")
|
249
|
-
result.should =~ /SUCCESS/
|
250
|
-
result.should_not =~ /DOWN-xxx/
|
251
|
-
result.should =~ /DOWN-yyy/
|
252
|
-
end
|
253
|
-
|
254
|
-
it "fails without version" do
|
255
|
-
make_migration('yyy')
|
256
|
-
result = run("rake db:migrate:down")
|
257
|
-
result.should_not =~ /SUCCESS/
|
258
|
-
end
|
189
|
+
write_multiple_migrations
|
190
|
+
run("rake db:migrate")
|
191
|
+
run("rake db:version").should =~ /version: 20100509095816/
|
192
|
+
run("rake db:rollback STEP=2") =~ /revert/
|
193
|
+
run("rake db:version").should =~ /version: 0/
|
259
194
|
end
|
260
|
-
|
261
|
-
context "multiple migration paths" do
|
262
|
-
before do
|
263
|
-
write_multiple_migrations
|
264
|
-
end
|
265
|
-
|
266
|
-
it "runs down on the correct path" do
|
267
|
-
run 'rake db:migrate'
|
268
|
-
result = run 'rake db:migrate:down VERSION=20100509095815'
|
269
|
-
result.should =~ /DOWN-CreateTests/
|
270
|
-
result.should_not =~ /DOWN-CreateTests2/
|
271
|
-
end
|
272
|
-
|
273
|
-
it "fails if migration number isn't found" do
|
274
|
-
run 'rake db:migrate'
|
275
|
-
result = run 'rake db:migrate:down VERSION=20100509095820'
|
276
|
-
result.should_not =~ /SUCCESS/
|
277
|
-
result.should =~ /wasn't found on path/
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
context 'sub-namespaced task' do
|
282
|
-
before do
|
283
|
-
write_rakefile %{t.sub_namespace = "widgets"}
|
284
|
-
end
|
285
|
-
it 'migrates down' do
|
286
|
-
make_sub_namespaced_migration('widgets', 'widget_xxx')
|
287
|
-
sleep 1
|
288
|
-
version = make_sub_namespaced_migration('widgets', 'widget_yyy')
|
289
|
-
run 'rake db:widgets:migrate'
|
290
|
-
|
291
|
-
result = run("rake db:widgets:migrate:down VERSION=#{version}")
|
292
|
-
result.should =~ /SUCCESS/
|
293
|
-
result.should_not =~ /DOWN-widget_xxx/
|
294
|
-
result.should =~ /DOWN-widget_yyy/
|
295
|
-
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
describe 'db:migrate:up' do
|
300
|
-
context "single migration path" do
|
301
|
-
it "migrates up" do
|
302
|
-
make_migration('xxx')
|
303
|
-
run 'rake db:migrate'
|
304
|
-
sleep 1
|
305
|
-
version = make_migration('yyy')
|
306
|
-
result = run("rake db:migrate:up VERSION=#{version}")
|
307
|
-
result.should =~ /SUCCESS/
|
308
|
-
result.should_not =~ /UP-xxx/
|
309
|
-
result.should =~ /UP-yyy/
|
310
|
-
end
|
311
|
-
|
312
|
-
it "fails without version" do
|
313
|
-
make_migration('yyy')
|
314
|
-
result = run("rake db:migrate:up")
|
315
|
-
result.should_not =~ /SUCCESS/
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
context "multiple migration paths" do
|
320
|
-
before do
|
321
|
-
write_multiple_migrations
|
322
|
-
end
|
323
|
-
|
324
|
-
it "runs down on the correct path" do
|
325
|
-
result = run 'rake db:migrate:up VERSION=20100509095815'
|
326
|
-
result.should =~ /UP-CreateTests/
|
327
|
-
result.should_not =~ /UP-CreateTests2/
|
328
|
-
end
|
329
|
-
|
330
|
-
it "fails if migration number isn't found" do
|
331
|
-
result = run 'rake db:migrate:up VERSION=20100509095820'
|
332
|
-
result.should_not =~ /SUCCESS/
|
333
|
-
result.should =~ /wasn't found on path/
|
334
|
-
end
|
335
|
-
end
|
336
|
-
|
337
|
-
context 'sub-namespaced task' do
|
338
|
-
before do
|
339
|
-
write_rakefile %{t.sub_namespace = "widgets"}
|
340
|
-
end
|
341
|
-
it 'migrates up' do
|
342
|
-
make_sub_namespaced_migration('widgets', 'widget_xxx')
|
343
|
-
run 'rake db:widgets:migrate'
|
344
|
-
sleep 1
|
345
|
-
version = make_sub_namespaced_migration('widgets', 'widget_yyy')
|
346
|
-
result = run("rake db:widgets:migrate:up VERSION=#{version}")
|
347
|
-
result.should =~ /SUCCESS/
|
348
|
-
result.should_not =~ /UP-widget_xxx/
|
349
|
-
result.should =~ /UP-widget_yyy/
|
350
|
-
end
|
351
|
-
end
|
352
|
-
end
|
195
|
+
end
|
353
196
|
|
354
197
|
describe 'schema:dump' do
|
355
198
|
it "dumps the schema" do
|
356
|
-
|
357
|
-
|
199
|
+
write('db/schema.rb', '')
|
200
|
+
run('rake db:schema:dump')
|
358
201
|
read('db/schema.rb').should =~ /ActiveRecord/
|
359
202
|
end
|
360
203
|
end
|
@@ -365,38 +208,57 @@ end
|
|
365
208
|
schema = "db/schema.rb"
|
366
209
|
write(schema, read(schema)+"\nputs 'LOADEDDD'")
|
367
210
|
result = run('rake db:schema:load')
|
368
|
-
result.should =~ /SUCCESS/
|
369
211
|
result.should =~ /LOADEDDD/
|
370
212
|
end
|
213
|
+
|
214
|
+
it "loads all migrations" do
|
215
|
+
make_migration('yyy')
|
216
|
+
run "rake db:migrate"
|
217
|
+
run "rake db:drop"
|
218
|
+
run "rake db:create"
|
219
|
+
run "rake db:schema:load"
|
220
|
+
run( "rake db:migrate").strip.should == ''
|
221
|
+
end
|
371
222
|
end
|
372
223
|
|
373
224
|
describe 'db:abort_if_pending_migrations' do
|
374
225
|
it "passes when no migrations are pending" do
|
375
|
-
run("rake db:abort_if_pending_migrations").
|
226
|
+
run("rake db:abort_if_pending_migrations").strip.should == ''
|
376
227
|
end
|
377
228
|
|
378
229
|
it "fails when migrations are pending" do
|
379
230
|
make_migration('yyy')
|
380
|
-
|
381
|
-
result.should =~ /try again/
|
382
|
-
result.should =~ /1 pending migration/
|
231
|
+
lambda{ run("rake db:abort_if_pending_migrations") }.should raise_error(/1 pending migration/)
|
383
232
|
end
|
384
233
|
end
|
385
234
|
|
386
235
|
describe 'db:test:load' do
|
387
236
|
it 'loads' do
|
388
237
|
write("db/schema.rb", "puts 'LOADEDDD'")
|
389
|
-
run("rake db:test:load").should =~ /LOADEDDD
|
238
|
+
run("rake db:test:load").should =~ /LOADEDDD/
|
390
239
|
end
|
391
240
|
|
392
241
|
it "fails without schema" do
|
393
|
-
run("rake db:test:load").should
|
242
|
+
lambda{ run("rake db:test:load") }.should raise_error(/try again/)
|
394
243
|
end
|
395
244
|
end
|
396
245
|
|
397
246
|
describe 'db:test:purge' do
|
398
247
|
it "runs" do
|
399
|
-
run('rake db:test:purge')
|
248
|
+
run('rake db:test:purge')
|
400
249
|
end
|
401
250
|
end
|
251
|
+
|
252
|
+
describe 'db:migrate when environment is specified' do
|
253
|
+
it "runs when using the DB environment variable" do
|
254
|
+
make_migration('yyy')
|
255
|
+
run('rake db:migrate DB=test')
|
256
|
+
run('rake db:version DB=test').should_not =~ /version: 0/
|
257
|
+
run('rake db:version').should =~ /version: 0/
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should error on an invalid database" do
|
261
|
+
lambda{ run("rake db:create DB=nonexistent")}.should raise_error(/rake aborted/)
|
262
|
+
end
|
263
|
+
end
|
402
264
|
end
|
@@ -5,16 +5,19 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{standalone_migrations}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "1.0.0"
|
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 = %q{2011-07-
|
12
|
+
s.date = %q{2011-07-10}
|
13
13
|
s.email = %q{thuss@gabrito.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.markdown"
|
16
16
|
]
|
17
17
|
s.files = [
|
18
|
+
"Gemfile",
|
19
|
+
"Gemfile.lock",
|
20
|
+
"MIT-LICENSE",
|
18
21
|
"README.markdown",
|
19
22
|
"Rakefile",
|
20
23
|
"VERSION",
|
@@ -37,14 +40,14 @@ Gem::Specification.new do |s|
|
|
37
40
|
|
38
41
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
39
42
|
s.add_runtime_dependency(%q<rake>, [">= 0"])
|
40
|
-
s.add_runtime_dependency(%q<activerecord>, [">=
|
43
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3"])
|
41
44
|
else
|
42
45
|
s.add_dependency(%q<rake>, [">= 0"])
|
43
|
-
s.add_dependency(%q<activerecord>, [">=
|
46
|
+
s.add_dependency(%q<activerecord>, [">= 3"])
|
44
47
|
end
|
45
48
|
else
|
46
49
|
s.add_dependency(%q<rake>, [">= 0"])
|
47
|
-
s.add_dependency(%q<activerecord>, [">=
|
50
|
+
s.add_dependency(%q<activerecord>, [">= 3"])
|
48
51
|
end
|
49
52
|
end
|
50
53
|
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: standalone_migrations
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
|
9
|
-
version: 0.4.11
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Todd Huss
|
@@ -15,12 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-10 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: rake
|
23
|
-
prerelease: false
|
24
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
24
|
none: false
|
26
25
|
requirements:
|
@@ -30,19 +29,20 @@ dependencies:
|
|
30
29
|
- 0
|
31
30
|
version: "0"
|
32
31
|
type: :runtime
|
32
|
+
prerelease: false
|
33
33
|
version_requirements: *id001
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: activerecord
|
36
|
-
prerelease: false
|
37
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
37
|
none: false
|
39
38
|
requirements:
|
40
39
|
- - ">="
|
41
40
|
- !ruby/object:Gem::Version
|
42
41
|
segments:
|
43
|
-
-
|
44
|
-
version: "
|
42
|
+
- 3
|
43
|
+
version: "3"
|
45
44
|
type: :runtime
|
45
|
+
prerelease: false
|
46
46
|
version_requirements: *id002
|
47
47
|
description:
|
48
48
|
email: thuss@gabrito.com
|
@@ -53,6 +53,9 @@ extensions: []
|
|
53
53
|
extra_rdoc_files:
|
54
54
|
- README.markdown
|
55
55
|
files:
|
56
|
+
- Gemfile
|
57
|
+
- Gemfile.lock
|
58
|
+
- MIT-LICENSE
|
56
59
|
- README.markdown
|
57
60
|
- Rakefile
|
58
61
|
- VERSION
|