-- Fresh schema for the integrated job. IF NOT EXISTS does not migrate older schemas.
CREATE TABLE IF NOT EXISTS azure_sponsorship_usage_gold (
  business_date DATE NOT NULL,
  grain_hash CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
  billing_account_id VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  subscription_id VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  provider VARCHAR(50) COLLATE utf8mb4_bin NOT NULL,
  billing_model VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  billing_currency VARCHAR(3) COLLATE utf8mb4_bin NOT NULL,
  charge_category VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  charge_class VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  charge_frequency VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  sku_key VARCHAR(280) COLLATE utf8mb4_bin NOT NULL,
  sku_id VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  meter_id VARCHAR(128) COLLATE utf8mb4_bin NOT NULL,
  region VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  consumed_unit VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  pricing_unit VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  pricing_source VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  pricing_confidence VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  valuation_currency VARCHAR(3) COLLATE utf8mb4_bin NOT NULL,
  billed_cost_basis VARCHAR(255) COLLATE utf8mb4_bin NOT NULL,
  rating_reconciliation_status VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  focus_compliance_status VARCHAR(100) COLLATE utf8mb4_bin NOT NULL,
  subscription_name TEXT NULL,
  service_name TEXT NULL,
  service_category TEXT NULL,
  sku_name TEXT NULL,
  meter_name TEXT NULL,
  consumed_quantity DECIMAL(38,12) NULL,
  pricing_quantity DECIMAL(38,12) NULL,
  billed_cost DECIMAL(38,12) NULL,
  effective_cost DECIMAL(38,12) NULL,
  contracted_cost DECIMAL(38,12) NULL,
  list_cost DECIMAL(38,12) NULL,
  contracted_valued_cost DECIMAL(38,12) NULL,
  retail_fallback_cost DECIMAL(38,12) NULL,
  valued_cost DECIMAL(38,12) NULL,
  source_line_count BIGINT UNSIGNED NOT NULL,
  valued_line_count BIGINT UNSIGNED NOT NULL,
  unpriced_line_count BIGINT UNSIGNED NOT NULL,
  billed_line_count BIGINT UNSIGNED NOT NULL,
  effective_line_count BIGINT UNSIGNED NOT NULL,
  currency_rejected_line_count BIGINT UNSIGNED NOT NULL,
  source_business_date_min DATE NOT NULL,
  source_business_date_max DATE NOT NULL,
  trigger_business_date DATE NOT NULL,
  source_generated_at_max DATETIME(6) NOT NULL,
  generated_at DATETIME(6) NOT NULL,
  ingestion_run_id CHAR(36) NOT NULL,
  PRIMARY KEY (business_date, grain_hash),
  KEY idx_sponsorship_gold_subscription_date (subscription_id, business_date),
  KEY idx_sponsorship_gold_sku_date (sku_key, business_date),
  KEY idx_sponsorship_gold_meter_date (meter_id, business_date),
  KEY idx_sponsorship_gold_run (ingestion_run_id),
  CONSTRAINT ck_sponsorship_gold_counts CHECK (source_line_count = valued_line_count + unpriced_line_count)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
