standalone_migrations 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -5,7 +5,7 @@ require 'logger'
5
5
  class MigratorTasks < ::Rake::TaskLib
6
6
  attr_accessor :name, :base, :vendor, :config, :schema, :env, :default_env, :verbose, :log_level, :logger
7
7
  attr_reader :migrations
8
-
8
+
9
9
  def initialize(name = :migrator)
10
10
  @name = name
11
11
  base = File.expand_path('.')
@@ -21,14 +21,14 @@ class MigratorTasks < ::Rake::TaskLib
21
21
  @log_level = Logger::ERROR
22
22
  yield self if block_given?
23
23
  # Add to load_path every "lib/" directory in vendor
24
- Dir["#{vendor}/**/lib"].each{|p| $LOAD_PATH << p }
24
+ Dir["#{vendor}/**/lib"].each { |p| $LOAD_PATH << p }
25
25
  define
26
26
  end
27
-
27
+
28
28
  def migrations=(*value)
29
29
  @migrations = value.flatten
30
30
  end
31
-
31
+
32
32
  def define
33
33
  namespace :db do
34
34
  task :ar_init do
@@ -39,7 +39,6 @@ class MigratorTasks < ::Rake::TaskLib
39
39
  ActiveRecord::Base.configurations = @config
40
40
  else
41
41
  require 'erb'
42
-
43
42
  ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(@config)).result)
44
43
  end
45
44
  ActiveRecord::Base.establish_connection(ENV[@env])
@@ -53,7 +52,7 @@ class MigratorTasks < ::Rake::TaskLib
53
52
  end
54
53
 
55
54
  desc "Migrate the database using the scripts in the migrations directory. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
56
- task :migrate => :ar_init do
55
+ task :migrate => :ar_init do
57
56
  require "#{@vendor}/migration_helpers/init"
58
57
  ActiveRecord::Migration.verbose = ENV['VERBOSE'] || @verbose
59
58
  @migrations.each do |path|
@@ -67,45 +66,98 @@ class MigratorTasks < ::Rake::TaskLib
67
66
  puts "Current version: #{ActiveRecord::Migrator.current_version}"
68
67
  end
69
68
 
70
- def create_database config
71
- options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
72
-
73
- create_db = lambda do |config|
74
- ActiveRecord::Base.establish_connection config.merge('database' => nil)
75
- ActiveRecord::Base.connection.create_database config['database'], options
76
- ActiveRecord::Base.establish_connection config
77
- end
78
-
69
+ def create_database(config)
79
70
  begin
80
- create_db.call config
81
- rescue Mysql::Error => sqlerr
82
- if sqlerr.errno == 1405
83
- print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
84
- root_password = $stdin.gets.strip
85
-
86
- grant_statement = <<-SQL
87
- GRANT ALL PRIVILEGES ON #{config['database']}.*
88
- TO '#{config['username']}'@'localhost'
89
- IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
90
- SQL
91
-
92
- create_db.call config.merge('database' => nil, 'username' => 'root', 'password' => root_password)
71
+ if config['adapter'] =~ /sqlite/
72
+ if File.exist?(config['database'])
73
+ $stderr.puts "#{config['database']} already exists"
74
+ else
75
+ begin
76
+ # Create the SQLite database
77
+ ActiveRecord::Base.establish_connection(config)
78
+ ActiveRecord::Base.connection
79
+ rescue Exception => e
80
+ $stderr.puts e, *(e.backtrace)
81
+ $stderr.puts "Couldn't create database for #{config.inspect}"
82
+ end
83
+ end
84
+ return # Skip the else clause of begin/rescue
93
85
  else
