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? -- Posted via http://www.ruby-forum.com/.
On 7/13/06, Eduardo Y??ez Parareda <eduardo.yanez@gmail.com> wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > 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. http://wiki.rubyonrails.com/rails/pages/UsingMigrations 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
> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Eduardo Y??ez Parareda > Sent: Thursday, July 13, 2006 10:11 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] 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?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) On 7/13/06, Roland Mai <roland.mai@gmail.com> wrote:> > I would try: > > rake db:migrate VERSION=1 > > then > > rake db:migrate VERSION=X <- X is your last version > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060713/310504a5/attachment.html