Ruby on Rails:如何使用 rake db:migrate 恢复迁移?

发布于 2024-12-08 16:02:23 字数 924 浏览 1 评论 0原文

安装 devise MODEL User 后我得到了这个。

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

现在,如果我执行 rake db:migrate 将创建用户表。

我如何恢复此迁移,即如何再次使用 rake 删除用户表?

After installing devise MODEL User i got this.

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

Now if i do rake db:migrate the users table will be created.

How can i revert this migration, i.e. how can I delete the users table using rake again ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

胡渣熟男 2024-12-15 16:02:23

运行以下命令

rake db:migrate:down VERSION=<version>

,其中 是要恢复的迁移文件的版本号。

例如。如果您想恢复文件名为 3846656238_create_users.rb 的迁移

rake db:migrate:down VERSION=3846656238

Run the following command

rake db:migrate:down VERSION=<version>

where <version> is the version number of your migration file you want to revert.

eg. if you want to revert a migration with file name 3846656238_create_users.rb

rake db:migrate:down VERSION=3846656238

携君以终年 2024-12-15 16:02:23

只需运行这个命令:

rake db:rollback

Just run this command:

rake db:rollback
背叛残局 2024-12-15 16:02:23

我相信可以使用三个选项来恢复迁移(它们也重叠):

  1. 滚动最近迁移:

    rake db:migrate:down # 仅适用于 Rails 2。

  2. 滚动一定数量最近的迁移:

    rake db:rollback STEP=n

  3. 向下滚动之前的特定版本:

    $ rake db:migrate:down VERSION=nnn # Rails 3(还提供版本号)。

版本号表示提交的 SHA(安全哈希算法),它是一个很长的十六进制数字,看起来像 886af3194768917c78e...您可以通过执行 git log 来查看它,

您可以看到这些命令(以及其他命令) )及其描述,使用 rake -T db: ,其中 Rails 3.2 包括:

rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status  # Display status of migrations
rake db:rollback        # Rolls the schema back to the previous version (specify steps w/ STEP=n)

I believe there are three options available for reverting migrations (they also overlap):

  1. Roll down the most recent migration:

    rake db:migrate:down # Rails 2 only.

  2. Roll down a number(n) of recent migrations:

    rake db:rollback STEP=n

  3. Roll down to a previous, specific version:

    $ rake db:migrate:down VERSION=nnn # Rails 3 (provide version number also).

Version Number means the SHA(Secure Hash Algorithm) for the commit which is a long hexadecimal number which looks something like 886af3194768917c78e... You can see it by doing git log

You can see these commands (and others) with their descriptions by using rake -T db: which for rails 3.2 includes:

rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status  # Display status of migrations
rake db:rollback        # Rolls the schema back to the previous version (specify steps w/ STEP=n)
你是年少的欢喜 2024-12-15 16:02:23

您可以进行回滚并指定将回滚多少个最后的迁移,例如

rake db:rollback STEP=3

3 个最后的迁移。

You can do rollback and specify how many last migrations will be rollbacked, e.g.

rake db:rollback STEP=3

for 3 last migrations.

我一直都在从未离去 2024-12-15 16:02:23

作为新程序员(或其他新程序员),

rake db:rollback 的工作时间大约是一半。我从这里开始。

如果没有,请 rake db:migrate:down VERSION=3846656238

插入 VERSION 作为要恢复的迁移文件的版本号。

As an new programmer (or to other new programmers)

rake db:rollback works about half the time. I start there.

If not, rake db:migrate:down VERSION=3846656238

plug in VERSION for the version number of your migration file you want to revert.

变身佩奇 2024-12-15 16:02:23
rake db:migrate:redo

它将撤消并重新应用上次迁移。

rake db:migrate:redo

It will undo and reapply the last migration.

狼性发作 2024-12-15 16:02:23

对于rails 5,我们可以使用rails命令代替rake

rails db:migrate:down VERSION=<version>

示例

rails db:migrate:down VERSION=20170330090327

For rails 5 we can use rails command instead of rake

rails db:migrate:down VERSION=<version>

example

rails db:migrate:down VERSION=20170330090327

半步萧音过轻尘 2024-12-15 16:02:23

在终端中运行此命令:

rake db:migrate:status

或者

bundle exec rake db:migrate:status

它显示我们之前运行的所有迁移的状态、迁移 ID、迁移名称。选择您的迁移 ID(即您的版本号)并将该 id 放入以下命令中的 version= ,, 后,然后按 Enter

bundle exec rake db:migrate:down VERSION=

Run this command in your terminal:

rake db:migrate:status

or

bundle exec rake db:migrate:status

It shows the status, migration ID's, migration name for all migration we ran previously. select your migration id (i.e your version number) and put that id in the following command after version= ,,, and press enter

bundle exec rake db:migrate:down VERSION=
伤感在游骋 2024-12-15 16:02:23

如何以另一种方式回滚迁移

。我更喜欢这个,因为你需要明确,而不是回滚 - 以防万一你可能会犯错误。

(1) 首先识别迁移ID

rake db:migrate:status

  • 复制ID号。

识别

(2) 然后回滚迁移

rake db:migrate:down VERSION=20190802023239

  • 粘贴上面的相关身份证号码。当然,在您的情况下,迁移 ID 会有所不同!

......现在你就可以参加比赛了!

How to Roll back a migration

Another way. I prefer this because you need to be explicit, rather than rolling back - just in case you might make a mistake.

(1) First Identify The Migration ID

rake db:migrate:status

  • Copy the ID number.

Identify the migration to roll back.

(2) Then Roll back the migration

rake db:migrate:down VERSION=20190802023239

  • Paste the relevant ID number above. Of course, in your case, the migration ID will be different!

.......and now you're off to the races!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文