class CreatePostTagSections < ActiveRecord::Migration[8.0]
  def change
    create_table :post_tag_sections, primary_key: [:post_id, :tag_id, :begin_ms] do |t|
      t.references :post, null: false, foreign_key: true, index: false
      t.references :tag, null: false, foreign_key: true, index: false
      t.integer :begin_ms, null: false
      t.integer :end_ms, null: true
      t.timestamps

      t.index [:post_id, :begin_ms], name: 'idx_post_tag_sections_post_id_begin_ms'

      t.check_constraint 'begin_ms >= 0',
                         name: 'chk_post_tag_sections_begin_ms_natural'

      t.check_constraint 'end_ms IS NULL OR begin_ms < end_ms',
                         name: 'chk_post_tag_sections_end_ms_after_begin_ms'
    end
  end
end
