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 | |||||
from sqlalchemy import Column, DateTime, Integer, String | from sqlalchemy import Column, DateTime, Integer, String | ||||
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta | from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta | ||||
from .abstractattribute import AbstractAttribute | from .abstractattribute import AbstractAttribute | ||||
logger = logging.getLogger(__name__) | |||||
SQLBase = declarative_base() | SQLBase = declarative_base() | ||||
class ABCSQLMeta(abc.ABCMeta, DeclarativeMeta): | class ABCSQLMeta(abc.ABCMeta, DeclarativeMeta): | ||||
pass | pass | ||||
class ModelBase(SQLBase, metaclass=ABCSQLMeta): | class ModelBase(SQLBase, metaclass=ABCSQLMeta): | ||||
Show All 20 Lines | |||||
class IndexingModelBase(ModelBase, metaclass=ABCSQLMeta): | class IndexingModelBase(ModelBase, metaclass=ABCSQLMeta): | ||||
__abstract__ = True | __abstract__ = True | ||||
__tablename__ = AbstractAttribute | __tablename__ = AbstractAttribute | ||||
# 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)') | ||||
def initialize(db_engine, drop_tables=False, **kwargs): | |||||
"""Default database initialization function for a lister. | |||||
Typically called from the lister's initialization hook. | |||||
Args: | |||||
models (list): list of SQLAlchemy tables/models to drop/create. | |||||
db_enfine (): the SQLAlchemy DB engine. | |||||
drop_tables (bool): if True, tables will be dropped before | |||||
(re)creating them. | |||||
""" | |||||
if drop_tables: | |||||
logger.info('Dropping tables') | |||||
SQLBase.metadata.drop_all(db_engine, checkfirst=True) | |||||
logger.info('Creating tables') | |||||
SQLBase.metadata.create_all(db_engine, checkfirst=True) |