From d0ae044f78fa343d2e7f9d71170d06a14eb3aadd Mon Sep 17 00:00:00 2001 From: Stafford Williams Date: Thu, 28 Mar 2024 00:47:25 +1100 Subject: [PATCH] add ship views --- src/migrations/Migration20240327134507.ts | 44 +++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/migrations/Migration20240327134507.ts diff --git a/src/migrations/Migration20240327134507.ts b/src/migrations/Migration20240327134507.ts new file mode 100644 index 0000000..659049c --- /dev/null +++ b/src/migrations/Migration20240327134507.ts @@ -0,0 +1,44 @@ +import { Migration } from '@mikro-orm/migrations' + +export class Migration20240327134507 extends Migration { + async up(): Promise { + this.addSql(`DROP VIEW IF EXISTS ships_cargo; + DROP VIEW IF EXISTS ships_nav; + DROP VIEW IF EXISTS ships; + CREATE VIEW ships AS + + select + REPLACE(symbol, agent.data->>'symbol', registration->>'role') as ship, + symbol, + nav->>'status' as status, + nav->'route'->'destination'->>'symbol' as destination_symbol, + nav->'route'->'destination'->>'type' as destination_type, + date_trunc('second', AGE((nav->'route'->>'arrival')::timestamp, NOW())) as arrival, + agent.reset_date + from + ship + JOIN + agent on agent.reset_date = ship.reset_date + ; + CREATE VIEW ships_nav AS + SELECT + ship, + CASE status WHEN 'IN_TRANSIT' THEN '🚀' + WHEN 'DOCKED' THEN '🌕' + ELSE '🌑' END as status, + destination_type || ' (' || destination_symbol || ')' as destination, + CASE status WHEN 'IN_TRANSIT' then arrival::text else '-' end as arrival, + reset_date + FROM + ships; + CREATE VIEW ships_cargo AS + select + symbol, + registration->>'role' as role, + (cargo->>'units')::int as units, + (cargo->>'capacity')::int as capacity, + (cargo->>'units')::float/CASE (cargo->>'capacity')::int WHEN 0 THEN 1 ELSE (cargo->>'capacity')::int END as percent, + reset_date + from "ship"`) + } +}