- 浏览: 2543482 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Rails Study(VI)Migrations
1 Anatomy of a Migration
A primary key column called id will be added, this is the default we do not need to ask for this.
The timestamp columns created_at and updated_at which Active Record populates automatically will also be added.
1.1 Migrations are Classes
A migration is a subclass of ActiveRecord::Migration that implements two class methods:
up ----- perform the required transformations
down-- revert them
1.2 What's in a Name
1.3 Changing Migrations
db:rollback ---> db:migrate
2 Creating a Migration
2.1 Creating a Model
>rails generate model Product name:string description:text
The migration class will be created.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
or
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :products
end
end
2.2 Creating a Standalone Migration
If you are creating migrations for other purposes (for example to add a column to an existing table)
>rails generate migration AddPartNumberToProducts
or
>rails generate migration AddPartNumberToProducts part_number:string
class AddPartNumberToProduct < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
>rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProduct < ActiveRecord::Migration
def up
remove_column :products, :part_number
end
def down
add_column :products, :part_number, :string
end
end
>rails generate migration AddDetailsToProducts part_number:string price:decimal
class AddDetailToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
end
end
3. Writing a Migration
3.1 Creating a Table
create_table :products do |t|
t.string :name
t.text :description
t.string :nickname
t.timestamps
end
By default create_table will create a primary key called id. You can change the name of the primary key with the :primary_key option
If you don't want a primary key at all, you can pass :id => false.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products, :primary_key => "productId", :force => true,
:primary_column => {:type => :integer, :limit => 2, :scale=>3} do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
If you need to pass database specific options you can place an SQL fragment in theptions option.
create_table :products,ptions => "ENGINE=BLACKHOLE" do |t|
t.string :name, :null => false
end
will append ENGINE=BLACKHOLE to the SQL statement used to create the table (when using MySQL the default is ENGINE=InnoDB).
The types supported by Active Record are
:primary_key
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
3.2 Changing Tables
change_table :products do |t|
t.remove :description, :name #removes the description and name columns
t.string :part_number
t.index :part_number #add index on part_number
t.rename :upccode, :upc_code
end
eq
remove_column :products, :description
remove_column :products, :name
add_column :products, :part_number, :string
add_index :products, :part_number
rename_column :products, :upccode, :upc_code
3.3 Special Helpers
create_table :products do |t|
t.references :category # one category many products
end
It will create a category_id column.
3.4 Writing Your down Method
4. Running Migrations
Running the db:migrate also invokes the db:schema:dump task, which will update our db/schema.rb file to match the structure of our database.
>rake db:migrate VERSION=20110804142910
This will run the up method on all migrations up to and including 20110804142910. If migrating downwards this will run the down method on all the migrations down to, but not including, 20110804142910.
4.1 Rolling Back
>rake db:rollback
A common task is to rollback the last migration.
>rake db:rollback STEP=3
Undo serveral migrations.
>rake db:reset # This will drop the database, recreate it and load the current schema into it.
4.2 Being Specific
>rake db:migrate:up VERSION=20110804142910
4.3 Being Talkative
To make some output in migration class.
5. Using Models in Your Migrations
5.1 Dealing with Changing Models
class AddPartNumberToProducts < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def self.up
add_column :product, :part_number, :string
Product.reset_column_information
...
end
def self.down
...
end
end
6. Schema Dumping and You
6.1 What are Schema Files for?
6.2 Types of Schema Dumps
db/schema.rb
6.3 Schema Dumps and Source Control
references:
http://guides.rubyonrails.org/migrations.html
1 Anatomy of a Migration
A primary key column called id will be added, this is the default we do not need to ask for this.
The timestamp columns created_at and updated_at which Active Record populates automatically will also be added.
1.1 Migrations are Classes
A migration is a subclass of ActiveRecord::Migration that implements two class methods:
up ----- perform the required transformations
down-- revert them
1.2 What's in a Name
1.3 Changing Migrations
db:rollback ---> db:migrate
2 Creating a Migration
2.1 Creating a Model
>rails generate model Product name:string description:text
The migration class will be created.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
or
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :products
end
end
2.2 Creating a Standalone Migration
If you are creating migrations for other purposes (for example to add a column to an existing table)
>rails generate migration AddPartNumberToProducts
or
>rails generate migration AddPartNumberToProducts part_number:string
class AddPartNumberToProduct < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
>rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProduct < ActiveRecord::Migration
def up
remove_column :products, :part_number
end
def down
add_column :products, :part_number, :string
end
end
>rails generate migration AddDetailsToProducts part_number:string price:decimal
class AddDetailToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_column :products, :price, :decimal
end
end
3. Writing a Migration
3.1 Creating a Table
create_table :products do |t|
t.string :name
t.text :description
t.string :nickname
t.timestamps
end
By default create_table will create a primary key called id. You can change the name of the primary key with the :primary_key option
If you don't want a primary key at all, you can pass :id => false.
class CreateProducts < ActiveRecord::Migration
def change
create_table :products, :primary_key => "productId", :force => true,
:primary_column => {:type => :integer, :limit => 2, :scale=>3} do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
If you need to pass database specific options you can place an SQL fragment in theptions option.
create_table :products,ptions => "ENGINE=BLACKHOLE" do |t|
t.string :name, :null => false
end
will append ENGINE=BLACKHOLE to the SQL statement used to create the table (when using MySQL the default is ENGINE=InnoDB).
The types supported by Active Record are
:primary_key
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
3.2 Changing Tables
change_table :products do |t|
t.remove :description, :name #removes the description and name columns
t.string :part_number
t.index :part_number #add index on part_number
t.rename :upccode, :upc_code
end
eq
remove_column :products, :description
remove_column :products, :name
add_column :products, :part_number, :string
add_index :products, :part_number
rename_column :products, :upccode, :upc_code
3.3 Special Helpers
create_table :products do |t|
t.references :category # one category many products
end
It will create a category_id column.
3.4 Writing Your down Method
4. Running Migrations
Running the db:migrate also invokes the db:schema:dump task, which will update our db/schema.rb file to match the structure of our database.
>rake db:migrate VERSION=20110804142910
This will run the up method on all migrations up to and including 20110804142910. If migrating downwards this will run the down method on all the migrations down to, but not including, 20110804142910.
4.1 Rolling Back
>rake db:rollback
A common task is to rollback the last migration.
>rake db:rollback STEP=3
Undo serveral migrations.
>rake db:reset # This will drop the database, recreate it and load the current schema into it.
4.2 Being Specific
>rake db:migrate:up VERSION=20110804142910
4.3 Being Talkative
To make some output in migration class.
5. Using Models in Your Migrations
5.1 Dealing with Changing Models
class AddPartNumberToProducts < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def self.up
add_column :product, :part_number, :string
Product.reset_column_information
...
end
def self.down
...
end
end
6. Schema Dumping and You
6.1 What are Schema Files for?
6.2 Types of Schema Dumps
db/schema.rb
6.3 Schema Dumps and Source Control
references:
http://guides.rubyonrails.org/migrations.html
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 468NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 330Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 430Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 377Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 469NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 415Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 332Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 243GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 446GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 321GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 307Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 313Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 288Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 303Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 282NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 258Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 566NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 258Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 362Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 366Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
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.
此外,Rails的迁移(Migrations)系统允许你对数据库结构进行修改,如创建新表: ```bash rake db:create # 创建数据库 rake db:migrate # 应用迁移文件,生成对应的数据库表 ``` Rails的scaffold命令是一个强大的...
12. **Migrations**:Rails的迁移机制允许开发者通过Ruby代码来管理数据库结构的变化,使得数据库版本控制变得简单。 学习Rails指南中文版,开发者将全面了解Rails的各个方面,包括基础概念、核心组件、最佳实践...
4. **Rails 数据库迁移(Migrations)**: Migrations 是 Rails 中用于数据库结构版本控制的工具。它们允许开发者以编程方式添加、修改或删除表列,而不必直接操作 SQL。文件将涵盖创建、运行和回滚迁移,以及使用 `...
本书《Component-Based Rails Applications》主要介绍了如何使用Rails引擎(Rails Engine)进行基于组件的Rails应用开发,以及如何对应用程序的大型模块进行拆分和模块化。以下是书中一些核心知识点的详细说明: 1....
2. **数据库迁移(Database Migrations)**:Rails使用ActiveRecord和SQLite、MySQL或PostgreSQL等数据库系统交互。开发者通过编写迁移文件来定义和修改数据库结构。 3. **路由(Routing)**:Rails的路由系统根据...
- **数据库迁移(Database Migrations)**:Rails使用迁移来管理数据库结构的变化。通过`rails generate migration AddTitleToPosts title:string`创建迁移,然后运行`rails db:migrate`执行迁移。 - **...
《Rails101_by_rails4.0》是一本专注于Rails 4.0.0版本和Ruby 2.0.0版本的自学教程书籍,它定位于中文读者,旨在成为学习Rails框架的参考教材。Rails(Ruby on Rails)是一个采用Ruby语言编写的开源Web应用框架,它...
Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...
4. **db**:数据库相关的文件,如迁移(migrations)用于数据库结构的版本控制。 5. **lib**:自定义库和扩展代码存放的地方。 6. **log**:应用的日志文件。 7. **public**:静态资源,如图片、CSS和JavaScript...
6. **数据库迁移(Migrations)** Rails使用迁移来管理数据库结构的变化。`rails generate migration AddColumnToBooks column:type`会生成一个新的迁移文件,然后通过`rails db:migrate`执行迁移,更新数据库。 7...
Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...
- Migrations:用于数据库结构变更的Ruby脚本,通过`rake db:migrate`执行。 - Associations:定义模型之间的关系,如has_many、belongs_to等。 - Validations:在保存到数据库前验证模型数据的有效性。 5. **...
然后,配置数据库连接,编写数据库迁移(migrations)以定义数据表结构,执行`rake db:migrate`来应用这些迁移。 在模型中,我们将定义属性和关系,如`attr_accessor`或`has_many`,ActiveRecord会自动处理与数据库...
**基于UTC的迁移(UTC-based migrations)** 为了确保数据的一致性和准确性,Rails 2.1现在支持基于协调世界时(UTC)的数据库迁移。这种改进有助于避免因不同的服务器时区设置而引起的潜在问题。 **更好的缓存...
7. **Migrations and Database Schema**: 书中详细讲解了如何使用Rails的迁移(migrations)来管理数据库结构的变化,以及如何编写有效的数据库模式(schema)。 8. **Testing**: 对于Rails应用,测试是必不可少的一...
从给定的文件信息来看,我们正在探讨的是一本关于Ruby on Rails的书籍,书名为《Simply Rails2》,作者是Patrick Lenz。本书旨在为初学者提供深入理解Ruby on Rails框架的指南,从基础概念到高级主题均有涵盖,是...
在开发Web应用时,Ruby on Rails(简称Rails)框架因其高效、简洁的代码风格和强大的社区支持而备受青睐。Aptana是一款强大的集成开发环境(IDE),尤其适用于Rails项目的开发,它提供了丰富的特性来提升开发效率。...
标题 "Rails" 指的是 Ruby on Rails,一个开源的Web应用程序框架,它基于Ruby编程语言,遵循MVC(模型-视图-控制器)架构模式。Rails由David Heinemeier Hansson在2004年创建,其设计理念是强调代码的简洁性、DRY...