94
- $stderr.puts sqlerr.error
95
- $stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
96
- $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
86
+ ActiveRecord::Base.establish_connection(config)
87
+ ActiveRecord::Base.connection
88
+ end
89
+ rescue
90
+ case config['adapter']
91
+ when /mysql/
92
+ @charset = ENV['CHARSET'] || 'utf8'
93
+ @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
94
+ creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
95
+ error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
96
+ access_denied_error = 1045
97
+ begin
98
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil))
99
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
100
+ ActiveRecord::Base.establish_connection(config)
101
+ rescue error_class => sqlerr
102
+ if sqlerr.errno == access_denied_error
103
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
104
+ root_password = $stdin.gets.strip
105
+ grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
106
+ "TO '#{config['username']}'@'localhost' " \
107
+ "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
108
+ ActiveRecord::Base.establish_connection(config.merge(
109
+ 'database' => nil, 'username' => 'root', 'password' => root_password))
110
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
111
+ ActiveRecord::Base.connection.execute grant_statement
112
+ ActiveRecord::Base.establish_connection(config)
113
+ else
114
+ $stderr.puts sqlerr.error
115
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
116
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
117
+ end
118
+ end
119
+ when 'postgresql'
120
+ @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
121
+ begin
122
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
123
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
124
+ ActiveRecord::Base.establish_connection(config)
125
+ rescue Exception => e
126
+ $stderr.puts e, *(e.backtrace)
127
+ $stderr.puts "Couldn't create database for #{config.inspect}"
128
+ end
97
129
  end
130
+ else
131
+ $stderr.puts "#{config['database']} already exists"
98
132
  end
99
133
  end
100
134
 
101
135
  desc 'Create the database from config/database.yml for the current DATABASE_ENV'
102
136
  task :create => :ar_init do
103
- create_database ActiveRecord::Base.configurations[self.default_env]
137
+ config = ActiveRecord::Base.configurations[self.default_env]
138
+ create_database config
139
+ end
140
+
141
+ def drop_database(config)
142
+ case config['adapter']
143
+ when /mysql/
144
+ ActiveRecord::Base.establish_connection(config)
145
+ ActiveRecord::Base.connection.drop_database config['database']
146
+ when /^sqlite/
147
+ require 'pathname'
148
+ path = Pathname.new(config['database'])
149
+ file = path.absolute? ? path.to_s : File.join(@base, path)
150
+ FileUtils.rm(file)
151
+ when 'postgresql'
152
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
153
+ ActiveRecord::Base.connection.drop_database config['database']
154
+ end
104
155
  end
105
156
 
106
157
  desc 'Drops the database for the current DATABASE_ENV'
107
158
  task :drop => :ar_init do
108
- ActiveRecord::Base.connection.drop_database ActiveRecord::Base.configurations[self.default_env]['database']
159
+ config = ActiveRecord::Base.configurations[self.default_env]
160
+ drop_database(config)
109
161
  end
110
162
 
111
163
  namespace :migrate do
@@ -134,12 +186,12 @@ class MigratorTasks < ::Rake::TaskLib
134
186
  end
135
187
  end
136
188
  end
137
-
189
+
138
190
  desc "Raises an error if there are pending migrations"
139
191
  task :abort_if_pending_migrations => :ar_init do
140
192
  @migrations.each do |path|
141
193
  pending_migrations = ActiveRecord::Migrator.new(:up, path).pending_migrations
142
-
194
+
143
195
  if pending_migrations.any?
144
196
  puts "You have #{pending_migrations.size} pending migrations:"
145
197
  pending_migrations.each do |pending_migration|
@@ -180,33 +232,33 @@ class MigratorTasks < ::Rake::TaskLib
180
232
  task :purge => 'db:ar_init' do
181
233
  config = ActiveRecord::Base.configurations['test']
182
234
  case config["adapter"]
