# frozen_string_literal: true

class CreateProjectRefreshingMv < ClickHouse::Migration
  def up
    execute <<~SQL
      CREATE MATERIALIZED VIEW IF NOT EXISTS namespace_traversal_path_refresh_to_projects_mv TO siphon_projects AS
      WITH
        base AS (
          SELECT id FROM siphon_namespaces WHERE type = 'Project'
        ),
        projects AS (
          SELECT
            id,
            argMax(name, _siphon_replicated_at) AS name,
            argMax(path, _siphon_replicated_at) AS path,
            argMax(description, _siphon_replicated_at) AS description,
            argMax(created_at, _siphon_replicated_at) AS created_at,
            argMax(updated_at, _siphon_replicated_at) AS updated_at,
            argMax(creator_id, _siphon_replicated_at) AS creator_id,
            argMax(namespace_id, _siphon_replicated_at) AS namespace_id,
            argMax(last_activity_at, _siphon_replicated_at) AS last_activity_at,
            argMax(import_url, _siphon_replicated_at) AS import_url,
            argMax(visibility_level, _siphon_replicated_at) AS visibility_level,
            argMax(archived, _siphon_replicated_at) AS archived,
            argMax(avatar, _siphon_replicated_at) AS avatar,
            argMax(merge_requests_template, _siphon_replicated_at) AS merge_requests_template,
            argMax(star_count, _siphon_replicated_at) AS star_count,
            argMax(merge_requests_rebase_enabled, _siphon_replicated_at) AS merge_requests_rebase_enabled,
            argMax(import_type, _siphon_replicated_at) AS import_type,
            argMax(import_source, _siphon_replicated_at) AS import_source,
            argMax(approvals_before_merge, _siphon_replicated_at) AS approvals_before_merge,
            argMax(reset_approvals_on_push, _siphon_replicated_at) AS reset_approvals_on_push,
            argMax(merge_requests_ff_only_enabled, _siphon_replicated_at) AS merge_requests_ff_only_enabled,
            argMax(issues_template, _siphon_replicated_at) AS issues_template,
            argMax(mirror, _siphon_replicated_at) AS mirror,
            argMax(mirror_last_update_at, _siphon_replicated_at) AS mirror_last_update_at,
            argMax(mirror_last_successful_update_at, _siphon_replicated_at) AS mirror_last_successful_update_at,
            argMax(mirror_user_id, _siphon_replicated_at) AS mirror_user_id,
            argMax(shared_runners_enabled, _siphon_replicated_at) AS shared_runners_enabled,
            argMax(build_allow_git_fetch, _siphon_replicated_at) AS build_allow_git_fetch,
            argMax(build_timeout, _siphon_replicated_at) AS build_timeout,
            argMax(mirror_trigger_builds, _siphon_replicated_at) AS mirror_trigger_builds,
            argMax(pending_delete, _siphon_replicated_at) AS pending_delete,
            argMax(public_builds, _siphon_replicated_at) AS public_builds,
            argMax(last_repository_check_failed, _siphon_replicated_at) AS last_repository_check_failed,
            argMax(last_repository_check_at, _siphon_replicated_at) AS last_repository_check_at,
            argMax(only_allow_merge_if_pipeline_succeeds, _siphon_replicated_at) AS only_allow_merge_if_pipeline_succeeds,
            argMax(has_external_issue_tracker, _siphon_replicated_at) AS has_external_issue_tracker,
            argMax(repository_storage, _siphon_replicated_at) AS repository_storage,
            argMax(repository_read_only, _siphon_replicated_at) AS repository_read_only,
            argMax(request_access_enabled, _siphon_replicated_at) AS request_access_enabled,
            argMax(has_external_wiki, _siphon_replicated_at) AS has_external_wiki,
            argMax(ci_config_path, _siphon_replicated_at) AS ci_config_path,
            argMax(lfs_enabled, _siphon_replicated_at) AS lfs_enabled,
            argMax(description_html, _siphon_replicated_at) AS description_html,
            argMax(only_allow_merge_if_all_discussions_are_resolved, _siphon_replicated_at) AS only_allow_merge_if_all_discussions_are_resolved,
            argMax(repository_size_limit, _siphon_replicated_at) AS repository_size_limit,
            argMax(printing_merge_request_link_enabled, _siphon_replicated_at) AS printing_merge_request_link_enabled,
            argMax(auto_cancel_pending_pipelines, _siphon_replicated_at) AS auto_cancel_pending_pipelines,
            argMax(service_desk_enabled, _siphon_replicated_at) AS service_desk_enabled,
            argMax(cached_markdown_version, _siphon_replicated_at) AS cached_markdown_version,
            argMax(delete_error, _siphon_replicated_at) AS delete_error,
            argMax(last_repository_updated_at, _siphon_replicated_at) AS last_repository_updated_at,
            argMax(disable_overriding_approvers_per_merge_request, _siphon_replicated_at) AS disable_overriding_approvers_per_merge_request,
            argMax(storage_version, _siphon_replicated_at) AS storage_version,
            argMax(resolve_outdated_diff_discussions, _siphon_replicated_at) AS resolve_outdated_diff_discussions,
            argMax(remote_mirror_available_overridden, _siphon_replicated_at) AS remote_mirror_available_overridden,
            argMax(only_mirror_protected_branches, _siphon_replicated_at) AS only_mirror_protected_branches,
            argMax(pull_mirror_available_overridden, _siphon_replicated_at) AS pull_mirror_available_overridden,
            argMax(jobs_cache_index, _siphon_replicated_at) AS jobs_cache_index,
            argMax(external_authorization_classification_label, _siphon_replicated_at) AS external_authorization_classification_label,
            argMax(mirror_overwrites_diverged_branches, _siphon_replicated_at) AS mirror_overwrites_diverged_branches,
            argMax(pages_https_only, _siphon_replicated_at) AS pages_https_only,
            argMax(packages_enabled, _siphon_replicated_at) AS packages_enabled,
            argMax(merge_requests_author_approval, _siphon_replicated_at) AS merge_requests_author_approval,
            argMax(pool_repository_id, _siphon_replicated_at) AS pool_repository_id,
            argMax(bfg_object_map, _siphon_replicated_at) AS bfg_object_map,
            argMax(detected_repository_languages, _siphon_replicated_at) AS detected_repository_languages,
            argMax(merge_requests_disable_committers_approval, _siphon_replicated_at) AS merge_requests_disable_committers_approval,
            argMax(require_password_to_approve, _siphon_replicated_at) AS require_password_to_approve,
            argMax(emails_disabled, _siphon_replicated_at) AS emails_disabled,
            argMax(max_pages_size, _siphon_replicated_at) AS max_pages_size,
            argMax(max_artifacts_size, _siphon_replicated_at) AS max_artifacts_size,
            argMax(pull_mirror_branch_prefix, _siphon_replicated_at) AS pull_mirror_branch_prefix,
            argMax(remove_source_branch_after_merge, _siphon_replicated_at) AS remove_source_branch_after_merge,
            argMax(marked_for_deletion_at, _siphon_replicated_at) AS marked_for_deletion_at,
            argMax(marked_for_deletion_by_user_id, _siphon_replicated_at) AS marked_for_deletion_by_user_id,
            argMax(autoclose_referenced_issues, _siphon_replicated_at) AS autoclose_referenced_issues,
            argMax(suggestion_commit_message, _siphon_replicated_at) AS suggestion_commit_message,
            argMax(project_namespace_id, _siphon_replicated_at) AS project_namespace_id,
            argMax(hidden, _siphon_replicated_at) AS hidden,
            argMax(organization_id, _siphon_replicated_at) AS organization_id,
            argMax(_siphon_deleted, _siphon_replicated_at) AS deleted,
            now64(6) AS _siphon_replicated_at
            FROM (
              SELECT * FROM siphon_projects
              WHERE project_namespace_id IN (SELECT id FROM base)
            )
          GROUP BY id
          HAVING deleted = false
        )
        SELECT
          projects.id AS id,
          projects.name AS name,
          projects.path AS path,
          projects.description AS description,
          projects.created_at AS created_at,
          projects.updated_at AS updated_at,
          projects.creator_id AS creator_id,
          projects.namespace_id AS namespace_id,
          projects.last_activity_at AS last_activity_at,
          projects.import_url AS import_url,
          projects.visibility_level AS visibility_level,
          projects.archived AS archived,
          projects.avatar AS avatar,
          projects.merge_requests_template AS merge_requests_template,
          projects.star_count AS star_count,
          projects.merge_requests_rebase_enabled AS merge_requests_rebase_enabled,
          projects.import_type AS import_type,
          projects.import_source AS import_source,
          projects.approvals_before_merge AS approvals_before_merge,
          projects.reset_approvals_on_push AS reset_approvals_on_push,
          projects.merge_requests_ff_only_enabled AS merge_requests_ff_only_enabled,
          projects.issues_template AS issues_template,
          projects.mirror AS mirror,
          projects.mirror_last_update_at AS mirror_last_update_at,
          projects.mirror_last_successful_update_at AS mirror_last_successful_update_at,
          projects.mirror_user_id AS mirror_user_id,
          projects.shared_runners_enabled AS shared_runners_enabled,
          projects.build_allow_git_fetch AS build_allow_git_fetch,
          projects.build_timeout AS build_timeout,
          projects.mirror_trigger_builds AS mirror_trigger_builds,
          projects.pending_delete AS pending_delete,
          projects.public_builds AS public_builds,
          projects.last_repository_check_failed AS last_repository_check_failed,
          projects.last_repository_check_at AS last_repository_check_at,
          projects.only_allow_merge_if_pipeline_succeeds AS only_allow_merge_if_pipeline_succeeds,
          projects.has_external_issue_tracker AS has_external_issue_tracker,
          projects.repository_storage AS repository_storage,
          projects.repository_read_only AS repository_read_only,
          projects.request_access_enabled AS request_access_enabled,
          projects.has_external_wiki AS has_external_wiki,
          projects.ci_config_path AS ci_config_path,
          projects.lfs_enabled AS lfs_enabled,
          projects.description_html AS description_html,
          projects.only_allow_merge_if_all_discussions_are_resolved AS only_allow_merge_if_all_discussions_are_resolved,
          projects.repository_size_limit AS repository_size_limit,
          projects.printing_merge_request_link_enabled AS printing_merge_request_link_enabled,
          projects.auto_cancel_pending_pipelines AS auto_cancel_pending_pipelines,
          projects.service_desk_enabled AS service_desk_enabled,
          projects.cached_markdown_version AS cached_markdown_version,
          projects.delete_error AS delete_error,
          projects.last_repository_updated_at AS last_repository_updated_at,
          projects.disable_overriding_approvers_per_merge_request AS disable_overriding_approvers_per_merge_request,
          projects.storage_version AS storage_version,
          projects.resolve_outdated_diff_discussions AS resolve_outdated_diff_discussions,
          projects.remote_mirror_available_overridden AS remote_mirror_available_overridden,
          projects.only_mirror_protected_branches AS only_mirror_protected_branches,
          projects.pull_mirror_available_overridden AS pull_mirror_available_overridden,
          projects.jobs_cache_index AS jobs_cache_index,
          projects.external_authorization_classification_label AS external_authorization_classification_label,
          projects.mirror_overwrites_diverged_branches AS mirror_overwrites_diverged_branches,
          projects.pages_https_only AS pages_https_only,
          projects.packages_enabled AS packages_enabled,
          projects.merge_requests_author_approval AS merge_requests_author_approval,
          projects.pool_repository_id AS pool_repository_id,
          projects.bfg_object_map AS bfg_object_map,
          projects.detected_repository_languages AS detected_repository_languages,
          projects.merge_requests_disable_committers_approval AS merge_requests_disable_committers_approval,
          projects.require_password_to_approve AS require_password_to_approve,
          projects.emails_disabled AS emails_disabled,
          projects.max_pages_size AS max_pages_size,
          projects.max_artifacts_size AS max_artifacts_size,
          projects.pull_mirror_branch_prefix AS pull_mirror_branch_prefix,
          projects.remove_source_branch_after_merge AS remove_source_branch_after_merge,
          projects.marked_for_deletion_at AS marked_for_deletion_at,
          projects.marked_for_deletion_by_user_id AS marked_for_deletion_by_user_id,
          projects.autoclose_referenced_issues AS autoclose_referenced_issues,
          projects.suggestion_commit_message AS suggestion_commit_message,
          projects.project_namespace_id AS project_namespace_id,
          projects.hidden AS hidden,
          projects.organization_id AS organization_id,
          projects.deleted AS _siphon_deleted,
          projects._siphon_replicated_at AS _siphon_replicated_at
        FROM base
        LEFT JOIN projects ON projects.project_namespace_id=base.id
    SQL
  end

  def down
    execute 'DROP VIEW IF EXISTS namespace_traversal_path_refresh_to_projects_mv'
  end
end
