def create_db(): """Creates database if it doesn't exist.""" db_uri = app.config['SQLALCHEMY_DATABASE_URI'] if not database_exists(db_uri): print('Creating database ...') create_database(db_uri) db.create_all() else: print('Database already exists. Nothing to create.')
def initdb(ctx, purge): """Initialize the database and all tables.""" if purge: ctx.invoke(drop) echo('Initializing database...', nl=False) db.create_all() echo('done')
def initdb(ctx, purge): """Initialize the database and all tables.""" if purge: ctx.invoke(drop) echo('Initializing database...', nl=False) db_exists = exists(join(basedir, 'tracker.db')) db.create_all() if not db_exists: stamp() echo('done')
def bootstrap(ctx, purge=False): """Bootstrap the environment. Create all folders, database tables and other things that are required to run the application. An initial administrator user must be created separately.""" from tracker import db def mkdir(path): Path(path).mkdir(parents=True, exist_ok=True) echo('Creating folders...', nl=False) mkdir(join(basedir, 'pacman/cache')) mkdir(join(basedir, 'pacman/log')) mkdir(join(basedir, 'pacman/arch/x86_64/db')) echo('done') # Auto rename old database for compatibility db_old = join(basedir, 'app.db') db_new = join(basedir, 'tracker.db') if exists(db_old) and not exists(db_new): echo('Renaming old database file...', nl=False) rename(db_old, db_new) echo('done') if purge: echo('Purging the database...', nl=False) db.drop_all() echo('done') ctx.invoke(vacuum) echo('Initializing the database...', nl=False) db.create_all() echo('done')
def db_create(): app.config.from_object(BaseConfig) db.create_all()
def upgrade(): # ensure new transaction/version tables exist db.create_all() # update CVE table op.add_column( 'cve', Column('created', DateTime, default=datetime.utcnow, nullable=True, index=True)) op.add_column( 'cve', Column('changed', DateTime, default=datetime.utcnow, nullable=True, index=True)) for cve in CVE.query.all(): cve.created = datetime.utcnow() cve.changed = cve.created db.session.commit() db.session.flush() # update AVG table op.add_column( 'cve_group', Column('changed', DateTime, default=datetime.utcnow, nullable=True, index=True)) for group in CVEGroup.query.all(): group.changed = group.created db.session.commit() db.session.flush() VersionClassGroup = version_class(CVEGroup) uow = versioning_manager.unit_of_work(db.session) uow.create_transaction(db.session) for group in VersionClassGroup.query.all(): for package in CVEGroupPackage.query.filter( CVEGroupPackage.group_id == group.id).all(): package_version = uow.get_or_create_version_object(package) package_version.group_id = group.id package_version.pkgname = package.pkgname package_version.transaction_id = group.transaction_id package_version.end_transaction_id = group.end_transaction_id package_version.operation_type = Operation.INSERT package_version.group_id_mod = 1 package_version.pkgname_mod = 1 uow.process_operation(Operation(package, Operation.INSERT)) for cve in CVEGroupEntry.query.filter( CVEGroupEntry.group_id == group.id).all(): cve_version = uow.get_or_create_version_object(cve) cve_version.group_id = group.id cve_version.cve_id = cve.cve_id cve_version.transaction_id = group.transaction_id cve_version.end_transaction_id = group.end_transaction_id cve_version.operation_type = Operation.INSERT cve_version.group_id_mod = 1 cve_version.cve_id_mod = 1 uow.process_operation(Operation(cve, Operation.INSERT)) uow.make_versions(db.session) db.session.commit() db.session.flush() with op.batch_alter_table('cve_group', schema=None) as batch_op: batch_op.alter_column('changed', nullable=False) # update advisory table op.add_column( 'advisory', Column('changed', DateTime, default=datetime.utcnow, nullable=True, index=True)) for advisory in Advisory.query.all(): advisory.changed = group.created db.session.commit() db.session.flush() with op.batch_alter_table('advisory', schema=None) as batch_op: batch_op.alter_column('changed', nullable=False) # set all fields to modified for initial insert VersionClassCVE = version_class(CVE) VersionClassCVE.query.update({ VersionClassCVE.operation_type: Operation.INSERT, VersionClassCVE.issue_type_mod: 1, VersionClassCVE.description_mod: 1, VersionClassCVE.severity_mod: 1, VersionClassCVE.remote_mod: 1, VersionClassCVE.reference_mod: 1, VersionClassCVE.notes_mod: 1 }) VersionClassGroup = version_class(CVEGroup) VersionClassGroup.query.update({ VersionClassGroup.operation_type: Operation.INSERT, VersionClassGroup.status_mod: 1, VersionClassGroup.severity_mod: 1, VersionClassGroup.affected_mod: 1, VersionClassGroup.fixed_mod: 1, VersionClassGroup.bug_ticket_mod: 1, VersionClassGroup.reference_mod: 1, VersionClassGroup.notes_mod: 1, VersionClassGroup.created_mod: 1, VersionClassGroup.changed_mod: 1, VersionClassGroup.advisory_qualified_mod: 1 }) VersionClassAdvisory = version_class(Advisory) VersionClassAdvisory.query.update({ VersionClassAdvisory.operation_type: Operation.INSERT, VersionClassAdvisory.group_package_id_mod: 1, VersionClassAdvisory.advisory_type_mod: 1, VersionClassAdvisory.publication_mod: 1, VersionClassAdvisory.workaround_mod: 1, VersionClassAdvisory.impact_mod: 1, VersionClassAdvisory.content_mod: 1, VersionClassAdvisory.created_mod: 1, VersionClassAdvisory.changed_mod: 1, VersionClassAdvisory.reference_mod: 1 }) db.session.commit()