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?
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.
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.
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)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.