Загрузка данных


BEGIN;

-- 0) Airline (routes.airl_code -> airlines.code FK).
INSERT INTO airlines (code, name)
VALUES ('6R', 'MOCK 6R Airline')
ON CONFLICT (code) DO NOTHING;

-- 1) Routes (routes.id is GENERATED ALWAYS AS IDENTITY -> OVERRIDING SYSTEM VALUE).
--    routes.number is TEXT. One route row per flight (route key includes number).
INSERT INTO routes (id, airl_code, number, origin_iata, destination_iata, distance)
OVERRIDING SYSTEM VALUE
VALUES
  (900000001, '6R', '1742', 'OVB', 'YKS', 2480),
  (900000002, '6R',  '938', 'YKS', 'AER', 5900),
  (900000003, '6R', '2615', 'VKO', 'LED',  600),
  (900000004, '6R', '4471', 'YKS', 'OVB', 2480),
  (900000005, '6R',  '307', 'VKO', 'OVB', 2810),
  (900000006, '6R', '5189', 'YKS', 'AER', 5900),
  (900000007, '6R', '1263', 'VKO', 'LED',  600),
  (900000008, '6R', '3856', 'OVB', 'YKS', 2480),
  (900000009, '6R',  '672', 'YKS', 'OVB', 2480),
  (900000010, '6R', '4920', 'VKO', 'OVB', 2810);

-- 2) Flights (flights.id is GENERATED BY DEFAULT -> explicit value allowed).
INSERT INTO flights
  (id, route_id, airl_code, number, orig_iata, dest_iata, departure_time, arrival_time,
   orig_order, dest_order, flt_date, aircraft, resource, total_cash, state,
   last_update, correctly_parsed, departure_tz, arrival_tz, departure, arrival,
   authorized_capacity, ovb)
VALUES
  (900000001, 900000001, '6R', 1742, 'OVB', 'YKS', '0615', '0940', 1, 2, '20260601', 'SU9', 100, 0, 'open', now(), true, '2026-06-01 06:15:00+00', '2026-06-01 09:40:00+00', '2026-06-01 06:15:00', '2026-06-01 09:40:00', 98, 0),
  (900000002, 900000002, '6R',  938, 'YKS', 'AER', '1320', '1705', 1, 2, '20260601', 'SU9', 100, 0, 'open', now(), true, '2026-06-01 13:20:00+00', '2026-06-01 17:05:00+00', '2026-06-01 13:20:00', '2026-06-01 17:05:00', 98, 0),
  (900000003, 900000003, '6R', 2615, 'VKO', 'LED', '0805', '0935', 1, 2, '20260602', 'SU9', 100, 0, 'open', now(), true, '2026-06-02 08:05:00+00', '2026-06-02 09:35:00+00', '2026-06-02 08:05:00', '2026-06-02 09:35:00', 98, 0),
  (900000004, 900000004, '6R', 4471, 'YKS', 'OVB', '1845', '2150', 1, 2, '20260601', 'SU9', 100, 0, 'open', now(), true, '2026-06-01 18:45:00+00', '2026-06-01 21:50:00+00', '2026-06-01 18:45:00', '2026-06-01 21:50:00', 98, 0),
  (900000005, 900000005, '6R',  307, 'VKO', 'OVB', '1110', '1420', 1, 2, '20260602', 'SU9', 100, 0, 'open', now(), true, '2026-06-02 11:10:00+00', '2026-06-02 14:20:00+00', '2026-06-02 11:10:00', '2026-06-02 14:20:00', 98, 0),
  (900000006, 900000006, '6R', 5189, 'YKS', 'AER', '0530', '0915', 1, 2, '20260602', 'SU9', 100, 0, 'open', now(), true, '2026-06-02 05:30:00+00', '2026-06-02 09:15:00+00', '2026-06-02 05:30:00', '2026-06-02 09:15:00', 98, 0),
  (900000007, 900000007, '6R', 1263, 'VKO', 'LED', '1655', '1830', 1, 2, '20260601', 'SU9', 100, 0, 'open', now(), true, '2026-06-01 16:55:00+00', '2026-06-01 18:30:00+00', '2026-06-01 16:55:00', '2026-06-01 18:30:00', 98, 0),
  (900000008, 900000008, '6R', 3856, 'OVB', 'YKS', '0915', '1300', 1, 2, '20260602', 'SU9', 100, 0, 'open', now(), true, '2026-06-02 09:15:00+00', '2026-06-02 13:00:00+00', '2026-06-02 09:15:00', '2026-06-02 13:00:00', 98, 0),
  (900000009, 900000009, '6R',  672, 'YKS', 'OVB', '2005', '2310', 1, 2, '20260602', 'SU9', 100, 0, 'open', now(), true, '2026-06-02 20:05:00+00', '2026-06-02 23:10:00+00', '2026-06-02 20:05:00', '2026-06-02 23:10:00', 98, 0),
  (900000010, 900000010, '6R', 4920, 'VKO', 'OVB', '0040', '0350', 1, 2, '20260601', 'SU9', 100, 0, 'open', now(), true, '2026-06-01 00:40:00+00', '2026-06-01 03:50:00+00', '2026-06-01 00:40:00', '2026-06-01 03:50:00', 98, 0);

