# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('userreports', '0008_new_table_name_views'),
    ]

    operations = [
        migrations.RunPython(migrations.RunPython.noop, migrations.RunPython.noop, elidable=True),
    ]

@@ -101,40 +101,2361 @@
 ls%0A%0A
-from django.db import migrations
+import sys%0Afrom collections import defaultdict%0A%0Afrom django.db import migrations%0A%0Afrom corehq.apps.userreports.models import DataSourceConfiguration, StaticDataSourceConfiguration%0Afrom corehq.apps.userreports.util import get_table_name, LEGACY_UCR_TABLE_PREFIX%0Afrom corehq.sql_db.connections import connection_manager%0A%0AGIT_COMMIT_WITH_MANAGEMENT_COMMAND = %228ec458ce9dd6a690c0b48bba07ffee2455f267d2%22%0AAUTO_MIGRATE_FAILED_MESSAGE = %22%22%22%0AA migration must be performed before this environment can be upgraded to the%0Alatest version of CommCareHQ. To perform the migration you will need to do the following:%0A%0A* Checkout an older version of CommCareHQ:%0A%0A    git checkout %7Bcommit%7D%0A%0A* Run the following management commands:%0A%0A    python manage.py rename_ucr_tables create-views --verbose --execute --noconfirm%0A%0A    python manage.py rename_ucr_tables rename-tables --verbose --execute --noconfirm%0A%0A%22%22%22.format(commit=GIT_COMMIT_WITH_MANAGEMENT_COMMAND)%0A%0A%0Adef table_exists(connection, table_name):%0A    res = connection.execute(%22select 1 from pg_tables where tablename = %25s%22, table_name)%0A    return bool(list(res))%0A%0A%0Adef get_legacy_table_name(data_source):%0A    return get_table_name(%0A        data_source.domain, data_source.table_id, max_length=63, prefix=LEGACY_UCR_TABLE_PREFIX%0A    )%0A%0A%0Adef _data_sources_by_engine_id():%0A    by_engine_id = defaultdict(list)%0A    for ds in StaticDataSourceConfiguration.all():%0A        ds_engine_id = ds%5B'engine_id'%5D%0A        by_engine_id%5Bds_engine_id%5D.append(ds)%0A%0A    for ds in DataSourceConfiguration.all():%0A        ds = DataSourceConfiguration.wrap(ds)%0A        ds_engine_id = ds%5B'engine_id'%5D%0A        by_engine_id%5Bds_engine_id%5D.append(ds)%0A%0A    return by_engine_id%0A%0A%0Adef _assert_migrated(apps, schema_editor):%0A    for engine_id, data_sources in _data_sources_by_engine_id().items():%0A        with connection_manager.get_engine(engine_id).begin() as conn:%0A            for data_source in data_sources:%0A                legacy_table_name = get_legacy_table_name(data_source)%0A                new_table_name = get_table_name(data_source.domain, data_source.table_id)%0A                if (%0A                    table_exists(conn, legacy_table_name)%0A                    and not table_exists(conn, new_table_name)%0A                ):%0A                    print(%22%22)%0A                    print(AUTO_MIGRATE_FAILED_MESSAGE)%0A                    sys.exit(1)
 %0A%0A%0Ac
@@ -2627,33 +2627,24 @@
 hon(
-migrations.RunPython.noop
+_assert_migrated
 , mi
@@ -2670,25 +2670,9 @@
 noop
-, elidable=True),
+)
 %0A   

update migration to fail if not yet run