Hiển thị cột nhận xét mysql

Di chuyển là một cách thuận tiện để thay đổi lược đồ cơ sở dữ liệu của bạn theo thời gian một cách nhất quán. Họ sử dụng Ruby DSL để bạn không phải viết SQL bằng tay, cho phép lược đồ và các thay đổi của bạn độc lập với cơ sở dữ liệu

Bạn có thể coi mỗi lần di chuyển là một 'phiên bản' mới của cơ sở dữ liệu. Một lược đồ bắt đầu không có gì trong đó và mỗi lần di chuyển sẽ sửa đổi lược đồ đó để thêm hoặc xóa bảng, cột hoặc mục nhập. Active Record biết cách cập nhật lược đồ của bạn theo dòng thời gian này, đưa lược đồ từ bất kỳ thời điểm nào trong lịch sử lên phiên bản mới nhất. Active Record cũng sẽ cập nhật tệp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 của bạn để phù hợp với cấu trúc cập nhật của cơ sở dữ liệu của bạn

Đây là một ví dụ về di chuyển

class CreateProducts < ActiveRecord::Migration[7.1]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end
Bản sao

Lần di chuyển này thêm một bảng có tên là

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
1 với một cột chuỗi có tên là
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
2 và một cột văn bản có tên là
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
3. Một cột khóa chính có tên là
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
4 cũng sẽ được thêm hoàn toàn, vì đây là khóa chính mặc định cho tất cả các mô hình Bản ghi Hoạt động. Macro
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
5 thêm hai cột,
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
6 và
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
7. Các cột đặc biệt này được Active Record tự động quản lý nếu chúng tồn tại

Lưu ý rằng chúng tôi xác định thay đổi mà chúng tôi muốn xảy ra trong tương lai. Trước khi quá trình di chuyển này được chạy, sẽ không có bảng. Sau đó, bảng sẽ tồn tại. Active Record cũng biết cách đảo ngược quá trình di chuyển này. nếu chúng tôi quay lại quá trình di chuyển này, nó sẽ xóa bảng

Trên cơ sở dữ liệu hỗ trợ các giao dịch với các câu lệnh thay đổi lược đồ, quá trình di chuyển được bao bọc trong một giao dịch. Nếu cơ sở dữ liệu không hỗ trợ điều này thì khi quá trình di chuyển không thành công, các phần của cơ sở dữ liệu đã thành công sẽ không được khôi phục. Bạn sẽ phải khôi phục các thay đổi được thực hiện thủ công

Có một số truy vấn không thể chạy bên trong một giao dịch. Nếu bộ điều hợp của bạn hỗ trợ các giao dịch DDL, bạn có thể sử dụng

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
8 để tắt chúng cho một lần di chuyển

Nếu bạn muốn di chuyển để thực hiện điều gì đó mà Bản ghi Hoạt động không biết cách đảo ngược, bạn có thể sử dụng

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
9

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
Bản sao

Quá trình di chuyển này sẽ thay đổi loại cột

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
0 thành chuỗi hoặc trở lại thành số nguyên khi quá trình di chuyển được hoàn nguyên. Lưu ý rằng khối được chuyển đến
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
1 và
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
2 tương ứng

Ngoài ra, bạn có thể sử dụng

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 và
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 thay vì
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
Bản sao

Quá trình di chuyển được lưu trữ dưới dạng tệp trong thư mục

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
6, một tệp cho mỗi lớp di chuyển. Tên của tệp có dạng
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
7, nghĩa là dấu thời gian UTC xác định quá trình di chuyển, theo sau là dấu gạch dưới, sau đó là tên của quá trình di chuyển. Tên của lớp di chuyển (phiên bản CamelCased) phải khớp với phần sau của tên tệp. Ví dụ:
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
8 nên xác định lớp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
9 và
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
0 nên xác định
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
1. Rails sử dụng dấu thời gian này để xác định di chuyển nào sẽ được chạy và theo thứ tự nào, vì vậy nếu bạn đang sao chép di chuyển từ ứng dụng khác hoặc tự tạo tệp, hãy lưu ý vị trí của nó theo thứ tự

Tất nhiên, việc tính toán dấu thời gian không có gì thú vị, vì vậy Active Record cung cấp một trình tạo để xử lý việc tạo dấu thời gian cho bạn

$ bin/rails generate migration AddPartNumberToProducts
Bản sao

Điều này sẽ tạo ra một di chuyển trống được đặt tên thích hợp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
Bản sao

Trình tạo này có thể làm được nhiều việc hơn là thêm dấu thời gian vào tên tệp. Dựa trên các quy ước đặt tên và các đối số bổ sung (tùy chọn), nó cũng có thể bắt đầu hoàn thiện quá trình di chuyển

Nếu tên di chuyển có dạng "AddColumnToTable" hoặc "RemoveColumnFromTable" và theo sau là danh sách tên và loại cột thì quá trình di chuyển có chứa các câu lệnh và thích hợp sẽ được tạo

$ bin/rails generate migration AddPartNumberToProducts part_number:string
Bản sao

