# frozen_string_literal: true

class CreateUxToolkitAttachmentTrashRecords < ActiveRecord::Migration[6.1]
  def change
    unless table_exists?(:ux_toolkit_attachment_trash_records)
      create_table :ux_toolkit_attachment_trash_records do |t|
        t.integer :attachment_id, null: false
        t.string :original_disk_filename, null: false
        t.string :original_path, null: false, limit: 1024
        t.string :trash_path, null: false, limit: 1024
        t.string :original_filename, null: false
        t.integer :file_size
        t.string :content_type
        t.string :container_type
        t.integer :container_id
        t.integer :moved_to_trash_by_id
        t.datetime :moved_to_trash_at
        t.datetime :expires_at
        t.integer :restored_by_id
        t.datetime :restored_at
        t.integer :permanently_deleted_by_id
        t.datetime :permanently_deleted_at
        t.string :status, null: false, default: 'trashed'
        t.text :last_message
        t.timestamps
      end
    end

    add_index :ux_toolkit_attachment_trash_records, :attachment_id, name: 'idx_ux_att_trash_attachment' unless index_exists?(:ux_toolkit_attachment_trash_records, :attachment_id, name: 'idx_ux_att_trash_attachment')
    add_index :ux_toolkit_attachment_trash_records, :status, name: 'idx_ux_att_trash_status' unless index_exists?(:ux_toolkit_attachment_trash_records, :status, name: 'idx_ux_att_trash_status')
    add_index :ux_toolkit_attachment_trash_records, :expires_at, name: 'idx_ux_att_trash_expires' unless index_exists?(:ux_toolkit_attachment_trash_records, :expires_at, name: 'idx_ux_att_trash_expires')
    add_index :ux_toolkit_attachment_trash_records, [:status, :expires_at], name: 'idx_ux_att_trash_cleanup' unless index_exists?(:ux_toolkit_attachment_trash_records, [:status, :expires_at], name: 'idx_ux_att_trash_cleanup')
  end
end
