async def initialize(self): """ create root object if necessary """ request = make_mocked_request('POST', '/') request._db_write_enabled = True tm = request._tm = self.get_transaction_manager() txn = await tm.begin() try: await txn._strategy.retrieve_tid() root = await tm._storage.load(txn, ROOT_ID) if root is not None: root = reader(root) root._p_jar = txn if root.__db_id__ is None: root.__db_id__ = self._database_name await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) except KeyError: from guillotina.db.db import Root root = Root(self._database_name) await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) finally: await tm.commit(txn=txn)
async def _test_pg_txn(postgres, guillotina_main): """Test a low level transaction""" dsn = "postgres://postgres:@localhost:5432/guillotina" partition_object = "guillotina.db.interfaces.IPartition" aps = APgStorage(dsn=dsn, partition=partition_object, name='db') await aps.initialize() conn = await aps.open() obj = Root() writer = IWriter(obj) tm = TransactionManager(DummyStorage()) txn = Transaction(tm) await aps.tpc_begin(txn, conn) await aps.precommit(txn) await aps.store(ROOT_ID, 0, writer, obj, txn) await aps.tpc_vote(txn) await aps.tpc_finish(txn) await aps.close(conn) tm = TransactionManager(DummyStorage()) txn = Transaction(tm) await aps.tpc_begin(txn, conn) result = await aps.load(txn, ROOT_ID) await aps.abort(txn) await aps.close(txn._db_conn) obj2 = reader(result) assert obj.__name__ == obj2.__name__ await cleanup(aps)
async def initialize(self): """ create root object if necessary """ tm = self.get_transaction_manager() txn = await tm.begin() try: await txn._strategy.retrieve_tid() root = await tm._storage.load(txn, ROOT_ID) if root is not None: root = app_settings['object_reader'](root) root.__txn__ = txn if root.__db_id__ is None: root.__db_id__ = self._database_name await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) except KeyError: from guillotina.db.db import Root root = Root(self._database_name) await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn) finally: await tm.commit(txn=txn)