# Path: seedbox/db/base.py
# class Connection(object):
#     def __init__(self, conf):
#     def upgrade(self):
#     def clear(self):
#     def backup(self):
#     def shrink_db(self):
#     def save(self, instance):
#     def bulk_create(self, instances):
#     def bulk_update(self, value_map, entity_type, qfilter):
#     def delete_by(self, entity_type, qfilter):
#     def delete(self, instance):
#     def fetch_by(self, entity_type, qfilter):
#     def fetch(self, entity_type, pk):
# 
# Path: seedbox/db/maintenance.py
# LOG = logging.getLogger(__name__)
# MAX_BACKUP_COUNT = 8
# def backup(conf):
# 
# Path: seedbox/db/sqlalchemy/migration.py
# INIT_VERSION = 0
# _REPO = None
#         _REPO = Repository(abspath)
# def db_sync(engine, version=None, init_version=INIT_VERSION):
# def db_version(engine, init_version=INIT_VERSION):
# def db_version_control(engine, version=None):
# def _find_migrate_repo():
# 
# Path: seedbox/db/sqlalchemy/model_util.py
# LOG = logging.getLogger(__name__)
# def from_db(db_item):
# def to_db(api_item, db_item=None):
# 
# Path: seedbox/db/sqlalchemy/models.py
# NONE_TYPE = 'none'
# BOOL_TYPE = 'bool'
# DATE_TYPE = 'datetime'
# INT_TYPE = 'int'
# STR_TYPE = 'str'
# VAL_TYPES = [NONE_TYPE, BOOL_TYPE, DATE_TYPE, INT_TYPE, STR_TYPE]
# def to_table_name(klass_name):
#     def __tablename__(cls):
#     def save(self, session):
#     def __repr__(self):
#     def __setitem__(self, key, value):
#     def __getitem__(self, key):
#     def get(self, key, default=None):
#     def __iter__(self):
#     def __next__(self):
#     def next(self):
#     def update(self, values):
#     def iteritems(self):
#     def __init__(self, id, value=None):
#     def __setitem__(self, key, value):
#     def __getitem__(self, key):
#     def get(self, key, default=None):
#     def update(self, values):
#     def get_value(self):
#     def set_value(self, value):
#     def reset_value(self):
#     def __repr__(self):
# def verify_tables(engine):
# def purge_all_tables(engine):
#     def __init__(self, table, query):
#     def _handle_complex_op(self, complex_op, nodes):
#     def _handle_simple_op(self, simple_op, nodes):
#     def _transform(self, sub_tree):
#     def apply_filter(self, expression_tree):
# class Base(six.Iterator):
# class HasId(object):
# class HasTimestamp(object):
# class Torrent(Base, HasId, HasTimestamp):
# class MediaFile(Base, HasId):
# class AppState(Base):
# class QueryTransformer(object):
# 
# Path: seedbox/db/sqlalchemy/session.py
# @property
# def session(self):
#     """Get a Session instance."""
#     return self._session_maker()


import logging
from seedbox.db import base
from seedbox.db import maintenance
from seedbox.db.sqlalchemy import migration
from seedbox.db.sqlalchemy import model_util
from seedbox.db.sqlalchemy import models as db_model
from seedbox.db.sqlalchemy import session as db_session
        """Clear database."""
        engine = self._engine_facade.engine
        db_model.purge_all_tables(engine)
        self._engine_facade.session_maker.close_all()
        engine.dispose()

    def backup(self):
        """Backup database."""
        maintenance.backup(self.conf)

    def shrink_db(self):
        """Shrink database."""
        engine = self._engine_facade.engine
        with engine.begin() as conn:
            conn.execute('VACUUM')
            LOG.debug('db space reclaimed')

    def save(self, instance):
        """Save the instance to the database

        :param instance: an instance of modeled data object
        """
        _model = getattr(db_model, instance.__class__.__name__)
        session = self._engine_facade.session
        with session.begin():
            _pk = getattr(instance, instance.PK_NAME)
            if _pk is not None:
                _row = session.query(_model).get(_pk)
                _row = model_util.to_db(instance, _row)
            else:

