def last_block(ctx): """Return the highest block stored in the database""" engine = ctx.obj['engine'] database_url = ctx.obj['database_url'] metadata = ctx.obj['metadata'] # init tables first init_tables(database_url, metadata) # configure session Session.configure(bind=engine) session = Session() click.echo(Block.highest_block(session))
def insert_blocks(ctx, blocks): """Insert blocks into the database""" engine = ctx.obj['engine'] database_url = ctx.obj['database_url'] metadata = ctx.obj['metadata'] # init tables first init_tables(database_url, metadata) # configure session Session.configure(bind=engine) session = Session() add_blocks( blocks, session, insert=True, merge_insert=False, insert_many=False)
def find_missing_blocks(ctx, url): """Return JSON array of block_nums from missing blocks""" from sbds.storages.db.tables import Block engine = ctx.obj['engine'] database_url = ctx.obj['database_url'] metadata = ctx.obj['metadata'] rpc = SimpleSteemAPIClient(url) # init tables first init_tables(database_url, metadata) # configure session Session.configure(bind=engine) session = Session() last_chain_block = rpc.last_irreversible_block_num() click.echo( json.dumps( Block.find_missing(session, last_chain_block=last_chain_block)))
def bulk_add_blocks(ctx, blocks, chunksize): """Insert many blocks in the database""" engine = ctx.obj['engine'] database_url = ctx.obj['database_url'] metadata = ctx.obj['metadata'] # init tables first init_tables(database_url, metadata) # configure session Session.configure(bind=engine) session = Session() click.echo("SQL: 'SET SESSION innodb_lock_wait_timeout=150'", err=True) session.execute('SET SESSION innodb_lock_wait_timeout=150') try: for chunk in chunkify(blocks, chunksize): bulk_add(chunk, session) except Exception as e: raise e finally: session.close_all()
def get_db_plugin(database_url): engine_config = configure_engine(database_url) Session.configure(bind=engine_config.engine) # pylint: disable=undefined-variable return sqlalchemy.Plugin( # SQLAlchemy engine created with create_engine function. engine_config.engine, # SQLAlchemy metadata, required only if create=True. Base.metadata, # Keyword used to inject session database in a route (default 'db'). keyword='db', # If it is true, execute `metadata.create_all(engine)` when plugin is applied (default False). create=True, # If it is true, plugin commit changes after route is executed (default True). commit=False, # If it is true and keyword is not defined, plugin uses **kwargs argument to inject session database (default False). use_kwargs=False, create_session=Session)