ricardo-bernardes/flyway-migrate icon
public
Published on 6/30/2025
Flyway Migrate

Prompts
Flyway Migrate
Generate DDL scripts following Flyway conventions
Based on the context information, create DDL scripts for database objects following these rules:

1. **Context Validation**
- Verify if context is sufficient for the task
- Infer only authorized elements
- Request additional information if needed

2. **Flyway File Structure**
- Use path: `src/main/resources/db/migration/V1.{{version}}__create_table_{{ModelName}}.sql`
- Consult existing Flyway files to increment versions

3. **Database Settings**
- Default DBMS: **PostgreSQL**
- Default schema: **"global"** (if not inferrable)

4. **Object Creation Rules**
- **Foreign Key Indexes**: Create non-unique index for EVERY foreign key constraint
- **Secondary Unique Index**: 
    a) Infer a secondary key candidate (e.g. natural key like `code` or `name` column)
    b) Create UNIQUE INDEX with condition: `WHERE deleted_at IS NULL` 
    c) Only apply if soft-delete pattern exists (`deleted_at`/`is_deleted`)
- **Index Placement**: All indexes created AFTER table definition
- **Comments**: Add `COMMENT ON` for all objects
- **Data Examples**: Include commented inserts (`-- INSERT INTO ...`)

5. **Required Header**
```sql
-- Copyright (c) {{YEAR}}, {{COMPANY}}. All rights reserved.
--
-- This source code is property of {{COMPANY}}. Access, use, copying, modification, 
-- distribution or disclosure are strictly prohibited without authorization.
--
-- File: {{FILE_NAME}}
```

**Index Creation Syntax Examples:**
```sql
-- Foreign Key Index
CREATE INDEX idx_{{table}}_{{fk_column}} ON global.{{table}} ({{fk_column}});
COMMENT ON INDEX idx_{{table}}_{{fk_column}} IS 'Index for FK {{fk_name}}';

-- Secondary Unique Index (with soft-delete)
CREATE UNIQUE INDEX idx_{{table}}_{{columns}}_unique 
  ON global.{{table}} ({{columns}}) 
  WHERE deleted_at IS NULL;  -- Adapt for boolean: WHERE NOT is_deleted
COMMENT ON INDEX idx_{{table}}_{{columns}}_unique IS 'Unique secondary key';
```

**Key Implementation Notes:**
- FK indexes: Automatically created for ALL foreign keys
- Secondary keys: 
    • Must be explicitly mentioned in context
    • Require unique business identifier (e.g. `product_code`)
    • Partial uniqueness ignores soft-deleted records
- Soft-delete detection: Look for columns named `deleted_at` (timestamp) or `is_deleted` (boolean)