Điều này sẽ tạo ra sự di chuyển sau

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
Bản sao

Nếu bạn muốn thêm một chỉ mục trên cột mới, bạn cũng có thể làm điều đó

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
Bản sao

Điều này sẽ tạo ra các báo cáo thích hợp và

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
Bản sao

Bạn không bị giới hạn ở một cột được tạo kỳ diệu. Ví dụ

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
Bản sao

Sẽ tạo một di chuyển lược đồ bổ sung thêm hai cột vào bảng

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
1

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
0Bản sao

Tương tự, bạn có thể tạo di chuyển để xóa một cột khỏi dòng lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
1Bản sao

Điều này tạo ra các báo cáo thích hợp

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
2Bản sao

Nếu tên di chuyển có dạng "CreateXXX" và theo sau là danh sách tên và loại cột thì quá trình di chuyển tạo bảng XXX với các cột được liệt kê sẽ được tạo. Ví dụ

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
3Bản sao

tạo ra

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
4Bản sao

Như mọi khi, những gì được tạo ra cho bạn chỉ là điểm khởi đầu. Bạn có thể thêm hoặc xóa khỏi nó khi bạn thấy phù hợp bằng cách chỉnh sửa tệp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
8

Ngoài ra, trình tạo chấp nhận loại cột là

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
9 (cũng có sẵn là
$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
0). Ví dụ,

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
5Bản sao

tạo cuộc gọi sau

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
6Bản sao

Quá trình di chuyển này sẽ tạo cột

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
2. là cách viết tắt để tạo cột, chỉ mục, khóa ngoại hoặc thậm chí là cột liên kết đa hình

Ngoài ra còn có một trình tạo sẽ tạo các bảng tham gia nếu

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
3 là một phần của tên

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
7Bản sao

sẽ tạo ra sự di chuyển sau

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
8Bản sao

Trình tạo mô hình, tài nguyên và giàn giáo sẽ tạo ra các di chuyển phù hợp để thêm một mô hình mới. Quá trình di chuyển này sẽ chứa các hướng dẫn để tạo bảng có liên quan. Nếu bạn cho Rails biết cột nào bạn muốn, thì các câu lệnh để thêm các cột này cũng sẽ được tạo. Ví dụ, chạy

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
9Bản sao

Điều này sẽ tạo ra một di chuyển giống như thế này

class CreateProducts < ActiveRecord::Migration[7.1]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description

      t.timestamps
    end
  end
end
Bản sao

Bạn có thể nối bao nhiêu cặp tên/loại cột tùy thích

Một số thường được sử dụng có thể được chuyển trực tiếp trên dòng lệnh. Chúng được bao quanh bởi dấu ngoặc nhọn và theo kiểu trường

Ví dụ, chạy

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
1Bản sao

sẽ tạo ra một di chuyển giống như thế này

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
2Bản sao

Hãy xem đầu ra trợ giúp của máy phát điện (

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
4) để biết thêm chi tiết

Khi bạn đã tạo quá trình di chuyển của mình bằng một trong các trình tạo, đã đến lúc bắt đầu làm việc

Phương pháp này là một trong những phương pháp cơ bản nhất, nhưng hầu hết thời gian, sẽ được tạo cho bạn từ việc sử dụng một mô hình, tài nguyên hoặc trình tạo giàn giáo. Một cách sử dụng điển hình sẽ là

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
3Bản sao

Phương pháp này tạo một bảng

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
1 với một cột có tên là
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
2

Theo mặc định,

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
5 sẽ ngầm tạo một khóa chính có tên là
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
4 cho bạn. Bạn có thể thay đổi tên của cột bằng tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
00 hoặc nếu bạn hoàn toàn không muốn có khóa chính, bạn có thể chuyển tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
01

Nếu bạn cần chuyển các tùy chọn cụ thể của cơ sở dữ liệu, bạn có thể đặt một đoạn SQL trong tùy chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
02. Ví dụ

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
4Bản sao

Điều này sẽ nối thêm

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
03 vào câu lệnh SQL được sử dụng để tạo bảng

Một chỉ mục có thể được tạo trên các cột được tạo trong khối

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
5 bằng cách chuyển
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
05 hoặc một hàm băm tùy chọn cho tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
06

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
5Bản sao

Ngoài ra, bạn có thể chuyển tùy chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
07 với bất kỳ mô tả nào cho bảng sẽ được lưu trữ trong chính cơ sở dữ liệu và có thể được xem bằng các công cụ quản trị cơ sở dữ liệu, chẳng hạn như MySQL Workbench hoặc PgAdmin III. Bạn nên chỉ định nhận xét trong quá trình di chuyển cho các ứng dụng có cơ sở dữ liệu lớn vì nó giúp mọi người hiểu mô hình dữ liệu và tạo tài liệu. Hiện tại chỉ có bộ điều hợp MySQL và PostgreSQL hỗ trợ nhận xét

Phương thức di chuyển tạo bảng tham gia HABTM (có và thuộc về nhiều). Một cách sử dụng điển hình sẽ là

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
6Bản sao

