args = parser.parse_args() configure_logging() with session_scope() as session: nb_cities = session.query(City).count() if args.force_clear_cities and not args.clear_cities: print('You must specify the --clear-cities argument if you want to' ' use the --force-clear-cities argument') if nb_cities > 0: if args.clear_cities: # Here we are erasing already existing cities before inserting new # ones. if args.force_clear_cities or are_you_sure( 'Are you sure you want to erase all the existing cities?'): with session_scope() as session: logger.info('deleting cities') session.query(MonthlyStat).delete() session.query(City).delete() session.commit() else: print('Did nothing.') sys.exit() else: # Here we are simply appending the new cities. You want to be sure # that this won't conflict in some way... if not are_you_sure( 'The database is not empty, there are already {} cities,' ' do you still wish to pursue with loading data? If you' ' want to clear already existing cities, use the'
' Only fills with the stat descriptions') parser.add_argument('--force-drop', action='store_true') args = parser.parse_args() configure_logging() db_name = DATABASE['name'] with session_scope(url=DATABASE_STR_WITUOUT_TABLE) as session: session.execute('commit') try: session.execute('create database ' + db_name) except ProgrammingError: # the database already exists if args.force_drop or are_you_sure( 'The database "{}" already exists, do you wish to DROP it?' .format(db_name)): session.execute('drop database ' + db_name) session.execute('create database ' + db_name) else: print('Did nothing.') sys.exit() with session_scope() as session: session.execute('create extension postgis') session.execute('create extension unaccent') # create all the tables engine = create_engine(DATABASE_STR) Base.metadata.create_all(engine)