def test_patch_sqlite_uris(self): if sqlite_version_info >= (3, 6, 19): self.assertTrue('foreign_keys=1' in config.Database().uri)
def test_patch_multiple_sqlite_uris(self): self.assertFalse('foreign_keys=1' in config.Database().uri['mamba']) self.database._patch_sqlite_uris() if sqlite_version_info >= (3, 6, 19): self.assertTrue('foreign_keys=1' in config.Database().uri['mamba'])
def test_database_load(self): config.Database('../mamba/test/dummy_app/config/database.json') self.assertTrue(config.Database().loaded) self.assertEqual(config.Database().uri, 'sqlite:///db/dummy.db') self.assertEqual(config.Database().min_threads, 5) self.assertEqual(config.Database().max_threads, 20)
def test_database_fallback_on_no_existent_file(self): self.assertFalse(config.Database().loaded) self.assertEqual(config.Database().uri, 'sqlite:') self.assertEqual(config.Database().min_threads, 5) self.assertEqual(config.Database().max_threads, 20)
class Database(object): """ Storm ORM database provider for Mamba. :param pool: the thrad pool for this database :type pool: :class:`twisted.python.threadpool.ThreadPool` """ monkey_patched = False pool = ThreadPool(config.Database().min_threads, config.Database().max_threads, 'DatabasePool') zstorm_configured = False def __init__(self, pool=None, testing=False): if pool is not None: self.pool = pool self.started = False self.__testing = testing if not self.zstorm_configured: provideUtility(global_zstorm, IZStorm) zstorm = getUtility(IZStorm) zstorm.set_default_uri('mamba', config.Database().uri) SQLite.register() MySQL.register() PostgreSQL.register() # MonkeyPatch Storm if not self.monkey_patched: monkey_patcher = MonkeyPatcher( (properties, 'PropertyColumn', PropertyColumnMambaPatch)) monkey_patcher.patch() self.monkey_patched = True def start(self): """Starts the Database (and the threadpool) """ if self.started: return if self.__testing is True: self.pool.start() # else: # self._database = create_database(config.Database().uri) self.started = True def stop(self): """Stops the Database (and the threadpool) """ if not self.started: return self.started = False def adjust_poolsize(self, min_threads=None, max_threads=None): """ Adjusts the underlying threadpool size :param min: minimum number of threads :type min: int :param max: maximum number of threads :type max: int """ self.pool.adjustPoolsize(min_threads, max_threads) def store(self): """ Returns a Store per-thread through :class:`storm.zope.zstorm.ZStorm` """ if not self.started: self.start() zstorm = getUtility(IZStorm) return zstorm.get('mamba') def dump(self, model_manager, full=False): """ Dumps the full database :param model_manager: the model manager from mamba application :type model_manager: :class:`~mamba.application.model.ModelManager` :param full: should be dumped full? :type full: bool """ references = [] sql = [ '--', '-- Mamba SQL dump {}'.format(version.short()), '--', '-- Database Backend: {}'.format(self.backend), '-- Host: {}\tDatabase: {}'.format(self.host, self.database) ] app = config.Application('config/application.json') try: sql += [ '-- Application: {}'.format(app.name.decode('utf-8')), '-- Application Version: {}'.format(app.version), '-- Application Description: {}'.format( app.description.encode('utf-8')) ] except AttributeError: pass sql += [ '-- ---------------------------------------------------------', '-- Dumped on: {}'.format(datetime.datetime.now().isoformat()), '--' ] if self.backend == 'mysql': sql += [ '-- Disable foreign key checks for table creation', '--', 'SET FOREIGN_KEY_CHECKS = 0;' ] if full is False: sql.append('') for model in model_manager.get_models().values(): if self.backend == 'postgres': references.append(model.get('object').dump_references()) sql += [model.get('object').dump_table() + '\n'] else: for model in model_manager.get_models().values(): model_object = model.get('object') sql.append('--') sql.append('-- Table structure for table {}'.format( model_object.__storm_table__)) sql.append('--\n') sql.append(model_object.dump_table()) sql.append('--') sql.append('-- Dumping data for table {}'.format( model_object.__storm_table__)) sql.append('--\n') sql.append(model_object.dump_data()) if self.backend == 'postgres': references.append(model_object.dump_references()) if self.backend == 'mysql': sql += [ '--', '-- Enable foreign key checks', '--', 'SET FOREIGN_KEY_CHECKS = 1;' ] for reference in references: sql.append(reference) return '\n'.join(sql) def reset(self, model_manager): """ Delete all the data in the database and return it to primitive state :param model_manager: the model manager from mamba application :type model_manager: :class:`~mamba.application.model.ModelManager` """ cfg = config.Database() cfg.create_table_behaviours['create_table_if_not_exists'] = False cfg.create_table_behaviours['drop_table'] = True sql = [ model.get('object').dump_table() for model in model_manager.get_models().values() ] return '\n'.join(sql) @property def backend(self): """Return the type of backend this databse is using """ return URI(config.Database().uri).scheme @property def host(self): """Return the hostname this database is using """ return URI(config.Database().uri).host @property def database(self): """Return the database name we are using """ return URI(config.Database().uri).database
def tearDown(self): config.Database('default')
def host(self): """Return the hostname this database is using """ return URI(config.Database().uri).host
def database(self): """Return the database name we are using """ return URI(config.Database().uri).database
def backend(self): """Return the type of backend this databse is using """ return URI(config.Database().uri).scheme
def test_get_uri(self): config.Database('clean') dummy = DummyModel() self.assertEqual(dummy.get_uri().scheme, URI('sqlite:').scheme)
def setUp(self): config.Database('../mamba/test/application/config/database.json') config.Database().uri = "sqlite:///db/dummy.db" self.database = Database(self.get_pool(), True)
class Database(object): """ Storm ORM database provider for Mamba. :param pool: the thrad pool for this database :type pool: :class:`twisted.python.threadpool.ThreadPool` """ monkey_patched = False pool = ThreadPool( config.Database().min_threads, config.Database().max_threads, 'DatabasePool' ) zstorm_configured = False transactor = Transactor(pool) def __init__(self, pool=None, testing=False): if pool is not None: self.pool = pool self.transactor = Transactor(pool) self.started = False self.__testing = testing if not self.zstorm_configured: provideUtility(global_zstorm, IZStorm) self._patch_sqlite_uris() self._set_zstorm_default_uris(getUtility(IZStorm)) SQLite.register() MySQL.register() PostgreSQL.register() @property def backend(self): """Return the type or types of backends this databse is using """ return self._parse_uri('scheme') @property def host(self): """Return the hostname or hostnames this database is using """ return self._parse_uri('host') @property def database(self): """Return the database name or names we are using """ return self._parse_uri('database') def start(self): """Starts the Database (and the threadpool) """ if self.started: return if self.__testing is True: self.pool.start() self.started = True def stop(self): """Stops the Database (and the threadpool) """ if not self.started: return self.started = False def adjust_poolsize(self, min_threads=None, max_threads=None): """ Adjusts the underlying threadpool size :param min: minimum number of threads :type min: int :param max: maximum number of threads :type max: int """ self.pool.adjustPoolsize(min_threads, max_threads) def store(self, database='mamba', ensure_connect=False): """ Returns a Store per-thread through :class:`storm.zope.zstorm.ZStorm` """ if not self.started: self.start() if ensure_connect is True: self._ensure_connect() zstorm = getUtility(IZStorm) return zstorm.get(database) def dump(self, model_manager, scheme=None, full=False): """ Dumps the full database :param model_manager: the model manager from mamba application :type model_manager: :class:`~mamba.application.model.ModelManager` :param scheme: dump which scheme? if None just everything :type scheme: str :param full: should be dumped full? :type full: bool """ references = [] indexes = [] backend, host, database = self._parse_config_scheme(scheme) sql = [ '--', '-- Mamba SQL dump {}'.format(version.short()), '--', '-- Database Backend: {}'.format(backend), '-- Host: {}\tDatabase: {}'.format(host, database) ] app = config.Application('config/application.json') try: sql += [ '-- Application: {}'.format(app.name.decode('utf-8')), '-- Application Version: {}'.format(app.version), '-- Application Description: {}'.format( app.description.encode('utf-8') ) ] except AttributeError: pass sql += [ '-- ---------------------------------------------------------', '-- Dumped on: {}'.format(datetime.datetime.now().isoformat()), '--' ] if self.backend == 'mysql': sql += [ '-- Disable foreign key checks for table creation', '--', 'SET FOREIGN_KEY_CHECKS = 0;' ] if full is False: self._dump_scheme(sql, references, indexes, model_manager, scheme) else: self._dump_data(sql, references, indexes, model_manager, scheme) if self.backend == 'mysql': sql += [ '--', '-- Enable foreign key checks', '--', 'SET FOREIGN_KEY_CHECKS = 1;' ] for reference in references: sql.append(reference) for index in indexes: sql.append(index) return '\n'.join(sql) def reset(self, model_manager, scheme=None): """ Delete all the data in the database and return it to primitive state :param model_manager: the model manager from mamba application :type model_manager: :class:`~mamba.application.model.ModelManager` :param scheme: the specific scheme to reset (if any) :type scheme: str """ sql = [] for model in model_manager.get_models().values(): if model.get('object').on_schema() is not True: continue if scheme is not None: if model.get('object').mamba_database() != scheme: continue sql.append( model.get('object').drop_table(script=True, async=False)) sql.append(model.get('object').dump_table())
from storm.database import URI from storm.zope.interfaces import IZStorm from storm.zope.zstorm import global_zstorm from twisted.python.threadpool import ThreadPool from zope.component import provideUtility, getUtility from storm.twisted.transact import Transactor, DisconnectionError from mamba import version from mamba.utils import config from mamba.enterprise.mysql import MySQL from mamba.enterprise.sqlite import SQLite from mamba.enterprise.common import CommonSQL from mamba.enterprise.postgres import PostgreSQL if (hasattr(config.Database(), 'storm_debug') and config.Database().storm_debug is True): from mamba.utils.logger import StormDebugLogFile StormDebugLogFile.start() class Database(object): """ Storm ORM database provider for Mamba. :param pool: the thrad pool for this database :type pool: :class:`twisted.python.threadpool.ThreadPool` """ monkey_patched = False pool = ThreadPool(