Việc di chuyển này sẽ tạo một bảng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
09 với hai cột có tên là
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
10 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
11

Các cột này có tùy chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
12 được đặt thành
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
13 theo mặc định, nghĩa là bạn phải cung cấp một giá trị để lưu bản ghi vào bảng này. Điều này có thể được ghi đè bằng cách chỉ định tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
14

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
7Bản sao

Theo mặc định, tên của bảng tham gia xuất phát từ sự kết hợp của hai đối số đầu tiên được cung cấp cho create_join_table, theo thứ tự bảng chữ cái

Để tùy chỉnh tên của bảng, hãy cung cấp tùy chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
15

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
8Bản sao

Điều này đảm bảo tên của bảng tham gia là

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
16 theo yêu cầu

Ngoài ra,

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
08 chấp nhận một khối mà bạn có thể sử dụng để thêm các chỉ mục (không được tạo theo mặc định) hoặc bất kỳ cột bổ sung nào mà bạn chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
9Bản sao

Nếu bạn muốn thay đổi một bảng hiện có tại chỗ, có

Nó được sử dụng theo cách tương tự như

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
5 nhưng đối tượng được tạo ra bên trong khối có quyền truy cập vào một số chức năng đặc biệt, chẳng hạn

$ bin/rails generate migration AddPartNumberToProducts
0Bản sao

Quá trình di chuyển này sẽ xóa các cột

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
3 và
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
2, tạo một cột chuỗi mới có tên là
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
22 và thêm một chỉ mục vào đó. Cuối cùng, nó đổi tên cột
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
23 thành
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
24

Tương tự như các phương thức

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
3 và
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
2 mà chúng tôi đã đề cập, Rails cũng cung cấp phương thức di chuyển

$ bin/rails generate migration AddPartNumberToProducts
1Bản sao

Điều này thay đổi cột

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
22 trên bảng sản phẩm thành trường
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
29

Lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
27 là không thể đảo ngược. Bạn nên cung cấp dịch chuyển
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
9 của riêng mình, như chúng ta đã thảo luận

Bên cạnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
27, các phương thức và được sử dụng cụ thể để thay đổi một ràng buộc null và các giá trị mặc định của một cột

$ bin/rails generate migration AddPartNumberToProducts
2Bản sao

Thao tác này đặt trường

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
35 trên sản phẩm thành cột
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
36 và giá trị mặc định của trường
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
37 từ đúng thành sai. Cả hai thay đổi này sẽ chỉ được áp dụng cho các giao dịch trong tương lai, mọi bản ghi hiện tại không áp dụng

Khi đặt ràng buộc null thành true, điều này có nghĩa là cột đó sẽ chấp nhận giá trị null, nếu không, ràng buộc

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
36 được áp dụng và giá trị phải được chuyển để duy trì bản ghi vào cơ sở dữ liệu

Bạn cũng có thể viết quá trình di chuyển

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
34 ở trên là
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
40, nhưng không giống như ví dụ trước, điều này sẽ khiến quá trình di chuyển của bạn không thể đảo ngược

Công cụ sửa đổi cột có thể được áp dụng khi tạo hoặc thay đổi cột

  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    41 Thêm nhận xét cho cột
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    42 Chỉ định đối chiếu cho cột
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    43 hoặc
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    44
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    45 Cho phép đặt giá trị mặc định trên cột. Lưu ý rằng nếu bạn đang sử dụng một giá trị động (chẳng hạn như ngày), giá trị mặc định sẽ chỉ được tính lần đầu tiên (i. e. vào ngày di chuyển được áp dụng). Sử dụng
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    46 cho
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    47
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    48 Đặt số ký tự tối đa cho cột
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    43 và số byte tối đa cho cột
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    50
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    51 Cho phép hoặc không cho phép giá trị
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    47 trong cột
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    53 Chỉ định độ chính xác cho cột
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    54
  • class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    55 Chỉ định tỷ lệ cho các cột
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    56 và
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def change
        reversible do |direction|
          change_table :products do |t|
            direction.up   { t.change :price, :string }
            direction.down { t.change :price, :integer }
          end
        end
      end
    end
    
    57, biểu thị số chữ số sau dấu thập phân

Đối với

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
2 hoặc
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
27 không có tùy chọn thêm chỉ mục. Chúng cần được thêm riêng bằng cách sử dụng
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
5

Một số bộ điều hợp có thể hỗ trợ các tùy chọn bổ sung;

Không thể chỉ định

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
51 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
45 qua dòng lệnh khi tạo di chuyển

Phương thức

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
1 cho phép tạo một cột được đặt tên phù hợp đóng vai trò là kết nối giữa một hoặc nhiều liên kết

$ bin/rails generate migration AddPartNumberToProducts
3Bản sao

Việc di chuyển này sẽ tạo một cột

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
64 trong bảng người dùng. Nó cũng tạo một chỉ mục cho cột này, trừ khi được thông báo rõ ràng là không với tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
65

Phương thức

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
66 là bí danh của
$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
1

