standalone_migrations 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  schema.rb
2
+ spec/tmp
@@ -71,4 +71,5 @@ This work is based on [Lincoln Stoll's blog post](http://lstoll.net/2008/04/stan
71
71
 
72
72
  - [Todd Huss](http://gabrito.com/)
73
73
  - [Michael Grosser](http://pragmatig.wordpress.com)
74
+ - [Eric Lindvall](http://bitmonkey.net)
74
75
  - [Steve Hodgkiss](http://stevehodgkiss.com/)`s [activerecord-migrator-standalone](http://github.com/stevehodgkiss/activerecord-migrator-standalone)
data/Rakefile CHANGED
@@ -1,3 +1,7 @@
1
+ task :default => :spec
2
+ require 'spec/rake/spectask'
3
+ Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
4
+
1
5
  # rake install -> install gem locally (for tests)
2
6
  # rake release -> push to github and release to gemcutter
3
7
  # rake version:bump:patch -> increase version and add a git-tag
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
@@ -1,4 +1,4 @@
1
- # Every important should be overwriteable with MIGRATION_OPTIONS
1
+ # Every important options should be overwriteable with MIGRATION_OPTIONS
2
2
  base = File.expand_path('.')
3
3
  here = File.expand_path(File.dirname(File.dirname(File.dirname((__FILE__)))))
4
4
 
@@ -18,10 +18,13 @@ Dir["#{options[:vendor]}/**/lib"].each{|p| $LOAD_PATH << p }
18
18
 
19
19
  namespace :db do
20
20
  task :ar_init do
21
+ require 'logger'
21
22
  require 'active_record'
22
23
  ENV[options[:env]] ||= options[:default_env]
23
- config = YAML.load_file(options[:config])[ENV[options[:env]]]
24
- ActiveRecord::Base.establish_connection(config)
24
+
25
+ require 'erb'
26
+ ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(options[:config])).result)
27
+ ActiveRecord::Base.establish_connection(ENV[options[:env]])
25
28
  logger = Logger.new $stderr
26
29
  logger.level = Logger::INFO
27
30
  ActiveRecord::Base.logger = logger
@@ -39,13 +42,26 @@ namespace :db do
39
42
  [:up, :down].each do |direction|
40
43
  desc "Runs the '#{direction}' for a given migration VERSION."
41
44
  task direction => :ar_init do
42
- version = (ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
43
- raise "VERSION is required" unless version
45
+ version = ENV["VERSION"].to_i
46
+ raise "VERSION is required (must be a number)" if version == 0
44
47
  ActiveRecord::Migrator.run(direction, options[:migrations], version)
45
48
  Rake::Task["db:schema:dump"].execute
46
49
  end
47
50
  end
48
51
  end
52
+
53
+ desc "Raises an error if there are pending migrations"
54
+ task :abort_if_pending_migrations => :ar_init do
55
+ pending_migrations = ActiveRecord::Migrator.new(:up, options[:migrations]).pending_migrations
56
+
57
+ if pending_migrations.any?
58
+ puts "You have #{pending_migrations.size} pending migrations:"
59
+ pending_migrations.each do |pending_migration|
60
+ puts ' %4d %s' % [pending_migration.version, pending_migration.name]
61
+ end
62
+ abort %{Run "rake db:migrate" to update your database then try again.}
63
+ end
64
+ end
49
65
 
50
66
  namespace :schema do
51
67
  desc "Create schema.rb file that can be portably used against any DB supported by AR"
@@ -63,12 +79,55 @@ namespace :db do
63
79
  end
64
80
  end
65
81
 
82
+ namespace :test do
83
+ desc "Recreate the test database from the current schema.rb"
84
+ task :load => ['db:ar_init', 'db:test:purge'] do
85
+ ActiveRecord::Base.establish_connection(:test)
86
+ ActiveRecord::Schema.verbose = false
87
+ Rake::Task["db:schema:load"].invoke
88
+ end
89
+
90
+ desc "Empty the test database"
91
+ task :purge => 'db:ar_init' do
92
+ config = ActiveRecord::Base.configurations['test']
93
+ case config["adapter"]
94
+ when "mysql"
95
+ ActiveRecord::Base.establish_connection(:test)
96
+ ActiveRecord::Base.connection.recreate_database(config["database"], config)
97
+ when "postgresql" #TODO i doubt this will work <-> methods are not defined
98
+ ActiveRecord::Base.clear_active_connections!
99
+ drop_database(config)
100
+ create_database(config)
101
+ when "sqlite", "sqlite3"
102
+ db_file = config["database"] || config["dbfile"]
103
+ File.delete(db_file) if File.exist?(db_file)
104
+ when "sqlserver"
105
+ drop_script = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
106
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{drop_script}`
107
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\test_structure.sql`
108
+ when "oci", "oracle"
109
+ ActiveRecord::Base.establish_connection(:test)
110
+ ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
111
+ ActiveRecord::Base.connection.execute(ddl)
112
+ end
113
+ when "firebird"
114
+ ActiveRecord::Base.establish_connection(:test)
115
+ ActiveRecord::Base.connection.recreate_database!
116
+ else
117
+ raise "Task not supported by #{config["adapter"].inspect}"
118
+ end
119
+ end
120
+
121
+ desc 'Check for pending migrations and load the test schema'
122
+ task :prepare => ['db:abort_if_pending_migrations', 'db:test:load']
123
+ end
124
+
66
125
  desc "Create a new migration"
67
126
  task :new_migration do |t|
68
127
  unless migration = ENV['name']
69
128
  puts "Error: must provide name of migration to generate."
70
129
  puts "For example: rake #{t.name} name=add_field_to_form"
71
- exit 1
130
+ abort
72
131
  end
73
132
 
74
133
  class_name = migration.split('_').map{|s| s.capitalize }.join
@@ -0,0 +1,168 @@
1
+ describe 'Standalone migrations' do
2
+ def write(file, content)
3
+ raise "cannot write nil" unless file
4
+ file = tmp_file(file)
5
+ folder = File.dirname(file)
6
+ `mkdir -p #{folder}` unless File.exist?(folder)
7
+ File.open(file,'w'){|f| f.write content}
8
+ end
9
+
10
+ def read(file)
11
+ File.read(tmp_file(file))
12
+ end
13
+
14
+ def migration(name)
15
+ m = `cd spec/tmp/db/migrations && ls`.split("\n").detect{|m| m =~ name}
16
+ m ? "db/migrations/#{m}" : m
17
+ end
18
+
19
+ def tmp_file(file)
20
+ "spec/tmp/#{file}"
21
+ end
22
+
23
+ def run(cmd)
24
+ `cd spec/tmp && #{cmd} 2>&1 && echo SUCCESS`
25
+ end
26
+
27
+ def make_migration(name)
28
+ migration = run("rake db:new_migration name=#{name}").match(%r{db/migrations/\d+.*.rb})[0]
29
+ content = read(migration)
30
+ content.sub!(/def self.down.*?\send/m, "def self.down;puts 'DOWN-#{name}';end")
31
+ content.sub!(/def self.up.*?\send/m, "def self.up;puts 'UP-#{name}';end")
32
+ write(migration, content)
33
+ migration.match(/\d{14}/)[0]
34
+ end
35
+
36
+ before do
37
+ `rm -rf spec/tmp` if File.exist?('spec/tmp')
38
+ `mkdir spec/tmp`
39
+ write 'Rakefile', <<-TXT
40
+ $LOAD_PATH.unshift '#{File.expand_path('lib')}'
41
+ begin
42
+ require 'tasks/standalone_migrations'
43
+ rescue LoadError => e
44
+ puts "gem install standalone_migrations to get db:migrate:* tasks! (Error: \#{e})"
45
+ end
46
+ TXT
47
+ write 'db/config.yml', <<-TXT
48
+ development:
49
+ adapter: sqlite3
50
+ database: db/development.sql
51
+ test:
52
+ adapter: sqlite3
53
+ database: db/test.sql
54
+ TXT
55
+ end
56
+
57
+ describe 'db:new_migration' do
58
+ it "fails if i do not add a name" do
59
+ run("rake db:new_migration").should_not =~ /SUCCESS/
60
+ end
61
+
62
+ it "generates a new migration with this name and timestamp" do
63
+ run("rake db:new_migration name=test_abc").should =~ %r{Created migration .*spec/tmp/db/migrations/\d+_test_abc\.rb}
64
+ run("ls db/migrations").should =~ /^\d+_test_abc.rb$/
65
+ end
66
+ end
67
+
68
+ describe 'db:migrate' do
69
+ it "does nothing when no migrations are present" do
70
+ run("rake db:migrate").should =~ /SUCCESS/
71
+ end
72
+
73
+ it "migrates if i add a migration" do
74
+ run("rake db:new_migration name=xxx")
75
+ result = run("rake db:migrate")
76
+ result.should =~ /SUCCESS/
77
+ result.should =~ /Migrating to Xxx \(#{Time.now.year}/
78
+ end
79
+ end
80
+
81
+ describe 'db:migrate:down' do
82
+ it "migrates down" do
83
+ make_migration('xxx')
84
+ sleep 1
85
+ version = make_migration('yyy')
86
+ run 'rake db:migrate'
87
+
88
+ result = run("rake db:migrate:down VERSION=#{version}")
89
+ result.should =~ /SUCCESS/
90
+ result.should_not =~ /DOWN-xxx/
91
+ result.should =~ /DOWN-yyy/
92
+ end
93
+
94
+ it "fails without version" do
95
+ make_migration('yyy')
96
+ result = run("rake db:migrate:down")
97
+ result.should_not =~ /SUCCESS/
98
+ end
99
+ end
100
+
101
+ describe 'db:migrate:up' do
102
+ it "migrates up" do
103
+ make_migration('xxx')
104
+ run 'rake db:migrate'
105
+ sleep 1
106
+ version = make_migration('yyy')
107
+ result = run("rake db:migrate:up VERSION=#{version}")
108
+ result.should =~ /SUCCESS/
109
+ result.should_not =~ /UP-xxx/
110
+ result.should =~ /UP-yyy/
111
+ end
112
+
113
+ it "fails without version" do
114
+ make_migration('yyy')
115
+ result = run("rake db:migrate:up")
116
+ result.should_not =~ /SUCCESS/
117
+ end
118
+ end
119
+
120
+ describe 'schema:dump' do
121
+ it "dumps the schema" do
122
+ result = run('rake db:schema:dump')
123
+ result.should =~ /SUCCESS/
124
+ read('db/schema.rb').should =~ /ActiveRecord/
125
+ end
126
+ end
127
+
128
+ describe 'db:schema:load' do
129
+ it "loads the schema" do
130
+ run('rake db:schema:dump')
131
+ schema = "db/schema.rb"
132
+ write(schema, read(schema)+"\nputs 'LOADEDDD'")
133
+ result = run('rake db:schema:load')
134
+ result.should =~ /SUCCESS/
135
+ result.should =~ /LOADEDDD/
136
+ end
137
+ end
138
+
139
+ describe 'db:abort_if_pending_migrations' do
140
+ it "passes when no migrations are pending" do
141
+ run("rake db:abort_if_pending_migrations").should_not =~ /try again/
142
+ end
143
+
144
+ it "fails when migrations are pending" do
145
+ make_migration('yyy')
146
+ result = run("rake db:abort_if_pending_migrations")
147
+ result.should =~ /try again/
148
+ result.should =~ /1 pending migration/
149
+ end
150
+ end
151
+
152
+ describe 'db:test:load' do
153
+ it 'loads' do
154
+ write("db/schema.rb", "puts 'LOADEDDD'")
155
+ run("rake db:test:load").should =~ /LOADEDDD.*SUCCESS/m
156
+ end
157
+
158
+ it "fails without schema" do
159
+ run("rake db:test:load").should =~ /no such file to load/
160
+ end
161
+ end
162
+
163
+ describe 'db:test:purge' do
164
+ it "runs" do
165
+ run('rake db:test:purge').should =~ /SUCCESS/
166
+ end
167
+ end
168
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{standalone_migrations}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
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{2010-03-31}
12
+ s.date = %q{2010-04-12}
13
13
  s.email = %q{thuss@gabrito.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  "Rakefile",
21
21
  "VERSION",
22
22
  "lib/tasks/standalone_migrations.rb",
23
+ "spec/standalone_migrations_spec.rb",
23
24
  "standalone_migrations.gemspec",
24
25
  "vendor/migration_helpers/MIT-LICENSE",
25
26
  "vendor/migration_helpers/README.markdown",
@@ -31,6 +32,9 @@ Gem::Specification.new do |s|
31
32
  s.require_paths = ["lib"]
32
33
  s.rubygems_version = %q{1.3.6}
33
34
  s.summary = %q{A thin wrapper to use Rails Migrations in non Rails projects}
35
+ s.test_files = [
36
+ "spec/standalone_migrations_spec.rb"
37
+ ]
34
38
 
35
39
  if s.respond_to? :specification_version then
36
40
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 4
9
- version: 0.2.4
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Todd Huss
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-31 00:00:00 +02:00
18
+ date: 2010-04-12 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,6 +56,7 @@ files:
56
56
  - Rakefile
57
57
  - VERSION
58
58
  - lib/tasks/standalone_migrations.rb
59
+ - spec/standalone_migrations_spec.rb
59
60
  - standalone_migrations.gemspec
60
61
  - vendor/migration_helpers/MIT-LICENSE
61
62
  - vendor/migration_helpers/README.markdown
@@ -91,5 +92,5 @@ rubygems_version: 1.3.6
91
92
  signing_key:
92
93
  specification_version: 3
93
94
  summary: A thin wrapper to use Rails Migrations in non Rails projects
94
- test_files: []
95
-
95
+ test_files:
96
+ - spec/standalone_migrations_spec.rb