93 lines
4.7 KiB
Markdown
93 lines
4.7 KiB
Markdown
# PlotDirector Database Operations
|
|
|
|
Deployable SQL now uses the [manifest/history standard](../Deployment/Production.md)
|
|
and `Tools/Deployment/DatabaseWorker`, shared by Development, Test and Production.
|
|
Do not bypass successful migration history with ad-hoc sqlcmd execution. Adopt an
|
|
existing database only through a verified current-state baseline; historical
|
|
execution is not inferred. See the [reviewed recovered-Production plan](../Deployment/ProductionUpgrade-20261125.plan.json).
|
|
|
|
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`.
|
|
- Current Production: `web1`, `85.190.124.132`, database `PlotLine` on `127.0.0.1,1433`; see [deployed state and Phase 2 handover](../Deployment/Phase2-20261125.md). Historical server `www`, `192.168.10.8`, is inactive and retained for recovery. 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 reviewed SQL scripts executed through the manifest/history worker. Do not manually apply scripts to ledger-managed databases or blindly replay the 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. Execute only the approved manifest through its deployment worker, in explicit 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.
|