$ bin/rails generate migration AddPartNumberToProducts
4Bản sao

Tùy chọn đa hình sẽ tạo hai cột trên bảng gắn thẻ có thể được sử dụng cho các liên kết đa hình.

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
68 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
69

Khóa ngoại có thể được tạo bằng tùy chọn

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
70

$ bin/rails generate migration AddPartNumberToProducts
5Bản sao

Để biết thêm các tùy chọn

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
1, hãy truy cập

Tài liệu tham khảo cũng có thể được gỡ bỏ

$ bin/rails generate migration AddPartNumberToProducts
6Bản sao

Mặc dù không bắt buộc, nhưng bạn có thể muốn thêm các ràng buộc khóa ngoại vào

$ bin/rails generate migration AddPartNumberToProducts
7Bản sao

Cuộc gọi này thêm một ràng buộc mới vào bảng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
73. Ràng buộc đảm bảo rằng một hàng trong bảng
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
74 tồn tại trong đó cột
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
4 khớp với
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
76

Nếu tên cột

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
77 không thể bắt nguồn từ tên
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
78, bạn có thể sử dụng tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
79. Sử dụng tùy chọn
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
00 nếu khóa chính được tham chiếu không phải là
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
81

Ví dụ: để thêm khóa ngoại trên

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
82 tham chiếu
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
83

$ bin/rails generate migration AddPartNumberToProducts
8Bản sao

Điều này sẽ thêm một ràng buộc vào bảng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
73 để đảm bảo một hàng trong bảng
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
74 tồn tại khi cột
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
86 khớp với trường
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
82

Một số tùy chọn khác như

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
2,
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
89,
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
90,
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
91 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
92 được hỗ trợ bởi
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
72

Khóa ngoại cũng có thể được xóa bằng cách sử dụng

$ bin/rails generate migration AddPartNumberToProducts
9Bản sao

Active Record chỉ hỗ trợ các khóa ngoại cột đơn.

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
95 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
96 được yêu cầu sử dụng khóa ngoại tổng hợp. Nhìn thấy

Nếu những người trợ giúp do Active Record cung cấp không đủ, bạn có thể sử dụng phương thức này để thực thi SQL tùy ý

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
0Bản sao

Để biết thêm chi tiết và ví dụ về các phương pháp riêng lẻ, hãy xem tài liệu API

Cụ thể là tài liệu cho

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
98, cung cấp các phương pháp có sẵn trong các phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5,
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 và
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4

Để biết các phương pháp có sẵn liên quan đến đối tượng do

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
5 tạo ra, hãy xem
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
03

Và đối với đối tượng được cung cấp bởi

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
18, xem
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
05

Phương pháp

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 là cách chính để viết di chuyển. Nó hoạt động cho phần lớn các trường hợp trong đó Bản ghi hoạt động biết cách tự động đảo ngược các hành động của quá trình di chuyển. Dưới đây là một số hành động mà
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 hỗ trợ

cũng có thể đảo ngược, miễn là khối chỉ gọi các hoạt động có thể đảo ngược như các thao tác được liệt kê ở trên

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
3 có thể đảo ngược nếu bạn cung cấp loại cột làm đối số thứ ba. Cung cấp cả các tùy chọn cột ban đầu, nếu không Rails không thể tạo lại cột chính xác khi khôi phục

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
1Bản sao

Nếu bạn cần sử dụng bất kỳ phương pháp nào khác, bạn nên sử dụng phương pháp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
9 hoặc viết phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 và
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 thay vì sử dụng phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5

Quá trình di chuyển phức tạp có thể yêu cầu xử lý mà Active Record không biết cách đảo ngược. Bạn có thể sử dụng để chỉ định những việc cần làm khi chạy di chuyển và những việc khác cần làm khi hoàn nguyên nó. Ví dụ

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
2Bản sao

Sử dụng

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
9 cũng sẽ đảm bảo rằng các hướng dẫn cũng được thực hiện theo đúng thứ tự. Nếu ví dụ di chuyển trước đó được hoàn nguyên, khối
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 sẽ được chạy sau khi cột
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
19 bị xóa và ngay trước khi bảng
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
20 bị xóa

Bạn cũng có thể sử dụng kiểu di chuyển cũ bằng phương pháp

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 và
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 thay vì phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5

Phương pháp

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 sẽ mô tả sự chuyển đổi mà bạn muốn thực hiện đối với lược đồ của mình và phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 trong quá trình di chuyển của bạn sẽ hoàn nguyên các phép biến đổi được thực hiện bởi phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3. Nói cách khác, lược đồ cơ sở dữ liệu sẽ không thay đổi nếu bạn thực hiện một
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 theo sau là một
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4

Ví dụ, nếu bạn tạo một bảng trong phương thức

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3, bạn nên thả nó vào phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4. Sẽ là khôn ngoan khi thực hiện các phép biến đổi theo đúng thứ tự ngược lại mà chúng được thực hiện trong phương pháp
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3. Ví dụ trong phần
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
9 tương đương với

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
3Bản sao

