def _handle_reset_command(self): """Take care of database reset """ mamba_services, db = self._prepare_model_db() if not self.options.subOptions.opts['noquestions']: question = ( 'This operation will {delete} all the data in your database.\n' 'Are you really sure this is what you want to do?'.format( delete=darkred('DELETE') ) ) if commons.Interaction.userquery(question) == 'No': sys.exit(0) reset_data = db.reset(ModelManager()) # PySQLite does not allow us to perform more than one operation if db.backend == 'sqlite': for query in reset_data.split(';'): db.store().execute(query) else: db.store().execute(reset_data) db.store().commit() print('All the data in your database has been reset.') sys.exit(0)
def register_packages(self): """Register packages found in the config file. Styles and Scripts are a bit special and we register the main application ones here so we can refer all of them from the :class:`~mamba.core.resource.Resource` subclasses and they will be auto imported in our templates """ log.msg('Registering packages...') for package, data in self.config.packages.iteritems(): log.msg('registring package {}'.format(package)) try: module = __import__(package, globals(), locals()) except ImportError: log.err('{} is not installed on the system'.format(package)) continue path = os.path.dirname(os.path.normpath(module.__file__)) self.packages[package] = {'path': path} self.packages[package].update( {'model': ModelManager('{}/model'.format(path), package)}) if data.get('autoimport', False): self.packages[package].update({ 'controller': ControllerManager('{}/controller'.format(path), package) })
def get_commons_for_dump(self): import sys sys.path.append('../mamba/test/dummy_app') mgr = ModelManager() if GNU_LINUX: self.addCleanup(mgr.notifier.loseConnection) from mamba import Model from mamba.test.test_model import DummyThreadPool, DatabaseModuleError try: threadpool = DummyThreadPool() database = Database(threadpool, True) Model.database = database store = database.store() store.execute( 'CREATE TABLE IF NOT EXISTS `dummy` (' ' id INTEGER PRIMARY KEY, name TEXT' ')' ) store.execute( 'CREATE TABLE IF NOT EXISTS `stubing` (' ' id INTEGER PRIMARY KEY, name TEXT' ')' ) store.commit() except DatabaseModuleError as error: raise unittest.SkipTest(error) return mgr
def drop_testing_tables(self, store=None): """Drop testing tables from the configured database (if any) :param store: an optional store to being used :type store: :class:`mamba.enterprise.Store` """ manager = ModelManager() store = self._valid_store(store) for model in [m['object'] for m in manager.get_models().values()]: store.execute(model.get_adapter().drop_table().replace( model.__storm_table__, '_mamba_test_{}'.format(model.__storm_table__)) ) store.commit()
def _handle_create_command(self, mgr=None): """Take care of SQL creation scripts using the application model """ mamba_services, db = self._prepare_model_db() # headers and footer mamba_services.config.Application('config/application.json') stdout = sys.stdout capture = StringIO() sys.stdout = capture # generate script if mgr is None: print(db.dump(ModelManager())) else: print(db.dump(mgr)) sys.stdout = stdout if self.options.subOptions.opts['file'] is not None: with open(self.options.subOptions.opts['file'], 'w') as dump_file: dump_file.write(capture.getvalue()) if self.options.subOptions.opts['dump']: print(capture.getvalue()) if self.options.subOptions.opts['live']: if mamba_services.config.Database().create_table_behaviours.get( 'drop_table' ) is True: pattern = re.compile(r'((?<=:)[\w\d\$]+(?=@))') question = ( 'You have `DROP TABLE` option set as True in your ' 'creation tables configuration behaviour, this means ' 'Mamba will perform a table drop before create any table ' 'so you can lose important data if the table exists and ' 'this is a production system\n\nAre you sure do you want ' 'to run this query in the {} database?' ).format( pattern.sub('*****', mamba_services.config.Database().uri) ) if commons.Interaction.userquery(question) == 'No': sys.exit(0) real_database = database.Database() store = real_database.store() if real_database.backend == 'sqlite': # the pysqlite module does not allow us to use more # than one operations per query for operation in capture.getvalue().split(';'): store.execute(operation) else: store.execute(capture.getvalue()) sys.exit(0)
def _handle_dump_command(self, mgr=None): """Take care of SQL dumping """ db = self._prepare_model_db()[1] stdout = sys.stdout capture = StringIO() sys.stdout = capture if mgr is None: print(db.dump(ModelManager(), None, True)) else: print(db.dump(mgr, None, True)) sys.stdout = stdout if self.options.subOptions.opts['file'] is None: print(capture.getvalue()) else: with open(self.options.subOptions.opts['file'], 'w') as dump_file: dump_file.write(capture.getvalue()) sys.exit(0)