SELECT table_name
FROM information_schema.tables
WHERE table_schema = DATABASE()
  AND table_name IN (
    'qipai_product_storage_records',
    'qipai_product_storage_items',
    'qipai_product_storage_events',
    'qipai_product_storage_event_items'
  )
ORDER BY table_name;

SELECT table_name, column_name, data_type, is_nullable, collation_name
FROM information_schema.columns
WHERE table_schema = DATABASE()
  AND (
    (table_name = 'qipai_product_storage_records' AND column_name IN (
      'storage_no', 'client_request_id', 'request_fingerprint', 'source_type', 'status',
      'total_quantity', 'remaining_quantity', 'expires_at', 'claim_credential_hash',
      'claim_credential_version', 'claim_credential_consumed_at', 'version'
    ))
    OR (table_name = 'qipai_product_storage_items' AND column_name IN (
      'source_order_item_id', 'product_code', 'product_name', 'sku_code', 'sku_name',
      'attributes_snapshot', 'total_quantity', 'remaining_quantity'
    ))
    OR (table_name = 'qipai_product_storage_events' AND column_name IN (
      'request_id', 'request_fingerprint', 'action', 'quantity',
      'credential_version_after', 'version_after'
    ))
    OR (table_name = 'qipai_product_storage_event_items' AND column_name IN (
      'storage_item_id', 'sku_id', 'quantity', 'remaining_after'
    ))
  )
ORDER BY table_name, ordinal_position;

SELECT table_name, index_name
FROM information_schema.statistics
WHERE table_schema = DATABASE()
  AND index_name IN (
    'uq_qipai_product_order_item_scope_id',
    'uq_qipai_product_storage_scope_id',
    'uq_qipai_product_storage_no',
    'uq_qipai_product_storage_client_request',
    'uq_qipai_product_storage_source_order',
    'idx_qipai_product_storage_store_status',
    'idx_qipai_product_storage_member',
    'uq_qipai_product_storage_item_line',
    'uq_qipai_product_storage_item_source',
    'uq_qipai_product_storage_item_scope_id',
    'idx_qipai_product_storage_item_sku',
    'uq_qipai_product_storage_event_request',
    'uq_qipai_product_storage_event_version',
    'uq_qipai_product_storage_event_scope_id',
    'idx_qipai_product_storage_event_history',
    'uq_qipai_product_storage_event_item_line',
    'idx_qipai_product_storage_event_item_storage'
  )
GROUP BY table_name, index_name
ORDER BY table_name, index_name;

SELECT constraint_name
FROM information_schema.table_constraints
WHERE constraint_schema = DATABASE()
  AND constraint_type = 'FOREIGN KEY'
  AND constraint_name IN (
    'fk_qipai_product_storage_tenant',
    'fk_qipai_product_storage_store',
    'fk_qipai_product_storage_member',
    'fk_qipai_product_storage_source_order',
    'fk_qipai_product_storage_created_by',
    'fk_qipai_product_storage_item_record',
    'fk_qipai_product_storage_item_source',
    'fk_qipai_product_storage_item_product',
    'fk_qipai_product_storage_item_sku',
    'fk_qipai_product_storage_event_record',
    'fk_qipai_product_storage_event_actor',
    'fk_qipai_product_storage_event_item_event',
    'fk_qipai_product_storage_event_item_storage_item',
    'fk_qipai_product_storage_event_item_sku'
  )
ORDER BY constraint_name;

SELECT constraint_name
FROM information_schema.table_constraints
WHERE constraint_schema = DATABASE()
  AND constraint_type = 'CHECK'
  AND constraint_name IN (
    'chk_qipai_product_storage_source',
    'chk_qipai_product_storage_status',
    'chk_qipai_product_storage_counts',
    'chk_qipai_product_storage_credential',
    'chk_qipai_product_storage_actor',
    'chk_qipai_product_storage_version',
    'chk_qipai_product_storage_item_line',
    'chk_qipai_product_storage_item_source',
    'chk_qipai_product_storage_item_quantity',
    'chk_qipai_product_storage_event_fingerprint',
    'chk_qipai_product_storage_event_from_status',
    'chk_qipai_product_storage_event_to_status',
    'chk_qipai_product_storage_event_action',
    'chk_qipai_product_storage_event_actor',
    'chk_qipai_product_storage_event_versions',
    'chk_qipai_product_storage_event_item_quantity'
  )
ORDER BY constraint_name;

SELECT trigger_name, event_manipulation
FROM information_schema.triggers
WHERE trigger_schema = DATABASE()
  AND trigger_name IN (
    'qipai_product_storage_events_no_update',
    'qipai_product_storage_events_no_delete',
    'qipai_product_storage_event_items_no_update',
    'qipai_product_storage_event_items_no_delete'
  )
ORDER BY trigger_name;

SELECT code
FROM qipai_permissions
WHERE code IN ('goods.storage.read', 'goods.storage.manage')
ORDER BY code;

SELECT COUNT(*) AS managed_role_count
FROM qipai_roles r
WHERE r.code IN ('STAFF', 'STORE_ADMIN', 'TENANT_ADMIN', 'PLATFORM_ADMIN')
  AND r.status = 'ACTIVE'
  AND r.deleted_at IS NULL
HAVING COUNT(*) = (
  SELECT COUNT(*)
  FROM (
    SELECT granted_role.tenant_id, granted_role.id
    FROM qipai_roles granted_role
    INNER JOIN qipai_role_permissions rp
      ON rp.tenant_id = granted_role.tenant_id AND rp.role_id = granted_role.id
    INNER JOIN qipai_permissions p ON p.id = rp.permission_id
    WHERE granted_role.code IN ('STAFF', 'STORE_ADMIN', 'TENANT_ADMIN', 'PLATFORM_ADMIN')
      AND granted_role.status = 'ACTIVE'
      AND granted_role.deleted_at IS NULL
      AND p.code IN ('goods.storage.read', 'goods.storage.manage')
    GROUP BY granted_role.tenant_id, granted_role.id
    HAVING COUNT(DISTINCT p.code) = 2
  ) fully_granted_goods_storage_roles
);

SELECT version, name
FROM qipai_schema_migrations
WHERE version = '2026081109'
  AND name = 'm09d3_product_storage'
  AND checksum REGEXP '^[0-9a-f]{64}$';
