| 1 | -- SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | -- name: CreateOrgGithubImport :one |
| 4 | INSERT INTO org_github_imports ( |
| 5 | org_id, source_org, requested_by_user_id, include_private, |
| 6 | token_present, token_ciphertext, token_nonce |
| 7 | ) VALUES ( |
| 8 | $1, $2, sqlc.narg(requested_by_user_id)::bigint, $3, |
| 9 | $4, sqlc.narg(token_ciphertext)::bytea, sqlc.narg(token_nonce)::bytea |
| 10 | ) |
| 11 | RETURNING *; |
| 12 | |
| 13 | -- name: GetOrgGithubImport :one |
| 14 | SELECT * FROM org_github_imports WHERE id = $1; |
| 15 | |
| 16 | -- name: GetOrgGithubImportForOrg :one |
| 17 | SELECT * FROM org_github_imports |
| 18 | WHERE id = $1 AND org_id = $2; |
| 19 | |
| 20 | -- name: ListOrgGithubImportsForOrg :many |
| 21 | SELECT * FROM org_github_imports |
| 22 | WHERE org_id = $1 |
| 23 | ORDER BY created_at DESC |
| 24 | LIMIT $2; |
| 25 | |
| 26 | -- name: MarkOrgGithubImportDiscovering :exec |
| 27 | UPDATE org_github_imports |
| 28 | SET status = 'discovering', |
| 29 | started_at = COALESCE(started_at, now()), |
| 30 | last_error = NULL, |
| 31 | updated_at = now() |
| 32 | WHERE id = $1 |
| 33 | AND status IN ('queued', 'discovering'); |
| 34 | |
| 35 | -- name: MarkOrgGithubImportImporting :exec |
| 36 | UPDATE org_github_imports |
| 37 | SET status = 'importing', |
| 38 | total_count = $2, |
| 39 | started_at = COALESCE(started_at, now()), |
| 40 | last_error = NULL, |
| 41 | updated_at = now() |
| 42 | WHERE id = $1 |
| 43 | AND status IN ('queued', 'discovering', 'importing'); |
| 44 | |
| 45 | -- name: MarkOrgGithubImportFailed :exec |
| 46 | UPDATE org_github_imports |
| 47 | SET status = 'failed', |
| 48 | last_error = $2, |
| 49 | token_ciphertext = NULL, |
| 50 | token_nonce = NULL, |
| 51 | completed_at = COALESCE(completed_at, now()), |
| 52 | updated_at = now() |
| 53 | WHERE id = $1; |
| 54 | |
| 55 | -- name: MarkOrgGithubImportCompleted :exec |
| 56 | UPDATE org_github_imports |
| 57 | SET status = 'completed', |
| 58 | token_ciphertext = NULL, |
| 59 | token_nonce = NULL, |
| 60 | completed_at = COALESCE(completed_at, now()), |
| 61 | updated_at = now() |
| 62 | WHERE id = $1; |
| 63 | |
| 64 | -- name: MarkOrgGithubImportCompletedIfDone :one |
| 65 | UPDATE org_github_imports AS i |
| 66 | SET status = 'completed', |
| 67 | token_ciphertext = NULL, |
| 68 | token_nonce = NULL, |
| 69 | completed_at = COALESCE(completed_at, now()), |
| 70 | updated_at = now() |
| 71 | WHERE i.id = $1 |
| 72 | AND i.status = 'importing' |
| 73 | AND NOT EXISTS ( |
| 74 | SELECT 1 |
| 75 | FROM org_github_import_repos |
| 76 | WHERE import_id = $1 |
| 77 | AND status IN ('queued', 'importing') |
| 78 | ) |
| 79 | RETURNING i.*; |
| 80 | |
| 81 | -- name: InsertOrgGithubImportRepo :one |
| 82 | INSERT INTO org_github_import_repos ( |
| 83 | import_id, github_id, source_full_name, source_name, target_name, |
| 84 | clone_url, description, default_branch, target_visibility, |
| 85 | is_private, is_fork |
| 86 | ) VALUES ( |
| 87 | $1, sqlc.narg(github_id)::bigint, $2, $3, $4, |
| 88 | $5, $6, $7, $8, $9, $10 |
| 89 | ) |
| 90 | ON CONFLICT (import_id, target_name) DO UPDATE |
| 91 | SET github_id = EXCLUDED.github_id, |
| 92 | source_full_name = EXCLUDED.source_full_name, |
| 93 | source_name = EXCLUDED.source_name, |
| 94 | clone_url = EXCLUDED.clone_url, |
| 95 | description = EXCLUDED.description, |
| 96 | default_branch = EXCLUDED.default_branch, |
| 97 | target_visibility = EXCLUDED.target_visibility, |
| 98 | is_private = EXCLUDED.is_private, |
| 99 | is_fork = EXCLUDED.is_fork, |
| 100 | updated_at = now() |
| 101 | RETURNING *; |
| 102 | |
| 103 | -- name: GetOrgGithubImportRepo :one |
| 104 | SELECT * FROM org_github_import_repos WHERE id = $1; |
| 105 | |
| 106 | -- name: ListOrgGithubImportRepos :many |
| 107 | SELECT * FROM org_github_import_repos |
| 108 | WHERE import_id = $1 |
| 109 | ORDER BY source_name ASC; |
| 110 | |
| 111 | -- name: MarkOrgGithubImportRepoImporting :exec |
| 112 | UPDATE org_github_import_repos |
| 113 | SET status = 'importing', |
| 114 | started_at = COALESCE(started_at, now()), |
| 115 | last_error = NULL, |
| 116 | updated_at = now() |
| 117 | WHERE id = $1 |
| 118 | AND status = 'queued'; |
| 119 | |
| 120 | -- name: MarkOrgGithubImportRepoImported :exec |
| 121 | UPDATE org_github_import_repos |
| 122 | SET status = 'imported', |
| 123 | repo_id = $2, |
| 124 | last_error = NULL, |
| 125 | completed_at = COALESCE(completed_at, now()), |
| 126 | updated_at = now() |
| 127 | WHERE id = $1; |
| 128 | |
| 129 | -- name: MarkOrgGithubImportRepoSkipped :exec |
| 130 | UPDATE org_github_import_repos |
| 131 | SET status = 'skipped', |
| 132 | last_error = $2, |
| 133 | completed_at = COALESCE(completed_at, now()), |
| 134 | updated_at = now() |
| 135 | WHERE id = $1; |
| 136 | |
| 137 | -- name: MarkOrgGithubImportRepoFailed :exec |
| 138 | UPDATE org_github_import_repos |
| 139 | SET status = 'failed', |
| 140 | repo_id = COALESCE(sqlc.narg(repo_id)::bigint, repo_id), |
| 141 | last_error = $2, |
| 142 | completed_at = COALESCE(completed_at, now()), |
| 143 | updated_at = now() |
| 144 | WHERE id = $1; |
| 145 | |
| 146 | -- name: GetOrgGithubImportProgress :one |
| 147 | SELECT |
| 148 | i.*, |
| 149 | count(r.id)::integer AS discovered_count, |
| 150 | count(r.id) FILTER (WHERE r.status = 'queued')::integer AS queued_count, |
| 151 | count(r.id) FILTER (WHERE r.status = 'importing')::integer AS importing_count, |
| 152 | count(r.id) FILTER (WHERE r.status = 'imported')::integer AS imported_count, |
| 153 | count(r.id) FILTER (WHERE r.status = 'skipped')::integer AS skipped_count, |
| 154 | count(r.id) FILTER (WHERE r.status = 'failed')::integer AS failed_count |
| 155 | FROM org_github_imports i |
| 156 | LEFT JOIN org_github_import_repos r ON r.import_id = i.id |
| 157 | WHERE i.id = $1 AND i.org_id = $2 |
| 158 | GROUP BY i.id; |
| 159 |