standalone_migrations 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -23,6 +23,12 @@ Add to `Rakefile` in your projects base directory:
23
23
 
24
24
  Add database configuration to `db/config.yml` in your projects base directory e.g.:
25
25
  development:
26
+ adapter: sqlite3
27
+ database: db/development.sqlite3
28
+ pool: 5
29
+ timeout: 5000
30
+
31
+ production:
26
32
  adapter: mysql
27
33
  encoding: utf8
28
34
  reconnect: false
@@ -32,8 +38,11 @@ Add database configuration to `db/config.yml` in your projects base directory e.
32
38
  password:
33
39
  socket: /var/run/mysqld/mysqld.sock
34
40
 
35
- test:
36
- ...something similar...
41
+ test: &test
42
+ adapter: sqlite3
43
+ database: db/test.sqlite3
44
+ pool: 5
45
+ timeout: 5000
37
46
 
38
47
  ### To create a new database migration:
39
48
 
@@ -62,18 +71,12 @@ If you're lazy and want to just execute raw SQL:
62
71
 
63
72
  ### To migrate a specific database (for example your "testing" database)
64
73
 
65
- rake db:migrate RAILS_ENV=test
74
+ rake db:migrate DB=test
66
75
 
67
76
  ### To execute a specific up/down of one single migration
68
77
 
69
78
  rake db:migrate:up VERSION=20081220234130
70
79
 
