Пример #1
0
def create_core(args):
    schema = args.name
    config = args.config and load(args.config) or {}
    db_config = Database()
    LazyEntityMeta.attach(db_config, database='CGRdb_config')
    db_config.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base,
                   port=args.port)
    db_config.generate_mapping()

    db = Database()
    LazyEntityMeta.attach(db, schema, 'CGRdb')
    db.bind('postgres', user=args.user, password=args.password, host=args.host, database=args.base, port=args.port)
    db.generate_mapping(create_tables=True)

    with db_session:
        db.execute('CREATE EXTENSION IF NOT EXISTS smlar')
        db.execute('CREATE EXTENSION IF NOT EXISTS intarray')
        db.execute('CREATE EXTENSION IF NOT EXISTS pg_cron')

    with db_session:
        db.execute(f'CREATE INDEX idx_smlar_molecule_structure ON "{schema}"."MoleculeStructure" USING '
                   'GIST (bit_array _int4_sml_ops)')
        db.execute(f'CREATE INDEX idx_smlar_reaction_index ON "{schema}"."ReactionIndex" USING '
                   'GIST (bit_array _int4_sml_ops)')
        db.execute(f'CREATE INDEX idx_subst_molecule_structure ON "{schema}"."MoleculeStructure" USING '
                   'GIN (bit_array gin__int_ops)')
        db.execute(f'CREATE INDEX idx_subst_reaction_index ON "{schema}"."ReactionIndex" USING '
                   'GIN (bit_array gin__int_ops)')

        db.execute(f"SELECT cron.schedule('0 3 * * *', $$$$\n"
                   f'DELETE FROM "{schema}"."MoleculeSearchCache"'
                   " WHERE date < CURRENT_TIMESTAMP - INTERVAL '1 day' $$$$)")
        db.execute(f"SELECT cron.schedule('0 3 * * *', $$$$\n"
                   f'DELETE FROM "{schema}"."ReactionSearchCache"'
                   " WHERE date < CURRENT_TIMESTAMP - INTERVAL '1 day' $$$$)")

    with db_session:
        db_config.Config(name=schema, config=config, version=major_version)
Пример #2
0
def create_core(args):
    major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1])
    schema = args.name
    config = args.config and load(args.config) or {}
    if 'packages' not in config:
        config['packages'] = []
    for p in config['packages']:
        try:
            p = get_distribution(p)
            import_module(p.project_name)
        except (DistributionNotFound, VersionConflict):
            raise ImportError(
                f'packages not installed or has invalid versions: {p}')

    db_config = Database()
    LazyEntityMeta.attach(db_config, database='CGRdb_config')
    db_config.bind('postgres',
                   user=args.user,
                   password=args.password,
                   host=args.host,
                   database=args.base,
                   port=args.port)
    db_config.generate_mapping()

    with db_session:
        if db_config.Config.exists(name=schema):
            raise KeyError('schema already exists')

    db = Database()
    LazyEntityMeta.attach(db, schema, 'CGRdb')
    db.bind('postgres',
            user=args.user,
            password=args.password,
            host=args.host,
            database=args.base,
            port=args.port)
    db.generate_mapping(create_tables=True)

    with db_session:
        db.execute(f'ALTER TABLE "{schema}"."Reaction" DROP COLUMN structure')
        db.execute(
            f'ALTER TABLE "{schema}"."Reaction" RENAME TO "ReactionRecord"')
        db.execute(
            f'CREATE VIEW "{schema}"."Reaction" AS SELECT id, NULL::bytea as structure '
            f'FROM "{schema}"."ReactionRecord"')

        db.execute(init_session.replace('{schema}', schema))
        db.execute(merge_molecules.replace('{schema}', schema))

        db.execute(insert_molecule.replace('{schema}', schema))
        db.execute(insert_molecule_trigger.replace('{schema}', schema))
        db.execute(after_insert_molecule.replace('{schema}', schema))
        db.execute(after_insert_molecule_trigger.replace('{schema}', schema))
        db.execute(delete_molecule.replace('{schema}', schema))
        db.execute(delete_molecule_trigger.replace('{schema}', schema))

        db.execute(insert_reaction.replace('{schema}', schema))
        db.execute(insert_reaction_trigger.replace('{schema}', schema))

        db.execute(search_similar_molecules.replace('{schema}', schema))
        db.execute(search_substructure_molecule.replace('{schema}', schema))
        db.execute(search_similar_reactions.replace('{schema}', schema))
        db.execute(search_substructure_reaction.replace('{schema}', schema))
        db.execute(
            search_substructure_fingerprint_molecule.replace(
                '{schema}', schema))
        db.execute(
            search_similar_fingerprint_molecule.replace('{schema}', schema))
        db.execute(search_reactions_by_molecule.replace('{schema}', schema))
        db.execute(search_mappingless_reaction.replace('{schema}', schema))

    if args.indexed:
        with db_session:
            db.execute(
                f'CREATE INDEX idx_moleculestructure__smlar ON "{schema}"."MoleculeStructure" USING '
                'GIST (fingerprint _int4_sml_ops)')
            db.execute(
                f'CREATE INDEX idx_moleculestructure__subst ON "{schema}"."MoleculeStructure" USING '
                'GIN (fingerprint gin__int_ops)')
            db.execute(
                f'CREATE INDEX idx_reactionindex__smlar ON "{schema}"."ReactionIndex" USING '
                'GIST (fingerprint _int4_sml_ops)')
            db.execute(
                f'CREATE INDEX idx_reactionindex__subst ON "{schema}"."ReactionIndex" USING '
                'GIN (fingerprint gin__int_ops)')

    with db_session:
        db_config.Config(name=schema, config=config, version=major_version)
