What happens if we change the database?

Pick the change you plan and see which reports it would break, before anyone writes the code.

Try an example: renaming organization_name to name

Renaming organization_name would break 23 of 50 reports.

Answered in 2.98 s from the map of every report, with the same rules as a release check. Example question

0 may show different numbers, 0 worth a look, 27 not affected.

The 23 reports it reaches

Broken 23 reports

These would stop working. Update their datasets, or keep the old name behind a view, before this ships.

Safety: 12 reports
Site and workforce: 6 reports
Finance and cost: 2 reports
Projects and procurement: 2 reports
Quality and defects: 1 report
Technical details
  • 23 Broken
  • 0 Affected
  • 27 Safe

167 charts on 33 datasets. Answered offline from the dependency map built 17 Nov 2026.

The 9 paths go through 2 foreign tables in 2 databases: site_management_services.user_schema.organizations (5 paths, 19 dashboards) and project_services.user_schema.organizations (4 paths, 4 dashboards).

How to avoid it: 2 statements, one in project_services and one in site_management_services, avoid all 23 breaks.
  • In project_services, point the foreign table at the new name so nothing above it changes. Run it in the same release as the migration.

    ALTER FOREIGN TABLE user_schema.organizations
      ALTER COLUMN organization_name OPTIONS (ADD column_name 'name');
  • In site_management_services, point the foreign table at the new name so nothing above it changes. Run it in the same release as the migration.

    ALTER FOREIGN TABLE user_schema.organizations
      ALTER COLUMN organization_name OPTIONS (ADD column_name 'name');
  • Safer alternative (expand/contract): add the new column, write both for one release, move reports, then drop the old one.
31 alternative SQL drafts (patch every view and dataset instead)

Re-import site_management_services.user_schema.organizations

DROP FOREIGN TABLE user_schema.organizations;  -- add CASCADE only together with the view/MV drafts below
IMPORT FOREIGN SCHEMA public LIMIT TO (organizations)
  FROM SERVER user_srv INTO user_schema;

Materialized view site_management_services.public.mv_defect_by_type: organization_name → name AS organization_name

DROP MATERIALIZED VIEW public.mv_defect_by_type;
CREATE MATERIALIZED VIEW public.mv_defect_by_type AS
 WITH filtered_defects AS (
         SELECT d.id,
            d.project_id,
            d.priority,
            d.trade,
            d.status,
            d.assign_to_id,
            d.due_date,
            d.created_by,
            d.created_at,
            d.updated_at,
            d.last_fixed_at,
            d.last_fixed_by,
            d.type_id,
            d.area_name,
            d.building_id
           FROM defect_schema.defects d
          WHERE d.is_archive IS NOT TRUE
        ), needed_user_ids AS (
         SELECT filtered_defects.assign_to_id AS id
           FROM filtered_defects
          WHERE filtered_defects.assign_to_id IS NOT NULL
        UNION
         SELECT filtered_defects.created_by AS id
           FROM filtered_defects
          WHERE filtered_defects.created_by IS NOT NULL
        UNION
         SELECT filtered_defects.last_fixed_by AS id
           FROM filtered_defects
          WHERE filtered_defects.last_fixed_by IS NOT NULL
        ), user_dim AS (
         SELECT u.id,
            concat_ws(' '::text, u.first_name, u.last_name) AS full_name,
            o.name AS organization_name
           FROM user_schema.users u
             LEFT JOIN user_schema.organizations o ON o.id = u.organization_id
             JOIN needed_user_ids n ON n.id = u.id
        ), project_dim AS (
         SELECT p_1.id,
            p_1.uuid,
            p_1.title
           FROM project_schema.projects p_1
        ), defect_type_dim AS (
         SELECT dt_1.id,
            dt_1.name
           FROM defect_schema.defect_types dt_1
        )
 SELECT fd.id,
    fd.project_id,
        CASE
            WHEN fd.priority IS NULL THEN NULL::text
            ELSE upper(TRIM(BOTH FROM fd.priority))
        END AS priority,
    fd.trade,
    fd.status,
    fd.assign_to_id,
    (fd.due_date AT TIME ZONE 'Australia/Sydney'::text) AS due_date,
    fd.created_by,
    (fd.created_at AT TIME ZONE 'Australia/Sydney'::text) AS created_at,
    (fd.updated_at AT TIME ZONE 'Australia/Sydney'::text) AS updated_at,
    (fd.last_fixed_at AT TIME ZONE 'Australia/Sydney'::text) AS last_fixed_at,
    fd.last_fixed_by,
    fd.type_id,
    fd.area_name,
    fd.building_id,
    b.name AS building_name,
    p.uuid AS project_uuid,
    p.title AS project_title,
    ua.full_name AS assign_to_name,
    ua.organization_name AS assign_to_organization_name,
    uc.full_name AS created_by_name,
    uc.organization_name AS created_by_organization_name,
    ulf.full_name AS last_fixed_by_name,
    ulf.organization_name AS last_fixed_by_organization_name,
    dt.name AS defect_type_name
   FROM filtered_defects fd
     LEFT JOIN project_dim p ON p.id = fd.project_id
     LEFT JOIN defect_type_dim dt ON dt.id = fd.type_id
     LEFT JOIN defect_schema.buildings b ON b.id = fd.building_id
     LEFT JOIN user_dim ua ON ua.id = fd.assign_to_id
     LEFT JOIN user_dim uc ON uc.id = fd.created_by
     LEFT JOIN user_dim ulf ON ulf.id = fd.last_fixed_by;;
-- then recreate its indexes and grants (see the production definition)

Materialized view site_management_services.public.mv_blacklist_warnings: organization_name → name AS organization_name

