# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2018-11-13 16:01
from __future__ import unicode_literals

from django.db import migrations, models



class Migration(migrations.Migration):

    dependencies = [
        ('tests', '0001_initial'),
    ]

    operations = [
        # necessary for tests
        migrations.RunSQL("CREATE EXTENSION IF NOT EXISTS citus;"),
        migrations.RunSQL("SELECT * from master_add_node('django-multitenant_worker1_1', 5432);"),
        migrations.RunSQL("SELECT * from master_add_node('django-multitenant_worker2_1', 5432);"),

        # Drop constraints
        migrations.RunSQL("ALTER TABLE tests_aliasedtask DROP CONSTRAINT tests_aliasedtask_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_country DROP CONSTRAINT tests_country_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_manager DROP CONSTRAINT tests_manager_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_project DROP CONSTRAINT tests_project_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_projectmanager DROP CONSTRAINT tests_projectmanager_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_record DROP CONSTRAINT tests_record_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_subtask DROP CONSTRAINT tests_subtask_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_task DROP CONSTRAINT tests_task_pkey CASCADE;"),

        # distribute
        migrations.RunSQL("SELECT create_reference_table('tests_country');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_account', 'id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_aliasedtask', 'account_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_manager', 'account_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_organization', 'id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_project', 'account_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_projectmanager', 'account_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_record', 'organization_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_subtask', 'account_id');"),
        migrations.RunSQL("SELECT create_distributed_table('tests_task', 'account_id');"),

        # Add constraints
        migrations.RunSQL("ALTER TABLE tests_aliasedtask ADD CONSTRAINT tests_aliasedtask_pkey PRIMARY KEY (account_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_country ADD CONSTRAINT tests_country_pkey PRIMARY KEY (id);"),
        migrations.RunSQL("ALTER TABLE tests_project ADD CONSTRAINT tests_project_pkey PRIMARY KEY (account_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_manager ADD CONSTRAINT tests_manager_pkey PRIMARY KEY (account_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_projectmanager ADD CONSTRAINT tests_projectmanager_pkey PRIMARY KEY (account_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_record ADD CONSTRAINT tests_record_pkey PRIMARY KEY (organization_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_subtask ADD CONSTRAINT tests_subtask_pkey PRIMARY KEY (account_id, id);"),
        migrations.RunSQL("ALTER TABLE tests_task ADD CONSTRAINT tests_task_pkey PRIMARY KEY (account_id, id);")
    ]