71
- ### To overwrite the default options
72
- [Possible options](http://github.com/thuss/standalone-migrations/blob/master/tasks/standalone_migrations.rake)
73
- MIGRATION_OPTIONS = {:config=>'database/config.yml', ... }
74
- gem 'standalone_migrations'
75
- require 'tasks/standalone_migrations'
76
-
77
80
  Contributors
78
81
  ============
79
82
  This work is based on [Lincoln Stoll's blog post](http://lstoll.net/2008/04/stand-alone-activerecord-migrations/) and [David Welton's post](http://journal.dedasys.com/2007/01/28/using-migrations-outside-of-rails).
@@ -81,4 +84,6 @@ This work is based on [Lincoln Stoll's blog post](http://lstoll.net/2008/04/stan
81
84
  - [Todd Huss](http://gabrito.com/)
82
85
  - [Michael Grosser](http://pragmatig.wordpress.com)
83
86
  - [Eric Lindvall](http://bitmonkey.net)
84
- - [Steve Hodgkiss](http://stevehodgkiss.com/)`s [activerecord-migrator-standalone](http://github.com/stevehodgkiss/activerecord-migrator-standalone)
87
+ - [Steve Hodgkiss](http://stevehodgkiss.com/)
88
+ - [Rich Meyers](https://github.com/richmeyers)
89
+ - [Wes Bailey](http://exposinggotchas.blogspot.com/)
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  task :default => :spec
2
- require 'spec/rake/spectask'
3
- Spec::Rake::SpecTask.new {|t| t.spec_opts = ['--color']}
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new {|t| t.rspec_opts = ['--color']}
4
4
 
5
5
  # rake install -> install gem locally (for tests)
6
6
  # rake release -> push to github and release to gemcutter
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -3,7 +3,7 @@ require 'rake/tasklib'
3
3
  require 'logger'
4
4
 
5
5
  class MigratorTasks < ::Rake::TaskLib
6
- attr_accessor :name, :base, :vendor, :config, :schema, :env, :default_env, :verbose, :log_level
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)
@@ -36,10 +36,19 @@ class MigratorTasks < ::Rake::TaskLib
36
36
  ENV[@env] ||= @default_env
37
37
 
38
38
  require 'erb'
39
- ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(@config)).result)
39
+
40
+ if @config.is_a?(Hash)
41
+ ActiveRecord::Base.configurations = @config
42
+ else
43
+ ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(@config)).result)
44
+ end
40
45
  ActiveRecord::Base.establish_connection(ENV[@env])
41
- logger = Logger.new $stderr
42
- logger.level = @log_level
46
+ if @logger
47
+ logger = @logger
48
+ else
49
+ logger = Logger.new($stderr)
50
+ logger.level = @log_level
51
+ end
43
52
  ActiveRecord::Base.logger = logger
44
53
  end
45
54
 
@@ -53,6 +62,52 @@ class MigratorTasks < ::Rake::TaskLib
53
62
  Rake::Task["db:schema:dump"].execute
54
63
  end
55
64
 
65
+ desc "Retrieves the current schema version number"
66
+ task :version => :ar_init do
67
+ puts "Current version: #{ActiveRecord::Migrator.current_version}"
68
+ end
69
+
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
+
79
+ 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)
93
+ 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']
97
+ end
98
+ end
99
+ end
100
+
101
+ desc 'Create the database from config/database.yml for the current DATABASE_ENV'
102
+ task :create => :ar_init do
103
+ create_database ActiveRecord::Base.configurations[self.default_env]
104
+ end
105
+
106
+ desc 'Drops the database for the current DATABASE_ENV'
107
+ task :drop => :ar_init do
108
+ ActiveRecord::Base.connection.drop_database ActiveRecord::Base.configurations[self.default_env]['database']
109
+ end
110
+
56
111
  namespace :migrate do
57
112
  [:up, :down].each do |direction|
58
113
  desc "Runs the '#{direction}' for a given migration VERSION."
@@ -98,9 +153,11 @@ class MigratorTasks < ::Rake::TaskLib
98
153
  namespace :schema do
99
154
  desc "Create schema.rb file that can be portably used against any DB supported by AR"
100
155
  task :dump => :ar_init do
101
- require 'active_record/schema_dumper'
102
- File.open(ENV['SCHEMA'] || @schema, "w") do |file|
103
- ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
156
+ if schema_file = ENV['SCHEMA'] || @schema
157
+ require 'active_record/schema_dumper'
158
+ File.open(schema_file, "w") do |file|
159
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
160
+ end
104
161
  end
105
162
  end
106
163
 
@@ -154,6 +211,44 @@ class MigratorTasks < ::Rake::TaskLib
154
211
  task :prepare => ['db:abort_if_pending_migrations', 'db:test:load']
155
212
  end
156
213
 
214
+ desc 'generate a model=name field="field1:type field2:type"'
215
+ task :generate do
216
+ ts = Time.now.strftime '%Y%m%d%H%%M%S'
217
+
218
+ if ENV['model']
219
+ table_name = ENV['model']
220
+ else
221
+ print 'model name> '
222
+ table_name = $stdin.gets.strip
223
+ end
224
+
225
+ raise ArgumentError, 'must provide a name for the model to generate' if table_name.empty?
226
+
227
+ create_table_str = "create_table :#{table_name} do |t|"
228
+
229
+ fields = ENV['fields'] if ENV['fields']
230
+
231
+ columns = ENV.has_key?('fields') ? ENV['fields'].split.map {|v| "t.#{v.sub(/:/, ' :')}"}.join("\n#{' '*6}") : nil
232
+
233
+ create_table_str << "\n #{columns}" if columns
234
+
235
+ contents = <<-MIGRATION
236
+ class Create#{class_name table_name} < ActiveRecord::Migration
237
+ def self.up
238
+ #{create_table_str}
239
+ t.timestamps
240
+ end
241
+ end
242
+
243
+ def self.down
244
+ drop_table :#{table_name}
245
+ end
246
+ end
247
+ MIGRATION
248
+
249
+ create_file @migrations.first, file_name("create_#{table_name}"), contents
250
+ end
251
+
157
252
  desc "Create a new migration"
158
253
  task :new_migration do |t|
159
254
  unless migration = ENV['name']
@@ -162,9 +257,8 @@ class MigratorTasks < ::Rake::TaskLib
162
257
  abort
163
258
  end
164
259
 
165
- class_name = migration.split('_').map{|s| s.capitalize }.join
166
260
  file_contents = <<eof
167
- class #{class_name} < ActiveRecord::Migration
261
+ class #{class_name migration} < ActiveRecord::Migration
168
262
  def self.up
169
263
  end
170
264
 
@@ -173,14 +267,24 @@ class #{class_name} < ActiveRecord::Migration
173
267
  end
174
268
  end
175
269
  eof
176
- migration_path = @migrations.first
177
- FileUtils.mkdir_p(migration_path) unless File.exist?(migration_path)
178
- file_name = "#{migration_path}/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{migration}.rb"
179
270
 
180
- File.open(file_name, 'w'){|f| f.write file_contents }
271
+ create_file @migrations.first, file_name(migration), file_contents
181
272
 
182
- puts "Created migration #{file_name}"
273
+ puts "Created migration #{file_name migration}"
183
274
  end
184
275
  end
185
276
  end
186
- end
277
+
278
+ def class_name str
279
+ str.split('_').map {|s| s.capitalize}.join
280
+ end
281
+
282
+ def create_file path, file, contents
283
+ FileUtils.mkdir_p path unless File.exists? path
284
+ File.open(file, 'w') {|f| f.write contents}
285
+ end
286
+
287
+ def file_name migration
288
+ File.join @migrations.first, "#{Time.now.utc.strftime '%Y%m%d%H%M%S'}_#{migration}.rb"
289
+ end
290
+ end
@@ -110,6 +110,12 @@ TXT
110
110
  end
111
111
  end
112
112
 
113
+ describe 'db:version' do
114
+ it "should start with a new database version" do
115
+ run("rake db:version").should =~ /Current version: 0/
116
+ end
117
+ end
118
+
113
119
  describe 'db:migrate' do
114
120
  context "single migration path" do
115
121
  it "does nothing when no migrations are present" do
@@ -130,8 +136,8 @@ TXT
130
136
  end
131
137
  it "runs the migrator on each migration path" do
132
138
  result = run("rake db:migrate")
133
- result.should =~ /Migrating to CreateTests \(#{Time.now.year}/
134
- result.should =~ /Migrating to CreateTests2 \(#{Time.now.year}/
139
+ result.should =~ /Migrating to CreateTests \(2010/
140
+ result.should =~ /Migrating to CreateTests2 \(2010/
135
141
  end
136
142
  end
137
143
  end
@@ -265,4 +271,4 @@ TXT
265
271
  run('rake db:test:purge').should =~ /SUCCESS/
266
272
  end
267
273
  end
268
- end
274
+ end
@@ -1,36 +1,34 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{standalone_migrations}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.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{2010-04-12}
12
+ s.date = %q{2011-02-16}
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
- ".gitignore",
19
- "README.markdown",
20
- "Rakefile",
21
- "VERSION",
22
- "lib/tasks/standalone_migrations.rb",
23
- "spec/standalone_migrations_spec.rb",
24
- "standalone_migrations.gemspec",
25
- "vendor/migration_helpers/MIT-LICENSE",
26
- "vendor/migration_helpers/README.markdown",
27
- "vendor/migration_helpers/init.rb",
28
- "vendor/migration_helpers/lib/migration_helper.rb"
18
+ "README.markdown",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "lib/tasks/standalone_migrations.rb",
22
+ "spec/standalone_migrations_spec.rb",
23
+ "standalone_migrations.gemspec",
24
+ "vendor/migration_helpers/MIT-LICENSE",
25
+ "vendor/migration_helpers/README.markdown",
26
+ "vendor/migration_helpers/init.rb",
27
+ "vendor/migration_helpers/lib/migration_helper.rb"
29
28
  ]
30
29
  s.homepage = %q{http://github.com/thuss/standalone-migrations}
31
- s.rdoc_options = ["--charset=UTF-8"]
32
30
  s.require_paths = ["lib"]
33
- s.rubygems_version = %q{1.3.6}
31
+ s.rubygems_version = %q{1.3.7}
34
32
  s.summary = %q{A thin wrapper to use Rails Migrations in non Rails projects}
35
33
  s.test_files = [
36
34
  "spec/standalone_migrations_spec.rb"
@@ -40,7 +38,7 @@ Gem::Specification.new do |s|
40
38
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
39
  s.specification_version = 3
42
40
 
43
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
42
  s.add_runtime_dependency(%q<rake>, [">= 0"])
45
43
  s.add_runtime_dependency(%q<activerecord>, [">= 0"])
46
44
  else
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standalone_migrations
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 3
7
+ - 4
9
8
  - 0
10
- version: 0.3.0
9
+ version: 0.4.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Todd Huss
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-04-12 00:00:00 -10:00
18
+ date: 2011-02-16 00:00:00 -08:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -27,7 +26,6 @@ dependencies:
27
26
  requirements:
28
27
  - - ">="
29
28
  - !ruby/object:Gem::Version
30
- hash: 3
31
29
  segments:
32
30
  - 0
33
31
  version: "0"
@@ -41,7 +39,6 @@ dependencies:
41
39
  requirements:
42
40
  - - ">="
43
41
  - !ruby/object:Gem::Version
44
- hash: 3
45
42
  segments:
46
43
  - 0
47
44
  version: "0"
@@ -56,7 +53,6 @@ extensions: []
56
53
  extra_rdoc_files:
57
54
  - README.markdown
58
55
  files:
59
- - .gitignore
60
56
  - README.markdown
61
57
  - Rakefile
62
58
  - VERSION
@@ -72,8 +68,8 @@ homepage: http://github.com/thuss/standalone-migrations
72
68
  licenses: []
73
69
 
74
70
  post_install_message:
75
- rdoc_options:
76
- - --charset=UTF-8
71
+ rdoc_options: []
72
+
77
73
  require_paths:
78
74
  - lib
79
75
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -81,7 +77,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
77
  requirements:
82
78
  - - ">="
83
79
  - !ruby/object:Gem::Version
84
- hash: 3
85
80
  segments:
86
81
  - 0
87
82
  version: "0"
@@ -90,7 +85,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
85
  requirements:
91
86
  - - ">="
92
87
  - !ruby/object:Gem::Version
93
- hash: 3
94
88
  segments:
95
89
  - 0
96
90
  version: "0"
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- schema.rb
2
- spec/tmp