-- 3) flight_snapshots (3 per flight). Drives getAllFlightSnapshotsByFilters.
INSERT INTO flight_snapshots (flight_id, time, total_cash, load, arr)
SELECT f.id, s.time, s.total_cash, s.load, s.arr
FROM flights f
CROSS JOIN (VALUES
  (TIMESTAMPTZ '2026-05-25 12:00:00+00',  8000::float8, 20, 250::float8),
  (TIMESTAMPTZ '2026-05-29 12:00:00+00', 12000::float8, 35, 300::float8),
  (TIMESTAMPTZ '2026-05-31 12:00:00+00', 18000::float8, 55, 327::float8)
) AS s(time, total_cash, load, arr)
WHERE f.id BETWEEN 900000001 AND 900000010;

-- 4) ticket_snapshots (per-class booking time series; PK (flight_id,time,class)).
--    Drives getTicketSnapshotsSegmentsByFilters.
INSERT INTO ticket_snapshots
  (flight_id, time, class, cabin, resource, booked_seats, nesting_capacity, available_seats, total_cash, last_booking, noshow)
SELECT f.id, t.time, t.class, t.cabin, t.resource, t.booked_seats, t.nesting_capacity, t.available_seats, t.total_cash, t.last_booking, t.noshow
FROM flights f
CROSS JOIN (VALUES
  (TIMESTAMPTZ '2026-05-25 12:00:00+00', 'Y', 'Y', 98, 20, 98, 78,  8000::float8, TIMESTAMPTZ '2026-05-25 11:00:00+00', 0),
  (TIMESTAMPTZ '2026-05-29 12:00:00+00', 'Y', 'Y', 98, 35, 98, 63, 12000::float8, TIMESTAMPTZ '2026-05-29 11:00:00+00', 0),
  (TIMESTAMPTZ '2026-05-31 12:00:00+00', 'Y', 'Y', 98, 55, 98, 43, 18000::float8, TIMESTAMPTZ '2026-05-31 11:00:00+00', 1),
  (TIMESTAMPTZ '2026-05-31 12:00:00+00', 'M', 'Y', 98, 10, 20, 10,  4000::float8, TIMESTAMPTZ '2026-05-31 10:00:00+00', 0)
) AS t(time, class, cabin, resource, booked_seats, nesting_capacity, available_seats, total_cash, last_booking, noshow)
WHERE f.id BETWEEN 900000001 AND 900000010;

-- 5) booking_data_changes (nesting/capacity change log; matched by flight identity).
--    Drives getLastNestingCapacityChangeByFlightIdentity -> {class: nesting_capacity}.
--    One shared segment_uuid per flight (md5 of id); number/flt_date/departure_time are varchar;
--    user_login must exist (FK -> users.login).
INSERT INTO booking_data_changes
  (segment_uuid, airl_code, number, orig_iata, dest_iata, flt_date, class, nesting_capacity,
   time, user_login, exported, last_nesting_capacity, creator_service, departure_time, action, is_auto)
SELECT md5(f.id::text || 'mock6r')::uuid, '6R', f.number::text, f.orig_iata, f.dest_iata, f.flt_date,
       c.class, c.nesting_capacity, now(), 'demouserAA', true, c.last_nesting_capacity,
       'business-rule-scheduler', f.departure_time, 'manualChange', false
FROM flights f
CROSS JOIN (VALUES ('Y', 100, 95), ('M', 80, 78)) AS c(class, nesting_capacity, last_nesting_capacity)
WHERE f.id BETWEEN 900000001 AND 900000010;

-- 6) rival_flights (competitor observations; feeds latest_rival_flights matview).
--    observation_time = now() => is_relevant = true. source='mock_6r_test' is the
--    rollback sentinel (real 6R rival data must NOT be touched). id is GENERATED ALWAYS -> omitted.
INSERT INTO rival_flights
  (airl_code, observation_time, origin, destination, is_one_way, outbound_flight_number, number,
   outbound_departure_time, outbound_arrival_time, departure_tz, arrival_tz,
   price, tax, currency, aircraft_code, aircraft_name, seats_amount_min, seats_amount_max,
   state, source, parsed_at)
SELECT '6R', now(), f.orig_iata, f.dest_iata, 1, '6R' || f.number::text, f.number::int,
       f.departure, f.arrival, f.departure_tz, f.arrival_tz,
       12500::float8, 0::float8, 'RUB', 'SU9', NULL, 98, 98,
       'actual', 'mock_6r_test', now()
FROM flights f
WHERE f.id BETWEEN 900000001 AND 900000010;

COMMIT;