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 _prepare_model_db(self): try: mamba_services = commons.import_services() except Exception: mamba_services_not_found() # load database configuration mamba_services.config.Database('config/database.json') # this is needed to don't have a threadpool waiting forever db = database.Database(DummyThreadPool()) Model.database = db return mamba_services, db