DROP MATERIALIZED VIEW public.mv_blacklist_warnings;
CREATE MATERIALIZED VIEW public.mv_blacklist_warnings AS
 SELECT bw.id AS history_id,
    bw.uuid AS history_uuid,
    bw.organization_id AS history_organization_id,
    bw.organization_uuid AS history_organization_uuid,
    ow.name AS history_organization_name,
    bw.personnel_id AS history_personnel_id,
    bw.personnel_uuid AS history_personnel_uuid,
    TRIM(BOTH FROM concat(hp.first_name, ' ', hp.last_name)) AS history_personnel_name,
    opw.name AS history_personnel_organization_name,
    COALESCE(bw.project_id, bb.project_id) AS project_id,
    COALESCE(bw.project_uuid, bb.project_uuid) AS project_uuid,
    pw.title AS history_project_title,
    bw.breach_category_id AS history_breach_category_id,
    bw.breach_category_uuid AS history_breach_category_uuid,
    bcw.name AS history_breach_category_name,
    bw.description AS history_description,
    (bw.date_time AT TIME ZONE 'Australia/Sydney'::text) AS history_date_time,
    (bw.created_at AT TIME ZONE 'Australia/Sydney'::text) AS history_created_at,
    bw.created_by AS history_created_by,
    (bw.updated_at AT TIME ZONE 'Australia/Sydney'::text) AS history_updated_at,
    (bw.deleted_at AT TIME ZONE 'Australia/Sydney'::text) AS history_deleted_at,
    bw.status AS history_status,
    bb.id AS current_id,
    bb.uuid AS current_uuid,
    bb.organization_id AS current_organization_id,
    bb.organization_uuid AS current_organization_uuid,
    ob.name AS current_organization_name,
    bb.personnel_id AS current_personnel_id,
    bb.personnel_uuid AS current_personnel_uuid,
    TRIM(BOTH FROM concat(cp.first_name, ' ', cp.last_name)) AS current_personnel_name,
    opb.name AS current_personnel_organization_name,
    pb.title AS current_project_title,
    bb.breach_category_id AS current_breach_category_id,
    bb.breach_category_uuid AS current_breach_category_uuid,
    bcb.name AS current_breach_category_name,
    bb.description AS current_description,
    (bb.date_time AT TIME ZONE 'Australia/Sydney'::text) AS current_date_time,
    (bb.created_at AT TIME ZONE 'Australia/Sydney'::text) AS current_created_at,
    bb.created_by AS current_created_by,
    (bb.updated_at AT TIME ZONE 'Australia/Sydney'::text) AS current_updated_at,
    (bb.deleted_at AT TIME ZONE 'Australia/Sydney'::text) AS current_deleted_at,
    bb.status AS current_status,
    bb.is_warning AS current_is_warning
   FROM blackbook_warnings bw
     FULL JOIN blackbooks bb ON bw.uuid = bb.uuid
     LEFT JOIN breach_categories bcw ON bw.breach_category_id = bcw.id
     LEFT JOIN breach_categories bcb ON bb.breach_category_id = bcb.id
     LEFT JOIN user_schema.organizations ow ON bw.organization_id = ow.id
     LEFT JOIN user_schema.organizations ob ON bb.organization_id = ob.id
     LEFT JOIN project_schema.projects pw ON bw.project_id = pw.id
     LEFT JOIN project_schema.projects pb ON bb.project_id = pb.id
     LEFT JOIN personnels hp ON bw.personnel_id = hp.id
     LEFT JOIN user_schema.organizations opw ON hp.organisation_id = opw.id
     LEFT JOIN personnels cp ON bb.personnel_id = cp.id
     LEFT JOIN user_schema.organizations opb ON cp.organisation_id = opb.id
  WHERE bw.deleted_at IS NULL AND bb.deleted_at IS NULL;;
-- then recreate its indexes and grants (see the production definition)

Materialized view site_management_services.public.mv_obs_detail: organization_name → name AS organization_name

DROP MATERIALIZED VIEW public.mv_obs_detail;
CREATE MATERIALIZED VIEW public.mv_obs_detail AS
 WITH obs AS (
         SELECT observations.id,
            observations.uuid,
            observations.project_id,
            observations.title,
            observations.priority,
            observations.location,
            observations.type,
            observations.description,
            observations.status,
            observations.assign_to_id,
            observations.due_date,
            observations.created_by,
            observations.created_at,
            observations.updated_at,
            observations.organization_uuid,
            observations.organization_id,
            observations.building_id,
            observations.building_uuid,
            observations.area_name,
            observations.area_levels,
            observations.code,
            observations.meeting_inspection_item_uuid,
            observations.meeting_inspection_item_checklist_index,
            observations.conformance,
            observations.sub_type,
            observations.updated_by,
            observations.approve_signature,
            observations.approve_at,
            observations.approve_by,
            observations.corrective_action_required,
            observations.corrective_action_taken,
            observations.additional_evidence_of_corrective_action,
            observations.last_status_updated_at,
            observations.last_status_updated_by,
            observations.last_fixed_at,
            observations.last_fixed_by,
            observations.is_archive,
            observations.last_reminder_at
           FROM observations
          WHERE observations.is_archive = false
        ), needed_project_ids AS (
         SELECT DISTINCT obs.project_id
           FROM obs
          WHERE obs.project_id IS NOT NULL
        ), needed_org_ids AS (
         SELECT DISTINCT obs.organization_id
           FROM obs
          WHERE obs.organization_id IS NOT NULL
        ), needed_user_ids AS (
         SELECT DISTINCT obs.created_by AS user_id
           FROM obs
          WHERE obs.created_by IS NOT NULL
        UNION
         SELECT DISTINCT obs.assign_to_id
           FROM obs
          WHERE obs.assign_to_id IS NOT NULL
        ), projects_needed AS (
         SELECT p_1.id,
            p_1.uuid,
            p_1.title
           FROM project_schema.projects p_1
          WHERE (p_1.id IN ( SELECT needed_project_ids.project_id
                   FROM needed_project_ids))
        ), orgs_needed AS (
         SELECT org_1.id,
            org_1.name AS organization_name
           FROM user_schema.organizations org_1
          WHERE (org_1.id IN ( SELECT needed_org_ids.organization_id
                   FROM needed_org_ids))
        ), users_needed AS (
         SELECT u.id,
            u.first_name,
            u.last_name
           FROM user_schema.users u
          WHERE (u.id IN ( SELECT needed_user_ids.user_id
                   FROM needed_user_ids))
        )
 SELECT o.id,
    o.code,
    o.project_id,
    p.uuid AS project_uuid,
    p.title AS project_title,
    o.title AS observation_title,
    o.type,
    o.created_by,
    concat(uc.first_name, ' ', uc.last_name) AS created_by_name,
    o.assign_to_id,
    concat(ua.first_name, ' ', ua.last_name) AS assign_to_name,
    o.organization_id,
    org.organization_name,
    (o.due_date AT TIME ZONE 'Australia/Sydney'::text) AS due_date_sydney,
    (o.created_at AT TIME ZONE 'Australia/Sydney'::text) AS created_at_sydney,
    o.status
   FROM obs o
     LEFT JOIN projects_needed p ON p.id = o.project_id
     LEFT JOIN users_needed uc ON uc.id = o.created_by
     LEFT JOIN users_needed ua ON ua.id = o.assign_to_id
     LEFT JOIN orgs_needed org ON org.id = o.organization_id;;
-- then recreate its indexes and grants (see the production definition)

Materialized view site_management_services.public.mv_prestart: organization_name → name AS organization_name

