PlotDirector/Docs/Codex/Database.md

87 lines
3.9 KiB
Markdown

# PlotDirector Database Operations
Read this document only when the current task requires database inspection, SQL migration, schema work or direct development-database access.
## Environment
- Development database: `PlotDirector_Development`.
- Runtime connection details: `/etc/plotdirector/plotdirector.env`, key `ConnectionStrings__DefaultConnection`.
- SQL Server container: `atlas-sql-server`.
- Host endpoint: `192.168.10.10,1433`.
- `/sql/mssql` on the host maps to `/var/opt/mssql` in the container.
- Host `sqlcmd` is not installed.
- Container sqlcmd: `/opt/mssql-tools18/bin/sqlcmd`.
- Production is separate: host `www`, `192.168.10.8`, database `PlotLine`. Development authorisation does not authorise production changes.
Never print, copy or commit connection strings, passwords, API keys, SMTP secrets, Stripe secrets or OpenAI keys.
## Connecting Safely
Read the environment file in a privileged helper if necessary. Parse `ConnectionStrings__DefaultConnection` as data; never shell-evaluate it.
Do not `source /etc/plotdirector/plotdirector.env`. Values containing spaces or special characters can break shell parsing.
A small .NET runner using `Microsoft.Data.SqlClient` and `SqlConnectionStringBuilder` may consume the configured connection string safely.
When using sqlcmd:
- parse connection fields with `SqlConnectionStringBuilder`;
- pass the password via `SQLCMDPASSWORD`, not `-P`;
- explicitly select `-d PlotDirector_Development`;
- use `-C -I -b -V 16`;
- forward credentials through the process environment, not command arguments;
- use `-i` for SQL files or `docker exec -i` for stdin.
Host paths are not automatically visible inside the SQL Server container.
Before database work verify `SELECT DB_NAME()` reports `PlotDirector_Development`.
## Application Database Rules
- Use stored procedures for application database access.
- Do not add inline SQL in application code.
- Do not introduce Entity Framework migrations.
- Existing numbered SQL scripts are immutable.
- Repair live schema issues with a new forward-only migration.
- Design deletion behaviour deliberately for new tables referencing projects, books, chapters, scenes, characters, candidates or runs.
## Migration Procedure
New scripts live under `PlotLine/Sql/`.
Before choosing a migration number, inspect current scripts and select the next appropriate prefix. Historical duplicate prefixes exist, so do not assume numbering without checking.
Scripts should be idempotent where practical:
- guard table, column and index creation;
- use `CREATE OR ALTER PROCEDURE`.
Migrations are manual SQL. Do not blindly replay the entire SQL directory, `Admin/`, or `Stripe_Price_IDs_Prod.sql`.
Before schema or data changes:
1. Verify the development database.
2. Back up development using a unique SQL Server backup filename.
3. Verify the backup command succeeded and the backup exists.
4. Apply only required scripts in dependency order.
Apply scripts separately with `SET ANSI_NULLS ON` and `SET QUOTED_IDENTIFIER ON`.
`GO` is a sqlcmd batch separator, not executable SqlClient SQL. A custom runner must split batches appropriately.
Stop on the first migration error. Record the failed script and do not deploy dependent code. Earlier successful batches may already have persisted.
After migration, run a focused smoke query confirming required tables, columns and procedures exist, and verify `DB_NAME()` again.
A successful backup is not itself a restore test. Restore testing must use a disposable verification database.
Never restore, reset or overwrite `PlotDirector_Development` unless explicitly requested and the target backup has been verified.
## Specialist Database Regression Tests
Database regressions are under `PlotLine.Tests/Sql/`.
They run separately from the normal regression harness. Read the individual regression script before executing it because migration, fixture and rollback requirements differ.
Never run development database regression scripts against production.