`
hideto
  • 浏览: 2666665 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Convert an Existing Application to Migrations

    博客分类:
  • Ruby
阅读更多
有时候我们的Rails应用是在已有数据库上的,我们需要把基于SQL的schema转化成ActiveRecord的schema

1,dump schema
运行rake db:schema:dump来将数据库中的表结构复制到db/schema.rb文件中
这时运行rake db:schema:load或者将schema.rb的内容copy到一个migration中并运行rake db:migrate会生成表
其中:force => true表示会覆盖数据库已存在的表,这样会让我们丢失数据库已有的data

2,migration的版本
Rails会自动生成schema_info表,该表的version列表示当前的migration的version,即migration文件开头的number
可以修改schema_info的version来控制要执行的migrate任务

3,避免丢失数据
一种方式是先从数据库extract fixtures,然后rake db:schema:load或rake db:migrate,并且:force => true,然后rake db:fixtures:load
另一种方式是修改schema_info的version来控制要执行的migrate任务
分享到:
评论

相关推荐

    Ruby.on.Rails.Enterprise.Application.Development

    《Ruby on Rails Enterprise Application Development》是一本非常实用的书籍,不仅适合已经有一定Ruby on Rails基础的开发者阅读,也适合希望了解企业级应用开发流程的项目经理和技术管理人员。通过学习本书,你...

    mediom, Forum web application, an example for from Rails to Go (Revel).zip

    "mediom, Forum web application, an example for from Rails to Go (Revel).zip" 这个压缩包文件是一个关于将Web应用从Ruby on Rails框架迁移到Go语言(使用Revel框架)的示例项目。这个名为"mediom"的论坛应用提供...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Also new is Webpack coverage, to develop the front-end code for your Rails application. Create a usable and attractive login form using Bootstrap's styles, while ensuring the database table backing ...

    mongoid_rails_migrations:Mongoid的数据迁移

    gem "mongoid_rails_migrations" 如何使用 创建迁移 $ rails generate mongoid:migration 运行迁移: $ rails db:migrate $ rails db:migrate:down VERSION= $ rails db:migrate:up VERSION= $ rails db:rollback...

    Agile Web Development with Rails 4

    You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...

    Ruby on Rails Enterprise Application Development.pdf

    《Ruby on Rails Enterprise Application Development》是一本专注于Ruby on Rails框架的书籍,主要讲述了如何利用Ruby on Rails开发企业级应用程序。本书作者是Elliot Smith和Rob Nichols,他们在IT行业有着丰富的...

    Enterprise Rails

    What does it take to develop an enterprise application with Rails? Enterprise Rails introduces several time-tested software engineering principles to prepare you for the challenge of building a high-...

    Agile Web Development with Rails Final

    - Rails encourages the DRY principle, aiming to reduce repetition of information across the application. It provides various tools and conventions to eliminate redundancy, making code more ...

    Rails 4 in Action

    It's famously simple to build a basic Rails application. Once you've started down the track, however, you discover that Rails is a powerful, deep, and rich full-stack platform capable of tackling ...

Global site tag (gtag.js) - Google Analytics