183
- when "mysql"
184
- ActiveRecord::Base.establish_connection(:test)
185
- ActiveRecord::Base.connection.recreate_database(config["database"], config)
186
- when "postgresql" #TODO i doubt this will work <-> methods are not defined
187
- ActiveRecord::Base.clear_active_connections!
188
- drop_database(config)
189
- create_database(config)
190
- when "sqlite", "sqlite3"
191
- db_file = config["database"] || config["dbfile"]
192
- File.delete(db_file) if File.exist?(db_file)
193
- when "sqlserver"
194
- drop_script = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/,'-')
195
- `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{drop_script}`
196
- `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\test_structure.sql`
197
- when "oci", "oracle"
198
- ActiveRecord::Base.establish_connection(:test)
199
- ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
200
- ActiveRecord::Base.connection.execute(ddl)
201
- end
202
- when "firebird"
203
- ActiveRecord::Base.establish_connection(:test)
204
- ActiveRecord::Base.connection.recreate_database!
205
- else
206
- raise "Task not supported by #{config["adapter"].inspect}"
235
+ when "mysql"
236
+ ActiveRecord::Base.establish_connection(:test)
237
+ ActiveRecord::Base.connection.recreate_database(config["database"], config)
238
+ when "postgresql" #TODO i doubt this will work <-> methods are not defined
239
+ ActiveRecord::Base.clear_active_connections!
240
+ drop_database(config)
241
+ create_database(config)
242
+ when "sqlite", "sqlite3"
243
+ db_file = config["database"] || config["dbfile"]
244
+ File.delete(db_file) if File.exist?(db_file)
245
+ when "sqlserver"
246
+ drop_script = "#{config["host"]}.#{config["database"]}.DP1".gsub(/\\/, '-')
247
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\#{drop_script}`
248
+ `osql -E -S #{config["host"]} -d #{config["database"]} -i db\\test_structure.sql`
249
+ when "oci", "oracle"
250
+ ActiveRecord::Base.establish_connection(:test)
251
+ ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
252
+ ActiveRecord::Base.connection.execute(ddl)
253
+ end
254
+ when "firebird"
255
+ ActiveRecord::Base.establish_connection(:test)
256
+ ActiveRecord::Base.connection.recreate_database!
257
+ else
258
+ raise "Task not supported by #{config["adapter"].inspect}"
207
259
  end
208
260
  end
209
-
261
+
210
262
  desc 'Check for pending migrations and load the test schema'
211
263
  task :prepare => ['db:abort_if_pending_migrations', 'db:test:load']
212
264
  end
@@ -214,7 +266,7 @@ class MigratorTasks < ::Rake::TaskLib
214
266
  desc 'generate a model=name field="field1:type field2:type"'
215
267
  task :generate do
216
268
  ts = Time.now.strftime '%Y%m%d%H%%M%S'
217
-
269
+
218
270
  if ENV['model']
219
271
  table_name = ENV['model']
220
272
  else
@@ -228,7 +280,7 @@ class MigratorTasks < ::Rake::TaskLib
228
280
 
229
281
  fields = ENV['fields'] if ENV['fields']
230
282
 
231
- columns = ENV.has_key?('fields') ? ENV['fields'].split.map {|v| "t.#{v.sub(/:/, ' :')}"}.join("\n#{' '*6}") : nil
283
+ columns = ENV.has_key?('fields') ? ENV['fields'].split.map { |v| "t.#{v.sub(/:/, ' :')}" }.join("\n#{' '*6}") : nil
232
284
 
233
285
  create_table_str << "\n #{columns}" if columns
234
286
 
@@ -244,9 +296,9 @@ class Create#{class_name table_name} < ActiveRecord::Migration
244
296
  drop_table :#{table_name}
245
297
  end
246
298
  end
247
- MIGRATION
299
+ MIGRATION
248
300
 
249
- create_file @migrations.first, file_name("create_#{table_name}"), contents
301
+ create_file @migrations.first, file_name("create_#{table_name}"), contents
250
302
  end
251
303
 
252
304
  desc "Create a new migration"
@@ -276,12 +328,12 @@ eof
276
328
  end
277
329
 
278
330
  def class_name str
279
- str.split('_').map {|s| s.capitalize}.join
331
+ str.split('_').map { |s| s.capitalize }.join
280
332
  end
281
333
 
282
334
  def create_file path, file, contents
283
335
  FileUtils.mkdir_p path unless File.exists? path
