1

因此,如果此迁移导致任何错误,我希望能够恢复/rake db:rollback。

编码:

class ChangeUidToPrimaryKey < ActiveRecord::Migration
def change
execute ALTER TABLE "users" DROP CONSTRAINT "users_pkey".
execute ALTER TABLE "users" ADD PRIMARY KEY "uid"
end
end

在这种情况下,我可以添加哪些向下/可逆代码来使 rake db:rollback 工作?

4

1 回答 1

1

email如果您需要恢复, 您可以执行以下操作,即保持唯一索引。

class ChangeUidToPrimaryKey < ActiveRecord::Migration
  def self.up
    execute "alter table users add constraint unique_users_email unique(email)"
    execute "ALTER TABLE users DROP CONSTRAINT users_pkey"
    execute "ALTER TABLE users ADD PRIMARY KEY (uid)"
  end

  def self.down
    execute "ALTER TABLE users DROP CONSTRAINT users_pkey"
    execute "ALTER TABLE users DROP CONSTRAINT unique_users_email"
    execute "ALTER TABLE users ADD PRIMARY KEY (email)"
  end
end
于 2013-08-10T07:10:24.420 回答