# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings

from django.db import migrations


def forwards_func(apps, schema_editor):
    DatabaseHost = apps.get_model("sites", "DatabaseHost")
    Database = apps.get_model("sites", "Database")

    mysql, created = DatabaseHost.objects.get_or_create(
        hostname=settings.MYSQL_DB_HOST,
        port=settings.MYSQL_DB_PORT,
        username=settings.MYSQL_DB_USER,
        password=settings.MYSQL_DB_PASS,
        dbms="mysql"
    )

    postgresql, created = DatabaseHost.objects.get_or_create(
        hostname=settings.POSTGRES_DB_HOST,
        port=settings.POSTGRES_DB_PORT,
        username=settings.POSTGRES_DB_USER,
        password=settings.POSTGRES_DB_PASS,
        dbms="postgresql"
    )

    for db in Database.objects.all():
        if db.category == "mysql":
            db.host = mysql
        else:
            db.host = postgresql
        db.save()


def reverse_func(apps, schema_editor):
    Database = apps.get_model("sites", "Database")

    for db in Database.objects.all():
        if db.host.dbms == "mysql":
            db.category = "mysql"
        else:
            db.category = "postgresql"
        db.save()


class Migration(migrations.Migration):
    dependencies = [
        ('sites', '0018_auto_20170818_1148'),
    ]

    operations = [
        migrations.RunPython(forwards_func, reverse_func)
    ]

@@ -377,389 +377,804 @@
 HOST
-,%0A        port=settings.MYSQL_DB_PORT,%0A        username=settings.MYSQL_DB_USER,%0A        password=settings.MYSQL_DB_PASS,%0A        dbms=%22mysql%22%0A    )%0A%0A    postgresql, created = DatabaseHost.objects.get_or_create(%0A        hostname=settings.POSTGRES_DB_HOST,%0A        port=settings.POSTGRES_DB_PORT,%0A        username=settings.POSTGRES_DB_USER,%0A        password=settings.POSTGRES_DB_PASS
+ if hasattr(settings, %22MYSQL_DB_HOST%22) else %22mysql1%22,%0A        port=settings.MYSQL_DB_PORT if hasattr(settings, %22MYSQL_DB_PORT%22) else 3306,%0A        username=settings.MYSQL_DB_USER if hasattr(settings, %22MYSQL_DB_USER%22) else %22web3%22,%0A        password=settings.MYSQL_DB_PASS if hasattr(settings, %22MYSQL_DB_PASS%22) else %22web3%22,%0A        dbms=%22mysql%22%0A    )%0A%0A    postgresql, created = DatabaseHost.objects.get_or_create(%0A        hostname=settings.POSTGRES_DB_HOST if hasattr(settings, %22POSTGRES_DB_HOST%22) else %22postgres1%22,%0A        port=settings.POSTGRES_DB_PORT if hasattr(settings, %22POSTGRES_DB_PORT%22) else 5432,%0A        username=settings.POSTGRES_DB_USER if hasattr(settings, %22POSTGRES_DB_USER%22) else %22web3%22,%0A        password=settings.POSTGRES_DB_PASS if hasattr(settings, %22POSTGRES_DB_PASS%22) else %22web3%22
 ,%0A  

fix build