forked from FanFicDev/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.py
executable file
·691 lines (582 loc) · 16.8 KB
/
schema.py
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env python3
from typing import List, Dict, Any, Tuple, Optional, IO, Sequence
import os
import sys
import shutil
import time
import datetime
import psycopg2
from psycopg2.extensions import AsIs, register_adapter, new_type, register_type
# return an ordered list of raw sql path, target, and link names
def walkSql() -> List[Tuple[str, str, str]]:
indexes: List[int] = []
path = ['./sql/']
scripts = []
with open('./doc/db_setup') as f:
started = False
for line in f:
line = line.rstrip()
# start at sql/
if not started:
started = line == 'sql/'
continue
# end with ==
if started and line == '==':
break
# skip empty lines
if len(line.strip()) < 1:
continue
# count all but the first tab for depth
depth = -1
while line.startswith('\t'):
depth += 1
line = line[1:]
while len(indexes) <= depth:
indexes += [0]
indexes[depth] += 1
indexes = indexes[:1 + depth]
path = path[:1 + depth]
line = line.strip()
if not line.endswith('/'):
scripts += [(path, line, list(indexes))]
else:
path += [line]
maxDepth = len(indexes)
res = []
for path, fname, idxs in scripts:
idxs += [0] * (maxDepth - len(idxs))
nstub = '-'.join(map(lambda i: f'{i:02}', idxs))
fstub = ''.join(path[1:]).replace('/', '-')
lname = './sql/fresh/' + nstub + '-' + fstub + fname
spath = ''.join(path) + fname
tname = '../../' + spath
res.append((spath, tname, lname))
return res
# recreate the sql/fresh folder by symlinking to raw sql files
def symlinkSql() -> None:
shutil.rmtree('./sql/fresh/')
os.mkdir('./sql/fresh/')
for _, tname, lname in walkSql():
os.symlink(tname, lname)
# run all sql scripts
def initDB() -> None:
from lite_oil import getConnection
with getConnection('hermes') as conn:
with conn.cursor() as curs:
for spath, _, _ in walkSql():
print(f'executing {spath}')
with open(spath, 'r') as f:
sql = f.read()
curs.execute(sql)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--init':
initDB()
if len(sys.argv) > 1 and sys.argv[1] == '--symlink':
symlinkSql()
tables = [
(
'source', '''
id serial primary key,
url url not null,
name varchar(1024) not null,
description varchar(4096) not null
'''
),
('language', '''
id serial primary key,
name varchar(1024) not null
'''),
(
'author', '''
id bigserial primary key,
name varchar(1024) not null,
urlId varchar(12) not null unique
'''
),
(
'author_source', '''
id bigserial primary key,
authorId int8 not null references author(id),
sourceId int4 not null references source(id),
name varchar(1024) not null,
url url not null,
localId varchar(1024) not null
'''
),
(
'fic', '''
id bigserial primary key,
urlId varchar(12) not null unique,
sourceId int4 not null references source(id),
localId varchar(1024) not null,
url url not null,
importStatus importStatus not null default('pending'),
created oil_timestamp not null,
fetched oil_timestamp not null,
authorId int8 not null references author(id),
-- optional metadata
ficStatus ficStatus not null default('broken'),
title varchar(4096) null,
description text null,
ageRating varchar(128) null,
languageId int4 null references language(id),
chapterCount int4 null,
wordCount int4 null,
reviewCount int4 null,
favoriteCount int4 null,
followCount int4 null,
updated oil_timestamp null,
published oil_timestamp null,
extraMeta text,
unique(sourceId, localId)
'''
),
(
'fic_chapter', '''
ficId int8 not null references fic(id),
chapterId int4 not null,
localChapterId varchar(1024) not null,
url url not null,
fetched oil_timestamp,
title varchar(4096) null,
content bytea null,
primary key(ficId, chapterId),
unique(ficId, localChapterId)
'''
),
(
'users', '''
id bigserial primary key,
created int8,
updated int8,
name text unique,
hash text,
mail text unique,
apikey text unique
'''
),
(
'user_fic', '''
userId int8 not null references users(id),
ficId int8 not null references fic(id),
readStatus ficStatus not null default('ongoing'),
lastChapterRead int4 null,
lastChapterViewed int4 null,
rating smallint null,
isFavorite boolean not null default(false),
lastViewed oil_timestamp null,
primary key(userId, ficId)
'''
),
(
'user_fic_chapter', '''
userId int8 not null references users(id),
ficId int8 not null references fic(id),
localChapterId varchar(1024) not null,
readStatus ficStatus not null default('ongoing'),
line int4 not null default(0),
subLine int4 not null default(0),
modified oil_timestamp not null default(oil_timestamp()),
markedRead oil_timestamp null,
markedAbandoned oil_timestamp null,
foreign key(ficId, loaclChapterId) references fic_chapter(ficId, localChapterId),
primary key(userId, ficId, localChapterId)
'''
),
(
'tag', '''
id bigserial primary key,
type tag_type not null,
name text not null,
parent bigint null,
sourceId bigint null
'''
),
(
'fic_tag', '''
ficId bigint not null references fic(id),
tagId bigint not null references tag(id),
priority integer not null default(0)
'''
),
(
'read_event', '''
userId int8 not null references users(id),
ficId int8 not null references fic(id),
localChapterId varchar(1024) not null,
created oil_timestamp not null,
ficStatus ficStatus not null default('complete'),
foreign key(ficId, localChapterId) references fic_chapter(ficId, localChapterId)
'''
),
]
enums = {
'ficStatus': ('broken', 'abandoned', 'ongoing', 'complete'),
'importStatus': ('pending', 'metadata', 'content', 'deep'),
'tag_type': ('tag', 'genre', 'fandom', 'character'),
}
#importQueueTable = '''CREATE TABLE `import_queue` (
# `ident` TEXT PRIMARY KEY NOT NULL,
# `added` INT NOT NULL,
# `touched` INT NULL,
# `tries` INT NOT NULL,
# `status` INT NOT NULL
#);'''
entities: Dict[str, Any] = {
'tables': tables,
'enums': enums,
}
def getClassName(name: str) -> str:
if name.startswith('ffn'):
name = 'FFN' + name[3:]
name = name.replace('_ffn', '_FFN')
name = ''.join([n[0:1].upper() + n[1:] for n in name.split('_')])
if name.endswith('ies'):
name = name[:-3] + 'y'
elif name.endswith('ueue') or name.endswith('atus'):
pass
elif name.endswith('s'):
name = name[:-1]
return name
def getTypeOid(typename: str, namespace: str = 'public') -> int:
from lite_oil import getConnection
with getConnection('hermes') as conn:
with conn.cursor() as curs:
curs.execute(
'''
SELECT pg_type.oid
FROM pg_type JOIN pg_namespace
ON typnamespace = pg_namespace.oid
WHERE typname = %(typename)s
AND nspname = %(namespace)s
''', {
'typename': typename,
'namespace': namespace
}
)
r = curs.fetchone()
if r is None:
raise Exception(f'unable to determine oid: {typename}, {namespace}')
return int(r[0])
class OilTimestamp:
def __init__(self, uts: float) -> None:
if uts > (time.time() * 5):
raise Exception(f"{uts} is almost certainly already an oil timestamp")
self.ots = int(uts * 1000)
def toUTS(self) -> int:
return self.ots // 1000
def withinDelta(
self,
rhs: Optional['OilTimestamp'] = None,
seconds: int = 0,
minutes: int = 0,
hours: int = 0,
days: int = 0
) -> bool:
if rhs is None:
rhs = OilTimestamp.now()
deltaSeconds = ((days * 24 + hours) * 60 + minutes) * 60 + seconds
return (rhs.toUTS() - self.toUTS()) < deltaSeconds
def toDateTime(self) -> 'datetime.datetime':
return datetime.datetime.fromtimestamp(self.toUTS())
def toDateString(self) -> str:
return self.toDateTime().strftime('%Y-%m-%d')
def __lt__(self, rhs: 'OilTimestamp') -> bool:
return self.ots < rhs.ots
@classmethod
def fromOil(cls, ots: int) -> 'OilTimestamp':
return OilTimestamp(ots / 1000)
@classmethod
def fromNullableOil(cls, ots: Optional[int]) -> Optional['OilTimestamp']:
if ots is None:
return None
return OilTimestamp(ots / 1000)
@classmethod
def now(cls) -> 'OilTimestamp':
return OilTimestamp(time.time())
def adaptOilTimestamp(oil_timestamp: OilTimestamp) -> AsIs:
return AsIs(str(oil_timestamp.ots))
def castOilTimestamp(value: Optional[str],
curs: 'psycopg2.cursor') -> Optional[OilTimestamp]:
if value is None: return None
return OilTimestamp.fromOil(int(value))
register_adapter(OilTimestamp, adaptOilTimestamp)
_OilTimestamp = new_type(
(getTypeOid('oil_timestamp'), ), "_OilTimestamp", castOilTimestamp
)
register_type(_OilTimestamp)
def oil_timestamp() -> OilTimestamp:
return OilTimestamp(time.time())
columnTypes = {
'boolean': 'bool',
'smallint': 'int',
'integer': 'int',
'bigint': 'int',
'serial': 'int',
'bigserial': 'int',
'int4': 'int',
'int8': 'int',
'text': 'str',
'bytea': 'bytes',
'character varying(4096)': 'str',
'character varying(1024)': 'str',
'character varying(128)': 'str',
'character varying(12)': 'str',
'varchar(4096)': 'str',
'varchar(1024)': 'str',
'varchar(128)': 'str',
'varchar(12)': 'str',
'url': 'str',
'oil_timestamp': 'OilTimestamp',
}
for enum in entities['enums']:
columnTypes[enum] = getClassName(enum)
ColumnInfoRow = Tuple[int, str, str, bool, Optional[Any], int, str]
class ColumnInfo:
def __init__(self, row: ColumnInfoRow):
(
self.cid, self.name, self.type, self.notnull, self.dflt_value, self.pk,
self.ptype
) = row
def toTuple(self) -> ColumnInfoRow:
return (
self.cid, self.name, self.type, self.notnull, self.dflt_value, self.pk,
self.ptype
)
def toSourceTuple(self) -> str:
return (
f"({self.cid}, {repr(self.name)}, {repr(self.type)}, "
+ f"{self.notnull}, {self.dflt_value}, {self.pk}, {repr(self.ptype)})"
)
def __str__(self) -> str:
return str(self.__dict__)
@staticmethod
def fromSQL(sql: str) -> List['ColumnInfo']:
cid = 0
pkCount = 0
info: List['ColumnInfo'] = []
for sqlLine in sql.split('\n'):
line = sqlLine.rstrip(',').strip()
if len(line) < 1:
continue
if line.startswith('--'):
continue
lline = line.lower()
if lline.startswith('unique'):
continue # TODO
if lline.startswith('foreign key'):
continue # TODO
if lline.startswith('primary key'):
if pkCount > 0:
raise Exception('multiple pk definition?')
names = line[len('primary key('):-1] # strip ) too
for n in names.split(','):
name = n.strip()
pkCount += 1
for ci in info:
if ci.name == name:
ci.pk = pkCount
break
else:
raise Exception(f'unable to find pk column {name}?')
continue
parts = line.split()
ctype = parts[1]
if ctype == 'character' and parts[2].startswith('varying'):
ctype += ' ' + parts[2]
if ctype.endswith(','):
ctype = ctype[:-1]
if ctype not in columnTypes:
raise Exception(f'unable to determine type of column: {ctype}')
notnull = lline.find('not null') > -1
pk = 0
if lline.find('primary key') > -1:
pkCount += 1
pk = pkCount
notnull = True
ptype = columnTypes[ctype]
dflt: Any = None
for p in parts:
if not p.lower().startswith('default('):
continue
v = p[len('default('):-1] # strip closing ) too
dflt = str(v)
if ctype in entities['enums']:
dflt = f'{ptype}[{dflt}]'
elif ptype == 'bool':
dflt = 'True' if p.lower() == 'true' else 'False'
elif ptype == 'str':
dflt = repr(dflt)
if not notnull:
ptype = f'Optional[{ptype}]'
info += [ColumnInfo((cid, parts[0], ctype, notnull, dflt, pk, ptype))]
cid += 1
return info
def writeColumnInfo(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
f.write('\tcolumns = [ColumnInfo(ct) for ct in [\n')
for column in columns:
f.write(f'\t\t{column.toSourceTuple()},\n')
f.write('\t]]\n')
pkColumnIds = [ci.cid for ci in columns if ci.pk > 0]
f.write('\tpkColumns: List[ColumnInfo] = [\n')
f.write('\t\t' + ','.join([f'columns[{cid}]' for cid in pkColumnIds]))
f.write('\n\t]\n')
regColumnIds = [ci.cid for ci in columns if ci.pk == 0]
f.write('\tregColumns: List[ColumnInfo] = [\n')
f.write('\t\t' + ','.join([f'columns[{cid}]' for cid in regColumnIds]))
f.write('\n\t]\n')
f.write('\tfields = {')
f.write(','.join([repr(ci.name) for ci in columns]))
f.write('}\n')
def writeInit(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
# default values?
f.write(f'\tdef __init__(self) -> None:\n')
for col in columns:
if col.dflt_value is not None:
f.write(f'\t\tself.{col.name}: {col.ptype} = {col.dflt_value}\n')
elif not col.notnull:
f.write(f'\t\tself.{col.name}: {col.ptype} = None\n')
else:
f.write(f'\t\tself.{col.name}: {col.ptype}\n')
f.write('\n')
def writeFromRow(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
f.writelines(
[
'\t@classmethod\n',
f"\tdef fromRow(cls: Type[_{clsName}T], row: Sequence[Any]) -> _{clsName}T:\n",
'\t\tself = cls()\n',
]
)
for col in columns:
if col.type == 'bytea':
if col.notnull:
f.write(f'\t\tself.{col.name} = row[{col.cid}].tobytes()\n')
else:
f.write(
f'\t\tself.{col.name} = row[{col.cid}].tobytes() if row[{col.cid}] is not None else None\n'
)
elif col.type == 'oil_timestamp':
if col.notnull:
f.write(f'\t\tself.{col.name} = OilTimestamp.fromOil(row[{col.cid}])\n')
else:
f.write(
f'\t\tself.{col.name} = OilTimestamp.fromNullableOil(row[{col.cid}])\n'
)
else:
f.write(f'\t\tself.{col.name} = row[{col.cid}]\n')
f.write('\t\treturn self\n\n')
def writeToTuple(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
tupleTypes = ', '.join([ci.ptype for ci in columns])
memberNames = ', '.join([f'self.{ci.name}' for ci in columns])
f.writelines(
[
f"\tdef toTuple(self) -> Tuple[{tupleTypes}]:\n",
f'\t\treturn ({memberNames},)\n',
]
)
f.write('\n')
def writeToInsertTuple(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
columns = [c for c in columns if c.type.lower().find('serial') < 0]
tupleTypes = ', '.join([ci.ptype for ci in columns])
memberNames = ', '.join([f'self.{ci.name}' for ci in columns])
f.writelines(
[
f"\tdef toInsertTuple(self) -> Tuple[{tupleTypes}]:\n",
f'\t\treturn ({memberNames},)\n',
]
)
f.write('\n')
def writeToJSONable(f: IO, clsName: str, columns: List[ColumnInfo]) -> None:
if len(columns) < 1:
f.writelines(
[
f"\tdef toJSONable(self) -> lite.JSONable:\n",
f'\t\treturn {{ }}\n',
]
)
return
f.writelines(
[
f"\tdef toJSONable(self) -> lite.JSONable:\n",
f'\t\treturn {{\n',
]
)
for col in columns:
if col.type == 'oil_timestamp':
if col.notnull:
f.write(
f'\t\t\t"{col.name}": self.{col.name}.toDateTime().isoformat(),\n'
)
else:
f.write(
f'\t\t\t"{col.name}": None if self.{col.name} is None else self.{col.name}.toDateTime().isoformat(),\n'
)
else:
f.write(f'\t\t\t"{col.name}": self.{col.name},\n')
f.writelines([
f'\t\t}}\n',
f'\n',
])
def writeEnum(f: IO, name: str, values: Sequence[str]) -> None:
clsName = getClassName(name)
print(f' generating enum {name} => {clsName}')
f.write(f'class {clsName}(enum.IntEnum):\n')
for value, vname in enumerate(values):
print(f' {vname} = {value}')
f.write(f'\t{vname} = {value}\n')
oid = getTypeOid(name.lower())
f.writelines(
[
'\n',
f'def adapt{clsName}({name}: {clsName}) -> AsIs:\n'
f"\treturn AsIs(\"'%s'::{name}\" % {name}.name)\n",
f"def cast{clsName}(value: Optional[str], curs: 'psycopg2.cursor'\n",
f'\t\t) -> Optional[{clsName}]:\n',
f'\tif value is None: return None\n',
f'\treturn {clsName}[value]\n',
'\n',
f'register_adapter({clsName}, adapt{clsName})\n',
f'_{clsName} = new_type(({oid},), "_{clsName}", cast{clsName})\n',
f'register_type(_{clsName})\n',
'\n',
]
)
def generateBaseClasses() -> None:
import os
us = os.path.realpath(os.path.expanduser(__file__))
here = os.path.dirname(us)
targ = os.path.join(here, 'store_bases.py')
with open(targ, 'w') as f:
f.writelines(
[
'import typing\n',
'from typing import Optional, Type, Sequence, List, Tuple, Any, NewType, TypeVar\n',
'import lite\n',
'import enum\n',
'import psycopg2\n',
'from psycopg2.extensions import AsIs, register_adapter, new_type, register_type\n',
'from schema import ColumnInfo, OilTimestamp, oil_timestamp\n',
'\n',
]
)
print(f'generating enums')
for enum, values in entities['enums'].items():
writeEnum(f, enum, values)
print(f'generating tables')
for table, sql in entities['tables']:
clsName = getClassName(table)
print(f' generating table {table} => {clsName}')
cols = ColumnInfo.fromSQL(sql)
for col in cols:
print(f' {str(col)}')
f.write(f"_{clsName}T = TypeVar('_{clsName}T', bound='{clsName}')\n")
f.write(f'class {clsName}(lite.StoreType):\n')
writeColumnInfo(f, clsName, cols)
writeInit(f, clsName, cols)
writeFromRow(f, clsName, cols)
writeToTuple(f, clsName, cols)
writeToInsertTuple(f, clsName, cols)
writeToJSONable(f, clsName, cols)
if __name__ == '__main__':
generateBaseClasses()
# TODO compare computed columns here to actual columns from psql?