DROP MATERIALIZED VIEW public.mv_prestart;
CREATE MATERIALIZED VIEW public.mv_prestart AS
 WITH target_meeting AS (
         SELECT mi.id AS meeting_id,
            mi.uuid AS meeting_uuid,
            mi.project_uuid,
            mi.code AS meeting_code,
            mt.name AS meeting_type_name,
            mi.title AS meeting_title,
            (mi.date_time AT TIME ZONE 'Australia/Sydney'::text)::date AS sydney_meeting_date,
            mi.attendees_list
           FROM meeting_inspections mi
             JOIN meeting_types mt ON mt.uuid = mi.type_uuid
          WHERE mi.source_type::text = 'MEETING'::text
        ), meeting_attendees AS (
         SELECT DISTINCT ON (tm.meeting_uuid, ((att.value ->> 'uuid'::text)::uuid)) tm.meeting_id,
            tm.meeting_uuid,
            tm.project_uuid,
            tm.meeting_code,
            tm.meeting_type_name,
            tm.meeting_title,
            tm.sydney_meeting_date,
            (att.value ->> 'uuid'::text)::uuid AS personnel_uuid,
            att.value ->> 'signature'::text AS attendee_signature
           FROM target_meeting tm
             CROSS JOIN LATERAL jsonb_array_elements(tm.attendees_list) att(value)
          WHERE (att.value ->> 'source'::text) = 'PERSONNEL'::text
          ORDER BY tm.meeting_uuid, ((att.value ->> 'uuid'::text)::uuid), (att.value ->> 'signature'::text) DESC
        ), signed_in AS (
         SELECT p_1.uuid AS personnel_uuid,
            a.project_id,
            a.project_uuid::uuid AS project_uuid,
            (a.datetime_in AT TIME ZONE 'Australia/Sydney'::text)::date AS sydney_signed_in_date
           FROM attendances a
             JOIN personnels p_1 ON p_1.id = a.personnel_id
          GROUP BY p_1.uuid, a.project_id, a.project_uuid, ((a.datetime_in AT TIME ZONE 'Australia/Sydney'::text)::date)
        ), organization_link AS (
         SELECT DISTINCT "organization-link".project_id,
            "organization-link".organization_id
           FROM user_schema."organization-link"
          WHERE "organization-link".is_archive IS FALSE
        )
 SELECT ma.sydney_meeting_date AS batch_sydney_date,
    proj.id AS project_id,
    proj.uuid AS project_uuid,
    proj.title AS project_title,
    o.name AS organization_name,
    p.id AS personnel_id,
    p.uuid AS personnel_uuid,
    p.personnel_id AS personnel_code,
    p.first_name,
    p.last_name,
    ma.meeting_type_name,
    ma.meeting_title,
    ma.meeting_code,
    ma.meeting_uuid,
        CASE
            WHEN COALESCE(ma.attendee_signature, ''::text) <> ''::text THEN true
            ELSE false
        END AS has_prestart,
        CASE
            WHEN si.personnel_uuid IS NOT NULL THEN true
            ELSE false
        END AS has_turnstile,
        CASE
            WHEN COALESCE(ma.attendee_signature, ''::text) <> ''::text AND si.personnel_uuid IS NOT NULL THEN 'GREEN'::text
            ELSE 'RED'::text
        END AS status
   FROM meeting_attendees ma
     JOIN project_schema.projects proj ON proj.uuid = ma.project_uuid
     JOIN personnels p ON p.uuid = ma.personnel_uuid
     LEFT JOIN signed_in si ON si.project_uuid = ma.project_uuid AND si.sydney_signed_in_date = ma.sydney_meeting_date AND si.personnel_uuid = ma.personnel_uuid
     LEFT JOIN organization_link ol ON ol.project_id = proj.id AND ol.organization_id = p.organisation_id
     LEFT JOIN user_schema.organizations o ON o.id = ol.organization_id
  ORDER BY ma.sydney_meeting_date DESC, ma.meeting_code DESC;;
-- then recreate its indexes and grants (see the production definition)

Dataset "incidents" (id 29): SQL with organization_name → name AS organization_name

SELECT
    i.id,
    i.uuid,
    i.organisation_uuid,
    i.project_uuid,
    i.incident_id,
    ic.name AS category_name,
    i.date_time,

    pu.personnel_uuid,

    o.name AS organization_name,

    i.category_uuid,
    i.sub_category_uuid,
    i.incident_contributor,
    i.incident_or_observe,
    i.additional_personnel,
    i.additional_equipment,
    i.additional_sds,
    i.additional_swms,
    i.images_uuid,
    i.status,
    CASE i.status
        WHEN 'PENDING'  THEN 'Draft'
        WHEN 'OPEN'     THEN 'Open'
        WHEN 'REVIEW'   THEN 'Under Investigation'
        WHEN 'APPROVED' THEN 'Approved & Closed'
        WHEN 'DECLINED' THEN 'Declined'
        ELSE i.status
    END AS status_display,
    i.created_by,
    i.created_at,
    i.updated_at,
    i.approved_at,
    i.approved_by,
    i.personnel_uuids,
    i.building_uuid,
    i.area_name,
    i.area_levels,

    p.id    AS project_id,
    p.title AS project_title,

    i.description
FROM incidents i
LEFT JOIN "incident-category" ic
    ON i.category_uuid = ic.uuid
LEFT JOIN project_schema.projects p
    ON p.uuid = i.project_uuid
LEFT JOIN LATERAL (
    SELECT jsonb_array_elements_text(
        COALESCE(i.personnel_uuids::jsonb, '[]'::jsonb)
    ) AS personnel_uuid
) pu ON TRUE
LEFT JOIN personnels per
    ON per.uuid = pu.personnel_uuid::uuid
LEFT JOIN user_schema.organizations o
    ON o.id = per.organisation_id;

Dataset "DATASET_Attendance Per Org Per Day" (id 53): SQL with organization_name → name AS organization_name

-- ✅ CEPAT: dedup dulu per hari (zona Australia/Sydney), lalu agregasi
WITH base AS (
  SELECT
    a.project_id,
    a.project_uuid,
    p.organisation_id,
    (a.datetime_in AT TIME ZONE 'Australia/Sydney')::date AS local_date,
    a.personnel_id
  FROM attendances a
  JOIN personnels p ON p.id = a.personnel_id
),
uniq AS (
  -- DISTINCT ON butuh ORDER BY; kita tidak butuh baris tertentu, jadi urutkan sesuai kunci
  SELECT DISTINCT ON (project_id, organisation_id, local_date, personnel_id)
         project_id, project_uuid, organisation_id, local_date, personnel_id
  FROM base
  ORDER BY project_id, organisation_id, local_date, personnel_id
)
SELECT
  u.project_id,
  u.project_uuid,
  o.name AS organization_name,
  u.local_date AS raw_date,
  COUNT(*) AS man_count
FROM uniq u
JOIN user_schema.organizations o ON o.id = u.organisation_id
GROUP BY
  u.project_id, u.project_uuid, o.name, raw_date

Dataset "DATASET_Attendance Per Org Per Week" (id 51): SQL with organization_name → name AS organization_name

WITH base AS (
  SELECT
      a.project_id,
      a.project_uuid,
      o.name AS organization_name,
      -- tanggal lokal sekali saja (menghindari ulang-ulang AT TIME ZONE)
      (a.datetime_in AT TIME ZONE 'Australia/Sydney')::date AS local_date,
      p.id AS personnel_id
  FROM attendances a
  JOIN personnels p ON p.id = a.personnel_id
  JOIN user_schema.organizations o ON o.id = p.organisation_id
),
daily AS (
  -- hitung unik person-per-day per project (ini jauh lebih murah)
  SELECT
      project_id,
      project_uuid,
      organization_name,
      local_date,
      COUNT(DISTINCT personnel_id) AS daily_persons
  FROM base
  GROUP BY
      project_id, project_uuid, organization_name, local_date
),
weekly AS (
  SELECT
      project_id,
      project_uuid,
      organization_name,
      DATE_TRUNC('week', local_date::timestamp)::date AS week_start,
      SUM(daily_persons) AS man_count
  FROM daily
  GROUP BY
      project_id, project_uuid, organization_name,
      DATE_TRUNC('week', local_date::timestamp)
)
SELECT
    project_id,
    project_uuid,
    organization_name,
    week_start,
    man_count
FROM weekly

Dataset "DATASET_Attendance Per Org Per Month" (id 52): SQL with organization_name → name AS organization_name

