Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/core/models.py
# Copyright (C) 2015-2017 the Software Heritage developers | # Copyright (C) 2015-2017 the Software Heritage developers | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import abc | import abc | ||||
from datetime import datetime | from datetime import datetime | ||||
import logging | import logging | ||||
from sqlalchemy import Column, DateTime, Integer, String | from sqlalchemy import Column, DateTime, Integer, String | ||||
from sqlalchemy.ext.declarative import DeclarativeMeta | from sqlalchemy.ext.declarative import DeclarativeMeta | ||||
from typing import Type, Union | |||||
from .abstractattribute import AbstractAttribute | from .abstractattribute import AbstractAttribute | ||||
from swh.storage.schemata.distribution import SQLBase | from swh.storage.schemata.distribution import SQLBase | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
class ABCSQLMeta(abc.ABCMeta, DeclarativeMeta): | class ABCSQLMeta(abc.ABCMeta, DeclarativeMeta): | ||||
pass | pass | ||||
class ModelBase(SQLBase, metaclass=ABCSQLMeta): | class ModelBase(SQLBase, metaclass=ABCSQLMeta): | ||||
"""a common repository""" | """a common repository""" | ||||
__abstract__ = True | __abstract__ = True | ||||
__tablename__ = AbstractAttribute | __tablename__ = \ | ||||
AbstractAttribute # type: Union[Type[AbstractAttribute], str] | |||||
uid = AbstractAttribute('Column(<uid_type>, primary_key=True)') | uid = AbstractAttribute( | ||||
'Column(<uid_type>, primary_key=True)' | |||||
) # type: Union[AbstractAttribute, Column] | |||||
name = Column(String, index=True) | name = Column(String, index=True) | ||||
full_name = Column(String, index=True) | full_name = Column(String, index=True) | ||||
html_url = Column(String) | html_url = Column(String) | ||||
origin_url = Column(String) | origin_url = Column(String) | ||||
origin_type = Column(String) | origin_type = Column(String) | ||||
last_seen = Column(DateTime, nullable=False) | last_seen = Column(DateTime, nullable=False) | ||||
task_id = Column(Integer) | task_id = Column(Integer) | ||||
def __init__(self, **kw): | def __init__(self, **kw): | ||||
kw['last_seen'] = datetime.now() | kw['last_seen'] = datetime.now() | ||||
super().__init__(**kw) | super().__init__(**kw) | ||||
class IndexingModelBase(ModelBase, metaclass=ABCSQLMeta): | class IndexingModelBase(ModelBase, metaclass=ABCSQLMeta): | ||||
__abstract__ = True | __abstract__ = True | ||||
__tablename__ = AbstractAttribute | __tablename__ = \ | ||||
AbstractAttribute # type: Union[Type[AbstractAttribute], str] | |||||
# The value used for sorting, segmenting, or api query paging, | # The value used for sorting, segmenting, or api query paging, | ||||
# because uids aren't always sequential. | # because uids aren't always sequential. | ||||
indexable = AbstractAttribute('Column(<indexable_type>, index=True)') | indexable = AbstractAttribute( | ||||
'Column(<indexable_type>, index=True)' | |||||
) # type: Union[AbstractAttribute, Column] | |||||
def initialize(db_engine, drop_tables=False, **kwargs): | def initialize(db_engine, drop_tables=False, **kwargs): | ||||
"""Default database initialization function for a lister. | """Default database initialization function for a lister. | ||||
Typically called from the lister's initialization hook. | Typically called from the lister's initialization hook. | ||||
Args: | Args: | ||||
Show All 12 Lines |