# Cosmic KSQL Data Fix

Use for KSQL/SQL data repair in Cosmic-family products. This is not Java plugin generation.

## Preconditions

- Confirm product, environment, target bill/form/entity, operation type, target fields, conditions, and risk boundary.
- Use Cosmic metadata capability to confirm table names, database names, DB keys, field names, field types, enum values, and base-data storage fields.
- If metadata is unavailable and the user has not supplied verified metadata, stop and list pending confirmations.

## Cross-Database Rules

- Check whether all participating tables are in the same database.
- Do not generate ordinary update SQL for cross-database scenarios.
- For cross-database fixes, require explicit handling strategy such as temporary table, export/import, dblink, or postgres_fdw.

## SQL Script Structure

- Impact query.
- Full table backup using a `bak_` table with timestamp.
- Pre-update confirmation query.
- Formal update statement.
- Post-update verification query.
- Rollback statement.
- Field mapping section from verified metadata.

## Safety Rules

- Every update and delete must have a `WHERE` clause.
- Write the matching query before the update.
- Query and verification statements should not use `SELECT *`; full backup is the exception.
- Default dialect is PostgreSQL unless the user specifies otherwise.
- Prefer readable `IN` membership conditions unless `EXISTS` is required for semantics.
- Run the official KSQL lint script when available and fix errors before delivery.
