GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of wycats/merb-plugins
Description: Merb Plugins: Even more modules to hook up your Merb installation
Homepage: http://www.merbivore.com
Clone URL: git://github.com/auser/merb-plugins.git
cleaned up the existing rake tasks + added rake db:drop:all, rake 
db:migrate:redo, rake db:migrate:reset + rake db:reset now drops your dbs, 
recreate them and load schema.rb + rake db:rollback + rake 
db:abort_if_pending_migrations
mattetti (author)
Sun Mar 09 19:29:59 -0700 2008
commit  eb1323e479233465c859072ada61295124440ea2
tree    a5801309e33625b3ec744c397210865fd5fef4e2
parent  e9e9c510680792c8ff9bb6abcda0ef2cd5633a68
...
52
53
54
55
56
57
58
 
 
59
60
61
62
63
64
65
 
66
67
 
68
 
69
70
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
 
 
 
 
 
 
 
 
 
73
74
75
 
76
77
78
79
80
81
82
 
 
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
85
86
 
87
88
89
...
95
96
97
98
 
99
100
101
...
52
53
54
 
 
 
 
55
56
57
58
 
 
 
 
 
59
60
 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 
100
101
102
103
104
105
 
 
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 
141
142
143
144
...
150
151
152
 
153
154
155
156
0
@@ -52,38 +52,93 @@ namespace :db do
0
       puts "This task only creates local databases. #{config[:database]} is on a remote host."
0
     end
0
   end
0
-
0
- desc 'Drops the database for the current environment'
0
- task :drop => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
0
+
0
+ def drop_database(config)
0
     case config[:adapter]
0
     when 'mysql'
0
- begin
0
- ActiveRecord::Base.connection.drop_database config[:database]
0
- rescue
0
- puts "#{config[:database]} seems to have been dropped already"
0
- end
0
+ ActiveRecord::Base.connection.drop_database config[:database]
0
     when /^sqlite/
0
- FileUtils.rm_f File.join(Merb.root, config[:database])
0
+ FileUtils.rm(File.join(RAILS_ROOT, config[:database]))
0
     when 'postgresql'
0
+ ActiveRecord::Base.clear_active_connections!
0
       `dropdb "#{config[:database]}"`
0
     end
0
   end
0
+
0
+ def local_database?(config, &block)
0
+ if %w( 127.0.0.1 localhost ).include?(config[:host]) || config[:host].blank?
0
+ yield
0
+ else
0
+ puts "This task only modifies local databases. #{config[:database]} is on a remote host."
0
+ end
0
+ end
0
+
0
+ namespace :drop do
0
+ desc 'Drops all the local databases defined in config/database.yml'
0
+ task :all => :merb_start do
0
+ ActiveRecord::Base.configurations.each_value do |config|
0
+ # Skip entries that don't have a database key
0
+ next unless config[:database]
0
+ # Only connect to local databases
0
+ local_database?(config) { drop_database(config) }
0
+ end
0
+ end
0
+ end
0
 
0
+ desc 'Drops the database for the current environment (set MERB_ENV to target another environment)'
0
+ task :drop => :merb_start do
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
0
+ begin
0
+ drop_database(config)
0
+ rescue Exception => e
0
+ puts "#{e.inspect} - #{config['database']} might have been already dropped"
0
+ end
0
+ end
0
+
0
   desc "Migrate the database through scripts in schema/migrations. Target specific version with VERSION=x"
0
   task :migrate => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
0
     ActiveRecord::Base.establish_connection(config)
0
     ActiveRecord::Migrator.migrate("schema/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
0
     Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
0
   end
0
 
0
- desc 'Drops, creates and then migrates the database for the current environment. Target specific version with VERSION=x'
0
- task :reset => ['db:drop', 'db:create', 'db:migrate']
0
+ namespace :migrate do
0
+ desc 'Rollbacks the database one migration and re migrate up. If you want to rollback more than one step, define STEP=x'
0
+ task :redo => [ 'db:rollback', 'db:migrate' ]
0
 
0
+ desc 'Resets your database using your migrations for the current environment'
0
+ task :reset => ["db:drop", "db:create", "db:migrate"]
0
+ end
0
+
0
+ desc 'Drops and recreates the database from db/schema.rb for the current environment.'
0
+ task :reset => ['db:drop', 'db:create', 'db:schema:load']
0
+
0
+ desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
0
+ task :rollback => :merb_start do
0
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
0
+ version = ActiveRecord::Migrator.current_version - step
0
+ ActiveRecord::Migrator.migrate('schema/migrations/', version)
0
+ end
0
+
0
+ desc "Raises an error if there are pending migrations"
0
+ task :abort_if_pending_migrations => :merb_start do
0
+ if defined? ActiveRecord
0
+ pending_migrations = ActiveRecord::Migrator.new(:up, 'schema/migrations').pending_migrations
0
+
0
+ if pending_migrations.any?
0
+ puts "You have #{pending_migrations.size} pending migrations:"
0
+ pending_migrations.each do |pending_migration|
0
+ puts ' %4d %s' % [pending_migration.version, pending_migration.name]
0
+ end
0
+ abort "Run `rake db:migrate` to update your database then try again."
0
+ end
0
+ end
0
+ end
0
+
0
   desc "Retrieves the charset for the current environment's database"
0
   task :charset => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
0
     case config[:adapter]
0
     when 'mysql'
0
       ActiveRecord::Base.establish_connection(config)
0
@@ -95,7 +150,7 @@ namespace :db do
0
 
0
   desc "Retrieves the collation for the current environment's database"
0
   task :collation => :merb_start do
0
- config = ActiveRecord::Base.configurations[Merb.environment.to_sym || :development]
0
+ config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
0
     case config[:adapter]
0
     when 'mysql'
0
       ActiveRecord::Base.establish_connection(config)

Comments

    No one has commented yet.