def copy_records(ctx, recreate_db=False): """Copy database records from old site. This is messy because only certain records are copied from the old site while others are loaded from fixtures. """ setup() from django.db import connections from django.contrib.auth import get_user_model from elasticmodels import suspended_updates settings = get_settings() settings.DATABASES['old'] = { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'HOST': 'pgsql.rc.pdx.edu', 'NAME': 'invhotline', 'USER': '******', 'PASSWORD': getpass( 'Old database password (get from ' '/vol/www/invasivespecieshotline/invasivespecieshotline/config/database.yml): ' ), } User = get_user_model() if recreate_db: createdb(ctx, drop=True) migrate(ctx) loaddata(ctx) with suspended_updates(): _copy_records(settings) default = connections['default'] print('Updating sequences...', end='') tables = ( 'category', 'comment', 'county', 'image', 'invite', 'notification', 'report', 'species', 'user', 'user_notification_query', ) statement = """ SELECT setval( pg_get_serial_sequence('{table}', '{table}_id'), coalesce(max({table}_id), 0) + 1, false ) FROM "{table}"; """ statements = ' '.join(statement.format(table=table) for table in tables) default.cursor().execute(statements) print('Done') print('Updating expert contact name...', end='') expert = User.objects.filter(first_name='EXPERT', last_name='CONTACT') expert.update(first_name='', last_name='') print('Done')
def copy_images(ctx, dry_run=False): """Copy images from old site.""" settings = get_settings() src_dir = '/vol/www/invasivespecieshotline/invasivespecieshotline/public/uploads/images/0000' dest_dir = os.path.join(settings.MEDIA_ROOT, 'images') if os.path.exists(dest_dir): print('Destination image directory exists at', dest_dir) else: print('Creating destination image directory at', dest_dir) os.makedirs(dest_dir) src_files = [] for dirpath, dirnames, filenames in os.walk(src_dir): paths = [os.path.join(dirpath, f) for f in filenames] paths = [os.path.relpath(p, src_dir) for p in paths] src_files.extend(paths) for src_file in src_files: src_path = os.path.join(src_dir, src_file) dest_path = os.path.join(dest_dir, src_file.replace(os.sep, '-')) print(src_path, '=>', dest_path) if not dry_run: shutil.copyfile(src_path, dest_path)