284
- File.open(file, 'w') {|f| f.write contents}
336
+ File.open(file, 'w') { |f| f.write contents }
285
337
  end
286
338
 
287
339
  def file_name migration
@@ -4,7 +4,7 @@ describe 'Standalone migrations' do
4
4
  file = tmp_file(file)
5
5
  folder = File.dirname(file)
6
6
  `mkdir -p #{folder}` unless File.exist?(folder)
7
- File.open(file,'w'){|f| f.write content}
7
+ File.open(file, 'w') { |f| f.write content }
8
8
  end
9
9
 
10
10
  def read(file)
@@ -12,7 +12,7 @@ describe 'Standalone migrations' do
12
12
  end
13
13
 
14
14
  def migration(name)
15
- m = `cd spec/tmp/db/migrations && ls`.split("\n").detect{|m| m =~ name}
15
+ m = `cd spec/tmp/db/migrations && ls`.split("\n").detect { |m| m =~ name }
16
16
  m ? "db/migrations/#{m}" : m
17
17
  end
18
18
 
@@ -32,7 +32,7 @@ describe 'Standalone migrations' do
32
32
  write(migration, content)
33
33
  migration.match(/\d{14}/)[0]
34
34
  end
35
-
35
+
36
36
  def write_rakefile(config=nil)
37
37
  write 'Rakefile', <<-TXT
38
38
  $LOAD_PATH.unshift '#{File.expand_path('lib')}'
@@ -47,7 +47,7 @@ describe 'Standalone migrations' do
47
47
  end
48
48
  TXT
49
49
  end
50
-
50
+
51
51
  def write_multiple_migrations
52
52
  write_rakefile %{t.migrations = "db/migrations", "db/migrations2"}
53
53
  write "db/migrations/20100509095815_create_tests.rb", <<-TXT
@@ -60,7 +60,7 @@ def self.down
60
60
  puts "DOWN-CreateTests"
61
61
  end
62
62
  end
63
- TXT
63
+ TXT
64
64
  write "db/migrations2/20100509095816_create_tests2.rb", <<-TXT
65
65
  class CreateTests2 < ActiveRecord::Migration
66
66
  def self.up
@@ -71,7 +71,7 @@ def self.down
71
71
  puts "DOWN-CreateTests2"
72
72
  end
73
73
  end
74
- TXT
74
+ TXT
75
75
  end
76
76
 
77
77
  before do
@@ -88,6 +88,13 @@ TXT
88
88
  TXT
89
89
  end
90
90
 
91
+ describe 'db:create and drop' do
92
+ it "should create the database and drop the database that was created" do
93
+ run("rake db:create").should =~ /SUCCESS/
94
+ run("rake db:drop").should =~ /SUCCESS/
95
+ end
96
+ end
97
+
91
98
  describe 'db:new_migration' do
92
99
  context "single migration path" do
93
100
  it "fails if i do not add a name" do
@@ -99,7 +106,7 @@ TXT
99
106
  run("ls db/migrations").should =~ /^\d+_test_abc.rb$/
100
107
  end
101
108
  end
102
-
109
+
103
110
  context "multiple migration paths" do
104
111
  before do
105
112
  write_rakefile %{t.migrations = "db/migrations", "db/migrations2"}