Đôi khi quá trình di chuyển của bạn sẽ làm điều gì đó đơn giản là không thể đảo ngược được;

Trong những trường hợp như vậy, bạn có thể tăng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
35 trong khối
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 của mình

Nếu ai đó cố hoàn nguyên quá trình di chuyển của bạn, một thông báo lỗi sẽ hiển thị nói rằng không thể thực hiện được

Bạn có thể sử dụng khả năng của Bản ghi Hoạt động để khôi phục quá trình di chuyển bằng phương pháp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
4Bản sao

Phương pháp

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
37 cũng chấp nhận một khối hướng dẫn để đảo ngược. Điều này có thể hữu ích để hoàn nguyên các phần đã chọn của các lần di chuyển trước đó

Ví dụ: hãy tưởng tượng rằng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
39 đã được cam kết và sau đó người ta quyết định rằng tốt nhất nên sử dụng xác thực Bản ghi đang hoạt động, thay cho ràng buộc
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
40, để xác minh mã zip

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
5Bản sao

Việc di chuyển tương tự cũng có thể được viết mà không cần sử dụng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
37 nhưng điều này sẽ bao gồm một vài bước nữa

  1. Đảo ngược thứ tự của
    $ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
    
    5 và
    class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
      def change
        add_column :products, :part_number, :string
      end
    end
    
    9
  2. Thay thế
    $ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
    
    5 bằng
    class ChangeProductsPrice < ActiveRecord::Migration[7.1]
      def up
        change_table :products do |t|
          t.change :price, :string
        end
      end
    
      def down
        change_table :products do |t|
          t.change :price, :integer
        end
      end
    end
    
    45
  3. Cuối cùng, thay thế
    $ bin/rails generate migration AddPartNumberToProducts part_number:string:index
    
    3 bằng
    $ bin/rails generate migration AddPartNumberToProducts part_number:string:index
    
    4 và ngược lại

Tất cả điều này được chăm sóc bởi

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
37

Rails cung cấp một bộ lệnh để chạy một số bộ di chuyển nhất định

Lệnh đường ray liên quan đến di chuyển đầu tiên bạn sẽ sử dụng có thể sẽ là

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49. Ở dạng cơ bản nhất, nó chỉ chạy phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 hoặc
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 cho tất cả các lần di chuyển chưa được chạy. Nếu không có di chuyển như vậy, nó sẽ thoát. Nó sẽ chạy các lần di chuyển này theo thứ tự dựa trên ngày di chuyển

Lưu ý rằng việc chạy lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
52 cũng gọi lệnh
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
53, lệnh này sẽ cập nhật tệp
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 của bạn để phù hợp với cấu trúc cơ sở dữ liệu của bạn

Nếu bạn chỉ định một phiên bản mục tiêu, Bản ghi Hoạt động sẽ chạy các lần di chuyển được yêu cầu (thay đổi, lên, xuống) cho đến khi đạt đến phiên bản đã chỉ định. Phiên bản là tiền tố số trên tên tệp của quá trình di chuyển. Ví dụ: để chuyển sang phiên bản 20080906120000, hãy chạy

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
6Bản sao

Nếu phiên bản 20080906120000 lớn hơn phiên bản hiện tại (i. e. , nó đang di chuyển lên trên), thao tác này sẽ chạy phương thức

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 (hoặc
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3) trên tất cả các lần di chuyển cho đến và bao gồm cả 20080906120000 và sẽ không thực hiện bất kỳ lần di chuyển nào sau này. Nếu di chuyển xuống dưới, điều này sẽ chạy phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 trên tất cả các lần di chuyển xuống, nhưng không bao gồm, 20080906120000

Một nhiệm vụ phổ biến là khôi phục lần di chuyển cuối cùng. Ví dụ, nếu bạn mắc lỗi trong đó và muốn sửa nó. Thay vì theo dõi số phiên bản được liên kết với lần di chuyển trước, bạn có thể chạy

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
7Bản sao

Điều này sẽ khôi phục lần di chuyển mới nhất, bằng cách hoàn nguyên phương thức

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 hoặc bằng cách chạy phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4. Nếu bạn cần hoàn tác một số lần di chuyển, bạn có thể cung cấp tham số
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
60

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
8Bản sao

3 lần di chuyển cuối cùng sẽ được hoàn nguyên

Lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
61 là một phím tắt để thực hiện khôi phục và sau đó di chuyển sao lưu lại. Như với lệnh
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
62, bạn có thể sử dụng tham số
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
60 nếu bạn cần quay lại nhiều phiên bản, chẳng hạn

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
  end
end
9Bản sao

Cả hai lệnh đường ray này đều không làm bất cứ điều gì bạn không thể làm với

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
52. Chúng ở đó để thuận tiện, vì bạn không cần chỉ định rõ ràng phiên bản sẽ chuyển sang

Lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
65 sẽ tạo cơ sở dữ liệu, tải lược đồ và khởi tạo nó với dữ liệu gốc

Lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
66 sẽ xóa cơ sở dữ liệu và thiết lập lại. Đây là chức năng tương đương với
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
67

