🚧 Add handling of static metadata
This commit is contained in:
+30
-3
@@ -46,14 +46,41 @@ CREATE TABLE IF NOT EXISTS app_tokens (
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- IMEI -> device name mapping used by the listener at ingest time.
|
||||
-- Device settings: one JSONB document per IMEI. The "device" key holds
|
||||
-- the device name injected into payloads by the listener; every other
|
||||
-- key is arbitrary settings data that the API merges into the payload
|
||||
-- at read time (GET /messages). The listener polls this table every 5 s.
|
||||
CREATE TABLE IF NOT EXISTS imei_map (
|
||||
imei TEXT PRIMARY KEY CHECK (imei ~ '^[0-9]{15}$'),
|
||||
device TEXT NOT NULL CHECK (device <> ''),
|
||||
meta JSONB NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Migration for deployments created before the meta column existed
|
||||
-- (they stored a flat device TEXT column): backfill each meta document
|
||||
-- from it, then drop it. Idempotent — on fresh databases (and after a
|
||||
-- successful run) the DO block is a no-op.
|
||||
ALTER TABLE imei_map ADD COLUMN IF NOT EXISTS meta JSONB;
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = 'imei_map'
|
||||
AND column_name = 'device'
|
||||
) THEN
|
||||
UPDATE imei_map
|
||||
SET meta = jsonb_build_object('device', device)
|
||||
WHERE meta IS NULL;
|
||||
ALTER TABLE imei_map DROP COLUMN device;
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
ALTER TABLE imei_map ALTER COLUMN meta SET NOT NULL;
|
||||
|
||||
COMMENT ON TABLE app_tokens IS
|
||||
'API app tokens as SHA-256 hex digests; seeded from BOOTSTRAP_APP_TOKEN when empty.';
|
||||
COMMENT ON TABLE imei_map IS
|
||||
'IMEI -> device name mapping, polled by the listener every 5 seconds.';
|
||||
'Device settings (one JSONB document per IMEI), polled by the listener every 5 seconds; the API merges the current document into message payloads at read time.';
|
||||
COMMENT ON COLUMN imei_map.meta IS
|
||||
'Settings document; "device" (non-empty string) is the device name injected at ingest, other keys are merged into message payloads at read time.';
|
||||
Reference in New Issue
Block a user