@@ -129,7 +136,7 @@ TXT
129
136
  result.should =~ /Migrating to Xxx \(#{Time.now.year}/
130
137
  end
131
138
  end
132
-
139
+
133
140
  context "multiple migration paths" do
134
141
  before do
135
142
  write_multiple_migrations
@@ -162,19 +169,19 @@ TXT
162
169
  result.should_not =~ /SUCCESS/
163
170
  end
164
171
  end
165
-
172
+
166
173
  context "multiple migration paths" do
167
174
  before do
168
175
  write_multiple_migrations
169
176
  end
170
-
177
+
171
178
  it "runs down on the correct path" do
172
179
  run 'rake db:migrate'
173
180
  result = run 'rake db:migrate:down VERSION=20100509095815'
174
181
  result.should =~ /DOWN-CreateTests/
175
182
  result.should_not =~ /DOWN-CreateTests2/
176
183
  end
177
-
184
+
178
185
  it "fails if migration number isn't found" do
179
186
  run 'rake db:migrate'
180
187
  result = run 'rake db:migrate:down VERSION=20100509095820'
@@ -203,18 +210,18 @@ TXT
203
210
  result.should_not =~ /SUCCESS/
204
211
  end
205
212
  end
206
-
213
+
207
214
  context "multiple migration paths" do
208
215
  before do
209
216
  write_multiple_migrations
210
217
  end
211
-
218
+
212
219
  it "runs down on the correct path" do
213
220
  result = run 'rake db:migrate:up VERSION=20100509095815'
214
221
  result.should =~ /UP-CreateTests/
215
222
  result.should_not =~ /UP-CreateTests2/
216
223
  end
217
-
224
+
218
225
  it "fails if migration number isn't found" do
219
226
  result = run 'rake db:migrate:up VERSION=20100509095820'
220
227
  result.should_not =~ /SUCCESS/
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{standalone_migrations}
8
- s.version = "0.4.1"
8
+ s.version = "0.4.2"
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-02-16}
12
+ s.date = %q{2011-02-21}
13
13
  s.email = %q{thuss@gabrito.com}
14
14
  s.extra_rdoc_files = [
15
15
  "README.markdown"
@@ -28,14 +28,13 @@ Gem::Specification.new do |s|
28
28
  ]
29
29
  s.homepage = %q{http://github.com/thuss/standalone-migrations}
30
30
  s.require_paths = ["lib"]
31
- s.rubygems_version = %q{1.3.7}
31
+ s.rubygems_version = %q{1.5.2}
32
32
  s.summary = %q{A thin wrapper to use Rails Migrations in non Rails projects}
33
33
  s.test_files = [
34
34
  "spec/standalone_migrations_spec.rb"
35
35
  ]
36
36
 
37
37
  if s.respond_to? :specification_version then
38
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
38
  s.specification_version = 3
40
39
 
41
40
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standalone_migrations
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 4
8
- - 1
9
- version: 0.4.1
4
+ prerelease:
5
+ version: 0.4.2
10
6
  platform: ruby
11
7
  authors:
12
8
  - Todd Huss
@@ -15,7 +11,7 @@ autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
13
 
18
- date: 2011-02-16 00:00:00 -08:00
14
+ date: 2011-02-21 00:00:00 -08:00
19
15
  default_executable:
20
16
  dependencies:
21
17
  - !ruby/object:Gem::Dependency
@@ -26,8 +22,6 @@ dependencies:
26
22
  requirements:
27
23
  - - ">="
28
24
  - !ruby/object:Gem::Version
29
- segments:
30
- - 0
31
25
  version: "0"
32
26
  type: :runtime
33
27
  version_requirements: *id001
@@ -39,8 +33,6 @@ dependencies:
39
33
  requirements:
40
34
  - - ">="
41
35
  - !ruby/object:Gem::Version
42
- segments:
43
- - 0
44
36
  version: "0"
45
37
  type: :runtime
46
38
  version_requirements: *id002
@@ -77,21 +69,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
77
69
  requirements:
78
70
  - - ">="
79
71
  - !ruby/object:Gem::Version
80
- segments:
81
- - 0
82
72
  version: "0"
83
73
  required_rubygems_version: !ruby/object:Gem::Requirement
84
74
  none: false
85
75
  requirements:
86
76
  - - ">="
87
77
  - !ruby/object:Gem::Version
88
- segments:
89
- - 0
90
78
  version: "0"
91
79
  requirements: []
92
80
 
93
81
  rubyforge_project:
94
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.5.2
95
83
  signing_key:
96
84
  specification_version: 3
97
85
  summary: A thin wrapper to use Rails Migrations in non Rails projects