Điều này không giống như chạy tất cả các lần di chuyển. Nó sẽ chỉ sử dụng nội dung của tệp

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 hoặc
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
69 hiện tại. Nếu không thể khôi phục quá trình di chuyển,
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
66 có thể không giúp được gì cho bạn. Để tìm hiểu thêm về kết xuất lược đồ, hãy xem phần

Nếu bạn cần chạy một di chuyển cụ thể lên hoặc xuống, các lệnh

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
71 và
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
72 sẽ thực hiện điều đó. Ví dụ: chỉ cần chỉ định phiên bản phù hợp và quá trình di chuyển tương ứng sẽ có phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5,
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3 hoặc
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
4 được gọi

$ bin/rails generate migration AddPartNumberToProducts part_number:string
0Bản sao

Bằng cách chạy lệnh này, phương thức

$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
5 (hoặc phương thức
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
3) sẽ được thực thi để di chuyển với phiên bản "20080906120000"

Đầu tiên, lệnh này sẽ kiểm tra xem việc di chuyển có tồn tại hay không và liệu nó đã được thực hiện chưa và sẽ không làm gì nếu có

Nếu phiên bản được chỉ định không tồn tại, Rails sẽ đưa ra một ngoại lệ

$ bin/rails generate migration AddPartNumberToProducts part_number:string
1Bản sao

Mặc định chạy

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49 sẽ chạy trong môi trường
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
79

Để chạy di chuyển đối với môi trường khác, bạn có thể chỉ định nó bằng cách sử dụng biến môi trường

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
80 trong khi chạy lệnh. Ví dụ: để chạy di chuyển đối với môi trường
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
81, bạn có thể chạy

$ bin/rails generate migration AddPartNumberToProducts part_number:string
2Bản sao

Theo mặc định, quá trình di chuyển cho bạn biết chính xác chúng đang làm gì và mất bao lâu. Việc di chuyển tạo bảng và thêm chỉ mục có thể tạo ra kết quả như thế này

$ bin/rails generate migration AddPartNumberToProducts part_number:string
3Bản sao

Một số phương pháp được cung cấp trong quá trình di chuyển cho phép bạn kiểm soát tất cả điều này

MethodPurposeLấy một khối làm đối số và chặn bất kỳ đầu ra nào được tạo bởi khối đó. Lấy một đối số tin nhắn và xuất nó như là. Đối số boolean thứ hai có thể được chuyển để chỉ định có thụt lề hay không. Xuất văn bản cùng với thời gian chạy khối của nó. Nếu khối trả về một số nguyên, nó cho rằng đó là số hàng bị ảnh hưởng

Ví dụ: thực hiện di chuyển sau

$ bin/rails generate migration AddPartNumberToProducts part_number:string
4Bản sao

Điều này sẽ tạo ra đầu ra sau

$ bin/rails generate migration AddPartNumberToProducts part_number:string
5Bản sao

Nếu bạn muốn Bản ghi Hoạt động không xuất bất kỳ thứ gì, thì việc chạy ____________85 sẽ chặn tất cả đầu ra

Thỉnh thoảng bạn sẽ mắc lỗi khi viết di chuyển. Nếu bạn đã chạy di chuyển, thì bạn không thể vừa chỉnh sửa di chuyển vừa chạy lại di chuyển. Rails cho rằng nó đã chạy quá trình di chuyển và do đó sẽ không làm gì khi bạn chạy

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49. Bạn phải khôi phục quá trình di chuyển (ví dụ: với
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
87), chỉnh sửa quá trình di chuyển của mình, sau đó chạy
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49 để chạy phiên bản đã sửa

Nói chung, chỉnh sửa di chuyển hiện tại không phải là một ý tưởng hay. Bạn sẽ tạo thêm công việc cho bản thân và đồng nghiệp của mình và gây ra nhiều vấn đề đau đầu nếu phiên bản di chuyển hiện có đã được chạy trên các máy sản xuất

Thay vào đó, bạn nên viết một di chuyển mới để thực hiện những thay đổi mà bạn yêu cầu. Việc chỉnh sửa một di chuyển mới được tạo chưa được cam kết kiểm soát nguồn (hay nói chung là chưa được phổ biến ra ngoài máy phát triển của bạn) là tương đối vô hại

Phương pháp

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
37 có thể hữu ích khi viết một lần di chuyển mới để hoàn tác toàn bộ hoặc một phần các lần di chuyển trước đó (xem bên trên)

Di chuyển, mạnh mẽ như chúng có thể, không phải là nguồn có thẩm quyền cho lược đồ cơ sở dữ liệu của bạn. Cơ sở dữ liệu của bạn vẫn là nguồn của sự thật

Theo mặc định, Rails tạo

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 cố gắng nắm bắt trạng thái hiện tại của lược đồ cơ sở dữ liệu của bạn