# Path: seedbox/db/base.py
# class Connection(object):
#     def __init__(self, conf):
#     def upgrade(self):
#     def clear(self):
#     def backup(self):
#     def shrink_db(self):
#     def save(self, instance):
#     def bulk_create(self, instances):
#     def bulk_update(self, value_map, entity_type, qfilter):
#     def delete_by(self, entity_type, qfilter):
#     def delete(self, instance):
#     def fetch_by(self, entity_type, qfilter):
#     def fetch(self, entity_type, pk):
# 
# Path: seedbox/db/maintenance.py
# LOG = logging.getLogger(__name__)
# MAX_BACKUP_COUNT = 8
# def backup(conf):
# 
# Path: seedbox/db/sqlalchemy/migration.py
# INIT_VERSION = 0
# _REPO = None
#         _REPO = Repository(abspath)
# def db_sync(engine, version=None, init_version=INIT_VERSION):
# def db_version(engine, init_version=INIT_VERSION):
# def db_version_control(engine, version=None):
# def _find_migrate_repo():
# 
# Path: seedbox/db/sqlalchemy/model_util.py
# LOG = logging.getLogger(__name__)
# def from_db(db_item):
# def to_db(api_item, db_item=None):
# 
# Path: seedbox/db/sqlalchemy/models.py
# NONE_TYPE = 'none'
# BOOL_TYPE = 'bool'
# DATE_TYPE = 'datetime'
# INT_TYPE = 'int'
# STR_TYPE = 'str'
# VAL_TYPES = [NONE_TYPE, BOOL_TYPE, DATE_TYPE, INT_TYPE, STR_TYPE]
# def to_table_name(klass_name):
#     def __tablename__(cls):
#     def save(self, session):
#     def __repr__(self):
#     def __setitem__(self, key, value):
#     def __getitem__(self, key):
#     def get(self, key, default=None):
#     def __iter__(self):
#     def __next__(self):
#     def next(self):
#     def update(self, values):
#     def iteritems(self):
#     def __init__(self, id, value=None):
#     def __setitem__(self, key, value):
#     def __getitem__(self, key):
#     def get(self, key, default=None):
#     def update(self, values):
#     def get_value(self):
#     def set_value(self, value):
#     def reset_value(self):
#     def __repr__(self):
# def verify_tables(engine):
# def purge_all_tables(engine):
#     def __init__(self, table, query):
#     def _handle_complex_op(self, complex_op, nodes):
#     def _handle_simple_op(self, simple_op, nodes):
#     def _transform(self, sub_tree):
#     def apply_filter(self, expression_tree):
# class Base(six.Iterator):
# class HasId(object):
# class HasTimestamp(object):
# class Torrent(Base, HasId, HasTimestamp):
# class MediaFile(Base, HasId):
# class AppState(Base):
# class QueryTransformer(object):
# 
# Path: seedbox/db/sqlalchemy/session.py
# @property
# def session(self):
#     """Get a Session instance."""
#     return self._session_maker()


# Path: seedbox/db/sqlalchemy/api.py
import logging
from seedbox.db import base
from seedbox.db import maintenance
from seedbox.db.sqlalchemy import migration
from seedbox.db.sqlalchemy import model_util
from seedbox.db.sqlalchemy import models as db_model
from seedbox.db.sqlalchemy import session as db_session

        """Clear database."""
        engine = self._engine_facade.engine
        db_model.purge_all_tables(engine)
        self._engine_facade.session_maker.close_all()
        engine.dispose()

    def backup(self):
        """Backup database."""
        maintenance.backup(self.conf)

    def shrink_db(self):
        """Shrink database."""
        engine = self._engine_facade.engine
        with engine.begin() as conn:
            conn.execute('VACUUM')
            LOG.debug('db space reclaimed')

    def save(self, instance):
        """Save the instance to the database

        :param instance: an instance of modeled data object
        """
        _model = getattr(db_model, instance.__class__.__name__)
        session = self._engine_facade.session
        with session.begin():
            _pk = getattr(instance, instance.PK_NAME)
            if _pk is not None:
                _row = session.query(_model).get(_pk)
                _row = model_util.to_db(instance, _row)
            else:

                _row = model_util.to_db(instance)