示例#1
0
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')
示例#2
0
# import the species
old.execute("SELECT id, category_id, severity_id, name_sci, name_comm, remedy, resources FROM issues")
for row in dictfetchall(old):
    species = Species.objects.filter(pk=row['id']).first()
    if not species:
        species = Species(pk=row['id'])

    species.name = row['name_comm'] or ""
    species.scientific_name = row['name_sci'] or ""
    species.remedy = row['remedy'] or ""
    species.resources = row['resources'] or ""
    species.severity_id = row['severity_id']
    species.category_id = row['category_id']
    species.save()

with suspended_updates():
    # create users
    user_id_to_user_id = {}
    report_submitter_user_id = {}
    key_to_user_id = {}
    old.execute("""
    SELECT cardable_id, users.id AS user_id, affiliations, enabled, vcards.id, n_family, n_given, n_prefix, n_suffix, cardable_type, email
    FROM vcards LEFT JOIN users ON vcards.cardable_id = users.id AND cardable_type = 'User'
    ORDER BY cardable_type
    """)
    for row in dictfetchall(old):
        user = User.objects.filter(email=row['email']).first()
        if not user:
            user = User(email=row['email'], is_active=False)

        user.email = row['email']