def get_operations():
    operations = [
        migrations.RunSQL(
            "ALTER TABLE tests_tenantnotidmodel DROP CONSTRAINT tests_tenantnotidmodel_pkey CASCADE;"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_somerelatedmodel DROP CONSTRAINT tests_somerelatedmodel_pkey CASCADE;"
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute('TenantNotIdModel'),
            tenant_migrations.Distribute('SomeRelatedModel'),
        ]

    operations += [
        migrations.RunSQL(
            "ALTER TABLE tests_somerelatedmodel ADD CONSTRAINT tests_somerelatedmodel_pkey PRIMARY KEY (related_tenant_id, id);"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_tenantnotidmodel ADD CONSTRAINT tests_tenantnotidmodel_pkey PRIMARY KEY (tenant_column);"
        )
    ]

    return operations
Пример #2
0
def get_operations():
    operations = [
        migrations.CreateModel(
            name='Employee',
            fields=[
                ('id',
                 models.AutoField(auto_created=True,
                                  primary_key=True,
                                  serialize=False,
                                  verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ('account',
                 models.ForeignKey(blank=True,
                                   null=True,
                                   on_delete=django.db.models.deletion.CASCADE,
                                   related_name='employees',
                                   db_constraint=False,
                                   to='tests.Account')),
                ('created_by',
                 models.ForeignKey(
                     blank=True,
                     null=True,
                     on_delete=django.db.models.deletion.SET_NULL,
                     related_name='users_created',
                     to='tests.Employee')),
            ],
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute('Employee', reference=True),
        ]

    return operations
def get_operations():
    operations = [
        migrations.RunSQL("ALTER TABLE tests_tempmodel DROP CONSTRAINT tests_tempmodel_pkey CASCADE;"),
        migrations.RunSQL("ALTER TABLE tests_tempmodel ADD CONSTRAINT tests_tempmodel_pkey PRIMARY KEY (account_id, id);")]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute('TempModel'),
        ]

    return operations
def get_operations():
    operations = [
        migrations.CreateModel(
            name="ModelConfig",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                (
                    "account",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="configs",
                        to="tests.Account",
                    ),
                ),
                (
                    "employee",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="configs",
                        to="tests.Employee",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=(django_multitenant.mixins.TenantModelMixin, models.Model),
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_modelconfig DROP CONSTRAINT tests_modelconfig_pkey CASCADE;",
            reverse_sql="",
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_modelconfig ADD CONSTRAINT tests_modelconfig_pkey PRIMARY KEY (account_id, id);",
            reverse_sql="",
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute("ModelConfig", reverse_ignore=True),
        ]

    return operations
Пример #5
0
class Migration(migrations.Migration):

    dependencies = [
        ("tests", "0022_merge_20200211_1000"),
    ]

    operations = [
        migrations.CreateModel(
            name="Revenue",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("value", models.CharField(max_length=30)),
                (
                    "acc",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="revenues",
                        to="tests.Account",
                    ),
                ),
                (
                    "project",
                    django_multitenant.fields.TenantForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="revenues",
                        to="tests.Project",
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
            bases=(django_multitenant.mixins.TenantModelMixin, models.Model),
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_revenue DROP CONSTRAINT tests_revenue_pkey CASCADE;",
            reverse_sql="",
        ),
        tenant_migrations.Distribute("Revenue", reverse_ignore=True),
        migrations.RunSQL(
            "ALTER TABLE tests_revenue ADD CONSTRAINT tests_revenue_pkey PRIMARY KEY (acc_id, id);",
            reverse_sql="",
        ),
    ]
class Migration(migrations.Migration):

    dependencies = [
        ('tests', '0002_distribute'),
    ]

    operations = [
        migrations.RunSQL(
            "ALTER TABLE tests_tenantnotidmodel DROP CONSTRAINT tests_tenantnotidmodel_pkey CASCADE;"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_somerelatedmodel DROP CONSTRAINT tests_somerelatedmodel_pkey CASCADE;"
        ),
        tenant_migrations.Distribute('TenantNotIdModel'),
        tenant_migrations.Distribute('SomeRelatedModel'),
        migrations.RunSQL(
            "ALTER TABLE tests_somerelatedmodel ADD CONSTRAINT tests_somerelatedmodel_pkey PRIMARY KEY (related_tenant_id, id);"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_tenantnotidmodel ADD CONSTRAINT tests_tenantnotidmodel_pkey PRIMARY KEY (tenant_column);"
        )
    ]
Пример #7
0
        def test_reference_different_app_table(self):
            project_state = ProjectState(real_apps=['auth'])
            operation = migrations.Distribute('auth.User', reference=True)

            self.assertEqual(operation.describe(), "Run create_(distributed/reference)_table statement")
            self.assertTableIsNotReference('auth_user')
            new_state = project_state.clone()

            with connection.schema_editor() as editor:
                operation.database_forwards("tests", editor, project_state, new_state)

            self.assertTableIsReference('auth_user')
            self.undistribute_table('auth_user')
Пример #8
0
        def test_distribute_table(self):
            project_state = ProjectState(real_apps=['tests'])
            operation = migrations.Distribute('MigrationTestModel')

            self.assertEqual(operation.describe(), "Run create_(distributed/reference)_table statement")

            self.assertTableIsNotDistributed('tests_migrationtestmodel', 'id')
            new_state = project_state.clone()

            with connection.schema_editor() as editor:
                operation.database_forwards("tests", editor, project_state, new_state)

            self.assertTableIsDistributed('tests_migrationtestmodel', 'id')
            self.undistribute_table('tests_migrationtestmodel')
Пример #9
0
class Migration(migrations.Migration):

    dependencies = [
        ('tests', '0010_auto_20190517_1514'),
    ]

    operations = [
        migrations.RunSQL(
            "ALTER TABLE tests_tempmodel DROP CONSTRAINT tests_tempmodel_pkey CASCADE;"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_tempmodel ADD CONSTRAINT tests_tempmodel_pkey PRIMARY KEY (account_id, id);"
        ),
        tenant_migrations.Distribute('TempModel'),
    ]
def get_operations():
    operations = [
        migrations.CreateModel(
            name="Employee",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                (
                    "account",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="employees",
                        db_constraint=False,
                        to="tests.Account",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="users_created",
                        to="tests.Employee",
                    ),
                ),
            ],
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute("Employee",
                                         reference=True,
                                         reverse_ignore=True),
        ]

    return operations
def get_operations():
    operations = [
        migrations.RunSQL(
            "ALTER TABLE tests_tempmodel DROP CONSTRAINT tests_tempmodel_pkey CASCADE;",
            reverse_sql="",
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_tempmodel ADD CONSTRAINT tests_tempmodel_pkey PRIMARY KEY (account_id, id);",
            reverse_sql="",
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute("TempModel", reverse_ignore=True),
        ]

    return operations
def get_operations():
    operations = [
        migrations.CreateModel(
            name='ModelConfig',
            fields=[
                ('id',
                 models.AutoField(auto_created=True,
                                  primary_key=True,
                                  serialize=False,
                                  verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
                ('account',
                 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
                                   related_name='configs',
                                   to='tests.Account')),
                ('employee',
                 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
                                   related_name='configs',
                                   to='tests.Employee')),
            ],
            options={
                'abstract': False,
            },
            bases=(django_multitenant.mixins.TenantModelMixin, models.Model),
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_modelconfig DROP CONSTRAINT tests_modelconfig_pkey CASCADE;"
        ),
        migrations.RunSQL(
            "ALTER TABLE tests_modelconfig ADD CONSTRAINT tests_modelconfig_pkey PRIMARY KEY (account_id, id);"
        ),
    ]

    if settings.USE_CITUS:
        operations += [
            tenant_migrations.Distribute('ModelConfig'),
        ]

    return operations
class Migration(migrations.Migration):

    dependencies = [
        ('tests', '0022_merge_20200211_1000'),
    ]

    operations = [
        migrations.CreateModel(
            name='Revenue',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('value', models.CharField(max_length=30)),
                ('acc', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='revenues', to='tests.Account')),
                ('project', django_multitenant.fields.TenantForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='revenues', to='tests.Project')),
            ],
            options={
                'abstract': False,
            },
            bases=(django_multitenant.mixins.TenantModelMixin, models.Model),
        ),
        migrations.RunSQL("ALTER TABLE tests_revenue DROP CONSTRAINT tests_revenue_pkey CASCADE;"),
        tenant_migrations.Distribute('Revenue'),
        migrations.RunSQL("ALTER TABLE tests_revenue ADD CONSTRAINT tests_revenue_pkey PRIMARY KEY (acc_id, id);")
    ]
class Migration(migrations.Migration):
    atomic = False

    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;

        ALTER TABLE tests_aliasedtask ADD CONSTRAINT
        tests_aliasedtask_pkey PRIMARY KEY (account_id, id);
        """),
        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
        tenant_migrations.Distribute('Country', reference=True),
        tenant_migrations.Distribute('Account'),
        tenant_migrations.Distribute('AliasedTask'),
        tenant_migrations.Distribute('Manager'),
        tenant_migrations.Distribute('Organization'),
        tenant_migrations.Distribute('Project'),
        tenant_migrations.Distribute('ProjectManager'),
        tenant_migrations.Distribute('Record'),
        tenant_migrations.Distribute('SubTask'),
        tenant_migrations.Distribute('Task'),

        # Add constraints
        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);"
        )
    ]
Пример #15
0
def get_operations():
    operations = []
    if settings.USE_CITUS:
        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);"
            )
        ]

    operations += [
        # Drop constraints
        migrations.RunSQL("""
        ALTER TABLE tests_aliasedtask
        DROP CONSTRAINT tests_aliasedtask_pkey CASCADE;

        ALTER TABLE tests_aliasedtask ADD CONSTRAINT
        tests_aliasedtask_pkey PRIMARY KEY (account_id, id);
        """),
        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;"),
    ]

    if settings.USE_CITUS:
        # distribute
        operations += [
            tenant_migrations.Distribute('Country', reference=True),
            tenant_migrations.Distribute('Account'),
            tenant_migrations.Distribute('AliasedTask'),
            tenant_migrations.Distribute('Manager'),
            tenant_migrations.Distribute('Organization'),
            tenant_migrations.Distribute('Project'),
            tenant_migrations.Distribute('ProjectManager'),
            tenant_migrations.Distribute('Record'),
            tenant_migrations.Distribute('SubTask'),
            tenant_migrations.Distribute('Task')
        ]

    # Add constraints
    operations += [
        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);"
        )
    ]

    return operations