清除或重新创建 Ruby on Rails 数据库
我有一个充满数据的开发 Ruby on Rails 数据库。我想删除所有内容并重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
I have a dev Ruby on Rails database full of data. I want to delete everything and rebuild the database. I'm thinking of using something like:
rake db:recreate
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
TL;DR - 我在开发过程中使用这个 rake 脚本来清除所有内容,包括架构文件,然后直接从迁移脚本重建。它同时重建
dev
和test
数据库。这是我发现的保证一切都按照我的预期进行的唯一方法。使用它多年没有任何问题。基本原理 - 所有提供的解决方案的问题在于 Rails 提供的本机 Rake 任务依赖于
schema.rb
。当我进行大量数据建模时,我直接对迁移文件进行更改;只有在它们被提交到上游之后,我们才会将它们视为不可变的。但是,如果我对迁移文件进行更改,它们不会反映在schema.rb
中。另一个问题是开发环境和测试环境之间的区别。 Rails 数据库任务独立处理它们,但根据我的经验,
dev
和test
数据库应始终保持奇偶性,这意味着我在开发时必须运行大量重复的数据库清理。TL;DR - I use this rake script during development to blow away everything, including the schema file, then rebuild directly from migration scripts. It rebuilds both
dev
andtest
databases simultaneously. It's the only way I've found to guarantee everything lines up the way I expect. Been using it for years without a problem.Rationale - The problem with all the provided solutions is that native Rake tasks provided by Rails rely on
schema.rb
. When I am doing heavy data modeling, I make changes directly to the migration files; only after they've been committed upstream do we treat them as immutable. But if I make changes to the migration file, they aren't reflected inschema.rb
.The other problem is the distinction between
dev
andtest
environments. Rails db tasks handle them independently, but in my experiencedev
andtest
databases should always maintain parity, which means I had to run lots of duplicative database cleanup when developing.我自己请注意,如果您只想清除数据,
它将从表中删除数据。我喜欢
rails
前缀,因为我通常通过以下方式运行 db 命令Note for myself if you want to just clear data
it will delete data from tables. I like
rails
prefix because I usually run db command via我知道有两种方法可以做到这一点:
这将重置您的数据库并重新加载当前架构:
这将销毁您的数据库,然后创建它,然后迁移您当前的架构:
在这两种情况下,所有数据都将丢失。
I know two ways to do this:
This will reset your database and reload your current schema with all:
This will destroy your db and then create it and then migrate your current schema:
All data will be lost in both scenarios.
在 Rails 4 上,所需要做的就是
删除数据库上的全部内容并从 schema.rb 文件重新创建架构,而不必一一应用所有迁移。
On Rails 4, all needed is
That would delete the entire contents on your DB and recreate the schema from your schema.rb file, without having to apply all migrations one by one.
我在终端中使用以下一种班轮。
我把它作为 shell 别名并将其命名为
remigrate
现在,您可以轻松地“链接”Rails 任务:
I use the following one liner in Terminal.
I put this as a shell alias and named it
remigrate
By now, you can easily "chain" Rails tasks:
更新:在 Rails 5 中,可以通过以下命令访问此命令:
rails db:purge db:create db:migrate RAILS_ENV=test
从最新的 Rails 4.2 版本开始,您现在可以运行:
来源:< a href="https://github.com/rails/rails/commit/e2f232aba15937a4b9d14bd91e0392c6d55be58d" rel="noreferrer">commit
可以像上面提到的那样一起使用:
Update: In Rails 5, this command will be accessible through this command:
rails db:purge db:create db:migrate RAILS_ENV=test
As of the newest rails 4.2 release you can now run:
Source: commit
It can be used together like mentioned above:
根据您的需求,您可以使用...
rake db:create
...从
config/database.yml
从头开始构建数据库,或者...rake db:schema:load
...从您的
schema.rb
文件从头开始构建数据库。Depending on what you're wanting, you can use…
rake db:create
…to build the database from scratch from
config/database.yml
, or…rake db:schema:load
…to build the database from scratch from your
schema.rb
file.从命令行运行
From the command line run
在 Rails 6 中,有一种重置数据库并再次播种的便捷方法:
https://weblog.rubyonrails.org/2019/3/15/this-week-in-rails-security-fixes-bulk-insert- and-upsert-seeds-replanting/
In Rails 6 there is a convenient way for resetting DB and planting seeds again:
https://weblog.rubyonrails.org/2019/3/15/this-week-in-rails-security-fixes-bulk-insert-and-upsert-seeds-replanting/
使用就像
“全部在一行”一样。由于环境不会一次又一次地重新加载,因此速度更快。
db:drop - 将删除数据库。
db:create - 将创建数据库(主机/db/密码将从 config/database.yml 中获取)
db:migrate - 将运行来自目录(db/migration/.rb)*的现有迁移。
db:seed - 将从目录(db/migration/seed.rb)运行种子数据。
我通常更喜欢:
一次完成所有操作。
干杯!
Use like
All in one line. This is faster since the environment doesn't get reloaded again and again.
db:drop - will drop database.
db:create - will create database (host/db/password will be taken from config/database.yml)
db:migrate - will run existing migrations from directory (db/migration/.rb)*.
db:seed - will run seed data possible from directory (db/migration/seed.rb)..
I usually prefer:
to do all at once.
Cheers!
只需执行以下步骤顺序:删除数据库,然后重新创建数据库,迁移数据,如果您有种子,则播种数据库:
由于
rake
的默认环境是 开发,如果您在规范测试中看到异常,您应该为测试环境重新创建数据库,如下所示:在大多数情况下,测试数据库是在测试过程中播种的,因此不需要传递 db:seed 任务操作。否则,您应该准备数据库:
或者
此外,要使用重新创建任务,您可以将以下代码添加到Rakefile中:
然后发出:
Just issue the sequence of the steps: drop the database, then re-create it again, migrate data, and if you have seeds, sow the database:
Since the default environment for
rake
is development, in case if you see the exception in spec tests, you should re-create db for the test environment as follows:In most cases the test database is being sowed during the test procedures, so
db:seed
task action isn't required to be passed. Otherwise, you shall to prepare the database:or
Additionally, to use the recreate task you can add into Rakefile the following code:
Then issue:
您可以手动执行以下操作:
或者只是
rake db:reset
,这将运行上述步骤,但也会运行您的db/seeds.rb
文件。另一个细微差别是,
rake db:reset
直接从schema.rb
文件加载,而不是再次运行所有迁移文件。在所有情况下,您的数据都会被破坏。
You can manually do:
Or just
rake db:reset
, which will run the above steps but will also run yourdb/seeds.rb
file.An added nuance is that
rake db:reset
loads directly from yourschema.rb
file as opposed to running all the migrations files again.You data gets blown away in all cases.
您可以使用以下命令行:
You can use this following command line:
要删除特定数据库,您可以在 Rails 控制台上执行此操作:
然后再次迁移数据库
To drop a particular database, you can do this on rails console:
And then migrate DB again
在 Rails 4.2 上,删除所有数据但保留数据库
https ://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md
On rails 4.2, to remove all data but preserve the database
https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md
3 个选项,相同结果:
1. 所有步骤:
2. 重置:
3. 迁移:重置:
备注:
3 options, same result:
1. All steps:
2. Reset:
3. Migrate:reset:
Notes:
您可以使用
db:reset
- 用于运行 db:drop 和 db:setup 或db:migrate:reset
- 运行 db:drop、db:create 和 db:migrate。依赖于您想要使用现有的 schema.rb
You can use
db:reset
- for run db:drop and db:setup ordb:migrate:reset
- which runs db:drop, db:create and db:migrate.dependent at you want to use exist schema.rb
根据 Rails 指南,应该使用这个衬垫,因为它会加载从
schema.rb
而不是逐一重新加载迁移文件:According to Rails guide, this one liner should be used because it would load from the
schema.rb
instead of reloading the migration files one by one:因为在开发中,您总是想要重新创建数据库,您可以像这样在 lib/tasks 文件夹中定义一个 rake 任务。
在终端中,您将运行
它将重建您的数据库
Because in development , you will always want to recreate the database,you can define a rake task in your lib/tasks folder like that.
and in terminal you will run
it will rebuild your database
我认为运行此命令的最佳方法:
I think the best way to run this command:
只需运行
rake db:setup ,
如果您使用一些数据创建了种子文件,它将删除数据库,创建新数据库并从种子填充数据库。
Simply you can run
rake db:setup
It will drop database, create new database and populate db from seed if you created seed file with some data.
我使用:
rails db:create
基于config/database.yml
创建数据库前面的命令可以替换为
rails db:reset
。不要忘记运行
rails db:migrate
来运行迁移。I use:
rails db:drop
to delete the databases.rails db:create
to create the databases based onconfig/database.yml
The previous commands may be replaced with
rails db:reset
.Don't forget to run
rails db:migrate
to run the migrations.RAILS 7 及更高版本
只需运行:
注意:这将删除测试、开发数据库中的数据,然后加载 seeds.rb(如果有)。它将按此顺序运行
希望这有帮助!
RAILS 7 and above
Just simply run:
Note: This would delete the data in test, development database and then load the seeds.rb if any. It would run in this order
Hope this helps!
今天我对我的 Rails 架构做了相当多的更改。我意识到我需要在层次结构中添加两个模型,并删除其他一些模型。模型和控制器需要进行许多小的更改。
我添加了两个新模型并创建了它们,使用:
然后我编辑了 schema.rb 文件。我手动删除了不再需要的旧模型,根据需要更改了外键字段,然后稍微重新排序以使其更清晰。我删除了所有迁移,然后通过以下方式重新运行构建:
它运行得很好。当然,所有数据都必须重新加载。 Rails 意识到迁移已被删除并重置了高水位线:
I've today made quite a few changes to my rails schema. I realised I needed an additional two models in a hierarchy and some others to be deleted. There were many little changes required to the models and controllers.
I added the two new models and created them, using:
Then I edited the schema.rb file. I manually removed the old models that were no longer required, changed the foreign key field as required and just reordered it a bit to make it clearer to me. I deleted all the migrations, and then re-ran the build via:
It worked perfectly. All the data has to be reloaded, of course. Rails realised the migrations had been deleted and reset the high-water mark: