# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: tests/models.py
# class AwareModel(models.Model):
#     name = models.CharField(max_length=10, unique=True)
#     status = models.BooleanField(default=False)
#     factor = models.PositiveSmallIntegerField(default=7)
# 
#     objects = MultiSchemaManager()
# 
# class ViewBackedModel(models.Model):
#     class Meta:
#         managed = False


from django.test import TestCase
from django.db.migrations.state import ProjectState
from django.db import connection, migrations, transaction
from boardinghouse.models import Schema
from ..models import AwareModel, ViewBackedModel
        TRIGGER_FUNCTION = '''CREATE OR REPLACE FUNCTION trigger_this() RETURNS TRIGGER AS $$
                              BEGIN
                                RAISE EXCEPTION 'Trigger fired correctly';
                              END;
                              $$ LANGUAGE plpgsql'''
        TRIGGER_TRIGGER = '''CREATE TRIGGER "test_trigger_this"
                             BEFORE INSERT ON tests_awaremodel
                             FOR EACH STATEMENT
                             EXECUTE PROCEDURE trigger_this()'''
        project_state = ProjectState()
        new_state = project_state.clone()

        trigger_function_op = migrations.RunSQL(
            sql=TRIGGER_FUNCTION,
            reverse_sql='DROP FUNCTION trigger_this()'
        )
        trigger_op = migrations.RunSQL(
            sql=TRIGGER_TRIGGER,
            reverse_sql='DROP TRIGGER "test_trigger_this" BEFORE EACH UPDATE ON tests_awaremodel'
        )

        trigger_function_op.state_forwards('tests', new_state)
        trigger_op.state_forwards('tests', new_state)

        with connection.schema_editor() as editor:
            trigger_function_op.database_forwards('tests', editor, project_state, new_state)
            trigger_op.database_forwards('tests', editor, project_state, new_state)

        Schema.objects.mass_create('a', 'b', 'c')


# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: tests/models.py
# class AwareModel(models.Model):
#     name = models.CharField(max_length=10, unique=True)
#     status = models.BooleanField(default=False)
#     factor = models.PositiveSmallIntegerField(default=7)
# 
#     objects = MultiSchemaManager()
# 
# class ViewBackedModel(models.Model):
#     class Meta:
#         managed = False


# Path: tests/tests/test_clone_schema_db_function.py
from django.test import TestCase
from django.db.migrations.state import ProjectState
from django.db import connection, migrations, transaction
from boardinghouse.models import Schema
from ..models import AwareModel, ViewBackedModel

        TRIGGER_FUNCTION = '''CREATE OR REPLACE FUNCTION trigger_this() RETURNS TRIGGER AS $$
                              BEGIN
                                RAISE EXCEPTION 'Trigger fired correctly';
                              END;
                              $$ LANGUAGE plpgsql'''
        TRIGGER_TRIGGER = '''CREATE TRIGGER "test_trigger_this"
                             BEFORE INSERT ON tests_awaremodel
                             FOR EACH STATEMENT
                             EXECUTE PROCEDURE trigger_this()'''
        project_state = ProjectState()
        new_state = project_state.clone()

        trigger_function_op = migrations.RunSQL(
            sql=TRIGGER_FUNCTION,
            reverse_sql='DROP FUNCTION trigger_this()'
        )
        trigger_op = migrations.RunSQL(
            sql=TRIGGER_TRIGGER,
            reverse_sql='DROP TRIGGER "test_trigger_this" BEFORE EACH UPDATE ON tests_awaremodel'
        )

        trigger_function_op.state_forwards('tests', new_state)
        trigger_op.state_forwards('tests', new_state)

        with connection.schema_editor() as editor:
            trigger_function_op.database_forwards('tests', editor, project_state, new_state)
            trigger_op.database_forwards('tests', editor, project_state, new_state)

        Schema.objects.mass_create('a', 'b', 'c')


        for schema in Schema.objects.all():