def main(*args, **kw): opts = parse_cli(args, kw) if opts.debug: log.setLevel(logging.DEBUG) db = DbFactory(verbose=opts.verbose) assert db, "No db_factory in build_db" Base.metadata.bind = db.engine if opts.verbose: db.meta.bind.echo = True if opts.delete_db == True: log.debug('Dropping database') db.drop_all_tables_and_sequences(no_confirm=True) if opts.populate: s = db.Session() assert s, "No Session in build_db.py populate" # Need to call this explicitely to make the __extra_table_args__ hack work configure_mappers() # Create all tables upfront Base.metadata.create_all(checkfirst=True) if opts.populate: load_from_file(s, opts.populate) env = os.environ.copy() env['AQDCONF'] = config.baseconfig rc = subprocess.call([os.path.join(BINDIR, 'add_admin.py')], env=env, stdout=1, stderr=2) if rc != 0: log.warn("Failed to add current user as administrator.") # CONSTRAINTS if db.engine.dialect.name == 'oracle': #TODO: rename should be able to dump DDL to a file log.debug('renaming constraints...') cnst.rename_non_null_check_constraints(db) log.info('database built and populated')
def main(*args, **kw): opts = parse_cli(args, kw) if opts.debug: log.setLevel(logging.DEBUG) db = DbFactory(verbose=opts.verbose) assert db, "No db_factory in build_db" Base.metadata.bind = db.engine if opts.verbose: db.meta.bind.echo = True if opts.delete_db == True: log.debug('Dropping database') db.drop_all_tables_and_sequences(no_confirm=True) if opts.populate: s = db.Session() assert s, "No Session in build_db.py populate" # Need to call this explicitely to make the __extra_table_args__ hack work configure_mappers() # Create all tables upfront Base.metadata.create_all(checkfirst=True) if opts.populate: load_from_file(s, opts.populate) env = os.environ.copy() env['AQDCONF'] = config.baseconfig rc = subprocess.call([os.path.join(BINDIR, 'add_admin.py')], env=env, stdout=1, stderr=2) if rc != 0: log.warn("Failed to add current user as administrator.") # CONSTRAINTS if db.engine.dialect.name == 'oracle': #TODO: rename should be able to dump DDL to a file log.debug('renaming constraints...') cnst.rename_non_null_check_constraints(db) log.info('database built and populated')
buildings = dsdb.run_query(dsdb_select) for row in buildings.fetchall(): try: dbf.engine.execute(upd.format(row[1], row[0])) except Exception, e: print e #wipe redundant comments fields upd = "update location set comments = NULL where location_type='building'" try: dbf.engine.execute(upd) except Exception, e: print e #what about redundant fullname fields? pull the 5 character codes from FBI? #or LDAP names? null_addrs = """ SELECT L.NAME, B.ADDRESS FROM LOCATION L, BUILDING B WHERE L.ID = B.ID AND B.ADDRESS IS NULL""".lstrip() no_addrs = dbf.engine.execute(null_addrs).fetchall() if not no_addrs: nonnull = "ALTER TABLE building MODIFY(address VARCHAR(255) NOT NULL)" dbf.engine.execute(nonnull) rename_non_null_check_constraints(dbf) else: #### If there are NULL addresses, they've probably been deleted in dsdb msg = "The following buildings have no address:\n%s" % no_addrs raise ValueError(msg)
buildings = dsdb.run_query(dsdb_select) for row in buildings.fetchall(): try: dbf.engine.execute(upd.format(row[1], row[0])) except Exception, e: print e #wipe redundant comments fields upd = "update location set comments = NULL where location_type='building'" try: dbf.engine.execute(upd) except Exception, e: print e #what about redundant fullname fields? pull the 5 character codes from FBI? #or LDAP names? null_addrs = """ SELECT L.NAME, B.ADDRESS FROM LOCATION L, BUILDING B WHERE L.ID = B.ID AND B.ADDRESS IS NULL""".lstrip() no_addrs = dbf.engine.execute(null_addrs).fetchall() if not no_addrs: nonnull="ALTER TABLE building MODIFY(address VARCHAR(255) NOT NULL)" dbf.engine.execute(nonnull) rename_non_null_check_constraints(dbf) else: #### If there are NULL addresses, they've probably been deleted in dsdb msg = "The following buildings have no address:\n%s" % no_addrs raise ValueError(msg)