Files
animaltrack/migrations/0001-reference-tables.sql
Petru Paler 25a91c3322 fix: remove trailing newlines from migrations to prevent SQLITE_MISUSE errors
Python's sqlite3.executescript() has a bug where trailing newlines after
the final semicolon create empty statements. When APSW's log_sqlite() is
enabled (via apswutils, imported by fastmigrate), these cause visible
"API called with NULL prepared statement" errors during interpreter shutdown.

- Strip trailing newlines from all 9 existing migration files
- Update migration template to end with semicolon, no trailing newline
- Document the requirement in CLAUDE.md

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-01 16:09:06 +00:00

52 lines
1.8 KiB
SQL

-- ABOUTME: Migration 0001 - reference-tables
-- ABOUTME: Creates the five reference tables: species, locations, products, feed_types, users.
-- Species: lookup table for animal species (duck, goose, sheep)
CREATE TABLE species (
code TEXT PRIMARY KEY,
name TEXT NOT NULL,
active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
created_at_utc INTEGER NOT NULL,
updated_at_utc INTEGER NOT NULL
);
-- Locations: physical locations (Strip 1-4, Nursery 1-4)
CREATE TABLE locations (
id TEXT PRIMARY KEY CHECK(length(id) = 26),
name TEXT NOT NULL UNIQUE,
active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
created_at_utc INTEGER NOT NULL,
updated_at_utc INTEGER NOT NULL
);
-- Products: things that can be collected/sold (eggs, meat, etc.)
CREATE TABLE products (
code TEXT PRIMARY KEY,
name TEXT NOT NULL,
unit TEXT NOT NULL CHECK(unit IN ('piece', 'kg')),
collectable INTEGER NOT NULL CHECK(collectable IN (0, 1)),
sellable INTEGER NOT NULL CHECK(sellable IN (0, 1)),
active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
created_at_utc INTEGER NOT NULL,
updated_at_utc INTEGER NOT NULL
);
-- Feed types: types of feed (starter, grower, layer)
CREATE TABLE feed_types (
code TEXT PRIMARY KEY,
name TEXT NOT NULL,
default_bag_size_kg INTEGER NOT NULL CHECK(default_bag_size_kg >= 1),
protein_pct REAL NULL,
active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
created_at_utc INTEGER NOT NULL,
updated_at_utc INTEGER NOT NULL
);
-- Users: system users with roles
CREATE TABLE users (
username TEXT PRIMARY KEY,
role TEXT NOT NULL CHECK(role IN ('admin', 'recorder')),
active INTEGER NOT NULL DEFAULT 1 CHECK(active IN (0, 1)),
created_at_utc INTEGER NOT NULL,
updated_at_utc INTEGER NOT NULL
);