ejhayes_standalone_migrations 2.2.0

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.
@@ -0,0 +1,331 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Standalone migrations' do
4
+
5
+ def write(file, content)
6
+ raise "cannot write nil" unless file
7
+ file = tmp_file(file)
8
+ folder = File.dirname(file)
9
+ `mkdir -p #{folder}` unless File.exist?(folder)
10
+ File.open(file, 'w') { |f| f.write content }
11
+ end
12
+
13
+ def read(file)
14
+ File.read(tmp_file(file))
15
+ end
16
+
17
+ def migration(name)
18
+ m = `cd spec/tmp/db/migrate && ls`.split("\n").detect { |m| m =~ /#{name}/ }
19
+ m ? "db/migrate/#{m}" : m
20
+ end
21
+
22
+ def tmp_file(file)
23
+ "spec/tmp/#{file}"
24
+ end
25
+
26
+ def run(cmd)
27
+ result = `cd spec/tmp && #{cmd} 2>&1`
28
+ raise result unless $?.success?
29
+ result
30
+ end
31
+
32
+ def make_migration(name, options={})
33
+ task_name = options[:task_name] || 'db:new_migration'
34
+ migration = run("rake #{task_name} name=#{name}").match(%r{db/migrate/\d+.*.rb})[0]
35
+ content = read(migration)
36
+ content.sub!(/def down.*?\send/m, "def down;puts 'DOWN-#{name}';end")
37
+ content.sub!(/def up.*?\send/m, "def up;puts 'UP-#{name}';end")
38
+ write(migration, content)
39
+ migration.match(/\d{14}/)[0]
40
+ end
41
+
42
+ def write_rakefile(config=nil)
43
+ write 'Rakefile', <<-TXT
44
+ $LOAD_PATH.unshift '#{File.expand_path('lib')}'
45
+ begin
46
+ require "standalone_migrations"
47
+ StandaloneMigrations::Tasks.load_tasks
48
+ rescue LoadError => e
49
+ puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: \#{e})"
50
+ end
51
+ TXT
52
+ end
53
+
54
+ def write_multiple_migrations
55
+ write_rakefile %{t.migrations = "db/migrations", "db/migrations2"}
56
+ write "db/migrate/20100509095815_create_tests.rb", <<-TXT
57
+ class CreateTests < ActiveRecord::Migration
58
+ def up
59
+ puts "UP-CreateTests"
60
+ end
61
+
62
+ def down
63
+ puts "DOWN-CreateTests"
64
+ end
65
+ end
66
+ TXT
67
+ write "db/migrate/20100509095816_create_tests2.rb", <<-TXT
68
+ class CreateTests2 < ActiveRecord::Migration
69
+ def up
70
+ puts "UP-CreateTests2"
71
+ end
72
+
73
+ def down
74
+ puts "DOWN-CreateTests2"
75
+ end
76
+ end
77
+ TXT
78
+ end
79
+
80
+ before do
81
+ `rm -rf spec/tmp` if File.exist?('spec/tmp')
82
+ `mkdir spec/tmp`
83
+ write_rakefile
84
+ write 'db/config.yml', <<-TXT
85
+ development:
86
+ adapter: sqlite3
87
+ database: db/development.sql
88
+ test:
89
+ adapter: sqlite3
90
+ database: db/test.sql
91
+ TXT
92
+ end
93
+
94
+ after(:all) do
95
+ `rm -rf spec/tmp` if File.exist?('spec/tmp')
96
+ end
97
+
98
+ it "warns of deprecated folder structure" do
99
+ warning = /DEPRECATED.* db\/migrate/
100
+ run("rake db:create").should_not =~ warning
101
+ write('db/migrations/fooo.rb', 'xxx')
102
+ run("rake db:create").should =~ warning
103
+ end
104
+
105
+ describe 'db:create and drop' do
106
+ it "should create the database and drop the database that was created" do
107
+ run "rake db:create"
108
+ run "rake db:drop"
109
+ end
110
+ end
111
+
112
+ describe 'callbacks' do
113
+ it 'runs the callbacks' do
114
+ StandaloneMigrations::Tasks.should_receive(:configure)
115
+
116
+ connection_established = false
117
+ ActiveRecord::Base.should_receive(:establish_connection) do
118
+ connection_established = true
119
+ end
120
+ StandaloneMigrations.should_receive(:run_on_load_callbacks) do
121
+ connection_established.should be_true
122
+ end
123
+
124
+ Dir.chdir(File.join(File.dirname(__FILE__), "tmp")) do
125
+ load "Rakefile"
126
+ Rake::Task['standalone:connection'].invoke
127
+ end
128
+ end
129
+ end
130
+
131
+ describe 'db:new_migration' do
132
+ it "fails if i do not add a name" do
133
+ lambda{ run("rake db:new_migration") }.should raise_error(/name=/)
134
+ end
135
+
136
+ it "generates a new migration with this name from ENV and timestamp" do
137
+ run("rake db:new_migration name=test_abc_env").should =~ %r{create(.*)db/migrate/\d+_test_abc_env\.rb}
138
+ run("ls db/migrate").should =~ /^\d+_test_abc_env.rb$/
139
+ end
140
+
141
+ it "generates a new migration with this name from args and timestamp" do
142
+ run("rake db:new_migration[test_abc_args]").should =~ %r{create(.*)db/migrate/\d+_test_abc_args\.rb}
143
+ run("ls db/migrate").should =~ /^\d+_test_abc_args.rb$/
144
+ end
145
+
146
+ it "generates a new migration with the name converted to the Rails migration format" do
147
+ run("rake db:new_migration name=MyNiceModel").should =~ %r{create(.*)db/migrate/\d+_my_nice_model\.rb}
148
+ read(migration('my_nice_model')).should =~ /class MyNiceModel/
149
+ run("ls db/migrate").should =~ /^\d+_my_nice_model.rb$/
150
+ end
151
+
152
+ it "generates a new migration with name and options from ENV" do
153
+ run("rake db:new_migration name=add_name_and_email_to_users options='name:string email:string'")
154
+ read(migration('add_name_and_email_to_users')).should =~ /add_column :users, :name, :string\n\s*add_column :users, :email, :string/
155
+ end
156
+
157
+ it "generates a new migration with name and options from args" do
158
+ run("rake db:new_migration[add_website_and_username_to_users,website:string/username:string]")
159
+ read(migration('add_website_and_username_to_users')).should =~ /add_column :users, :website, :string\n\s*add_column :users, :username, :string/
160
+ end
161
+ end
162
+
163
+ describe 'db:version' do
164
+ it "should start with a new database version" do
165
+ run("rake db:version").should =~ /Current version: 0/
166
+ end
167
+
168
+ it "should display the current version" do
169
+ run("rake db:new_migration name=test_abc")
170
+ run("rake --trace db:migrate")
171
+ run("rake db:version").should =~ /Current version: #{Time.now.year}/
172
+ end
173
+ end
174
+
175
+ describe 'db:migrate' do
176
+ it "does nothing when no migrations are present" do
177
+ run("rake db:migrate").should_not =~ /Migrating/
178
+ end
179
+
180
+ it "migrates if i add a migration" do
181
+ run("rake db:new_migration name=xxx")
182
+ run("rake db:migrate").should =~ /Xxx: Migrating/i
183
+ end
184
+ end
185
+
186
+ describe 'db:migrate:down' do
187
+ it "migrates down" do
188
+ make_migration('xxx')
189
+ sleep 1
190
+ version = make_migration('yyy')
191
+ run 'rake db:migrate'
192
+
193
+ result = run("rake db:migrate:down VERSION=#{version}")
194
+ result.should_not =~ /DOWN-xxx/
195
+ result.should =~ /DOWN-yyy/
196
+ end
197
+
198
+ it "fails without version" do
199
+ make_migration('yyy')
200
+ lambda{ run("rake db:migrate:down") }.should raise_error(/VERSION/)
201
+ end
202
+ end
203
+
204
+ describe 'db:migrate:up' do
205
+ it "migrates up" do
206
+ make_migration('xxx')
207
+ run 'rake db:migrate'
208
+ sleep 1
209
+ version = make_migration('yyy')
210
+ result = run("rake db:migrate:up VERSION=#{version}")
211
+ result.should_not =~ /UP-xxx/
212
+ result.should =~ /UP-yyy/
213
+ end
214
+
215
+ it "fails without version" do
216
+ make_migration('yyy')
217
+ lambda{ run("rake db:migrate:up") }.should raise_error(/VERSION/)
218
+ end
219
+ end
220
+
221
+ describe 'db:rollback' do
222
+ it "does nothing when no migrations have been run" do
223
+ run("rake db:version").should =~ /version: 0/
224
+ run("rake db:rollback").should == ''
225
+ run("rake db:version").should =~ /version: 0/
226
+ end
227
+
228
+ it "rolls back the last migration if one has been applied" do
229
+ write_multiple_migrations
230
+ run("rake db:migrate")
231
+ run("rake db:version").should =~ /version: 20100509095816/
232
+ run("rake db:rollback").should =~ /revert/
233
+ run("rake db:version").should =~ /version: 20100509095815/
234
+ end
235
+
236
+ it "rolls back multiple migrations if the STEP argument is given" do
237
+ write_multiple_migrations
238
+ run("rake db:migrate")
239
+ run("rake db:version").should =~ /version: 20100509095816/
240
+ run("rake db:rollback STEP=2") =~ /revert/
241
+ run("rake db:version").should =~ /version: 0/
242
+ end
243
+ end
244
+
245
+ describe 'schema:dump' do
246
+ it "dumps the schema" do
247
+ write('db/schema.rb', '')
248
+ run('rake db:schema:dump')
249
+ read('db/schema.rb').should =~ /ActiveRecord/
250
+ end
251
+ end
252
+
253
+ describe 'db:schema:load' do
254
+ it "loads the schema" do
255
+ run('rake db:schema:dump')
256
+ schema = "db/schema.rb"
257
+ write(schema, read(schema)+"\nputs 'LOADEDDD'")
258
+ result = run('rake db:schema:load')
259
+ result.should =~ /LOADEDDD/
260
+ end
261
+
262
+ it "loads all migrations" do
263
+ make_migration('yyy')
264
+ run "rake db:migrate"
265
+ run "rake db:drop"
266
+ run "rake db:create"
267
+ run "rake db:schema:load"
268
+ run( "rake db:migrate").strip.should == ''
269
+ end
270
+ end
271
+
272
+ describe 'db:abort_if_pending_migrations' do
273
+ it "passes when no migrations are pending" do
274
+ run("rake db:abort_if_pending_migrations").strip.should == ''
275
+ end
276
+
277
+ it "fails when migrations are pending" do
278
+ make_migration('yyy')
279
+ lambda{ run("rake db:abort_if_pending_migrations") }.should raise_error(/1 pending migration/)
280
+ end
281
+ end
282
+
283
+ describe 'db:test:load' do
284
+ it 'loads' do
285
+ write("db/schema.rb", "puts 'LOADEDDD'")
286
+ run("rake db:test:load").should =~ /LOADEDDD/
287
+ end
288
+
289
+ it "fails without schema" do
290
+ lambda{ run("rake db:test:load") }.should raise_error(/try again/)
291
+ end
292
+ end
293
+
294
+ describe 'db:test:purge' do
295
+ it "runs" do
296
+ run('rake db:test:purge')
297
+ end
298
+ end
299
+
300
+ describe "db:seed" do
301
+ it "loads" do
302
+ write("db/seeds.rb", "puts 'LOADEDDD'")
303
+ run("rake db:seed").should =~ /LOADEDDD/
304
+ end
305
+
306
+ it "does nothing without seeds" do
307
+ run("rake db:seed").length.should == 0
308
+ end
309
+ end
310
+
311
+ describe "db:reset" do
312
+ it "should not error when a seeds file does not exist" do
313
+ make_migration('yyy')
314
+ run('rake db:migrate DB=test')
315
+ run("rake db:reset").should_not raise_error(/rake aborted/)
316
+ end
317
+ end
318
+
319
+ describe 'db:migrate when environment is specified' do
320
+ it "runs when using the DB environment variable", :travis_error => true do
321
+ make_migration('yyy')
322
+ run('rake db:migrate RAILS_ENV=test')
323
+ run('rake db:version RAILS_ENV=test').should_not =~ /version: 0/
324
+ run('rake db:version').should =~ /version: 0/
325
+ end
326
+
327
+ it "should error on an invalid database", :travis_error => true do
328
+ lambda{ run("rake db:create RAILS_ENV=nonexistent")}.should raise_error(/rake aborted/)
329
+ end
330
+ end
331
+ end
@@ -0,0 +1,74 @@
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 = "standalone_migrations"
8
+ s.version = "2.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Todd Huss", "Michael Grosser"]
12
+ s.date = "2014-08-22"
13
+ s.email = "thuss@gabrito.com"
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".travis.yml",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "example/.gitignore",
27
+ "example/Rakefile",
28
+ "example/db/config.yml",
29
+ "example/db/migrate/20120930053225_create_table_awesome_migration.rb",
30
+ "lib/standalone_migrations.rb",
31
+ "lib/standalone_migrations/callbacks.rb",
32
+ "lib/standalone_migrations/configurator.rb",
33
+ "lib/standalone_migrations/generator.rb",
34
+ "lib/standalone_migrations/minimal_railtie_config.rb",
35
+ "lib/standalone_migrations/tasks.rb",
36
+ "lib/standalone_migrations/tasks/connection.rake",
37
+ "lib/standalone_migrations/tasks/db/new_migration.rake",
38
+ "lib/standalone_migrations/tasks/environment.rake",
39
+ "lib/tasks/standalone_migrations.rb",
40
+ "spec/spec_helper.rb",
41
+ "spec/standalone_migrations/callbacks_spec.rb",
42
+ "spec/standalone_migrations/configurator_spec.rb",
43
+ "spec/standalone_migrations_spec.rb",
44
+ "standalone_migrations.gemspec",
45
+ "vendor/migration_helpers/MIT-LICENSE",
46
+ "vendor/migration_helpers/README.markdown",
47
+ "vendor/migration_helpers/init.rb",
48
+ "vendor/migration_helpers/lib/migration_helper.rb"
49
+ ]
50
+ s.homepage = "http://github.com/thuss/standalone-migrations"
51
+ s.licenses = ["MIT"]
52
+ s.require_paths = ["lib"]
53
+ s.rubygems_version = "1.8.23.2"
54
+ s.summary = "A thin wrapper to use Rails Migrations in non Rails projects"
55
+
56
+ if s.respond_to? :specification_version then
57
+ s.specification_version = 3
58
+
59
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
+ s.add_runtime_dependency(%q<rake>, ["~> 10.0"])
61
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.2"])
62
+ s.add_runtime_dependency(%q<railties>, ["~> 3.2"])
63
+ else
64
+ s.add_dependency(%q<rake>, ["~> 10.0"])
65
+ s.add_dependency(%q<activerecord>, ["~> 3.2"])
66
+ s.add_dependency(%q<railties>, ["~> 3.2"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<rake>, ["~> 10.0"])
70
+ s.add_dependency(%q<activerecord>, ["~> 3.2"])
71
+ s.add_dependency(%q<railties>, ["~> 3.2"])
72
+ end
73
+ end
74
+
@@ -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.