开发者

Deleted Schema Migration Ruby on Rails sqlite3

开发者 https://www.devze.com 2023-02-27 10:06 出处:网络
So I was stupid and deleted all the rows in the schema_m开发者_JAVA百科igration of my sqlite3 database. How do I fix this? Did I destroy my whole project?Did you delete db\\schema.rb or db\\migrate fi

So I was stupid and deleted all the rows in the schema_m开发者_JAVA百科igration of my sqlite3 database. How do I fix this? Did I destroy my whole project?


Did you delete db\schema.rb or db\migrate files too?

In the first case you can try move db file to other location and execute:

rake db:migrate

If you removed schema.rb and migration files you can execute

rails generate migration

and recreate migration manually.


So long as you don't need to run rake db:migrate again you'll be fine. Otherwise you'll run into problems.

I probably should do this in Ruby, but i'm more familiar with Perl - so here's a quick script that'll help you resurect the schema_migrations table

!#/usr/bin/perl

opendir(DH, 'path/to/rails/app/db/migrate');

while($filename = readdir(DH))
{
    next unless $filename =~ /\.rb$/;
    @parts = split("_", $filename);
    print "INSERT INTO schema_migration VALUES (" . $parts[0] . ");\n";
}

running the script will give you a series of insert statements that'll then be able to copy and paste into the sqllite command line tool


or you can just kill the DB and restart

rake db:drop; rake db:migrate; rake db:test:prepare
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号