[英]Ruby on Rails: How can I revert a migration with rake db:migrate?
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. 现在,如果我执行rake db:migrate,将创建users表。
How can i revert this migration, ie how can I delete the users table using rake again ? 如何恢复此迁移,即如何再次使用rake删除users表?
Run the following command 运行以下命令
rake db:migrate:down VERSION=<version>
where <version>
is the version number of your migration file you want to revert. 其中
<version>
是您要还原的迁移文件的版本号。
eg. 例如。 if you want to revert a migration with file name 3846656238_create_users.rb
如果要还原文件名为3846656238_create_users.rb的迁移
rake db:migrate:down VERSION=3846656238
rake db:migrate:down VERSION = 3846656238
只需运行此命令:
rake db:rollback
I believe there are three options available for reverting migrations (they also overlap): 我相信有三种方法可用于还原迁移(它们也重叠):
Roll down the most recent migration: 下载最近的迁移:
rake db:migrate:down
# Rails 2 only. rake db:migrate:down
#Rails 2 only。
Roll down a number(n) of recent migrations: 下载最近一次(n)次迁移:
rake db:rollback STEP=n
Roll down to a previous, specific version: 滚下来到以前特定版本:
$ rake db:migrate:down VERSION=nnn
# Rails 3 (provide version number also). $ rake db:migrate:down VERSION=nnn
#Rails 3(也提供版本号)。
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
版本号表示提交的SHA(安全哈希算法),它是一个长十六进制数字,看起来像886af3194768917c78e ...你可以通过
git log
看到它
You can see these commands (and others) with their descriptions by using rake -T db:
which for rails 3.2 includes: 您可以使用
rake -T db:
来查看这些命令(和其他命令)及其描述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)
You can do rollback and specify how many last migrations will be rollbacked, eg 您可以执行回滚并指定将回滚的最后一次迁移的数量,例如
rake db:rollback STEP=3
for 3 last migrations. 最后3次迁移。
rake db:migrate:redo
它将撤消并重新应用上次迁移。
As an new programmer (or to other new programmers) 作为一名新程序员(或其他新程序员)
rake db:rollback
works about half the time. rake db:rollback
工作时间大约是一半。 I start there. 我从那里开始。
If not, rake db:migrate:down VERSION=3846656238
如果没有,则
rake db:migrate:down VERSION=3846656238
plug in VERSION for the version number of your migration file you want to revert. 插入VERSION以获取要还原的迁移文件的版本号。
For rails 5 we can use rails command instead of rake
对于rails 5,我们可以使用
rails command instead of rake
rails db:migrate:down VERSION=<version>
example 例
rails db:migrate:down VERSION=20170330090327
rails db:migrate:down VERSION = 20170330090327
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. 它显示了我们之前运行的所有迁移的状态,迁移ID和迁移名称。 select your migration id (ie your version number) and put that id in the following command after version= ,,, and press enter
选择您的迁移ID(即您的版本号)并在version = ,,,之后将该id放入以下命令中,然后按Enter键
bundle exec rake db:migrate:down VERSION=
rake db:migrate:status
rake db:migrate:down VERSION=20190802023239
.......and now you're off to the races! .......现在你要参加比赛了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.