-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
129 lines (121 loc) · 6.19 KB
/
Copy pathschema.sql
File metadata and controls
129 lines (121 loc) · 6.19 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
128
129
-- The DDL the deploy applies. `clawnify deploy` reconciles THIS file and
-- nothing else, and reconciliation is additive: it adds missing columns and
-- never drops anything, so editing this file in place is safe against a live
-- database. Template repos carry no migrations/ folder: migrations are per
-- deployed instance, tracked in each app's own D1.
--
-- LOCAL DEVELOPMENT DOES NOT RECONCILE. `clawnify dev` runs this file as-is,
-- and every CREATE below is IF NOT EXISTS, so adding a column to a table you
-- have already created locally is a silent no-op. Delete
-- .clawnify/.wrangler/state/v3/d1 and restart to pick the change up.
--
-- One deployment tracks one GitHub account (a user or an organisation). The
-- database belongs to the org that deployed the app, so rows carry no org id.
--
-- Why this app keeps anything at all: GitHub's traffic numbers (views, clones,
-- referrers, popular pages) only ever cover the last 14 days, and star counts
-- are a single number with no history. Everything older than two weeks exists
-- only because a daily sync wrote it down here.
-- What to track. A single row.
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY CHECK (id = 1),
owner TEXT NOT NULL, -- GitHub login, as GitHub spells it
owner_type TEXT NOT NULL DEFAULT 'Organization', -- 'User' | 'Organization'
owner_avatar TEXT,
include_forks INTEGER NOT NULL DEFAULT 0,
include_archived INTEGER NOT NULL DEFAULT 0,
listed_on TEXT, -- UTC day the repo list was last read
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
-- Every public repository the owner has, whether or not it is tracked.
-- `hidden` is the user's choice. A repo whose `listed_on` is older than the
-- settings row's has left GitHub's list (deleted, renamed, or made private):
-- it drops out of every view and its history is kept.
CREATE TABLE IF NOT EXISTS repos (
full_name TEXT PRIMARY KEY, -- owner/name
name TEXT NOT NULL,
description TEXT,
html_url TEXT NOT NULL,
homepage TEXT,
language TEXT,
topics TEXT NOT NULL DEFAULT '[]', -- JSON array
is_fork INTEGER NOT NULL DEFAULT 0,
archived INTEGER NOT NULL DEFAULT 0,
hidden INTEGER NOT NULL DEFAULT 0,
listed_on TEXT, -- UTC day GitHub last listed it
stars INTEGER NOT NULL DEFAULT 0,
forks INTEGER NOT NULL DEFAULT 0,
open_issues INTEGER NOT NULL DEFAULT 0, -- GitHub counts open pull requests here too
created_at TEXT,
pushed_at TEXT,
first_seen TEXT NOT NULL DEFAULT (datetime('now')),
-- The 14-day totals exactly as GitHub reports them. Unique visitors cannot
-- be summed across days, so these are the only honest two-week uniques.
views_14d INTEGER,
view_uniques_14d INTEGER,
clones_14d INTEGER,
clone_uniques_14d INTEGER,
traffic_access TEXT, -- 'ok' | 'denied' | null (never tried)
traffic_error TEXT, -- GitHub's own words when it refused
detail_synced_on TEXT, -- UTC day traffic was last read
-- Star history is rebuilt once from GitHub's weekly star history.
-- `history_since` is the first day it covers, the day before the first star.
history_backfilled INTEGER NOT NULL DEFAULT 0,
history_since TEXT
);
-- Cumulative counters per repo per day. A 'snapshot' row is what the daily
-- sync saw; a 'backfill' row was rebuilt from GitHub's star history and only
-- knows stars. A snapshot always wins over a backfill for the same day.
CREATE TABLE IF NOT EXISTS repo_daily (
full_name TEXT NOT NULL,
day TEXT NOT NULL, -- YYYY-MM-DD, UTC
stars INTEGER NOT NULL,
forks INTEGER,
open_issues INTEGER,
source TEXT NOT NULL DEFAULT 'snapshot', -- 'snapshot' | 'backfill'
PRIMARY KEY (full_name, day)
);
-- Views and clones per repo per day, straight from GitHub's daily breakdown.
-- Each sync rewrites the last 14 days, because GitHub revises the latest ones.
CREATE TABLE IF NOT EXISTS traffic_daily (
full_name TEXT NOT NULL,
day TEXT NOT NULL,
views INTEGER NOT NULL DEFAULT 0,
view_uniques INTEGER NOT NULL DEFAULT 0,
clones INTEGER NOT NULL DEFAULT 0,
clone_uniques INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (full_name, day)
);
-- GitHub's top-10 referrers and top-10 pages for the trailing 14 days, as
-- they stood on `captured_on`. Kept per capture day so the history of where
-- visitors came from survives GitHub's window.
CREATE TABLE IF NOT EXISTS traffic_sources (
full_name TEXT NOT NULL,
captured_on TEXT NOT NULL,
kind TEXT NOT NULL, -- 'referrer' | 'path'
key TEXT NOT NULL, -- referring site, or page path
title TEXT, -- page title (paths only)
count INTEGER NOT NULL DEFAULT 0,
uniques INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (full_name, captured_on, kind, key)
);
CREATE TABLE IF NOT EXISTS sync_runs (
id TEXT PRIMARY KEY,
started_at TEXT NOT NULL DEFAULT (datetime('now')),
finished_at TEXT,
trigger TEXT NOT NULL DEFAULT 'manual', -- 'manual' | 'schedule'
status TEXT NOT NULL DEFAULT 'running', -- running | ok | partial | failed
repos INTEGER NOT NULL DEFAULT 0, -- repos whose detail this step read
api_calls INTEGER NOT NULL DEFAULT 0,
error TEXT
);
-- The next daily sync the platform queue holds for this app. A single row.
CREATE TABLE IF NOT EXISTS sync_schedule (
id INTEGER PRIMARY KEY CHECK (id = 1),
job_id TEXT NOT NULL,
run_at TEXT NOT NULL, -- ISO-8601 UTC
booked_at TEXT NOT NULL DEFAULT (datetime('now'))
);
CREATE INDEX IF NOT EXISTS idx_repo_daily_day ON repo_daily (day);
CREATE INDEX IF NOT EXISTS idx_traffic_daily_day ON traffic_daily (day);
CREATE INDEX IF NOT EXISTS idx_sync_runs_started ON sync_runs (started_at);