Source code for rattail.db.config
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2024 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Database Configuration
"""
import logging
import warnings
from wuttjamaican.util import parse_bool
from wuttjamaican.db import get_engines as wutta_get_engines
from wuttjamaican.db.conf import make_engine_from_config as wutta_make_engine_from_config
from rattail.exceptions import SQLAlchemyNotInstalled
log = logging.getLogger(__name__)
[docs]
def get_engines(config, section='rattail.db'):
"""
DEPRECATED; please use
:func:`wuttjamaican:wuttjamaican.db.conf.get_engines()` instead.
"""
warnings.warn("rattail.db.config.get_engines() is deprecated; "
"please use wuttjamaican.db.get_engines() instead",
DeprecationWarning, stacklevel=2)
return wutta_get_engines(config, section)
[docs]
def get_default_engine(config, section='rattail.db'):
"""
DEPRECATED; please use
:func:`wuttjamaican:wuttjamaican.db.conf.get_engines()` instead.
"""
warnings.warn("rattail.db.config.get_default_engine() is deprecated; "
"please use wuttjamaican.db.get_engines() instead",
DeprecationWarning, stacklevel=2)
return wutta_get_engines(config, section).get('default')
# TODO: DEPRECATED - this should be removed soon
def configure_session(config, session):
""" """
if config.getbool('rattail.db', 'changes.record', usedb=False):
warnings.warn("setting rattail.db.changes.record is deprecated; "
"please set per-engine .record_changes instead",
DeprecationWarning)
from rattail.db.changes import record_changes
record_changes(session, config=config)
[docs]
def make_engine_from_config(
config_dict,
prefix='sqlalchemy.',
**kwargs):
"""
This is the same as
:func:`wuttjamaican:wuttjamaican.db.conf.make_engine_from_config()`
except Rattail may customize the engine a bit further:
The engine can be told to "record changes" for sake of
datasync; for instance:
.. code-block:: ini
[rattail.db]
default.url = sqlite:///
default.record_changes = true
And/or the engine can be told to log its SQLAlchemy connection
pool status:
.. code-block:: ini
[rattail.db]
default.url = sqlite:///
default.log_pool_status = true
"""
config_dict = dict(config_dict)
# stash flag for recording changes
record_changes = False
key = f'{prefix}record_changes'
if key in config_dict:
record_changes = parse_bool(config_dict.pop(key))
# stash flag for logging db pool status
log_pool_status = False
key = f'{prefix}log_pool_status'
if key in config_dict:
log_pool_status = parse_bool(config_dict.pop(key))
# make engine per usual
engine = wutta_make_engine_from_config(config_dict, prefix=prefix, **kwargs)
# then apply flags from stash
if record_changes:
engine.rattail_record_changes = True
if log_pool_status:
engine.rattail_log_pool_status = log_pool_status
return engine