Việc tạo một phiên bản mới của cơ sở dữ liệu ứng dụng của bạn bằng cách tải tệp lược đồ qua

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
91 có xu hướng nhanh hơn và ít xảy ra lỗi hơn so với việc phát lại toàn bộ lịch sử di chuyển. có thể không áp dụng chính xác nếu những quá trình di chuyển đó sử dụng các phụ thuộc bên ngoài thay đổi hoặc dựa vào mã ứng dụng phát triển tách biệt với quá trình di chuyển của bạn

Các tệp sơ đồ cũng hữu ích nếu bạn muốn xem nhanh các thuộc tính mà một đối tượng Bản ghi Hoạt động có. Thông tin này không có trong mã của mô hình và thường được lan truyền qua một số lần di chuyển, nhưng thông tin này được tóm tắt độc đáo trong tệp lược đồ

Định dạng của kết xuất lược đồ do Rails tạo ra được kiểm soát bởi cài đặt được xác định trong

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
93. Theo mặc định, định dạng là _______18_______94 hoặc có thể đặt thành _______18_______95

Khi

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
94 được chọn, thì lược đồ được lưu trữ trong
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0. Nếu bạn nhìn vào tệp này, bạn sẽ thấy rằng nó trông rất giống một cuộc di chuyển rất lớn

$ bin/rails generate migration AddPartNumberToProducts part_number:string
6Bản sao

Theo nhiều cách, đây chính xác là những gì nó là. Tệp này được tạo bằng cách kiểm tra cơ sở dữ liệu và thể hiện cấu trúc của nó bằng cách sử dụng

$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
5,
class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
    add_index :products, :part_number
  end
end
5, v.v.

Tuy nhiên,

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 không thể diễn đạt mọi thứ mà cơ sở dữ liệu của bạn có thể hỗ trợ, chẳng hạn như trình kích hoạt, trình tự, thủ tục được lưu trữ, v.v.

Mặc dù quá trình di chuyển có thể sử dụng

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
95 để tạo các cấu trúc cơ sở dữ liệu không được hỗ trợ bởi DSL di chuyển của Ruby, nhưng các cấu trúc này có thể không được khôi phục bởi trình kết xuất lược đồ

Nếu bạn đang sử dụng các tính năng như thế này, bạn nên đặt định dạng lược đồ thành

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
95 để có được tệp lược đồ chính xác, hữu ích để tạo các phiên bản cơ sở dữ liệu mới

Khi định dạng lược đồ được đặt thành

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
95, cấu trúc cơ sở dữ liệu sẽ được kết xuất bằng công cụ dành riêng cho cơ sở dữ liệu thành
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
69. Ví dụ: đối với PostgreSQL, tiện ích
$ bin/rails generate migration AddPartNumberToProducts
07 được sử dụng. Đối với MySQL và MariaDB, tệp này sẽ chứa đầu ra của
$ bin/rails generate migration AddPartNumberToProducts
08 cho các bảng khác nhau

Để tải lược đồ từ

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
69, hãy chạy
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
91. Việc tải tệp này được thực hiện bằng cách thực thi các câu lệnh SQL chứa trong đó. Theo định nghĩa, điều này sẽ tạo ra một bản sao hoàn hảo của cấu trúc cơ sở dữ liệu

Vì các tệp lược đồ thường được sử dụng để tạo cơ sở dữ liệu mới, bạn nên kiểm tra tệp lược đồ của mình trong kiểm soát nguồn

Xung đột hợp nhất có thể xảy ra trong tệp lược đồ của bạn khi hai nhánh sửa đổi lược đồ. Để giải quyết những xung đột này, hãy chạy

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49 để tạo lại tệp lược đồ

Các ứng dụng Rails mới được tạo sẽ có sẵn thư mục di chuyển trong cây git, vì vậy tất cả những gì bạn phải làm là đảm bảo thêm bất kỳ lần di chuyển mới nào mà bạn thêm và cam kết chúng

Cách Active Record tuyên bố rằng trí thông minh thuộc về các mô hình của bạn, không phải trong cơ sở dữ liệu. Do đó, các tính năng như trình kích hoạt hoặc ràng buộc đẩy một số thông tin tình báo đó trở lại cơ sở dữ liệu không được khuyến nghị

Các xác thực như

$ bin/rails generate migration AddPartNumberToProducts
12 là một cách mà các mô hình có thể thực thi tính toàn vẹn của dữ liệu. Tùy chọn
$ bin/rails generate migration AddPartNumberToProducts
13 trên các liên kết cho phép các mô hình tự động hủy các đối tượng con khi cha mẹ bị hủy. Giống như bất kỳ thứ gì hoạt động ở cấp ứng dụng, những thứ này không thể đảm bảo tính toàn vẹn tham chiếu và vì vậy một số người bổ sung chúng trong cơ sở dữ liệu

Mặc dù Bản ghi Hoạt động không cung cấp tất cả các công cụ để làm việc trực tiếp với các tính năng như vậy, nhưng phương pháp

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def change
    reversible do |direction|
      change_table :products do |t|
        direction.up   { t.change :price, :string }
        direction.down { t.change :price, :integer }
      end
    end
  end
end
95 có thể được sử dụng để thực thi SQL tùy ý

