Migration

1、Generate migrations

l         You can use the “ruby script/generate model model-name” command to generate model and the corresponding migration file.

l         You can also generate a migration on its own command:ruby script/generate migration migration-name

2、Running Migrations

       rake db:migrate [VERSION=23]

3、API of ActiveRecord::Migration class

l         Column Types

The common options of column:

:null=>true or false(If false,the underlying column has a not null constraint added)

:limit=>size(Requests a maximum column length (:string, :text, :binary or :integer columns only))

:default=>value

l         add_column

# Adds a new column to the named table.

# See TableDefinition#column for details of the options you can use.

def add_column(table_name, column_name, type, options = {})

       #.....

end

example:

add_column :orders,:name,:string,:limit=>100,:null=>false

add_column :orders, :age, :integer

add_column :orders, :ship_class, :string, :limit => 15, :default => 'priority'

add_column :orders, :price, :decimal, :precision => 8, :scale => 2

add_column :meter, :reading, :decimal, :precision => 24, :scale => 0

l         remove_column

def remove_column(table_name, column_name)

       #....

end

example:

remove_column :products,:price

l         rename_column

def rename_column(table, column, new_column_name)

     execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'"

end

example:

rename_column :orders, :e_mail, :customer_email

l         change_column

Changes the column’s definition according to the new options.

l         create_table

options for create_table

:force=>false or true(If you specify true,the migration will drop an existing table of the same name before creating the new one)

:temporary=>true or false(if youo specify true,the migration will create a temporary table)

:options=>”XXX”

options=>”auto_increment=10000”

example:

create_table :tickets,:options=>”auto_increment=10000” do |t|

       t.column :column_name,:column_type

       …….

end

l         drop_table

l         rename_table

rename_table(name, new_name)

l         add_index:define an index

add_index(table_name, column_name, options = {})

l         remove_index

l         primary_key

example:

create_table :tickets, :primary_key => :number do |t|

t.column :created_at, :timestamp

t.column :description, :text

end

 

Tables with no primary key

create_table :authors_books, :id => false do |t|

t.column :author_id, :integer, :null => false

t.column :book_id, :integer, :null => false

end

4、Using Native SQL

       You can use “execute” method to execute native sql.For example:

execute “alter table line_items add constraint fk_line_item_products foreign key (product_id) references products(id)”

In this example you use ‘alter’ command to add a foreign key for line_items table.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在计算机领域,live migration(实时迁移)是指在不中断正在运行的虚拟机的情况下将其从一台物理主机迁移到另一台物理主机的过程。这种迁移可以在虚拟机继续运行的同时进行,对用户来说是透明的。 live migration通常用于以下情况: 1. 负载均衡:通过将虚拟机从一个物理主机迁移到另一个物理主机,可以实现负载均衡,从而更好地利用资源。 2. 硬件维护:当需要对物理主机进行维护或升级时,可以将虚拟机迁移到其他物理主机上,以避免服务中断。 3. 故障恢复:当一台物理主机发生故障时,可以将其上的虚拟机迁移到其他正常运行的物理主机上,以实现快速恢复。 在live migration过程中,需要考虑一些参数来控制迁移的行为。其中,live_migration_downtime(实时迁移停机时间)表示迁移过程中虚拟机的停机时间,live_migration_downtime_steps(实时迁移停机时间步长)表示迁移过程中每个步骤的停机时间,live_migration_downtime_delay(实时迁移停机时间延迟)表示在迁移过程中每个步骤之间的延迟时间。 以下是一个live migration的示例代码: ```python live_migration_downtime = 5000 live_migration_downtime_steps = 7 live_migration_downtime_delay = 75 # 进行live migration def live_migration(): # 实现live migration的代码逻辑 pass # 设置live migration参数 def set_live_migration_parameters(downtime, steps, delay): live_migration_downtime = downtime live_migration_downtime_steps = steps live_migration_downtime_delay = delay # 调用live migration函数 live_migration() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值