def startTransaction(self): from config.database import DB DB.begin_transaction() self.__class__._transaction = True
def stopTransaction(self): from config.database import DB DB.rollback() self.__class__._transaction = False
def staticStopTransaction(cls): from config.database import DB DB.rollback() cls._transaction = False
def __init__( self, grammar=None, connection="default", connection_class=None, table=None, connection_details=None, connection_driver="default", model=None, scopes=None, dry=False, ): """QueryBuilder initializer Arguments: grammar {masonite.orm.grammar.Grammar} -- A grammar class. Keyword Arguments: connection {masonite.orm.connection.Connection} -- A connection class (default: {None}) table {str} -- the name of the table (default: {""}) """ self.grammar = grammar self._table = table self.dry = dry self.connection = connection self.connection_class = connection_class self._connection = None self._connection_details = connection_details or {} self._connection_driver = connection_driver self._scopes = scopes or {} self._eager_relation = EagerRelations() if model: self._global_scopes = model._global_scopes if model.__with__: self.with_(model.__with__) else: self._global_scopes = {} self.builder = self self._columns = () self._creates = {} self._sql = "" self._sql_binding = "" self._bindings = () self._updates = () self._wheres = () self._order_by = () self._group_by = () self._joins = () self._having = () self._macros = {} self._aggregates = () self._limit = False self._offset = False self._model = model self.set_action("select") if not self._connection_details: from config.database import DB self._connection_details = DB.get_connection_details() self.on(connection) if grammar: self.grammar = grammar if connection_class: self.connection_class = connection_class
def test_transaction_globally(self): connection = DB.begin_transaction("dev") self.assertEqual(connection, self.get_builder().new_connection()) DB.commit("dev") DB.begin_transaction("dev") DB.rollback("dev")
def show_user(self, view: View): conn = DB.get_schema_manager().list_table_columns('users') return view.render('{0}/management/add_user'.format(self.template_prefix), {'schema': conn})
def consume(self, channel, **options): # skipcq from config.database import DB as schema, DATABASES from wsgi import container if not channel or channel == 'default': channel = DATABASES['default'] self.info( '[*] Waiting to process jobs from the "queue_jobs" table on the "{}" connection. To exit press CTRL + C' .format(channel)) schema = schema.connection(channel) while True: jobs = schema.table('queue_jobs').where('ran_at', None).get() if not jobs.count(): time.sleep(5) for job in jobs: unserialized = pickle.loads(job.serialized) obj = unserialized['obj'] args = unserialized['args'] callback = unserialized['callback'] ran = job.attempts wait_time = job['wait_until'] if not job['wait_until']: wait_time = pendulum.now() else: if isinstance(wait_time, str): wait_time = pendulum.parse(job['wait_until']) else: wait_time = pendulum.instance(job['wait_until']) # print(job['wait_until'], wait_time.is_future()) if job['wait_until'] and wait_time.is_future(): continue try: try: if inspect.isclass(obj): obj = container.resolve(obj) getattr(obj, callback)(*args) except AttributeError: obj(*args) try: # attempts = 1 schema.table('queue_jobs').where( 'id', job['id']).update({ 'ran_at': pendulum.now().to_datetime_string(), 'attempts': job['attempts'] + 1, }) self.success('[\u2713] Job Successfully Processed') except UnicodeEncodeError: self.success('[Y] Job Successfully Processed') except Exception as e: # skipcq self.danger('Job Failed: {}'.format(str(e))) if not obj.run_again_on_fail: # ch.basic_ack(delivery_tag=method.delivery_tag) schema.table('queue_jobs').where( 'id', job['id']).update({ 'ran_at': pendulum.now().to_datetime_string(), 'failed': 1, 'attempts': job['attempts'] + 1, }) if ran < obj.run_times and isinstance(obj, Queueable): time.sleep(1) schema.table('queue_jobs').where( 'id', job['id']).update({ 'attempts': job['attempts'] + 1, }) continue else: schema.table('queue_jobs').where( 'id', job['id']).update({ 'attempts': job['attempts'] + 1, 'ran_at': pendulum.now().to_datetime_string(), 'failed': 1, }) if hasattr(obj, 'failed'): getattr(obj, 'failed')(unserialized, str(e)) self.add_to_failed_queue_table(unserialized, driver='database') time.sleep(5)