WITH attendance_days AS (
  -- Satu baris per (project, personnel, local_date)
  SELECT DISTINCT
    a.project_id,
    a.project_uuid,
    o.name AS organization_name,
    p.id AS personnel_id,
    (a.datetime_in AT TIME ZONE 'Australia/Sydney')::date AS local_date
  FROM attendances a
  JOIN personnels p ON p.id = a.personnel_id
  JOIN user_schema.organizations o ON o.id = p.organisation_id
)
SELECT
  ad.project_id,
  ad.project_uuid,
  ad.organization_name,
  date_trunc('month', ad.local_date)::date AS month_start,
  COUNT(*) AS man_count               -- sudah terdeduplikasi di atas
FROM attendance_days ad
GROUP BY
  ad.project_id, ad.project_uuid, ad.organization_name, month_start

Dataset "DATASET_All Site Attendance" (id 59): SQL with organization_name → name AS organization_name

SELECT 
    a.project_id,
    a.project_uuid,
    o.name AS organization_name,
    -- Kolom tambahan sebagai referensi untuk urutan
    DATE(a.datetime_in AT TIME ZONE 'Australia/Sydney') AS raw_date,
    COUNT(DISTINCT a.personnel_id) AS man_count
FROM 
    attendances a
JOIN 
    personnels p ON a.personnel_id = p.id
JOIN 
    user_schema.organizations o ON p.organisation_id = o.id
GROUP BY 
    a.project_id, a.project_uuid, o.name, raw_date

Dataset "DATASET_Expiring SWMS" (id 195): SQL with organization_name → name AS organization_name

WITH swms_filtered AS (
    SELECT
        s.id,
        s.uuid,
        s.organisation_uuid,
        s.project_uuid,
        s.reference_id,
        s.activity,
        s.description,
        s.assigned_user,
        s.approve_by_date,
        s.is_high_risk,
        s.approval_status,
        s.approval_note,
        s.approver,
        s.review_or_expiry_date,
        s.approved_at,
        s.is_archive,
        s.created_by,
        s.created_at,
        s.updated_at,
        (s.review_or_expiry_date AT TIME ZONE 'Australia/Sydney')::date AS expiry_date_syd,
        (NOW()                   AT TIME ZONE 'Australia/Sydney')::date AS today_syd,
        NULLIF(btrim(s.assigned_user), '')::uuid                        AS assigned_user_uuid
    FROM public.swms s
    WHERE s.is_archive IS NOT TRUE
      AND s.review_or_expiry_date IS NOT NULL
)
SELECT
    s.*,
    p.id    AS project_id,
    p.title AS project_title,
    o.name AS organization_name,
    btrim(concat_ws(' ', uc.first_name, uc.last_name)) AS created_by_name,
    btrim(concat_ws(' ', ua.first_name, ua.last_name)) AS assigned_user_name,
    s.expiry_date_syd - s.today_syd AS days_to_expiry,
    CASE
        WHEN s.expiry_date_syd <  s.today_syd      THEN 'Expired'
        WHEN s.expiry_date_syd <= s.today_syd + 7  THEN 'Due within 7 days'
        WHEN s.expiry_date_syd <= s.today_syd + 30 THEN 'Due in 8-30 days'
        WHEN s.expiry_date_syd <= s.today_syd + 90 THEN 'Due in 31-90 days'
        ELSE                                            'Due in 90+ days'
    END AS expiry_status
FROM swms_filtered s
LEFT JOIN project_schema.projects   p  ON p.uuid = s.project_uuid
LEFT JOIN user_schema.organizations o  ON o.uuid = s.organisation_uuid
LEFT JOIN user_schema.users         uc ON uc.id  = s.created_by
LEFT JOIN user_schema.users         ua ON ua.uuid = s.assigned_user_uuid

Dataset "DATASET_Expiring Insurances" (id 196): SQL with organization_name → name AS organization_name

WITH base AS (
    SELECT
        il.*,
        o.uuid AS organisation_uuid,
        o.name AS organization_name,
        p.id    AS project_id,
        p.uuid  AS project_uuid,
        p.title AS project_title,
        (il.exp_date AT TIME ZONE 'Australia/Sydney')::date AS expiry_date_syd,
        (NOW()       AT TIME ZONE 'Australia/Sydney')::date AS today_syd
    FROM public.insurance_licenses il
    LEFT JOIN user_schema.organizations o
           ON o.id = il.ref_id
          AND il.ref = 'ORGANIZATIONS'
    LEFT JOIN project_schema.projects p ON p.id = o.project_id
    WHERE il.type = 'insurance'
      AND il.exp_date IS NOT NULL
)
SELECT
    b.*,
    b.expiry_date_syd - b.today_syd AS days_to_expiry,
    CASE
        WHEN b.expiry_date_syd <  b.today_syd      THEN 'Expired'
        WHEN b.expiry_date_syd <= b.today_syd + 7  THEN 'Due within 7 days'
        WHEN b.expiry_date_syd <= b.today_syd + 30 THEN 'Due in 8-30 days'
        WHEN b.expiry_date_syd <= b.today_syd + 90 THEN 'Due in 31-90 days'
        ELSE                                            'Due in 90+ days'
    END AS expiry_status
FROM base b

Dataset "DATASET_Total Workers" (id 62): SQL with organization_name → name AS organization_name

SELECT 
    pp.project_id,
    pp.project_uuid,
    p.title AS project_name,
    pp.personnel_id,
    pr.organisation_id,
    o.name AS organization_name
FROM 
    personnel_projects pp
JOIN 
    project_schema.projects p 
    ON pp.project_id = p.id
JOIN 
    personnels pr
    ON pp.personnel_id = pr.id
JOIN 
    user_schema.organizations o
    ON pr.organisation_id = o.id
WHERE
    pp.is_on_site = TRUE
    AND pp.is_archive = FALSE
GROUP BY 
    pp.project_id,
    pp.project_uuid,
    p.title,
    pp.personnel_id,
    pr.organisation_id,
    o.name

Dataset "DATASET_Total Meeting/Inspection" (id 63): SQL with organization_name → name AS organization_name

SELECT
    prj.id                 AS project_id,
    prj.uuid               AS project_uuid,
    prj.title              AS project_name,
    mi.id                  AS meeting_inspection_id,   -- yang dihitung
    (mi.date_time AT TIME ZONE 'Australia/Sydney')::date AS date_time, -- tanggal Sydney
    o.name AS organization_name,
    mi.source_type,
    mt.name                AS meeting_type_name,
    mt.id AS meeting_type_id,-- dari meeting_types,
    mi.title AS mi_title,
    mi.status,
    mi.created_at::date AS date_created
FROM meeting_inspections mi
JOIN project_schema.projects prj
  ON prj.uuid = mi.project_uuid
JOIN user_schema.users u
  ON u.id = mi.created_by
JOIN user_schema.organizations o
  ON o.id = u.organization_id
LEFT JOIN meeting_types mt
  ON mt.uuid = mi.type_uuid
WHERE
    mi.created_by IS NOT NULL
GROUP BY
    prj.id, prj.uuid, prj.title,
    mi.id,
    o.name,
    mi.source_type,
    mt.name,
    meeting_type_id,
    mi.title

Dataset "DATASET_Permits" (id 64): SQL with organization_name → name AS organization_name

