version: Rails 4.1
7 Schema Dumping and You 数据库模式输出
7.1 What are Schema Files for? 数据库模式文件是为了什么?
基本上就是 Migrations,对于你的数据库模式而言,Migrations 是没有验证的代码。其作用就是形成由 Active Record 产生的 db/schema.rb 或者 一个 SQL 文件, 用来测试数据库的. 他们不是被设计成用来编辑的, 他们仅仅只是显示数据库当前的状态.
对于部署一个应用的实例用来回放整个数据迁移文件(migration) 的历史而言, 这不是必须的(也容易出错). 而仅仅载入到当前模式的数据描述是比较简单和快速的.
例如, test 数据库是如何创建的: 当前开发模式数据库被输出为文件(可能是 db/schema.rb 或者 db/structure.sql), 然后载入到 test 数据库.
数据库模式文件也是很有用的, 如果你想快速浏览一下一个 Active Record 对象所拥有哪些特性. 这信息不在模型的代码里, 经常地遍布在几个数据迁移文件(migrations)里, 但这信息是很漂亮地被归纳在数据库模式(schema)文件中. annotate_models gem 自动地在每一个模型的概述顶部增加和更新注释, 如果你想要那个功能的话.
7.2 Types of Schema Dumps 数据库模式输出的类型
有两种方式来输出数据库模式. 这是在 config/application.rb 中设置的, 把config.active_record.schema_format 设置为 :sql 或者 :ruby.
如果设置了 :ruby, 那么数据库模式被保存为 db/schema.rb. 如果你看到这个文件, 你将会发现一大堆可怕的标签,就像一个巨大的数据迁移文件(migration):
ActiveRecord::Schema.define(version: 20080906171750 ) do
create_table "authors" , force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "products" , force: true do |t|
t.string "name"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "part_number"
end
end |
在许多方面, 确实是这样的. 这个文件的创建是用来检查数据库和描述它的数据结构, 使用 create_table, add_index之类的. 因为这是不依赖数据库, 它可以被载入到任何数据库, 只要Active Record 支持的数据库种类. 这是非常有用的, 如果你过去做过的应用, 它是能够运行在多种数据库上面的.
然而这里也有一种权衡让步. db/schema.rb 不能描述数据库的特有属性, 例如外键限制, 触发器, 或者是存储过程. 当在一个数据迁移文件(migration)中, 你可以生成定制的SQL语句, 数据库模式输出中不能够重新组成那些来自数据库的语句. 如果你正在使用类似于这种特性, 那么你应该设置数据库模式的格式为 :sql.
等价于使用Active Record 的数据库模式输出的另外一个情况是, 数据库的结构将会被使用一个工具来输出(通过 db:structure:dump 的Rake task) 把数据库的详细描述都放到 db/structure.sql 文件里.例如, 对于 PostgreSQL, 使用的是 pg_dump 工具. 对于 MySQL, 这个文件将包含 SHOW CREATE TABLE 对于多个数据表的输出内容.
载入这些数据库模式是一个相对简单的问题, 用来生成他们所包含的SQL语句. 在定义上, 这是会创建一个完美的数据库结构拷贝. 然而使用:sql模式格式将会阻止载入数据库模式到一个 RDBMS(关系型数据库)中, 除了是用来创建它的数据库.
7.3 Schema Dumps and Source Control 数据库模式的输出和代码控制
因为数据库模式输出是验证过的代码, 所以强烈建议你用代码控制来管理他们.
相关推荐
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
gem "mongoid_rails_migrations" 如何使用 创建迁移 $ rails generate mongoid:migration 运行迁移: $ rails db:migrate $ rails db:migrate:down VERSION= $ rails db:migrate:up VERSION= $ rails db:rollback...
mongoid_rails_migrations, Mongoid的数据迁移 发行说明最新版本,1.1. x, 目标 Mongoid> = 4.0.0和 Rails> = 4.2.0.对于 Rails> = 3.2.0和 Mongoid> = 3.0.0,使用版本 1.0.0.
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with...
卡桑德拉迁移描述Cassandra Migrations是用于Rails应用程序的Cassandra数据库架构迁移库。 该宝石提供: 多环境数据库配置版本化的CQL模式迁移管理用于简化迁移代码的模式修改DSL 用于数据库架构管理的瑞克任务支持...
rails-angular-postgres-and-bootstrap-second-edition 英文原版
“PHP 数据库迁移”使您能够拥有不同版本的数据库模式。 每个迁移文件都会改变架构,这个概念来自 Ruby on Rails。 可以在所有数据库服务器上执行迁移,确保一致性和可靠性。
Rails, Angular, Postgres, and Bootstrap(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
While most books written about Rails cater to ...Once you complete Learning Rails, you'll be comfortable working with the Rails web framework, and you'll be well on your way to becoming a Rails guru.
NULL 博文链接:https://like-eagle.iteye.com/blog/994634
Rails, Angular, Postgres, and Bootstrap(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,...
Rails, Angular, Postgres, and Bootstrap(2nd),电子书实战版
From the latest Ajax effects to time-saving automation tips for your development process, "Rails Recipes" will show you how the experts have already solved the problems you have. Use generators to ...
You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...
Rails数据库模式的Autocomplete +提供程序。 特征 自动完成活动记录 根据当前上下文打开模式文件 设定值 将Rails语法设置为默认语法。 " * " : core : customFileTypes : " source.ruby.rails " : [ " rb " ...
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 ...
It’s true: Rails enables you to create database-driven web applications with remarkable speed and ease. And like many other open source projects, Rails has been most easily adopted by individuals ...
With the intuitive, straightforward nature of Ruby and the development platform provided by Rails, you can put together full-fledged web applications quickly, even if you're new to web programming. ...
In Part I, you'll learn Ruby and Rails fundamentals and then dive straight into models, controllers, views, and deployment. In Part II, you'll take your skills to the next level as you build a social...