async def initialize(self, app=None): from guillotina import app_settings if not app_settings["store_json"]: return root = get_utility(IApplication, name="root") for _id, db in root: if not IDatabase.providedBy(db): continue tm = db.get_transaction_manager() if not IPostgresStorage.providedBy(tm.storage): continue try: async with tm.storage.pool.acquire() as conn: for func in PG_FUNCTIONS: await conn.execute(func) for index in [BasicJsonIndex("container_id") ] + [v for v in get_pg_indexes().values()]: sqls = index.get_index_sql(tm.storage) for sql in sqls: logger.debug(f"Creating index:\n {sql}") await conn.execute(sql) except asyncpg.exceptions.ConnectionDoesNotExistError: # pragma: no cover # closed before it could be setup pass except AttributeError as ex: # pragma: no cover if "'reset'" in str(ex): # ignore error removing from pool if already closed return raise
async def _clear_dbs(root): # make sure to completely clear db before carrying on... for _, db in root: if not IDatabase.providedBy(db): continue storage = db.storage if IPostgresStorage.providedBy(storage) or ICockroachStorage.providedBy(storage): async with storage.pool.acquire() as conn: await conn.execute( """ DELETE from {} WHERE zoid != '{}' AND zoid != '{}' """.format( storage._objects_table_name, ROOT_ID, TRASHED_ID ) ) await conn.execute( """ SELECT 'DROP INDEX ' || string_agg(indexrelid::regclass::text, ', ') FROM pg_index i LEFT JOIN pg_depend d ON d.objid = i.indexrelid AND d.deptype = 'i' WHERE i.indrelid = '{}'::regclass AND d.objid IS NULL """.format( storage._objects_table_name ) )
def get_storage(): txn = get_transaction() storage = txn.manager._storage if not IPostgresStorage.providedBy(storage): # would already get big warning log about this logger.debug("Storage does not support link integrity") return None return storage
async def migrate_contraint(db, conn=None): storage = db.storage if not IPostgresStorage.providedBy(storage): return # only for pg if conn is not None: await _migrate_constraint(storage, conn) else: async with storage.pool.acquire() as conn: await _migrate_constraint(storage, conn)
async def _clear_dbs(root): # make sure to completely clear db before carrying on... async for db in iter_databases(root): storage = db.storage if IPostgresStorage.providedBy(storage) or ICockroachStorage.providedBy(storage): async with storage.pool.acquire() as conn: await conn.execute(''' DELETE from {} WHERE zoid != '{}' AND zoid != '{}' '''.format(storage._objects_table_name, ROOT_ID, TRASHED_ID))
async def _clear_dbs(root): # make sure to completely clear db before carrying on... async for db in iter_databases(root): storage = db.storage if IPostgresStorage.providedBy( storage) or ICockroachStorage.providedBy(storage): async with storage.pool.acquire() as conn: await conn.execute(''' DELETE from {} WHERE zoid != '{}' AND zoid != '{}' '''.format(storage._objects_table_name, ROOT_ID, TRASHED_ID))
async def db_initialized(event): ''' Initialize additional pg indexes ''' storage = event.database.storage if not IPostgresStorage.providedBy(storage) or ICockroachStorage.providedBy(storage): return # create json data indexes async with storage.lock: for statement in statements: await storage.read_conn.execute( statement.format(storage._objects_table_name))
async def initialize(event): storage = event.database.storage if not IPostgresStorage.providedBy(storage): logger.error( 'Link integrity support only available for ' 'postgresql and cockroachdb') return statements = [ get_table_definition( 'aliases', _aliases_schema, primary_keys=('zoid', 'path')), get_table_definition( 'links', _links_schema, primary_keys=('source_id', 'target_id')) ] statements.extend(_initialize_statements) for statement in statements: async with storage.pool.acquire() as conn: await conn.execute( statement.format(storage._objects_table_name) )
async def initialize(self): from guillotina import app_settings if not app_settings['store_json']: return root = get_utility(IApplication, name='root') for _id, db in root: if not IDatabase.providedBy(db): continue tm = db.get_transaction_manager() if not IPostgresStorage.providedBy(tm.storage): continue async with tm.storage.pool.acquire() as conn: for func in PG_FUNCTIONS: await conn.execute(func) for index in [BasicJsonIndex('container_id') ] + [v for v in get_pg_indexes().values()]: sqls = index.get_index_sql(tm.storage) for sql in sqls: logger.debug(f'Creating index:\n {sql}') await conn.execute(sql)
async def migrate_contraint(db): storage = db.storage if not IPostgresStorage.providedBy(storage): return # only for pg table_name = clear_table_name(storage._objects_table_name) result = await storage.read_conn.fetch(''' SELECT * FROM pg_indexes WHERE tablename = '{}' AND indexname = '{}_parent_id_id_key'; '''.format(table_name, table_name)) if len(result) > 0: # check if we need to drop and create new constraint if TRASHED_ID not in result[0]['indexdef']: await storage.read_conn.execute(''' ALTER TABLE {} DROP CONSTRAINT {}_parent_id_id_key; '''.format(storage._objects_table_name, table_name)) await storage.read_conn.execute(storage._unique_constraint.format( objects_table_name=storage._objects_table_name, constraint_name=table_name, TRASHED_ID=TRASHED_ID ))
async def migrate_contraint(db): storage = db.storage if not IPostgresStorage.providedBy(storage): return # only for pg table_name = clear_table_name(storage._objects_table_name) result = await storage.read_conn.fetch(''' SELECT * FROM pg_indexes WHERE tablename = '{}' AND indexname = '{}_parent_id_id_key'; '''.format(table_name, table_name)) if len(result) > 0: # check if we need to drop and create new constraint if TRASHED_ID not in result[0]['indexdef']: await storage.read_conn.execute(''' ALTER TABLE {} DROP CONSTRAINT {}_parent_id_id_key; '''.format(storage._objects_table_name, table_name)) await storage.read_conn.execute( storage._unique_constraint.format( objects_table_name=storage._objects_table_name, constraint_name=table_name, TRASHED_ID=TRASHED_ID))
def supports_ordering(storage): if not app_settings.get("store_json", False): return False return IPostgresStorage.providedBy( storage) and not ICockroachStorage.providedBy(storage)