SELECT
    prj.id                 AS project_id,
    prj.uuid               AS project_uuid,
    prj.title              AS project_name,
    p.id                   AS permit_id,                 
    (p.created_at AT TIME ZONE 'Australia/Sydney')::date AS date, -- tanggal Sydney
    COALESCE(o_explicit.name,
             o_owner.name)     AS organization_name
FROM permits p
JOIN project_schema.projects prj
  ON prj.uuid = p.project_uuid
LEFT JOIN user_schema.organizations o_explicit
  ON o_explicit.uuid = p.organisation_uuid      -- org eksplisit pada permit (bisa NULL)
JOIN user_schema.organizations o_owner
  ON o_owner.id = prj.organization_id           -- owner project (fallback)
WHERE p.status = 'COMPLETED'                    -- (opsional)
GROUP BY
    prj.id, prj.uuid, prj.title,
    p.id, date,
    COALESCE(o_explicit.name, o_owner.name),
    p.status

Dataset "injuries" (id 31): SQL with organization_name → name AS organization_name

SELECT
    i.*,
    CASE i.status
        WHEN 'PENDING'  THEN 'Draft'
        WHEN 'OPEN'     THEN 'Open'
        WHEN 'REVIEW'   THEN 'Under Investigation'
        WHEN 'APPROVED' THEN 'Approved & Closed'
        WHEN 'DECLINED' THEN 'Declined'
        ELSE i.status
    END AS status_display,
    ic.name AS injury_category_name,

    pu.personnel_uuid,

    o.name AS organization_name,

    p.id    AS project_id,
    p.title AS project_title
FROM injuries i
LEFT JOIN "injury-category" ic
    ON i.category_uuid = ic.uuid
LEFT JOIN project_schema.projects p
    ON p.uuid = i.project_uuid
LEFT JOIN LATERAL (
    SELECT jsonb_array_elements_text(
        COALESCE(i.personnel_uuids::jsonb, '[]'::jsonb)
    ) AS personnel_uuid
) pu ON TRUE
LEFT JOIN personnels per
    ON per.uuid = pu.personnel_uuid::uuid
LEFT JOIN user_schema.organizations o
    ON o.id = per.organisation_id;

Dataset "DATASET_Injury Leaderboard" (id 118): SQL with organization_name → name AS organization_name

SELECT
  p.id AS project_id,
  i.project_uuid,
  p.title AS project_title,
  i.date_time,
  o.name AS organization_name,
  COUNT(DISTINCT i.id) AS injury_count
FROM injuries i
JOIN project_schema.projects p
  ON p.uuid = i.project_uuid
JOIN LATERAL jsonb_array_elements_text(i.personnel_uuids::jsonb) pu(personnel_uuid_text)
  ON TRUE
JOIN personnels per
  ON per.uuid = pu.personnel_uuid_text::uuid
JOIN user_schema.organizations o
  ON o.id = per.organisation_id
GROUP BY
  p.id,
  i.project_uuid,
  p.title,
  i.date_time,
  o.name
ORDER BY
  i.date_time DESC,
  p.id,
  o.name;

Dataset "DATASET_SELECT * FROM observations" (id 43): SQL with organization_name → name AS organization_name

WITH now_ref AS (
  SELECT 
    CURRENT_TIMESTAMP                          AS now_utc,
    CURRENT_TIMESTAMP AT TIME ZONE 'Australia/Sydney' AS now_sydney
)
SELECT 
    o.*,
    pr.uuid  AS project_uuid,
    pr.title AS project_name,
    org.name AS organization_name,

    -- Tampilkan waktu lokal Sydney (timestamp tanpa TZ)
    o.due_date    AT TIME ZONE 'Australia/Sydney' AS due_date_sydney,
    o.created_at  AT TIME ZONE 'Australia/Sydney' AS created_at_sydney,
    o.created_at  AT TIME ZONE 'Australia/Sydney' AS month,
    o.created_at  AT TIME ZONE 'Australia/Sydney' AS week_created,
    o.approve_at  AT TIME ZONE 'Australia/Sydney' AS approve_at_sydney,

    -- Overdue dihitung relatif ke waktu lokal Sydney
    CASE 
        WHEN (o.due_date AT TIME ZONE 'Australia/Sydney') < (SELECT now_sydney FROM now_ref)
        THEN (SELECT now_sydney FROM now_ref) - (o.due_date AT TIME ZONE 'Australia/Sydney')
        ELSE NULL
    END AS overdue_duration,

    CASE 
        WHEN (o.due_date AT TIME ZONE 'Australia/Sydney') < (SELECT now_sydney FROM now_ref)
        THEN 'Overdue'
        ELSE 'On Time'
    END AS is_overdue

FROM public.observations o
JOIN project_schema.projects pr ON o.project_id = pr.id
JOIN user_schema.organizations org ON o.organization_id = org.id
WHERE o.is_archive IS FALSE

Dataset "DATASET_Observations by Organization (All Stasuses & Types)" (id 44): SQL with organization_name → name AS organization_name

SELECT 
    organization_name,
    project_id,
    project_uuid,
    SUM(total_observations) AS "Total Observations",
    SUM(total_status_open) AS "# of Open",
    SUM(status_pending) AS "# of Pending",
    SUM(status_in_progress) AS "# of In Progress",
    SUM(status_fixed) AS "# of Fixed",
    SUM(status_approved_and_closed) AS "# of Approved and Closed"
FROM (
    SELECT 
        o.name AS organization_name,
        p.id AS project_id,
        p.uuid AS project_uuid,

        COUNT(obs.id) AS total_observations,

        -- Count per type
        COUNT(*) FILTER (WHERE obs.type = 'COMPLAINTS') AS total_type_complaints,
        COUNT(*) FILTER (WHERE obs.type = 'CORRECTION_TO_INJURY_OR_INCIDENT') AS total_type_correction_to_injury_or_incident,
        COUNT(*) FILTER (WHERE obs.type = 'ENVIRONMENTAL') AS total_type_environmental,
        COUNT(*) FILTER (WHERE obs.type = 'ERGONOMIC_BREECHES') AS total_type_ergonomic_breaches,
        COUNT(*) FILTER (WHERE obs.type = 'HAZARDS') AS total_type_hazards,
        COUNT(*) FILTER (WHERE obs.type = 'INCOMPLETE_WORKS') AS total_type_incomplete_works,
        COUNT(*) FILTER (WHERE obs.type = 'QUALITY') AS total_type_quality,
        COUNT(*) FILTER (WHERE obs.type = 'SAFETY') AS total_type_safety,

        -- Count per status
        COUNT(*) FILTER (WHERE obs.status = 'OPEN') AS total_status_open,
        COUNT(*) FILTER (WHERE obs.status = 'PENDING') AS status_pending,
        COUNT(*) FILTER (WHERE obs.status = 'IN_PROGRESS') AS status_in_progress,
        COUNT(*) FILTER (WHERE obs.status = 'FIXED') AS status_fixed,
        COUNT(*) FILTER (WHERE obs.status = 'CLOSED') AS status_approved_and_closed
    FROM 
        observations AS obs
    JOIN 
        user_schema.organizations AS o 
        ON obs.organization_id = o.id
    LEFT JOIN 
        project_schema.projects AS p 
        ON obs.project_id = p.id
    GROUP BY 
        o.name, p.id, p.uuid
    ORDER BY 
        total_observations DESC
    LIMIT 1000
) AS virtual_table 
GROUP BY organization_name, project_id, project_uuid
ORDER BY "Total Observations" DESC 
LIMIT 1000;

