-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetl_load.py
More file actions
127 lines (107 loc) · 3.53 KB
/
Copy pathetl_load.py
File metadata and controls
127 lines (107 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from datetime import datetime
from typing import Optional
from sqlalchemy import text
from db import get_engine
def ensure_schema():
engine = get_engine()
with open("schema.sql", "r", encoding="utf-8") as f:
ddl = f.read()
with engine.begin() as conn:
for stmt in ddl.split(";"):
if stmt.strip():
conn.execute(text(stmt))
def get_since_ts(repo_id: int) -> Optional[datetime]:
engine = get_engine()
with engine.begin() as conn:
row = conn.execute(
text("SELECT MAX(committed_at) AS ts FROM commits WHERE repo_id = :rid"),
{"rid": repo_id}
).mappings().first()
return row["ts"] if row and row["ts"] else None
def upsert_repository(repo_meta, owner, name):
repo_id = repo_meta["id"]
engine = get_engine()
with engine.begin() as conn:
conn.execute(
text("""
INSERT INTO repo (repo_id, owner, name)
VALUES (:repo_id, :owner, :name)
ON CONFLICT (repo_id) DO UPDATE SET
owner = EXCLUDED.owner,
name = EXCLUDED.name
"""),
{"repo_id": repo_id, "owner": owner, "name": name}
)
return repo_id
def upsert_branches(repo_id, branches):
rows = []
for b in branches:
rows.append({
"repo_id": repo_id,
"name": b["name"],
"sha": b["commit"]["sha"],
"is_main": b["name"] == "main"
})
engine = get_engine()
with engine.begin() as conn:
conn.execute(
text("""
INSERT INTO branches (repo_id, name, sha, is_main)
VALUES (:repo_id, :name, :sha, :is_main)
ON CONFLICT (repo_id, name) DO UPDATE SET
sha = EXCLUDED.sha,
is_main = EXCLUDED.is_main
"""),
rows
)
def upsert_commits(repo_id, commits):
rows = []
for c in commits:
rows.append({
"repo_id": repo_id,
"sha": c["sha"],
"committed_at": c["commit"]["author"]["date"]
})
if not rows:
return
engine = get_engine()
with engine.begin() as conn:
conn.execute(
text("""
INSERT INTO commits (repo_id, sha, committed_at)
VALUES (:repo_id, :sha, :committed_at)
ON CONFLICT (repo_id, sha) DO NOTHING
"""),
rows
)
def upsert_pulls(repo_id, pulls):
rows = []
for pr in pulls:
rows.append({
"pull_id": pr["id"],
"repo_id": repo_id,
"number": pr["number"],
"state": pr["state"],
"merged": pr["merged_at"] is not None,
"created_at": pr["created_at"],
"merged_at": pr["merged_at"],
"updated_at": pr["updated_at"]
})
if not rows:
return
engine = get_engine()
with engine.begin() as conn:
conn.execute(
text("""
INSERT INTO pulls
(pull_id, repo_id, number, state, merged, created_at, merged_at, updated_at)
VALUES
(:pull_id, :repo_id, :number, :state, :merged, :created_at, :merged_at, :updated_at)
ON CONFLICT (pull_id) DO UPDATE SET
state = EXCLUDED.state,
merged = EXCLUDED.merged,
merged_at = EXCLUDED.merged_at,
updated_at = EXCLUDED.updated_at
"""),
rows
)