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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
# This file is part of GAnarchy - decentralized project hub
# Copyright (C) 2020 Soni L.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""This module handles GAnarchy's database.
Attributes:
MIGRATIONS: Migrations.
"""
import sqlite3
import ganarchy.dirs
import ganarchy.data
# FIXME this should not be used directly because it's a pain.
MIGRATIONS = {
"toml-config": (
(
'''UPDATE "repo_history"
SET "project" = (SELECT "git_commit" FROM "config")
WHERE "project" IS NULL''',
'''ALTER TABLE "repos"
RENAME TO "repos_old"''',
),
(
'''UPDATE "repo_history"
SET "project" = NULL
WHERE "project" = (SELECT "git_commit" FROM "config")''',
'''ALTER TABLE "repos_old"
RENAME TO "repos"''',
),
"switches to toml config format. the old 'repos' " #cont
"table is preserved as 'repos_old'"
),
"better-project-management": (
(
'''ALTER TABLE "repos"
ADD COLUMN "branch" TEXT''',
'''ALTER TABLE "repos"
ADD COLUMN "project" TEXT''',
'''CREATE UNIQUE INDEX "repos_url_branch_project"
ON "repos" ("url", "branch", "project")''',
'''CREATE INDEX "repos_project"
ON "repos" ("project")''',
'''ALTER TABLE "repo_history"
ADD COLUMN "branch" TEXT''',
'''ALTER TABLE "repo_history"
ADD COLUMN "project" TEXT''',
'''CREATE INDEX "repo_history_url_branch_project"
ON "repo_history" ("url", "branch", "project")''',
),
(
'''DELETE FROM "repos"
WHERE "branch" IS NOT NULL OR "project" IS NOT NULL''',
'''DELETE FROM "repo_history"
WHERE "branch" IS NOT NULL OR "project" IS NOT NULL''',
),
"supports multiple projects, and allows choosing " #cont
"non-default branches"
),
"test": (
(
'''-- apply''',
),
(
'''-- revert''',
),
"does nothing"
)
}
class Database:
"""A database connection/session, returned by ``connect_database``.
Some methods may require repos to be loaded.
"""
def __init__(self, conn):
self.conn = conn
def initialize(self):
"""Initializes the database tables as expected by GAnarchy.
"""
c = self.conn.cursor()
c.execute('''
CREATE TABLE "repo_history" (
"entry" INTEGER PRIMARY KEY ASC AUTOINCREMENT,
"url" TEXT,
"count" INTEGER,
"head_commit" TEXT,
"branch" TEXT,
"project" TEXT
)
''')
c.execute('''
CREATE INDEX "repo_history_url_branch_project"
ON "repo_history" ("url", "branch", "project")
''')
self.conn.commit()
c.close()
def apply_migration(self, migration):
"""Applies a migration, by name.
WARNING: Destructive operation.
Args:
migration (str): The name of the migration.
"""
c = self.conn.cursor()
for migration in MIGRATIONS[migration][0]:
c.execute(migration)
self.conn.commit()
c.close()
def revert_migration(self, migration):
"""Reverts a previously-applied migration, by name.
WARNING: Destructive operation.
Args:
migration (str): The name of the migration.
"""
c = self.conn.cursor()
for migration in MIGRATIONS[migration][1]:
c.execute(migration)
self.conn.commit()
c.close()
def load_repos(self, effective_repo_list):
"""Loads repos from repo list.
Must be done once for each instance of Database.
Args:
effective_repo_list (ganarchy.data.DataSource): The data
source for the repo list.
"""
c = self.conn.cursor()
c.execute('''
CREATE TEMPORARY TABLE "repos" (
"url" TEXT,
"active" INT,
"branch" TEXT,
"project" TEXT
)
''')
c.execute('''
CREATE UNIQUE INDEX "temp"."repos_url_branch_project"
ON "repos" ("url", "branch", "project")
''')
c.execute('''
CREATE INDEX "temp"."repos_project"
ON "repos" ("project")
''')
c.execute('''
CREATE INDEX "temp"."repos_active"
ON "repos" ("active")
''')
for repo in effective_repo_list.get_property_values(
ganarchy.data.DataProperty.VCS_REPOS
):
if repo.active:
c.execute(
'''INSERT INTO "repos" VALUES (?, ?, ?, ?)''',
(repo.uri, 1, repo.branch, repo.project_commit)
)
self.conn.commit()
c.close()
def insert_activity(self, project_commit, uri, branch, head, count):
"""Inserts activity of a repo-branch.
Args:
project_commit: The project commit.
uri: The repo uri.
branch: The branch.
head: The latest known head commit.
count: The number of new commits.
"""
self.insert_activities([(project_commit, uri, branch, head, count)])
def insert_activities(self, activities):
"""Inserts activities of repo-branches.
Args:
activities: List of tuple. The tuple must match up with the
argument order specified by ``insert_activity``.
"""
c = self.conn.cursor()
c.executemany(
'''
INSERT INTO "repo_history" (
"project",
"url",
"branch",
"head_commit",
"count"
)
VALUES (?, ?, ?, ?, ?)
''',
activities
)
self.conn.commit()
c.close()
def list_projects(self):
"""Lists loaded projects.
Repos must be loaded first.
Yields:
str: Project commit of each project.
"""
c = self.conn.cursor()
try:
for (project,) in c.execute(
'''SELECT DISTINCT "project" FROM "repos" '''
):
yield project
finally:
c.close()
def list_repobranches(self, project_commit):
"""Lists repo-branches of a project.
Repos must be loaded first.
Results are sorted by recent activity.
Args:
project_commit: The project commit.
Yields:
A 3-tuple holding the URI, branch name, and last known head
commit.
"""
c = self.conn.cursor()
try:
for (e, url, branch, head_commit) in c.execute(
'''
SELECT "max"("e"), "url", "branch", "head_commit"
FROM (
SELECT
"max"("T1"."entry") "e",
"T1"."url",
"T1"."branch",
"T1"."head_commit"
FROM "repo_history" "T1"
WHERE (
SELECT "active"
FROM "repos" "T2"
WHERE
"url" = "T1"."url"
AND "branch" IS "T1"."branch"
AND "project" IS ?1
)
GROUP BY "T1"."url", "T1"."branch"
UNION
SELECT null, "T3"."url", "T3"."branch", null
FROM "repos" "T3"
WHERE "active" AND "project" IS ?1
)
GROUP BY "url", "branch"
ORDER BY "e"
''',
(project_commit,)
):
yield url, branch, head_commit
finally:
c.close()
def list_repobranch_activity(self, project_commit, uri, branch):
"""Lists activity of a repo-branch.
Args:
project_commit: The project commit.
uri: The repo uri.
branch: The branch.
Returns:
list of int: Number of commits between updates.
"""
c = self.conn.cursor()
history = c.execute(
'''
SELECT "count"
FROM "repo_history"
WHERE
"url" = ?
AND "branch" IS ?
AND "project" IS ?
ORDER BY "entry" ASC
''',
(uri, branch, project_commit)
).fetchall()
history = [x for [x] in history]
c.close()
return history
def close(self):
"""Closes the database.
"""
self.conn.close()
def connect_database(effective_config):
"""Opens the database specified by the given config.
Args:
effective_config (ganarchy.data.DataSource): The data source
for the config.
"""
del effective_config # currently unused, intended for the future
conn = sqlite3.connect(ganarchy.dirs.DATA_HOME + "/ganarchy.db")
return Database(conn)
|