Files
evig/scripts/db/migrations/117_storage_locations.sql
Mao NakamotoandClaude Opus 4.8 ea5e717f53 feat(erfassung): structured storage-location picker (replaces free-text location)
When erfassing a product, staff now pick WHERE it physically is from a
runtime-addable list instead of typing free text — and can add a new location
inline without leaving the form.

- Migration 117: `storage_locations` table (name, kind, holder_user_id for
  member-possession, is_active) seeded with Hauptlager/Laden/Nebenlager +
  `inventory_items.storage_location_id` FK. Free-text `location` kept for compat.
- Config SSOT `config/erfassung/storage-locations.ts`: 5 kinds (main_storage,
  shop, secondary_storage, member_possession, other) + DE labels + seeds.
- API `/api/admin/storage-locations` GET (active list) + POST (add).
- `<StorageLocationSelect>`: fetch + Select + inline "+ Standort" add, wired
  into ProductDimensionFields; threaded through the erfassung zod schemas →
  formDataToPayload → create-product so it persists.
- i18n: components.erfassung.storageLocation (de + en).

Distinct from the public `locations` table (venues). One location per product.
0 tsc errors, lint clean, umlauts + i18n-parity clean, 342 erfassung/schema/
config tests green. Built in an isolated worktree (a parallel session held the
main tree with in-progress blog work).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 18:51:39 +02:00

42 lines
1.9 KiB
SQL

-- Migration 117: structured storage locations for inventory
--
-- Replaces the free-text inventory_items.location with a runtime-addable list of
-- physical storage locations (staff pick one when erfassing a product, and can
-- add new ones from the picker). The old free-text `location` column is KEPT for
-- backfill/compat — new entries write storage_location_id.
--
-- Distinct from the public `locations` table (workshop/service venues). `kind`
-- is validated at the app layer (STORAGE_LOCATION_KINDS) — no SQL CHECK, per the
-- migration-110 policy (app enums live in config + zod, not DB constraints).
CREATE TABLE IF NOT EXISTS storage_locations (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL,
kind text NOT NULL DEFAULT 'other',
holder_user_id uuid REFERENCES users(id) ON DELETE SET NULL,
is_active boolean NOT NULL DEFAULT true,
created_by uuid REFERENCES users(id) ON DELETE SET NULL,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_storage_locations_active ON storage_locations (is_active);
CREATE INDEX IF NOT EXISTS idx_storage_locations_kind ON storage_locations (kind);
-- Seed the standard locations (idempotent on name).
INSERT INTO storage_locations (name, kind)
SELECT v.name, v.kind
FROM (VALUES
('Hauptlager', 'main_storage'),
('Laden', 'shop'),
('Nebenlager', 'secondary_storage')
) AS v(name, kind)
WHERE NOT EXISTS (SELECT 1 FROM storage_locations sl WHERE sl.name = v.name);
-- Link inventory items to a storage location (nullable; free-text `location` stays).
ALTER TABLE inventory_items
ADD COLUMN IF NOT EXISTS storage_location_id uuid REFERENCES storage_locations(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS idx_inventory_items_storage_location
ON inventory_items (storage_location_id);