class CreateMaterials < ActiveRecord::Migration[8.0] def change create_table :materials do |t| t.string :url t.references :parent, index: true, foreign_key: { to_table: :materials } t.references :tag, index: true, foreign_key: true t.references :created_by_user, foreign_key: { to_table: :users } t.references :updated_by_user, foreign_key: { to_table: :users } t.timestamps t.datetime :discarded_at, index: true t.virtual :active_url, type: :string, as: 'IF(discarded_at IS NULL, url, NULL)', stored: false t.index :active_url, unique: true end create_table :material_versions do |t| t.references :material, null: false, foreign_key: true t.integer :version_no, null: false t.string :url, index: true t.references :parent, index: true, foreign_key: { to_table: :materials } t.references :tag, index: true, foreign_key: true t.references :created_by_user, foreign_key: { to_table: :users } t.references :updated_by_user, foreign_key: { to_table: :users } t.timestamps t.datetime :discarded_at, index: true t.index [:material_id, :version_no], unique: true, name: 'index_material_versions_on_material_id_and_version_no' end end end