Dataset "DATASET_SELECT * FROM quality_observations" (id 42): SQL with organization_name → name AS organization_name

SELECT 
    qo.*, 
    pr.uuid AS project_uuid,
    pr.title AS project_name, 
    org.name AS organization_name,
    CASE 
        WHEN qo.due_date < CURRENT_TIMESTAMP 
        THEN CURRENT_TIMESTAMP - qo.due_date
        ELSE NULL
    END AS overdue_duration,
    CASE 
        WHEN qo.due_date < CURRENT_TIMESTAMP 
        THEN 'Overdue'
        ELSE 'On Time'
    END AS is_overdue
FROM 
    public.quality_observations qo
JOIN 
    project_schema.projects pr 
        ON qo.project_id = pr.id
JOIN 
    user_schema.organizations org 
        ON qo.organization_id = org.id

Dataset "DATASET_SELECT * FROM smp" (id 58): SQL with organization_name → name AS organization_name

SELECT
  p.id AS project_id,
  p.title AS project_title,
  o.name AS organization_name,
  st.name AS smp_type_name,
  s.*
FROM 
    user_schema.organizations o
INNER JOIN 
    public.smp s ON o.uuid = s.organisation_uuid
LEFT JOIN 
    public.smp_types st ON st.uuid = s.type_uuid
LEFT JOIN 
    smp_personnel_signatures sps ON s.id = sps.smp_id
LEFT JOIN 
    project_schema.projects p ON p.uuid = s.project_uuid  -- ✅ match project_uuid, not organization_id
GROUP BY
  s.id, o.name, p.id, p.title, st.name

Dataset "DATASET_SELECT * FROM injuries" (id 95): SQL with organization_name → name AS organization_name

SELECT
  i.id,
  i.project_uuid,
  i.injury_id,
  i.description,
  i.injury_body,
  i.injury_contributor,
  -- waktu lokal Australia/Sydney (timestamp tanpa time zone)
  i.date_time AT TIME ZONE 'Australia/Sydney' AS date_time,
  p.id   AS project_id,
  p.title AS project_title,
  o.name AS organization_name
FROM public.injuries AS i
LEFT JOIN project_schema.projects   AS p ON i.project_uuid     = p.uuid
LEFT JOIN user_schema.organizations AS o ON i.organisation_uuid = o.uuid;

Dataset "DATASET_Current Week Injuries" (id 97): SQL with organization_name → name AS organization_name

-- Minggu ini + minggu lalu dihitung berbasis Australia/Sydney (awalnya Senin 00:00)
WITH bounds AS (
  SELECT
    date_trunc('week', (now() AT TIME ZONE 'Australia/Sydney'))                     AS start_this_week_local,
    date_trunc('week', (now() AT TIME ZONE 'Australia/Sydney') + interval '1 week') AS start_next_week_local
)
SELECT
  i.*,
  p.id    AS project_id,
  p.title AS project_title,
  o.name AS organization_name
FROM public.injuries AS i
LEFT JOIN project_schema.projects   AS p ON i.project_uuid     = p.uuid
LEFT JOIN user_schema.organizations AS o ON i.organisation_uuid = o.uuid
WHERE (i.date_time AT TIME ZONE 'Australia/Sydney') >= (SELECT start_this_week_local - interval '1 week' FROM bounds)
  AND (i.date_time AT TIME ZONE 'Australia/Sydney') <  (SELECT start_next_week_local                    FROM bounds);

Dataset "DATASET_Injuries (Weekly Stats) Bar Charts" (id 98): SQL with organization_name → name AS organization_name

SELECT
  i.id,
  i.project_uuid,
  i.date_time AT TIME ZONE 'Australia/Sydney' AS date_time,
  p.id   AS project_id,
  p.title AS project_title,
  o.name AS organization_name
FROM public.injuries AS i
LEFT JOIN project_schema.projects   AS p ON i.project_uuid     = p.uuid
LEFT JOIN user_schema.organizations AS o ON i.organisation_uuid = o.uuid;

Dataset "DATASET_SELECT * FROM equipments" (id 45): SQL with organization_name → name AS organization_name

SELECT e.*,
       p.id AS project_id,
       p.title AS project_title,
       o.id AS organisation_id,
       o.name AS organization_name,
       