Mục đích chính của tính năng di chuyển Rails là đưa ra các lệnh sửa đổi lược đồ bằng một quy trình nhất quán. Di chuyển cũng có thể được sử dụng để thêm hoặc sửa đổi dữ liệu. Điều này hữu ích trong cơ sở dữ liệu hiện có không thể bị hủy và tạo lại, chẳng hạn như cơ sở dữ liệu sản xuất

$ bin/rails generate migration AddPartNumberToProducts part_number:string
7Bản sao

Để thêm dữ liệu ban đầu sau khi cơ sở dữ liệu được tạo, Rails có tính năng 'hạt giống' tích hợp giúp tăng tốc quá trình. Điều này đặc biệt hữu ích khi tải lại cơ sở dữ liệu thường xuyên trong môi trường phát triển và thử nghiệm. Để bắt đầu với tính năng này, hãy điền vào

$ bin/rails generate migration AddPartNumberToProducts
15 một số mã Ruby và chạy
$ bin/rails generate migration AddPartNumberToProducts
16

$ bin/rails generate migration AddPartNumberToProducts part_number:string
8Bản sao

Đây thường là một cách sạch hơn nhiều để thiết lập cơ sở dữ liệu của một ứng dụng trống

class AddPartNumberToProducts < ActiveRecord::Migration[7.1]
  def change
    add_column :products, :part_number, :string
  end
end
0 hoặc
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
69 là ảnh chụp nhanh trạng thái hiện tại của cơ sở dữ liệu của bạn và là nguồn có thẩm quyền để xây dựng lại cơ sở dữ liệu đó. Điều này cho phép xóa hoặc cắt bớt các tệp di chuyển cũ

Khi bạn xóa các tệp di chuyển trong thư mục

$ bin/rails generate migration AddPartNumberToProducts
19, bất kỳ môi trường nào mà
class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
49 được chạy khi các tệp đó vẫn tồn tại sẽ giữ một tham chiếu đến dấu thời gian di chuyển dành riêng cho chúng bên trong bảng cơ sở dữ liệu Rails nội bộ có tên là
$ bin/rails generate migration AddPartNumberToProducts
21. Bảng này được sử dụng để theo dõi xem việc di chuyển đã được thực hiện trong một môi trường cụ thể hay chưa

Nếu bạn chạy lệnh

$ bin/rails generate migration AddPartNumberToProducts
22, hiển thị trạng thái (lên hoặc xuống) của mỗi lần di chuyển, bạn sẽ thấy
$ bin/rails generate migration AddPartNumberToProducts
23 hiển thị bên cạnh bất kỳ tệp di chuyển đã xóa nào đã từng được thực thi trên một môi trường cụ thể nhưng không còn có thể tìm thấy trong thư mục
$ bin/rails generate migration AddPartNumberToProducts
19

Có một báo trước, mặc dù với Động cơ. Các tác vụ cào để cài đặt di chuyển từ các công cụ là bình thường, có nghĩa là chúng sẽ có kết quả giống nhau bất kể chúng được gọi bao nhiêu lần. Các lần di chuyển có trong ứng dụng gốc do cài đặt trước đó sẽ bị bỏ qua và những lần di chuyển bị thiếu sẽ được sao chép bằng dấu thời gian hàng đầu mới. Nếu bạn đã xóa di chuyển công cụ cũ và chạy lại tác vụ cài đặt, bạn sẽ nhận được các tệp mới có dấu thời gian mới và

class ChangeProductsPrice < ActiveRecord::Migration[7.1]
  def up
    change_table :products do |t|
      t.change :price, :string
    end
  end

  def down
    change_table :products do |t|
      t.change :price, :integer
    end
  end
end
52 sẽ cố chạy lại chúng

Do đó, bạn thường muốn duy trì các lần di chuyển đến từ các công cụ. Họ có một nhận xét đặc biệt như thế này

$ bin/rails generate migration AddPartNumberToProducts part_number:string
9Bản sao

Nhận xét

Bạn được khuyến khích giúp cải thiện chất lượng của hướng dẫn này

Vui lòng đóng góp nếu bạn thấy bất kỳ lỗi chính tả hoặc lỗi thực tế nào. Để bắt đầu, bạn có thể đọc phần của chúng tôi

Bạn cũng có thể tìm thấy nội dung không đầy đủ hoặc nội dung không được cập nhật. Vui lòng thêm bất kỳ tài liệu còn thiếu nào cho chính. Đảm bảo kiểm tra Hướng dẫn cạnh trước để xác minh xem sự cố đã được khắc phục hay chưa trên nhánh chính. Kiểm tra Hướng dẫn Hướng dẫn Ruby on Rails để biết phong cách và quy ước

Nếu vì bất kỳ lý do gì mà bạn phát hiện ra điều gì đó cần khắc phục nhưng không thể tự vá nó, vui lòng mở một vấn đề

Và cuối cùng nhưng không kém phần quan trọng, bất kỳ loại thảo luận nào liên quan đến tài liệu Ruby on Rails đều rất được hoan nghênh trên Diễn đàn Ruby on Rails chính thức