# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-01-18 14:06
from __future__ import unicode_literals

from django.db import connection, migrations

from ralph_scrooge.utils.tasks import create_dailycosts_partitions


def forwards(apps, schema_editor):
    # We don't want to override any existing partitions.
    if not _has_partition('ralph_scrooge_dailycost'):
        with connection.cursor() as cursor:
            # A PRIMARY KEY must include all columns in the table's partitioning
            # function.
            cursor.execute('''
                ALTER TABLE `ralph_scrooge_dailycost`
                DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `date`, `forecast`);
            ''')
            cursor.execute('''
                ALTER TABLE `ralph_scrooge_dailycost`
                PARTITION BY RANGE(TO_DAYS(date))
                PARTITIONS 1
                SUBPARTITION BY KEY(forecast) (
                    PARTITION p_max VALUES LESS THAN (MAXVALUE) (SUBPARTITION p_max_0, SUBPARTITION p_max_1)
                );
            ''')
        create_dailycosts_partitions()


def backwards(apps, schema_editor):
    with connection.cursor() as cursor:
        cursor.execute('''
            ALTER TABLE `ralph_scrooge_dailycost` REMOVE PARTITIONING;
        ''')
        cursor.execute('''
            ALTER TABLE `ralph_scrooge_dailycost`
            DROP PRIMARY KEY, ADD PRIMARY KEY (`id`);
        ''')


def _has_partition(table_name):
    with connection.cursor() as cursor:
        # Table names can't be passed as parameters to SQL query, but
        # table_name is safe to concat it directly.
        cursor.execute('SHOW CREATE TABLE %s;' % table_name)
        result = cursor.fetchone()
        return 'PARTITION' in result[1]


class Migration(migrations.Migration):

    dependencies = [
        ('ralph_scrooge', '0013_auto_20180119_0824'),
    ]

    operations = [
        migrations.RunPython(forwards, backwards)
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2018-01-18 14:06
from __future__ import unicode_literals

from django.db import connection, migrations

from ralph_scrooge.utils.tasks import create_dailycosts_partitions


def forwards(apps, schema_editor):
    # We don't want to override any existing partitions.
    if not _has_partition('ralph_scrooge_dailycost'):
        with connection.cursor() as cursor:
            # A PRIMARY KEY must include all columns in the table's partitioning
            # function.
            cursor.execute('''
                ALTER TABLE `ralph_scrooge_dailycost`
                DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `date`, `forecast`);
            ''')
            cursor.execute('''
                ALTER TABLE `ralph_scrooge_dailycost`
                PARTITION BY RANGE(TO_DAYS(date))
                PARTITIONS 1
                SUBPARTITION BY KEY(forecast) (
                    PARTITION p_max VALUES LESS THAN (MAXVALUE) (SUBPARTITION p_max_0, SUBPARTITION p_max_1)
                );
            ''')
        create_dailycosts_partitions()


def backwards(apps, schema_editor):
    with connection.cursor() as cursor:
        cursor.execute('''
            ALTER TABLE `ralph_scrooge_dailycost` REMOVE PARTITIONING;
        ''')
        cursor.execute('''
            ALTER TABLE `ralph_scrooge_dailycost`
            DROP PRIMARY KEY, ADD PRIMARY KEY (`id`);
        ''')


def _has_partition(table_name):
    with connection.cursor() as cursor:
        # Table names can't be passed as parameters to SQL query, but
        # table_name is safe to concat it directly.
        cursor.execute('SHOW CREATE TABLE %s;' % table_name)
        result = cursor.fetchone()
        return 'PARTITION' in result[1]


class Migration(migrations.Migration):

    dependencies = [
        ('ralph_scrooge', '0013_auto_20180119_0824'),
    ]

    operations = [
        migrations.RunPython(forwards, backwards)
    ]

# -*- coding: utf-8 -*- # Generated by Django 1.10.2 on 2018-01-18 14:06 # We don't want to override any existing partitions. # A PRIMARY KEY must include all columns in the table's partitioning # function. ALTER TABLE `ralph_scrooge_dailycost`                 DROP PRIMARY KEY, ADD PRIMARY KEY (`id`, `date`, `forecast`); ALTER TABLE `ralph_scrooge_dailycost`                 PARTITION BY RANGE(TO_DAYS(date))                 PARTITIONS 1                 SUBPARTITION BY KEY(forecast) (                     PARTITION p_max VALUES LESS THAN (MAXVALUE) (SUBPARTITION p_max_0, SUBPARTITION p_max_1)                 ); ALTER TABLE `ralph_scrooge_dailycost` REMOVE PARTITIONING; ALTER TABLE `ralph_scrooge_dailycost`             DROP PRIMARY KEY, ADD PRIMARY KEY (`id`); # Table names can't be passed as parameters to SQL query, but # table_name is safe to concat it directly.