如何为旧数据库创建迁移

如何为旧数据库创建迁移

本文介绍了如何为旧数据库创建迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用一个在我意识到Rails存在之前设计的数据库来开发Rails应用程序.
我目前已经创建了一些迁移,以向现有表中添加一些新表和新列.

I am currently developing a Rails application using a database that was designed before I was aware of Rails existence.
I have currently created some migrations to add some new tables and new columns to existing tables.

我想进行迁移以重新创建完整的数据库.

I would like to have the migrations to recreate the full database.

我应该遵循哪些步骤?
我应该手动创建所有迁移吗?

Which steps should I follow?
Should I create all the migrations by hand?

我对数据库模式感兴趣,而不对数据库内容感兴趣

I am interested in the database schema not in the database contents

推荐答案

我认为这需要一些手动工作.

I think this will require some manual work.

如果项目中没有文件db/schema.rb,则可以通过运行rake db:schema:dump开始.如果您一直在使用迁移,则您可能已经有一个db/schema.rb文件.该文件将包含您已配置用于开发的数据库的ruby表示形式.要获取另一个数据库的红宝石表示形式,请在RAILS_ENV中将其传递给rake命令(即RAILS_ENV=production rake db:schema:dump)

You can start out by running rake db:schema:dump if you do not have the file db/schema.rb in your project. You probably already have a db/schema.rb file if you have been using migrations. That file will contain the ruby representation of the database you have configured for development. To get the ruby representation of another database pass in RAILS_ENV to the rake command (ie RAILS_ENV=production rake db:schema:dump)

然后,您可以使用该schema.rb文件作为起点来创建新迁移,以重新创建预先存在的表.创建新的迁移文件时,请确保以这样的方式命名该文件,使其在其他迁移之前运行.

You could then use that schema.rb file as a starting point to create new migrations that recreate the preexisting tables. When creating the new migration file make sure that it is named in such a way that it will be run prior to your other migrations.

这篇关于如何为旧数据库创建迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 23:03