Heroku:将生产数据库拉到测试并运行 rake db:migrate 时出现迁移问题
我在 Heroku 上有 3 个 Rails 应用程序实例(测试、阶段和生产)。当我想测试真实用户数据发生的问题时,我想先进行 heroku db:pull --app 生产,然后进行 heroku db:push --app 测试。问题是,此时 heroku rake db:migrate --app test 会抛出错误,因为迁移尝试创建的列已经创建。
我的理解是,heroku db:push 将数据推送到现有的数据库模式中,而不是字面上推送整个数据库(包括模式)。这意味着我们推送的模式可能比我们推送的迁移表更高级,因为此迁移表将丢失尚未在我们从中提取的数据库上运行但显然已在我们推送到的数据库上运行的迁移记录。
我的第一个问题是,我对其工作原理的理解是否正确?我的第二个问题是如何解决这个问题,以便我可以提取生产数据,将其保留在测试中并运行迁移而不会收到此错误。理想情况下,我希望复制生产数据库并将其保留在测试中,然后完全迁移它,因为如果我可以做到这一点,我就不必担心测试中的现有模式。有办法做到这一点吗?
如果没有,是否有办法通过使用已在测试数据库上运行的每个迁移的记录填充新的迁移表来伪造迁移已经运行?
I have 3 instances of my rails app on heroku (test, stage and production). When I want to test an issue that is happening with real users' data, I would like to heroku db:pull --app production and then heroku db:push --app test. The problem is that at this point heroku rake db:migrate --app test throws an error because the columns the migration is trying to create have already been created.
My understanding is that heroku db:push pushes data into an existing database schema and rather than literally pushing the entire database (schema included). This means that the schema we are pushing to may be more advanced than the migrations table we are pushing since this migrations table will be missing migration records that have not run on the database we pulled from but have obviously run on the database we are pushing to.
My first question is, am I correct in my understanding of how this works? My second question is how do I fix this so that I can pull production data, stick it in testing and run migrations without receiving this error. Ideally, I would want to copy the production database and stick it in test and then migrate it fully since if I could do this I wouldn't have to worry about the existing schema on test. Is there a way to do this?
If not, is there a way to fake that migrations have already run by populating the new migrations table with records for each migration that has already run on my test database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,db:push 推送本地架构和数据。您可以将本地数据库推送到 Heorku 上的空数据库中,这就是我将站点上线的方式 - 当您运行它时,您会看到它创建架构,然后推送数据。
我的工作方式如下 - Heroku 上的测试环境与实时代码相同- IE。 master 的一个分支(即正在运行并推送测试的分支)。从 Live 中拉取数据库。修复我的本地系统。推动测试并运行迁移。针对 Heroku 上的 DB 进行测试发布。当我高兴时,将测试代码合并到主代码中,然后部署并运行迁移。冲洗并重复以防止将来的错误。生产数据库不应该有更高级的模式版本来测试。您始终可以通过查看 schema_migrations 表来检查这一点 - 这就是 Rails 如何知道到目前为止运行了哪些迁移,因此您可以将其与 db/migrations 文件进行比较。
No, db:push pushes the local schema and data. You can push your local DB into an empty DB on Heorku, this is how I put sites live - when you run it you see it creating the schema then pushing the data in.
I work like this - Test environment on Heroku same code as live - ie. a branch of master (ie what's live and pushed to test). Pull DB from Live. Fix on my local system. Push to test and run migrations. Test release against DB on Heroku. When I'm happy merge test code into master and then deploy and run migrations. Rinse and Repeat for future bugs. The production DB should never have a more advanced schema version that test. You can always check this out by looking in the schema_migrations table - this is how Rails knows what migrations have run so far, so you can compare this to db/migrations files.