adds schema view and schema generating command
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
# schema.json contract
|
||||
|
||||
The [Schema view](guide/schema.md) draws a Laravel app's database from `database/helder/schema.json`. The app's own Artisan command, `app/Console/Commands/HelderSchemaCommand.php` (`php artisan helder:schema`), writes that file. Helder does not ship the command: **Generate command** in the view asks Claude to write it from the prompt in `SchemaCommandPrompt.text` (`Sources/HelderCore/Schema/SchemaCommandPrompt.swift`).
|
||||
|
||||
[Docs index](index.md) · Related: [Schema view guide](guide/schema.md), [Behaviour § 4](behaviour.md#4-schema-view)
|
||||
|
||||
## Paths
|
||||
|
||||
| What | Path, relative to the project root |
|
||||
|---|---|
|
||||
| Turns the view on | `artisan` (a Laravel app) |
|
||||
| The command | `app/Console/Commands/HelderSchemaCommand.php` |
|
||||
| The dump | `database/helder/schema.json` |
|
||||
| How Helder runs it | `php artisan helder:schema --no-ansi --no-interaction`, with Herd's `php` first, else `php` on `PATH`, 120 s timeout |
|
||||
|
||||
## The file (version 1)
|
||||
|
||||
```json
|
||||
{
|
||||
"version": 1,
|
||||
"connection": "<connection name>",
|
||||
"driver": "<DB::connection($connection)->getDriverName()>",
|
||||
"database": "<DB::connection($connection)->getDatabaseName()>",
|
||||
"generated_at": "<now()->toIso8601String()>",
|
||||
"tables": [
|
||||
{
|
||||
"name": "<table name>",
|
||||
"rows": <int or null>,
|
||||
"columns": [ <Schema::getColumns($table), unchanged> ],
|
||||
"indexes": [ <Schema::getIndexes($table), unchanged> ],
|
||||
"foreign_keys": [ <Schema::getForeignKeys($table), unchanged> ]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
What the command must do besides the JSON:
|
||||
|
||||
- Read the structure only with Laravel's schema builder: `Schema::getTables()`, `getColumns`, `getIndexes`, `getForeignKeys`.
|
||||
- Skip `migrations`, `jobs`, `job_batches`, `failed_jobs`, `cache`, `cache_locks`, `sessions` and `password_reset_tokens`.
|
||||
- Count rows with `DB::table($table)->count()`; a failed count is `null`.
|
||||
- Sort the tables by name, and encode with `JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR`.
|
||||
- Write a temporary file in `database/helder/` and rename it, so Helder never reads half a file.
|
||||
- Keep `database/helder/.gitignore` with `*` and `!.gitignore`.
|
||||
- Print the counts with `twoColumnDetail`, then `Wrote database/helder/schema.json (<size> KB)`, and return `SUCCESS`; on an error print it with `error()` and return `FAILURE`.
|
||||
|
||||
## How Helder reads it
|
||||
|
||||
`SchemaDump` (`Sources/HelderCore/Schema/SchemaDump.swift`) decodes the file; `SchemaModel` turns it into cards and lines.
|
||||
|
||||
- Decoding is tolerant. A missing or mistyped key gets its default; a table, column or key without a name is skipped. Booleans may come as `true`, `1` or `"1"`. The older prototype shape (no `version`, `driver`, `database` or `rows`) still loads.
|
||||
- A file that is not a JSON object is an error: the console shows it and a toast reads `Could not read the schema`.
|
||||
- The connection label is `driver · database`, else the connection name.
|
||||
- Primary keys come from the primary index, else the auto-increment column. `UQ` marks a column alone in a unique index that is not the primary key.
|
||||
- One line per foreign key, from its first column to the referenced column (else the target's primary key, else its first row). A key to a table that is not in the dump draws no line.
|
||||
- The view re-reads the file when its modification date changes, so rewrite it in place; Helder keeps the user's pan, zoom and card positions across a re-read.
|
||||
|
||||
Change the prompt and `SchemaDump` together: the prompt is the only place the contract is written for the agent.
|
||||
Reference in New Issue
Block a user