Пример #3
0
def create_core(args):
    major_version = '.'.join(get_distribution('CGRdb').version.split('.')[:-1])
    schema = args.name
    config = args.config and load(args.config) or {}
    if 'packages' not in config:
        config['packages'] = []
    for p in config['packages']:
        try:
            p = get_distribution(p)
            import_module(p.project_name)
        except (DistributionNotFound, VersionConflict):
            raise ImportError(
                f'packages not installed or has invalid versions: {p}')

    db_config = Database()
    LazyEntityMeta.attach(db_config, database='CGRdb_config')
    db_config.bind('postgres', **args.connection)
    db_config.generate_mapping()

    with db_session:
        if db_config.Config.exists(name=schema):
            raise KeyError('schema already exists')
    with db_session:
        db_config.execute(f'DROP SCHEMA IF EXISTS {schema} CASCADE')
        db_config.execute(f'CREATE SCHEMA {schema}')

    db = Database()
    LazyEntityMeta.attach(db, schema, 'CGRdb')
    db.bind('postgres', **args.connection)
    db.generate_mapping(create_tables=True)

    with db_session:
        db.execute(f'ALTER TABLE "{schema}"."Reaction" DROP COLUMN structure')
        db.execute(
            f'ALTER TABLE "{schema}"."Reaction" RENAME TO "ReactionRecord"')
        db.execute(
            f'CREATE VIEW "{schema}"."Reaction" AS SELECT id, NULL::bytea as structure '
            f'FROM "{schema}"."ReactionRecord"')

    with db_session:
        db.execute(init_session.replace('{schema}', schema))

        db.execute(insert_molecule.replace('{schema}', schema))
        db.execute(after_insert_molecule.replace('{schema}', schema))
        db.execute(delete_molecule.replace('{schema}', schema))
        db.execute(insert_reaction.replace('{schema}', schema))
        db.execute(merge_molecules.replace('{schema}', schema))

        db.execute(insert_molecule_trigger.replace('{schema}', schema))
        db.execute(after_insert_molecule_trigger.replace('{schema}', schema))
        db.execute(delete_molecule_trigger.replace('{schema}', schema))
        db.execute(insert_reaction_trigger.replace('{schema}', schema))

        db.execute(search_structure_molecule.replace('{schema}', schema))
        db.execute(search_structure_reaction.replace('{schema}', schema))
        db.execute(search_similar_molecules.replace('{schema}', schema))
        db.execute(search_substructure_molecule.replace('{schema}', schema))
        db.execute(search_similar_reactions.replace('{schema}', schema))
        db.execute(search_substructure_reaction.replace('{schema}', schema))
        db.execute(search_reactions_by_molecule.replace('{schema}', schema))
        db.execute(search_mappingless_reaction.replace('{schema}', schema))

    with db_session:
        db_config.Config(name=schema, config=config, version=major_version)