@mxpicture/tesla-superchargers-mate 0.1.52 → 0.1.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ -- Address clustering, promoted out of the unapplied sql/address_clustering.sql
2
+ -- (deleted by this change; its functions are now in ../../sql/views.sql).
3
+ --
4
+ -- The clustering groups nearby TeslaMate addresses under one "seed" address, so
5
+ -- a Supercharger site logged under half a dozen slightly different Nominatim
6
+ -- addresses reads as one place. 001_baseline.sql has always carried its two
7
+ -- real tables (pic_cluster, pic_cluster_member) and pic_cluster_map_v selects
8
+ -- through them, but the scaffolding and every function stayed in an unapplied
9
+ -- file -- which did not stop someone applying it to production by hand.
10
+ -- Measured 2026-09-03: production had all six functions, both tables below and
11
+ -- the trigger, none of them in any repository, and 52 clusters with 71 members
12
+ -- being read by the clustered cost views. The schema was not reproducible.
13
+ --
14
+ -- What changes on the way in: the AFTER INSERT trigger on `addresses` does NOT
15
+ -- come with it, and this migration drops the one production is carrying. Every
16
+ -- TeslaMate table is read-only in this project -- no new columns, no triggers,
17
+ -- no writes -- and that rule is exactly why the file stayed unapplied; a
18
+ -- trigger also puts a whole clustering pass inside TeslaMate's own insert
19
+ -- transaction. The scheduled `assign-clusters` command replaces it, which is
20
+ -- the alternative the file itself named.
21
+ --
22
+ -- The functions are derived, so they live in ../../sql/views.sql with the rest
23
+ -- of the routines, dropped and recreated around every migration run. Only the
24
+ -- two tables are state, and only they are here.
25
+ -- migrate:up
26
+ -- Scratch space for a full rebuild. UNLOGGED: contents are disposable, and a
27
+ -- crash may leave the table empty -- pic_rebuild TRUNCATEs it before every use.
28
+ -- Permanent rather than TEMP to avoid plpgsql cached-plan invalidation when
29
+ -- pic_rebuild is called more than once in a session.
30
+ CREATE UNLOGGED TABLE IF NOT EXISTS pic_rebuild_work (
31
+ address_id INTEGER PRIMARY KEY,
32
+ lat DOUBLE PRECISION NOT NULL,
33
+ lon DOUBLE PRECISION NOT NULL
34
+ );
35
+
36
+ -- pic_rebuild drops and recreates this index around its bulk load (much faster
37
+ -- than maintaining it row by row), so it is created here only so that a fresh
38
+ -- database matches one that has already rebuilt once.
39
+ CREATE INDEX IF NOT EXISTS pic_rebuild_work_gix ON pic_rebuild_work USING gist (ll_to_earth (lat, lon));
40
+
41
+ -- Audit trail of rebuilds: one row per pic_rebuild() call. The only table here
42
+ -- whose rows are worth keeping, and the reason this pair is promoted rather
43
+ -- than dropped.
44
+ CREATE TABLE IF NOT EXISTS pic_rebuild_log (
45
+ run_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
46
+ diameter_m DOUBLE PRECISION NOT NULL,
47
+ address_count BIGINT NOT NULL,
48
+ cluster_count BIGINT NOT NULL,
49
+ started_at TIMESTAMPTZ NOT NULL,
50
+ finished_at TIMESTAMPTZ NOT NULL
51
+ );
52
+
53
+ -- The trigger this project must not own, and the function behind it. Guarded on
54
+ -- `addresses` existing at all: the pic_* schema applies to a database whose
55
+ -- TeslaMate half may not be created yet, and DROP TRIGGER IF EXISTS still fails
56
+ -- when the table it names is missing.
57
+ DO $$
58
+ BEGIN
59
+ IF to_regclass('addresses') IS NOT NULL THEN
60
+ EXECUTE 'DROP TRIGGER IF EXISTS pic_addr_trg_after_insert_notify ON addresses';
61
+ END IF;
62
+ END $$;
63
+
64
+ DROP FUNCTION IF EXISTS pic_addr_trg_after_insert ();
65
+
66
+ -- migrate:down
67
+ DROP TABLE IF EXISTS pic_rebuild_log;
68
+
69
+ DROP TABLE IF EXISTS pic_rebuild_work;
@@ -0,0 +1,36 @@
1
+ -- Drop pic_fx_rates, the last physical trace of the retired FX cache.
2
+ --
3
+ -- docs/plans/2026-07-28_fx-rates-simplification-review.md replaced the
4
+ -- write-once daily rate table and its `sync-fx-rates` command with an
5
+ -- in-process LRU cache over the same base-EUR feed. The code went; the table
6
+ -- did not, because it was never in a migration and sql/reset.sql drops by
7
+ -- explicit name, so it survived both a full reset and a postgres_backup
8
+ -- restore. Measured on production 2026-09-03: 0 rows, and 0 of the 7
9
+ -- pic_charging_process_links rows pointing at one -- the column that recorded
10
+ -- "which rate this cost was written with" has recorded nothing since the
11
+ -- feature was removed.
12
+ --
13
+ -- The column goes first: its FK is ON DELETE SET NULL, which keeps the table
14
+ -- alive, not the reference. Nothing reads either -- the generated types under
15
+ -- packages/generated carry no PicFxRates, and the only PicFxRatesStore left is
16
+ -- in a stale dist/ build output.
17
+ -- migrate:up
18
+ ALTER TABLE pic_charging_process_links
19
+ DROP COLUMN IF EXISTS fx_rates_id;
20
+
21
+ DROP TABLE IF EXISTS pic_fx_rates;
22
+
23
+ -- migrate:down
24
+ -- Plain CREATE, not CREATE IF NOT EXISTS: rolling this back onto a database
25
+ -- that still has the table is a mistake worth failing on, not absorbing.
26
+ CREATE TABLE pic_fx_rates (
27
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
28
+ rate_date DATE NOT NULL,
29
+ quote_currency TEXT NOT NULL,
30
+ rate_to_eur NUMERIC(18, 8) NOT NULL,
31
+ fetched_at TIMESTAMPTZ NOT NULL DEFAULT now(),
32
+ UNIQUE (rate_date, quote_currency)
33
+ );
34
+
35
+ ALTER TABLE pic_charging_process_links
36
+ ADD COLUMN IF NOT EXISTS fx_rates_id BIGINT REFERENCES pic_fx_rates (id) ON DELETE SET NULL;