Skip to main content

Overview

ChameleonDB includes multiple layers of safety guards to prevent accidental data loss or corruption. These protections are built into the mutation system and cannot be disabled without explicit confirmation.

WHERE Clause Requirement

The most important safety guard: all updates and deletes require a WHERE clause.

The Problem

In traditional databases, forgetting a WHERE clause can be catastrophic:

ChameleonDB’s Solution

ChameleonDB prevents this at the API level:
The WHERE clause requirement applies to both Update and Delete operations. This is enforced before any SQL is generated.

Safe Approach

Always use .Filter() to specify which records to modify:

Intentional Full-Table Operations

If you genuinely need to update/delete all records, use the explicit bypass:
Use with extreme caution: ForceUpdateAll() and ForceDeleteAll() bypass the safety guard and affect every record in the table.Recommended safeguards:
  • Only use in development/test environments
  • Require code review before production use
  • Always have recent backups
  • Test on a copy of production data first
  • Document why the full-table operation is necessary

Three-Stage Validation Pipeline

Every mutation goes through three validation stages before execution:

Stage 1: Schema Validation

Verifies the operation against your schema definition:
What it checks:
  • Entity exists in schema
  • All fields are defined
  • Field types match schema
  • Required fields are present

Stage 2: Constraint Validation

Enforces logical constraints before hitting the database:
What it checks:
  • NOT NULL constraints
  • Required field presence
  • WHERE clause requirement (for updates/deletes)
  • Valid filter operators

Stage 3: Database Validation

Final enforcement at the database level:
What it checks:
  • UNIQUE constraints
  • Foreign key references
  • Check constraints
  • Database triggers

Error Hierarchy

ChameleonDB provides typed errors for precise error handling:

Error Types

HTTP Error Mapping

Recommended pattern for web applications:

Validation Best Practices

1. Validate Early

Validate input before attempting mutations:

2. Use Typed Errors

Provide meaningful error messages to users:

3. Check Affected Count

Verify operations affected the expected number of records:

4. Use Debug Mode During Development

Enable debug mode to see generated SQL:

Pre-Flight Checklist

Before running mutations, verify:

For Development

  • Engine created with engine.NewEngine()
  • Mutation package imported: _ "github.com/chameleon-db/chameleondb/chameleon/pkg/engine/mutation"
  • Connected to database with eng.Connect(ctx, cfg)
  • Database connection verified with eng.Ping(ctx)
  • Schema loaded successfully
  • Debug mode enabled for diagnostics

For Updates/Deletes

  • .Filter() clause is present
  • Filter targets correct records
  • Expected affected count is known
  • Tested with .Debug() first
  • Foreign key relationships understood

For Production

  • Input validation implemented
  • Error handling covers all typed errors
  • HTTP status codes mapped correctly
  • User-friendly error messages provided
  • Logging includes context (user ID, operation, etc.)
  • Backups are current
  • Rollback plan exists

Common Safety Violations

Missing WHERE Clause

Type Mismatches

Missing Required Fields

Invalid Foreign Keys

Next Steps

Insert Operations

Create new records with validation

Update Operations

Safely modify existing records

Delete Operations

Remove records with safety guards

Error Handling

Complete error handling reference