-- Overdue flag & interval (treat date-only as 23:59:59)
 CASE
     WHEN (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') < NOW() THEN TRUE
     ELSE FALSE
 END AS overdue,
 CASE
     WHEN (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') < NOW() 
     THEN (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))
     ELSE NULL
 END AS overdue_duration,
 CASE
     WHEN (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') < NOW() 
     THEN 
         EXTRACT(DAY FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) || 
         CASE WHEN EXTRACT(DAY FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) = 1 THEN ' day, ' ELSE ' days, ' END ||
         EXTRACT(HOUR FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) || 
         CASE WHEN EXTRACT(HOUR FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) = 1 THEN ' hour, ' ELSE ' hours, ' END ||
         EXTRACT(MINUTE FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) || 
         CASE WHEN EXTRACT(MINUTE FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds'))) = 1 THEN ' minute, and ' ELSE ' minutes, and ' END ||
         FLOOR(EXTRACT(SECOND FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds')))) || 
         CASE WHEN FLOOR(EXTRACT(SECOND FROM (NOW() - (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds')))) = 1 THEN ' second' ELSE ' seconds' END
     ELSE NULL
 END AS overdue_duration_readable,
 
 -- Due today (using 23:59:59 as end of day)
 CASE
     WHEN e.date_next_service::date = CURRENT_DATE THEN TRUE
     ELSE FALSE
 END AS due_today,
 CASE
     WHEN e.date_next_service::date = CURRENT_DATE 
     THEN ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW())
     ELSE NULL
 END AS due_today_remaining,
  CASE
     WHEN e.date_next_service::date = CURRENT_DATE 
     THEN 
         EXTRACT(HOUR FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW())) || 
         CASE WHEN EXTRACT(HOUR FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW())) = 1 THEN ' hour, ' ELSE ' hours, ' END ||
         EXTRACT(MINUTE FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW())) || 
         CASE WHEN EXTRACT(MINUTE FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW())) = 1 THEN ' minute, and ' ELSE ' minutes, and ' END ||
         FLOOR(EXTRACT(SECOND FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW()))) || 
         CASE WHEN FLOOR(EXTRACT(SECOND FROM ((e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') - NOW()))) = 1 THEN ' second' ELSE ' seconds' END
     ELSE NULL
 END AS due_today_remaining_readable,
 
 -- Due in 24 hours (only flag, no remaining time needed)
 CASE
     WHEN e.date_next_service > NOW()
          AND e.date_next_service <= NOW() + interval '1 day' THEN TRUE
     ELSE FALSE
 END AS due_in_24_hours,
 
 -- Due in next week
 CASE
     WHEN e.date_next_service > NOW() + interval '1 day'
          AND e.date_next_service <= NOW() + interval '1 week' THEN TRUE
     ELSE FALSE
 END AS due_in_next_week,
 CASE
     WHEN e.date_next_service > NOW() + interval '1 day'
          AND e.date_next_service <= NOW() + interval '1 week' THEN (e.date_next_service - NOW())
     ELSE NULL
 END AS due_in_next_week_remaining,
 CASE
     WHEN e.date_next_service > NOW() + interval '1 day'
          AND e.date_next_service <= NOW() + interval '1 week' 
     THEN 
         EXTRACT(DAY FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(DAY FROM (e.date_next_service - NOW())) = 1 THEN ' day, ' ELSE ' days, ' END ||
         EXTRACT(HOUR FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(HOUR FROM (e.date_next_service - NOW())) = 1 THEN ' hour, ' ELSE ' hours, ' END ||
         EXTRACT(MINUTE FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(MINUTE FROM (e.date_next_service - NOW())) = 1 THEN ' minute, and ' ELSE ' minutes, and ' END ||
         FLOOR(EXTRACT(SECOND FROM (e.date_next_service - NOW()))) || 
         CASE WHEN FLOOR(EXTRACT(SECOND FROM (e.date_next_service - NOW()))) = 1 THEN ' second' ELSE ' seconds' END
     ELSE NULL
 END AS due_in_next_week_remaining_readable,
 
 -- Due in next month
 CASE
     WHEN e.date_next_service > NOW() + interval '1 week'
          AND e.date_next_service <= NOW() + interval '1 month' THEN TRUE
     ELSE FALSE
 END AS due_in_next_month,
 CASE
     WHEN e.date_next_service > NOW() + interval '1 week'
          AND e.date_next_service <= NOW() + interval '1 month' THEN (e.date_next_service - NOW())
     ELSE NULL
 END AS due_in_next_month_remaining,
  CASE
     WHEN e.date_next_service > NOW() + interval '1 week'
          AND e.date_next_service <= NOW() + interval '1 month' 
     THEN 
         EXTRACT(DAY FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(DAY FROM (e.date_next_service - NOW())) = 1 THEN ' day, ' ELSE ' days, ' END ||
         EXTRACT(HOUR FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(HOUR FROM (e.date_next_service - NOW())) = 1 THEN ' hour, ' ELSE ' hours, ' END ||
         EXTRACT(MINUTE FROM (e.date_next_service - NOW())) || 
         CASE WHEN EXTRACT(MINUTE FROM (e.date_next_service - NOW())) = 1 THEN ' minute, and ' ELSE ' minutes, and ' END ||
         FLOOR(EXTRACT(SECOND FROM (e.date_next_service - NOW()))) || 
         CASE WHEN FLOOR(EXTRACT(SECOND FROM (e.date_next_service - NOW()))) = 1 THEN ' second' ELSE ' seconds' END
     ELSE NULL
 END AS due_in_next_month_remaining_readable,
 
 -- status summary
 CASE
 	 WHEN (e.date_next_service::date + interval '23 hours 59 minutes 59 seconds') < NOW() THEN 'Overdue'
     WHEN e.date_next_service::date = CURRENT_DATE THEN 'Due Today'
     WHEN e.date_next_service > NOW()
          AND e.date_next_service <= NOW() + interval '1 day' THEN 'Due in 24 Hours'
     WHEN e.date_next_service > NOW() + interval '1 day'
          AND e.date_next_service <= NOW() + interval '1 week' THEN 'Due in Next Week'
     WHEN e.date_next_service > NOW() + interval '1 week'
          AND e.date_next_service <= NOW() + interval '1 month' THEN 'Due in Next Month'
     ELSE 'Due After Next Month'
 END AS service_status
FROM equipments e
LEFT JOIN project_schema.projects p ON e.project_uuid::UUID = p.uuid
LEFT JOIN user_schema.organizations o ON e.organisation_uuid::UUID = o.uuid
ORDER BY e.date_next_service ASC;

Re-import project_services.user_schema.organizations

DROP FOREIGN TABLE user_schema.organizations;  -- add CASCADE only together with the view/MV drafts below
IMPORT FOREIGN SCHEMA public LIMIT TO (organizations)
  FROM SERVER user_srv INTO user_schema;

Materialized view project_services.reporting.mv_weekly_payment_run: organization_name → name AS organization_name

DROP MATERIALIZED VIEW reporting.mv_weekly_payment_run;
CREATE MATERIALIZED VIEW reporting.mv_weekly_payment_run AS
 SELECT prj.id AS project_id,
    prj.uuid AS project_uuid,
    prj.title AS "Project",
    b.invoice_date AS "Invoice Date",
    b.invoice_claim_no AS "Invoice Number",
    b.invoice_uuid AS "Invoice UUID",
    COALESCE(sub_org.name, b.supplier_name) AS "Vendor",
    b.invoice_type AS "Invoice Type",
    b.payment_amount_excl_gst AS "Payment Amount (excl. GST)",
    b.po_number AS "PO / Contract Number",
    b.po_contract_description AS "PO / Contract Description",
    b.po_cost_committed AS "PO / Contract Value",
    b.po_cost_committed - b.po_amount_approved AS "Contract/PO Balance",
        CASE
            WHEN b.po_amount_approved <> 0::numeric THEN b.po_cost_committed / b.po_amount_approved * 100::numeric
            ELSE NULL::numeric
        END AS "% of Completion",
    concat_ws(' '::text, NULLIF(au.first_name::text, ''::text), NULLIF(au.last_name::text, ''::text)) AS "End Approver"
   FROM finance_schema.mv_payment_run_base b
     LEFT JOIN projects prj ON prj.id = b.project_id
     LEFT JOIN user_schema.users sub_user ON sub_user.id = b.subcontract_user_id
     LEFT JOIN user_schema.organizations sub_org ON sub_org.id = sub_user.organization_id
     LEFT JOIN reporting.mv_invoice_end_approver wea ON wea.invoice_uuid = b.invoice_uuid::text
     LEFT JOIN user_schema.users au ON au.uuid::text = wea.approved_by_uuid;;
-- then recreate its indexes and grants (see the production definition)

Materialized view project_services.reporting.mv_invitee_orgs: organization_name → name AS organization_name

DROP MATERIALIZED VIEW reporting.mv_invitee_orgs;
CREATE MATERIALIZED VIEW reporting.mv_invitee_orgs AS
 SELECT p.id AS project_id,
    p.uuid AS project_uuid,
    p.title AS project_title,
    p.organization_id AS inviter_org_id,
    o_owner.name AS inviter_org_name,
    ol.organization_id AS invitee_org_id,
    o_inv.name AS invitee_org_name
   FROM projects p
     JOIN user_schema.organizations o_owner ON o_owner.id = p.organization_id
     JOIN ( SELECT DISTINCT "organization-link".project_id,
            "organization-link".organization_id
           FROM user_schema."organization-link") ol ON ol.project_id = p.id
     JOIN user_schema.organizations o_inv ON o_inv.id = ol.organization_id
  WHERE ol.organization_id <> p.organization_id;;
-- then recreate its indexes and grants (see the production definition)

Materialized view project_services.reporting.mv_ar_monthly: organization_name → name AS organization_name

DROP MATERIALIZED VIEW reporting.mv_ar_monthly;
CREATE MATERIALIZED VIEW reporting.mv_ar_monthly AS
 WITH approved_claims_monthly AS (
         SELECT h.project_uuid::uuid AS project_uuid,
            date_trunc('month'::text, (h.meta ->> 'approved_rejected_date'::text)::timestamp with time zone) AS month_start,
            sum(COALESCE(NULLIF(h.meta ->> 'approved_total_invoice_variation'::text, ''::text)::numeric, 0::numeric)) AS total_accounts_receivable
           FROM finance_schema.head_contract_claim_histories h
          WHERE h.type::text = 'CLAIM'::text AND h.is_variation = false AND (h.meta ->> 'approval_status'::text) = 'APPROVED'::text AND (h.meta ->> 'approved_rejected_date'::text) IS NOT NULL
          GROUP BY h.project_uuid, (date_trunc('month'::text, (h.meta ->> 'approved_rejected_date'::text)::timestamp with time zone))
        )
 SELECT p.id AS project_id,
    p.uuid AS project_uuid,
    p.title,
    o.id AS organization_id,
    o.name AS organization_name,
    acm.month_start::date AS month_start_date,
    acm.total_accounts_receivable
   FROM approved_claims_monthly acm
     JOIN projects p ON p.uuid = acm.project_uuid
     JOIN user_schema.organizations o ON o.id = p.organization_id;;
-- then recreate its indexes and grants (see the production definition)

Dataset "DATASET_SYD List of Subcontractors" (id 197): SQL with organization_name → name AS organization_name

SELECT
        p.id                      AS project_id,
        p.uuid                    AS project_uuid,
        p.title                   AS project_title,
        p.organization_id         AS inviter_org_id,
        o_owner.name AS inviter_org_name,
        ol.organization_id        AS invitee_org_id,
        o_inv.name   AS invitee_org_name
    FROM public.projects p
    JOIN user_schema.organizations o_owner
      ON o_owner.id = p.organization_id
    JOIN (
        SELECT DISTINCT
            l.project_id,
            l.organization_id
        FROM user_schema."organization-link" l
        WHERE l.deleted_at IS NULL          -- link removed from the project
          AND l.is_archive IS NOT TRUE      -- contractor archived (NULL counts as active)
    ) ol
      ON ol.project_id = p.id
    JOIN user_schema.organizations o_inv
      ON o_inv.id = ol.organization_id
    WHERE ol.organization_id <> p.organization_id   -- exclude the project owner

Dataset "DATASET_Vendor Ratings" (id 164): SQL with organization_name → name AS organization_name

SELECT
    pvrr.*,
    pvrq.question,
    p.uuid AS project_uuid,
    p.title AS project_title,
    o.name AS organization_name,

    CONCAT_WS(' ', u_created.first_name, u_created.last_name) AS created_by_name,
    CONCAT_WS(' ', u_updated.first_name, u_updated.last_name) AS updated_by_name,

    pvrr.created_at AT TIME ZONE 'Australia/Sydney' AS created_at_sydney,
    pvrr.updated_at AT TIME ZONE 'Australia/Sydney' AS updated_at_sydney,

    c.comment,
    c.created_at AT TIME ZONE 'Australia/Sydney' AS comment_created_at_sydney,
    CONCAT_WS(' ', u_comment.first_name, u_comment.last_name) AS comment_created_by_name

FROM procurement_vendor_rating_records pvrr
LEFT JOIN procurement_vendor_rating_questions pvrq
    ON pvrr.question_id = pvrq.id
LEFT JOIN projects p
    ON pvrr.project_id = p.id
LEFT JOIN user_schema.organizations o
    ON pvrr.organization_uuid = o.uuid
LEFT JOIN user_schema.users u_created
    ON pvrr.created_by = u_created.id
LEFT JOIN user_schema.users u_updated
    ON pvrr.updated_by = u_updated.id
LEFT JOIN comment_schema.comments c
    ON pvrr.id = c.module_id
   AND c.module = 'VENDOR_RATING'
LEFT JOIN user_schema.users u_comment
    ON c.created_by = u_comment.id;

Drafts to discuss. The Report Impact Checker AI Tool never runs them.

How it reaches them

9 distinct paths from user_services.public.organizations.organization_name lead to the 33 Superset datasets that read it. Open a path to see its datasets and the dashboards on it.

Path 1: site_management_services.user_schema.organizations, 15 dashboards (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tablesite_management_services.user_schema.organizations
  3. DatasetsDATASET_Attendance Per Org Per Week, DATASET_Attendance Per Org Per Month, DATASET_Attendance Per Org Per Day and 19 more
  4. Dashboards
Path 2: project_services.user_schema.organizations → project_services.reporting.mv_ar_monthly, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tableproject_services.user_schema.organizations
  3. Materialized viewproject_services.reporting.mv_ar_monthly
  4. DatasetsDATASET_Total Accounts Receivable, DATASET_Budget Utilisation, DATASET_Net Working Capital vs Gross Working Capital
  5. Dashboards
Path 3: site_management_services.user_schema.organizations → site_management_services.public.mv_prestart, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tablesite_management_services.user_schema.organizations
  3. Materialized viewsite_management_services.public.mv_prestart
  4. DatasetDATASET_Prestart
  5. Dashboards
Path 4: site_management_services.user_schema.organizations → site_management_services.public.mv_obs_detail, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tablesite_management_services.user_schema.organizations
  3. Materialized viewsite_management_services.public.mv_obs_detail
  4. DatasetDATASET_Observations - Detailed View
  5. Dashboards
Path 5: site_management_services.user_schema.organizations → site_management_services.public.mv_blacklist_warnings, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tablesite_management_services.user_schema.organizations
  3. Materialized viewsite_management_services.public.mv_blacklist_warnings
  4. DatasetDATASET_Blacklist & Warnings
  5. Dashboards
Path 6: project_services.user_schema.organizations → project_services.reporting.mv_invitee_orgs, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tableproject_services.user_schema.organizations
  3. Materialized viewproject_services.reporting.mv_invitee_orgs
  4. DatasetsDATASET_Invitee ORGs, DATASET_SYD List of Subcontractors
  5. Dashboards
Path 7: site_management_services.user_schema.organizations → site_management_services.public.mv_defect_by_type, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tablesite_management_services.user_schema.organizations
  3. Materialized viewsite_management_services.public.mv_defect_by_type
  4. DatasetDATASET_MV Defect By Type
  5. Dashboards
Path 8: project_services.user_schema.organizations → project_services.reporting.mv_weekly_payment_run, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tableproject_services.user_schema.organizations
  3. Materialized viewproject_services.reporting.mv_weekly_payment_run
  4. DatasetDATASET_Weekly Payment Run
  5. Dashboards
Path 9: project_services.user_schema.organizations, 1 dashboard (Broken)
  1. Columnuser_services.public.organizations.organization_name
  2. Foreign tableproject_services.user_schema.organizations
  3. DatasetDATASET_Vendor Ratings
  4. Dashboards

Every report

Safety21 dashboards, 12 Broken

Site and workforce11 dashboards, 6 Broken

Finance and cost10 dashboards, 2 Broken

Projects and procurement6 dashboards, 2 Broken

Quality and defects2 dashboards, 1 Broken

Examples