Rails - 使用现有的数据库,不能运行迁移而不丢失数据 [英] Rails -- working with an existing db, cannot run migrations without losing data

查看:134
本文介绍了Rails - 使用现有的数据库,不能运行迁移而不丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个使用Rails 4.1的初学者。我试图在开发环境中集成现有的sqlite3数据库与我的应用程序。

I am a beginner working with Rails 4.1. I am trying to integrate an existing sqlite3 db with my app in the development environment.

为了让这个工作,我已经按照列出的步骤Joel Berghoff的 blog (for MySQL):

To try to get this to work, I've followed the steps listed at Joel Berghoff's blog (for MySQL):


  • 在config / database.yml中引用db

  • 运行rake db:schema:dump

  • 将schema.rb转换为db / migrate / 001_create_database.rb

我面临的问题是,每当我运行rake db:migrate整个数据库刷新,我失去所有预填充的数据。我绕过这一段时间,首先运行迁移,然后替换用预填充的副本生成的空白数据库 - 这允许我在rails控制台中玩我的模型,并查看数据。但是,当我尝试在本地计算机上启动服务器时,会收到一条消息,指出迁移正在等待处理。

The issue I am facing is, whenever I run "rake db:migrate" the entire db refreshes and I lose all the pre-populated data. I got around this for awhile by running migrations first, then replacing the blank db that was generated with my pre-populated copy -- this allowed me to play around with my models in the rails console and see the data. However when I try to boot up the server on my local machine, I get a message that migrations are pending.

我不太确定在这里做什么...我读过我应该从rake db:seed种子数据库,但我现有的数据库是相当大 - 几乎1毫米的记录,当我尝试这个(虽然笨拙时尚)它运行超过3小时前我放弃。非常感谢任何有关如何继续的指导。

I am not quite sure what to do here...I've read that I should be seeding the db from "rake db:seed", but my existing db is quite large -- almost 1mm records, and when I attempted this (in albeit clumsy fashion) it ran for over 3 hours before I gave up. Any guidance on how to proceed is much appreciated!

推荐答案

迁移应用于创建和更改表和字段,数据,如您在此处可以看到的 Ruby on Rails指南

Migrates should be used to create and changes the tables and fields, not load data, as you can see here Ruby on Rails Guides

如果你想导入数据,你可以在种子上做,但在你的具体情况下,我认为你应该从你的源数据库创建一个转储,并将其加载到目标数据库。

If you want to import data you could do it on the seeds, but in your specific case it seems to me that you should create a dump from your origin database and load it on the target database.

这里是一个教程 sqlite3:如何从/向文件导入/导出数据

这篇关于Rails - 使用现有的数据库,不能运行迁移而不丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