Skip to content

Commit 037543b

Browse files
committed
fix: prepare moving repo into pg
1 parent 6d6519f commit 037543b

6 files changed

Lines changed: 106 additions & 0 deletions

File tree

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Dockerfile
2626
.output
2727
build
2828
dist
29+
clickhouse_backup/

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ yarn-error.log*
3939
tsconfig.tsbuildinfo
4040
.vinxi/
4141
.output/
42+
43+
clickhouse_backup/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { sql } from 'kysely';
2+
3+
import type { Database } from '../types.js';
4+
import type { Kysely } from 'kysely';
5+
6+
export async function up(db: Kysely<Database>): Promise<void> {
7+
await sql`
8+
CREATE TABLE repositories2 (
9+
id UUID DEFAULT generateUUIDv7(),
10+
org LowCardinality(String),
11+
name LowCardinality(String),
12+
stars UInt32 DEFAULT 0,
13+
updated_at DateTime DEFAULT now(),
14+
PRIMARY KEY (org, name)
15+
) ENGINE = ReplacingMergeTree()
16+
ORDER BY (org, name);
17+
`.execute(db);
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { sql } from 'kysely';
2+
3+
import type { Database } from '../types.js';
4+
import type { Kysely } from 'kysely';
5+
6+
export async function up(db: Kysely<Database>): Promise<void> {
7+
await sql`
8+
CREATE TABLE repositories (
9+
id UUID DEFAULT gen_random_uuid(),
10+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
11+
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
12+
github_id TEXT,
13+
org TEXT,
14+
name TEXT,
15+
branch TEXT DEFAULT 'main',
16+
stars BIGINT DEFAULT 0,
17+
url TEXT,
18+
ignored SMALLINT DEFAULT 0,
19+
last_fetched_at TIMESTAMP,
20+
errored SMALLINT DEFAULT 0,
21+
ignored_reason TEXT,
22+
size BIGINT DEFAULT 0,
23+
last_analyzed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
24+
avatar_url TEXT,
25+
homepage_url TEXT,
26+
description TEXT,
27+
forks INTEGER DEFAULT 0,
28+
repo_created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
29+
PRIMARY KEY (id)
30+
);
31+
`.execute(db);
32+
33+
await sql`
34+
CREATE UNIQUE INDEX "idx_repositories_unique" ON "repositories" USING BTREE ("org","name");
35+
`.execute(db);
36+
37+
await sql`
38+
CREATE INDEX "idx_repositories_analyze" ON "repositories" USING BTREE ("last_fetched_at" ASC NULLS FIRST,"stars") WHERE ignored = 0 AND errored = 0;
39+
`.execute(db);
40+
}

apps/backend/src/db/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export type CreatedAt = ColumnType<string, Date | undefined, never>;
77
export type UpdatedAt = ColumnType<string, Date | undefined, Date>;
88
export type BooleanDefault = ColumnType<boolean, boolean | undefined>;
99

10+
export interface RepositoriesClickhouse {
11+
id: ColumnType<string, never, never>;
12+
org: string;
13+
name: string;
14+
stars: number;
15+
updated_at: Timestamp;
16+
}
1017
export interface RepositoriesTable {
1118
id: ColumnType<string, never, never>;
1219
github_id: string;
@@ -73,6 +80,7 @@ export type LicensesWeeklyRow = Selectable<LicensesWeeklyTable>;
7380

7481
export interface Clickhouse {
7582
repositories: RepositoriesTable;
83+
repositories2: RepositoriesClickhouse;
7684
technologies: TechnologiesTable;
7785
technologies_weekly: TechnologiesWeeklyTable;
7886
licenses: LicensesTable;
@@ -100,6 +108,7 @@ export type LicensesInfoTableRow = Selectable<LicensesInfoTable>;
100108
export interface Database {
101109
progress: ProgressTable;
102110
licenses_info: LicensesInfoTable;
111+
repositories: RepositoriesTable;
103112
}
104113

105114
export type TX = Transaction<Database>;

scripts/backup.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ] || [ -z "$2" ]; then
4+
echo "Usage: $0 <CLICKHOUSE_HOST> <CLICKHOUSE_PASSWORD>"
5+
echo "Example: $0 ldp3g2mxso.us-east1.gcp.clickhouse.cloud mypassword"
6+
exit 1
7+
fi
8+
9+
CLICKHOUSE_HOST="$1"
10+
CLICKHOUSE_PASSWORD="$2"
11+
CLICKHOUSE_USER="default"
12+
CLICKHOUSE_DB="default"
13+
14+
# List of tables to backup (from types.ts Clickhouse interface)
15+
TABLES=(
16+
"repositories"
17+
"technologies"
18+
"licenses"
19+
)
20+
21+
BACKUP_DIR="./clickhouse_backup/$(date +%Y%m%d_%H%M%S)"
22+
mkdir -p "$BACKUP_DIR"
23+
24+
for TABLE in "${TABLES[@]}"; do
25+
echo "Backing up $TABLE as CSV..."
26+
clickhouse client \
27+
--host="$CLICKHOUSE_HOST" \
28+
--user="$CLICKHOUSE_USER" \
29+
--password="$CLICKHOUSE_PASSWORD" \
30+
--secure \
31+
--database="$CLICKHOUSE_DB" \
32+
--query="SELECT * FROM $TABLE FORMAT CSVWithNames" \
33+
> "$BACKUP_DIR/${TABLE}.csv"
34+
done
35+
36+
echo "Backup completed. CSV files are in $BACKUP_DIR"

0 commit comments

Comments
 (0)