diff --git a/db/migrate/20260630102817_create_captain_usages.rb b/db/migrate/20260630102817_create_captain_usages.rb new file mode 100644 index 000000000..7e2401795 --- /dev/null +++ b/db/migrate/20260630102817_create_captain_usages.rb @@ -0,0 +1,20 @@ +class CreateCaptainUsages < ActiveRecord::Migration[7.1] + def change + create_table :captain_usages do |t| + t.references :account, null: false + t.references :assistant, null: false + t.integer :usage_type, null: false + # Usage is binned into 15-minute UTC buckets so it can be re-sliced into + # any caller timezone (including :45 offset zones). Do not widen to hourly. + t.datetime :bucket_started_at, null: false + t.integer :credits_used, null: false, default: 0 + + t.timestamps + end + + add_index :captain_usages, + [:account_id, :assistant_id, :usage_type, :bucket_started_at], + unique: true, + name: 'index_captain_usages_unique_bucket' + end +end diff --git a/db/schema.rb b/db/schema.rb index 05afea9a1..f3cd2ccd3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2026_06_20_000000) do +ActiveRecord::Schema[7.1].define(version: 2026_06_30_102817) do # These extensions should be enabled to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -431,6 +431,19 @@ ActiveRecord::Schema[7.1].define(version: 2026_06_20_000000) do t.index ["enabled"], name: "index_captain_scenarios_on_enabled" end + create_table "captain_usages", force: :cascade do |t| + t.bigint "account_id", null: false + t.bigint "assistant_id", null: false + t.integer "usage_type", null: false + t.datetime "bucket_started_at", null: false + t.integer "credits_used", default: 0, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id", "assistant_id", "usage_type", "bucket_started_at"], name: "index_captain_usages_unique_bucket", unique: true + t.index ["account_id"], name: "index_captain_usages_on_account_id" + t.index ["assistant_id"], name: "index_captain_usages_on_assistant_id" + end + create_table "categories", force: :cascade do |t| t.integer "account_id", null: false t.integer "portal_id", null: false