Skip to content
← All work

06 · Schema generator

AutoER

Business rules in, database schema out.

Database-first projects start with a whiteboard and a long conversation about what the business actually does. AutoER records that conversation as written rules and walks them through seven staged prompts — rules, tables, an audit, three normal forms, an ER diagram — until it can emit a schema.

Each stage is reviewable before the next runs, so the tool assists the modelling instead of replacing it. Built with PHP and the Gemini API; output is a crow's-foot diagram for draw.io and plain SQL for MySQL.

Year
2025
Role
Backend, prompt design
Stack
PHP 8, MySQL, Gemini 2.5 Flash-Lite, Tailwind CSS, Viz.js
Languages
  • PHP99.6%
  • Hack0.4%

From the repository

Pipeline stages
7
AI endpoints
9
Tables tracking a project
10
Normal forms enforced
3

Screens

AutoER business rule extraction stage
Stage 2: each rule with its rationale, implementation type, component and category.
AutoER generated ER diagram
Stage 6: the generated crow's-foot diagram, ready for draw.io or SQL.

Technical architecture

PHP 8 with PDO over MySQL, organised API-first: every stage page is a thin view that calls an endpoint, and every endpoint that needs a model goes through one GeminiService class.

  1. L1

    Stage pages

    • Tailwind CSS
    • Lucide
    • Vanilla JS
    • Viz.js
    • svg-pan-zoom

    Rules, tables, three normalisation pages, the ER diagram and SQL export — each one reviewable before moving on.

  2. L2

    API

    • services/ai/api/*
    • services/er/api/*
    • services/sql/api/generate
    • admin/projects/api/*

    Request–response endpoints for extraction, missing-rule detection, table and relationship generation, normalisation and export.

  3. L3

    AI service

    • GeminiService
    • Gemini 2.5 Flash-Lite
    • JSON mode
    • Retry on 503

    Loads each user's own API key from settings, asks for JSON, strips code fences and retries when the model is overloaded.

  4. L4

    Data

    • MySQL
    • PDO prepared statements
    • Sessions

    Every intermediate result is stored, so a project can be reopened at any stage and every normalisation step is logged.

The seven stages

Each stage feeds the next, and each can be re-run on its own.

  1. 01

    Describe

    Name, domain and a plain-language description of the business.

  2. 02

    Extract rules

    Rules are classified as Structural, Operational, Threshold or Authorization, tagged E/R/A/C, and mapped to a key, trigger or access control.

  3. 03

    Design tables

    A first schema with columns, types and keys.

  4. 04

    Audit

    Missing keys, wrong types and orphaned relationships are flagged and resolved rule by rule.

  5. 05

    Normalise

    1NF, 2NF and 3NF as three separate, logged passes.

  6. 06

    Diagram

    Crow's-foot ER diagram rendered from Graphviz DOT, exportable to draw.io.

  7. 07

    Generate SQL

    CREATE TABLE statements with keys, ON DELETE / ON UPDATE rules and indexes.

Data model

The tool models its own work relationally: a project owns rules, tables, columns and relationships, plus a log of every normalisation pass.

  • projects

    • user_id → users
    • name
    • description
    • status
  • project_rules

    • project_id
    • rule_id, rule_type
    • rule_statement, rule_rationale
    • implementation_type
    • entity_component
  • project_tables

    • project_id
    • table_name
    • description
    • normalization_level
  • project_columns

    • table_id → project_tables
    • name, data_type
    • is_primary_key, is_foreign_key
    • is_nullable, is_unique
    • check_constraint
  • project_relationships

    • parent_table_id, child_table_id
    • cardinality
    • label
  • project_missing_rules

    • project_id
    • missing_rule, related_br
    • solution, status
  • project_normalization_logs

    • project_id
    • stage, log_type
    • table_name
    • description
  • settings / project_exports

    • user_id, gemini_api_key
    • project_id, sql_content
    • version_note

Relationships · 10

  • users 1 ─< N projects
  • users 1 ── 1 settings
  • projects 1 ─< N project_rules
  • projects 1 ─< N project_tables
  • project_tables 1 ─< N project_columns
  • projects 1 ─< N project_relationships
  • project_tables 1 ─< N project_relationships (parent, child)
  • projects 1 ─< N project_missing_rules
  • projects 1 ─< N project_normalization_logs
  • projects 1 ─< N project_exports

API examples

Request and response shapes are taken from the code; values are examples.

POST/services/ai/api/extract_rules.php

Checks the session, loads the project only if the caller owns it, asks Gemini for rules, then replaces the project's rules inside one transaction.

Request

project_id=12

Response

{ "success": true, "message": "8 rules extracted with rationale and implementation details." }

// without a session
HTTP 401  { "success": false, "message": "Unauthorized" }

Gemini → one rule

The shape the prompt demands for every rule before it is inserted.

Response

{
  "rule_id": "BR-01",
  "rule_type": "O",
  "rule_statement": "A student cannot register for more than 5 courses per semester.",
  "rule_rationale": "To prevent academic overload and scheduling conflicts.",
  "implementation_type": "Trigger",
  "entity_component": "C"
}

Engineering decisions

  • Small prompts beat one big one

    A model asked for a finished schema in one call has nowhere to show its working. Seven narrow tasks, each building on the last, give it room to reason and give me a checkpoint after every one.

  • The human stays in the loop

    Nothing advances automatically. Each stage is saved and shown, and can be regenerated, so the tool assists the modelling instead of replacing it.

  • Bring your own key

    Each user stores their own Gemini key, so the platform carries no shared quota or bill.

Engineering checklist

The questions a reviewer asks, answered from the code — including the ones where the honest answer is no.

Database designIn place
Ten tables and eleven foreign keys, all ON DELETE CASCADE from projects, so deleting a project cleans up its rules, tables, columns, relationships, logs and exports in one statement.
Authentication & authorizationIn place
password_hash with PHP's default algorithm, session-based login, and a session_control include at the top of every protected page.
API & data securityIn place
PDO prepared statements throughout, and writes are scoped to the owner (DELETE … WHERE id = :id AND user_id = :user_id). Each user's Gemini key is stored server-side, never sent to the browser.
PerformancePartly
Gemini 2.5 Flash-Lite was chosen for latency, and the work is split into stages so each call stays small; ER diagrams are drawn in the browser with Viz.js, not on the server.
Errors & loggingIn place
GeminiService retries up to three times on 503 or "overloaded", strips Markdown fences before decoding JSON, and every normalisation pass is written to project_normalization_logs.
TestsNot yet
No automated tests.
Deployment & CI/CDPartly
Apache with .htaccess routing on shared PHP hosting, uploaded over SFTP from the IDE. No CI.

What I would change now

  • Diffing each stage's output against the previous one would make the normalisation logs easier to review.
  • Retries should back off exponentially, and long stages should run as jobs rather than inside a request.

Next project

go-football-core

A football season, simulated week by week, with Monte Carlo title odds.