-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
47 lines (37 loc) · 1.33 KB
/
db.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
""" This is the module that manages the database operations.
Current ORM is called Peewee.
"""
import logging
import peewee
from playhouse import migrate
database = peewee.SqliteDatabase('db/search.db')
def migrate_db():
""" Migrate the database to get new tables
"""
migrator = migrate.SqliteMigrator(database)
try:
description = peewee.TextField(default='')
migrate.migrate(
migrator.add_column('search', 'description', description)
)
except peewee.OperationalError:
logging.warning(f'Could not update search table, maybe already migrated?')
class BaseModel(peewee.Model): # pylint: disable=too-few-public-methods
""" Base to be used by other tables
"""
class Meta: # pylint: disable=too-few-public-methods
""" Default peewee metal class to set the database used
"""
database = database
class Search(BaseModel): # pylint: disable=too-few-public-methods
""" Default table that stores the search results
"""
title = peewee.CharField()
url = peewee.CharField()
last_fetched = peewee.DateTimeField()
description = peewee.TextField()
class Token(BaseModel): # pylint: disable=too-few-public-methods
""" Table that stores the admin tokens
"""
token = peewee.CharField()
expiry_date = peewee.DateTimeField()