-
Notifications
You must be signed in to change notification settings - Fork 0
/
Migration20240327134507.ts
44 lines (42 loc) · 1.52 KB
/
Migration20240327134507.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Migration } from '@mikro-orm/migrations'
export class Migration20240327134507 extends Migration {
async up(): Promise<void> {
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"`)
}
}