From 2303a7bb8b476b85e7cb65abcceb17c989ff02e8 Mon Sep 17 00:00:00 2001 From: akash1810 Date: Tue, 18 Jun 2024 10:04:55 +0100 Subject: [PATCH] fix(prisma): Updates after introspection Update Prisma's `views` directory, following #1066, via an introspection. See https://www.prisma.io/docs/orm/prisma-schema/introspection. --- .../prisma/views/public/aws_accounts.sql | 25 +- .../views/public/view_aws_resources.sql | 320 ------------------ .../views/public/view_github_actions.sql | 37 +- .../views/public/view_old_ec2_instances.sql | 49 ++- .../views/public/view_repo_ownership.sql | 38 ++- .../views/public/view_running_instances.sql | 77 ++--- 6 files changed, 89 insertions(+), 457 deletions(-) delete mode 100644 packages/common/prisma/views/public/view_aws_resources.sql diff --git a/packages/common/prisma/views/public/aws_accounts.sql b/packages/common/prisma/views/public/aws_accounts.sql index 1346dcee4..6f2c0f422 100644 --- a/packages/common/prisma/views/public/aws_accounts.sql +++ b/packages/common/prisma/views/public/aws_accounts.sql @@ -1,15 +1,16 @@ SELECT - DISTINCT acc.id, - acc.name, - acc.email, - acc.status, - acc.joined_timestamp, - COALESCE(ou.name, 'ROOT' :: text) AS organizational_unit + fn_aws_accounts.id, + fn_aws_accounts.name, + fn_aws_accounts.email, + fn_aws_accounts.status, + fn_aws_accounts.joined_timestamp, + fn_aws_accounts.organizational_unit FROM - ( - ( - aws_organizations_accounts acc - LEFT JOIN aws_organizations_account_parents par ON ((acc.id = par.id)) - ) - LEFT JOIN aws_organizations_organizational_units ou ON ((par.parent_id = ou.id)) + fn_aws_accounts() fn_aws_accounts( + id, + name, + email, + STATUS, + joined_timestamp, + organizational_unit ); \ No newline at end of file diff --git a/packages/common/prisma/views/public/view_aws_resources.sql b/packages/common/prisma/views/public/view_aws_resources.sql deleted file mode 100644 index 04cf4971c..000000000 --- a/packages/common/prisma/views/public/view_aws_resources.sql +++ /dev/null @@ -1,320 +0,0 @@ -SELECT - aws_ec2_instances._cq_id, - aws_ec2_instances._cq_source_name, - aws_ec2_instances._cq_sync_time, - 'aws_ec2_instances' :: text AS _cq_table, - COALESCE( - aws_ec2_instances.account_id, - split_part(aws_ec2_instances.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - NULL :: text, - aws_ec2_instances.account_id, - split_part(aws_ec2_instances.arn, ':' :: text, 5) - ) AS request_account_id, - aws_ec2_instances.region, - split_part(aws_ec2_instances.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_ec2_instances.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_ec2_instances.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_ec2_instances.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_ec2_instances.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_ec2_instances.arn, - aws_ec2_instances.tags -FROM - aws_ec2_instances -UNION -ALL -SELECT - aws_ec2_images._cq_id, - aws_ec2_images._cq_source_name, - aws_ec2_images._cq_sync_time, - 'aws_ec2_images' :: text AS _cq_table, - COALESCE( - aws_ec2_images.account_id, - split_part(aws_ec2_images.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - NULL :: text, - aws_ec2_images.account_id, - split_part(aws_ec2_images.arn, ':' :: text, 5) - ) AS request_account_id, - aws_ec2_images.region, - split_part(aws_ec2_images.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_ec2_images.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_ec2_images.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_ec2_images.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_ec2_images.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_ec2_images.arn, - aws_ec2_images.tags -FROM - aws_ec2_images -UNION -ALL -SELECT - aws_cloudformation_stacks._cq_id, - aws_cloudformation_stacks._cq_source_name, - aws_cloudformation_stacks._cq_sync_time, - 'aws_cloudformation_stacks' :: text AS _cq_table, - COALESCE( - aws_cloudformation_stacks.account_id, - split_part(aws_cloudformation_stacks.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - NULL :: text, - aws_cloudformation_stacks.account_id, - split_part(aws_cloudformation_stacks.arn, ':' :: text, 5) - ) AS request_account_id, - aws_cloudformation_stacks.region, - split_part(aws_cloudformation_stacks.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_cloudformation_stacks.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_cloudformation_stacks.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_cloudformation_stacks.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_cloudformation_stacks.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_cloudformation_stacks.arn, - aws_cloudformation_stacks.tags -FROM - aws_cloudformation_stacks -UNION -ALL -SELECT - aws_organizations_accounts._cq_id, - aws_organizations_accounts._cq_source_name, - aws_organizations_accounts._cq_sync_time, - 'aws_organizations_accounts' :: text AS _cq_table, - COALESCE( - NULL :: text, - split_part(aws_organizations_accounts.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - aws_organizations_accounts.request_account_id, - NULL :: text, - split_part(aws_organizations_accounts.arn, ':' :: text, 5) - ) AS request_account_id, - 'unavailable' :: text AS region, - split_part(aws_organizations_accounts.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_organizations_accounts.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_organizations_accounts.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_organizations_accounts.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_organizations_accounts.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_organizations_accounts.arn, - aws_organizations_accounts.tags -FROM - aws_organizations_accounts -UNION -ALL -SELECT - aws_organizations_organizational_units._cq_id, - aws_organizations_organizational_units._cq_source_name, - aws_organizations_organizational_units._cq_sync_time, - 'aws_organizations_organizational_units' :: text AS _cq_table, - COALESCE( - NULL :: text, - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 5 - ) - ) AS account_id, - COALESCE( - aws_organizations_organizational_units.request_account_id, - NULL :: text, - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 5 - ) - ) AS request_account_id, - 'unavailable' :: text AS region, - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 2 - ) AS PARTITION, - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 3 - ) AS service, - CASE - WHEN ( - ( - split_part( - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 6 - ), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 7 - ) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part( - aws_organizations_organizational_units.arn, - ':' :: text, - 6 - ), - '/' :: text, - 1 - ) - END AS TYPE, - aws_organizations_organizational_units.arn, - '{}' :: jsonb AS tags -FROM - aws_organizations_organizational_units -UNION -ALL -SELECT - aws_lambda_functions._cq_id, - aws_lambda_functions._cq_source_name, - aws_lambda_functions._cq_sync_time, - 'aws_lambda_functions' :: text AS _cq_table, - COALESCE( - aws_lambda_functions.account_id, - split_part(aws_lambda_functions.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - NULL :: text, - aws_lambda_functions.account_id, - split_part(aws_lambda_functions.arn, ':' :: text, 5) - ) AS request_account_id, - aws_lambda_functions.region, - split_part(aws_lambda_functions.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_lambda_functions.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_lambda_functions.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_lambda_functions.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_lambda_functions.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_lambda_functions.arn, - aws_lambda_functions.tags -FROM - aws_lambda_functions -UNION -ALL -SELECT - aws_s3_buckets._cq_id, - aws_s3_buckets._cq_source_name, - aws_s3_buckets._cq_sync_time, - 'aws_s3_buckets' :: text AS _cq_table, - COALESCE( - aws_s3_buckets.account_id, - split_part(aws_s3_buckets.arn, ':' :: text, 5) - ) AS account_id, - COALESCE( - NULL :: text, - aws_s3_buckets.account_id, - split_part(aws_s3_buckets.arn, ':' :: text, 5) - ) AS request_account_id, - aws_s3_buckets.region, - split_part(aws_s3_buckets.arn, ':' :: text, 2) AS PARTITION, - split_part(aws_s3_buckets.arn, ':' :: text, 3) AS service, - CASE - WHEN ( - ( - split_part( - split_part(aws_s3_buckets.arn, ':' :: text, 6), - '/' :: text, - 2 - ) = '' :: text - ) - AND ( - split_part(aws_s3_buckets.arn, ':' :: text, 7) = '' :: text - ) - ) THEN NULL :: text - ELSE split_part( - split_part(aws_s3_buckets.arn, ':' :: text, 6), - '/' :: text, - 1 - ) - END AS TYPE, - aws_s3_buckets.arn, - aws_s3_buckets.tags -FROM - aws_s3_buckets; \ No newline at end of file diff --git a/packages/common/prisma/views/public/view_github_actions.sql b/packages/common/prisma/views/public/view_github_actions.sql index 3b31186c2..a4a2d2ea6 100644 --- a/packages/common/prisma/views/public/view_github_actions.sql +++ b/packages/common/prisma/views/public/view_github_actions.sql @@ -1,25 +1,18 @@ -WITH data AS ( - SELECT - tbl.evaluated_on, - tbl.full_name, - tbl.workflow_path, - use_string.use_string AS ACTION, - split_part(use_string.use_string, '@' :: text, 1) AS action_name, - split_part(use_string.use_string, '@' :: text, '-1' :: integer) AS version - FROM - guardian_github_actions_usage tbl, - LATERAL unnest(tbl.workflow_uses) use_string(use_string) -) SELECT - d.evaluated_on, - d.full_name, - r.archived, - d.workflow_path, - d.action, - d.action_name, - d.version + fn_github_actions.evaluated_on, + fn_github_actions.full_name, + fn_github_actions.archived, + fn_github_actions.workflow_path, + fn_github_actions.action, + fn_github_actions.action_name, + fn_github_actions.version FROM - ( - data d - JOIN github_repositories r ON ((d.full_name = r.full_name)) + fn_github_actions() fn_github_actions( + evaluated_on, + full_name, + archived, + workflow_path, + ACTION, + action_name, + version ); \ No newline at end of file diff --git a/packages/common/prisma/views/public/view_old_ec2_instances.sql b/packages/common/prisma/views/public/view_old_ec2_instances.sql index 691d46b40..58e5a1c13 100644 --- a/packages/common/prisma/views/public/view_old_ec2_instances.sql +++ b/packages/common/prisma/views/public/view_old_ec2_instances.sql @@ -1,31 +1,24 @@ SELECT - ac.id AS account_id, - ac.name AS account_name, - ec2.instance_id, - (ec2.state ->> 'Name' :: text) AS state, - (ec2.tags ->> 'Stack' :: text) AS stack, - (ec2.tags ->> 'Stage' :: text) AS stage, - (ec2.tags ->> 'App' :: text) AS app, - (ec2.tags ->> 'gu:repo' :: text) AS repo, - ec2.region, - coalesce_dates(img.creation_date, ec2.launch_time) AS creation_or_launch_time + fn_old_ec2_instances.account_id, + fn_old_ec2_instances.account_name, + fn_old_ec2_instances.instance_id, + fn_old_ec2_instances.state, + fn_old_ec2_instances.stack, + fn_old_ec2_instances.stage, + fn_old_ec2_instances.app, + fn_old_ec2_instances.repo, + fn_old_ec2_instances.region, + fn_old_ec2_instances.creation_or_launch_time FROM - ( - ( - aws_ec2_instances ec2 - LEFT JOIN aws_ec2_images img ON ((ec2.image_id = img.image_id)) - ) - LEFT JOIN aws_accounts ac ON ((ec2.account_id = ac.id)) - ) -WHERE - ( - ( - ( - coalesce_dates(img.creation_date, ec2.launch_time) IS NULL - ) - OR ( - coalesce_dates(img.creation_date, ec2.launch_time) < (NOW() - '30 days' :: INTERVAL) - ) - ) - AND ((ec2.state ->> 'Name' :: text) = 'running' :: text) + fn_old_ec2_instances() fn_old_ec2_instances( + account_id, + account_name, + instance_id, + state, + stack, + stage, + app, + repo, + region, + creation_or_launch_time ); \ No newline at end of file diff --git a/packages/common/prisma/views/public/view_repo_ownership.sql b/packages/common/prisma/views/public/view_repo_ownership.sql index a1c3cd7ec..b7ecea3b8 100644 --- a/packages/common/prisma/views/public/view_repo_ownership.sql +++ b/packages/common/prisma/views/public/view_repo_ownership.sql @@ -1,20 +1,22 @@ SELECT - ght.id AS github_team_id, - ght.name AS github_team_name, - ght.slug AS github_team_slug, - tr.name AS short_repo_name, - tr.full_name AS full_repo_name, - tr.role_name, - tr.archived, - gtt.team_name AS galaxies_team, - gtt.team_contact_email + fn_repo_ownership.github_team_id, + fn_repo_ownership.github_team_name, + fn_repo_ownership.github_team_slug, + fn_repo_ownership.short_repo_name, + fn_repo_ownership.full_repo_name, + fn_repo_ownership.role_name, + fn_repo_ownership.archived, + fn_repo_ownership.galaxies_team, + fn_repo_ownership.team_contact_email FROM - ( - ( - github_team_repositories tr - JOIN github_teams ght ON ((tr.team_id = ght.id)) - ) - LEFT JOIN galaxies_teams_table gtt ON ((ght.slug = gtt.team_primary_github_team)) - ) -WHERE - (tr.role_name = 'admin' :: text); \ No newline at end of file + fn_repo_ownership() fn_repo_ownership( + github_team_id, + github_team_name, + github_team_slug, + short_repo_name, + full_repo_name, + role_name, + archived, + galaxies_team, + team_contact_email + ); \ No newline at end of file diff --git a/packages/common/prisma/views/public/view_running_instances.sql b/packages/common/prisma/views/public/view_running_instances.sql index 41f8fd36b..131368f0f 100644 --- a/packages/common/prisma/views/public/view_running_instances.sql +++ b/packages/common/prisma/views/public/view_running_instances.sql @@ -1,59 +1,22 @@ -WITH id_and_tags AS ( - SELECT - aws_ec2_images.image_id, - (aws_ec2_images.tags ->> 'BuiltBy' :: text) AS built_by - FROM - aws_ec2_images - WHERE - (aws_ec2_images.tags IS NOT NULL) - ORDER BY - aws_ec2_images.image_id -), -aggregated_images AS ( - SELECT - id_and_tags.image_id, - CASE - WHEN ( - 'amigo' :: text = ANY (array_agg(id_and_tags.built_by)) - ) THEN TRUE - ELSE false - END AS built_by_amigo - FROM - id_and_tags - GROUP BY - id_and_tags.image_id, - id_and_tags.built_by - ORDER BY - id_and_tags.image_id -) SELECT - DISTINCT ON (instances.instance_id) accts.name AS account_name, - (instances.tags ->> 'App' :: text) AS app, - (instances.tags ->> 'Stack' :: text) AS stack, - (instances.tags ->> 'Stage' :: text) AS stage, - instances.image_id, - instances.instance_id, - CASE - WHEN images.built_by_amigo THEN TRUE - ELSE false - END AS built_by_amigo, - instances.launch_time, - instances.instance_type AS TYPE + fn_running_ec2_instances.account_name, + fn_running_ec2_instances.app, + fn_running_ec2_instances.stack, + fn_running_ec2_instances.stage, + fn_running_ec2_instances.image_id, + fn_running_ec2_instances.instance_id, + fn_running_ec2_instances.built_by_amigo, + fn_running_ec2_instances.launch_time, + fn_running_ec2_instances.type FROM - ( - ( - aws_ec2_instances instances - LEFT JOIN aggregated_images images ON ((instances.image_id = images.image_id)) - ) - LEFT JOIN aws_organizations_accounts accts ON ((instances.account_id = accts.id)) - ) -WHERE - ( - (instances.state ->> 'Name' :: text) = 'running' :: text - ) -ORDER BY - instances.instance_id, - CASE - WHEN images.built_by_amigo THEN TRUE - ELSE false - END DESC; \ No newline at end of file + fn_running_ec2_instances() fn_running_ec2_instances( + account_name, + app, + stack, + stage, + image_id, + instance_id, + built_by_amigo, + launch_time, + TYPE + ); \ No newline at end of file