# typed: true
class CreateBlobFiles < ActiveRecord::Migration[7.0]
  NUM_PARTITIONS = 64

  def up
    main_table_sql = <<~SQL
      CREATE TABLE blob_files (
        sha256 bytea NOT NULL, -- sha256 of the file
        version integer NOT NULL, -- how to reconstruct the file path based on sha256
        metadata jsonb NOT NULL DEFAULT '{}', -- metadata about the file, where it came from, etc
        content_type character varying NOT NULL, -- content type of the file
        size_bytes integer NOT NULL, -- size of the file in bytes
        created_at timestamp(6) without time zone NOT NULL
      ) PARTITION BY HASH (sha256)
    SQL
    execute main_table_sql

    NUM_PARTITIONS.times do |partnum|
      partition_table_name = :"blob_files_#{partnum.to_s.rjust(2, "0")}"
      partition_table_sql = <<~SQL
        CREATE TABLE #{partition_table_name}
        PARTITION OF blob_files FOR
        VALUES WITH (
            MODULUS #{NUM_PARTITIONS},
            REMAINDER #{partnum}
          )
      SQL
      execute partition_table_sql
      add_index partition_table_name, :sha256, unique: true
    end

    add_index :blob_files, :sha256, unique: true
  end

  def down
    drop_table :blob_files
    0..NUM_PARTITIONS.times do |partnum|
      partition_table_name = :"blob_files_#{partnum.to_s.rjust(2, "0")}"
      drop_table partition_table_name
    end
  end
end
