Regenerate DB

Hi again :),

I’m playing with migrations. I’ve created some migrations classes and
with
rake db:migrate the tables are created and altered fine, but then I
dropped
some of the tables and when I run db:migrate command again I don’t get
tables
being re-created.

What am I doing wrong? Is there a way to run db:migration in order to
drop the
tables?

On 7/13/06, Eduardo Yáñez Parareda [email protected] wrote:

What am I doing wrong? Is there a way to run db:migration in order to
drop the
tables?


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Rails uses the schema_info table to determine what migrations to run.
When you drop a table via the command line, it doesn’t update the
schema_info table. The proper way is to run migrate with the version
option. For example if you had

class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :username, :string, :default => “”, :null => false
t.column :password_salt, :string
t.column :password_hash, :string
t.column :email, :string
end
end

def self.down
drop_table :users
end
end

And that was in 006_create_user.rb, then you would do
rake db:migrate VERSION=5

to migrate to the version below this one. It would call the down
method, which drops the users table.

Run rake db:migrate --help to see all the options.
Peak Obsession has a lot of
very useful information as well.

Pat

I would try:

rake db:migrate VERSION=1

then

rake db:migrate VERSION=X <- X is your last version

with
rake db:migrate the tables are created and altered fine, but then I
dropped
some of the tables and when I run db:migrate command again I don’t get
tables
being re-created.

What am I doing wrong? Is there a way to run db:migration in order to
drop the
tables?

I messed my tables up pretty good and found that rake migrate VERSION=0
dropped all tables in the database. Rake migrate then created new ones
to the latest level. Be very careful if you have data you would like to
preserve.

Regards,
Rich

Or add this to your Rakefile:

namespace :db do
desc “Revert database schema back to version 0, then up to the current
version.”
task :reload => :environment do
# migrate back to the stone age
ENV[‘VERSION’] = ‘0’
Rake::Task[“db:migrate”].execute

# forward, comrades, to the future!
ENV.delete('VERSION')
Rake::Task["db:migrate"].execute

# finally, load up the fixtures
Rake::Task["db:fixtures:load"].invoke

end
end

(snipped of technoweenie)