Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add idempotent mode #55

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# django-pg-zero-downtime-migrations changelog

## 0.15
- added idempotent mode and `ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL` setting
- fixed django 3.2 degradation with missing `skip_default_on_alter` method

## 0.14
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,23 @@ Define way to apply deferred sql, default `True`:
ZERO_DOWNTIME_DEFERRED_SQL = True

Allowed values:
- `True` - run deferred sql similar to default django way.
- `False` - run deferred sql as soon as possible.
- `True` - run deferred sql similar to default django way
- `False` - run deferred sql as soon as possible

#### ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL

Define idempotent mode, default `False`:

ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL = False

Allowed values:
- `True` - skip already applied sql migrations
- `False` - standard non atomic django behaviour

As this backend doesn't use transactions for migrations any failed migration can be cause of stopped process in intermediate state.
To avoid manual schema manipulation idempotent mode allows to rerun failed migration after fixed issue (eg. data issue or long running CRUD queries).

> _NOTE:_ idempotent mode checks rely only on name and index and constraint valid state, so it can ignore name collisions and recommended do not use it for CI checks.

#### PgBouncer and timeouts

Expand Down
388 changes: 321 additions & 67 deletions django_zero_downtime_migrations/backends/postgres/schema.py

Large diffs are not rendered by default.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="RelatedTestTable",
fields=[
("test_field_int", models.IntegerField(primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name="TestTable",
fields=[
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("test_field_int", models.IntegerField()),
("test_field_str", models.CharField(max_length=10)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

dependencies = [
("idempotency_add_auto_field_app", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="relatedtesttable",
name="test_field_int",
field=models.AutoField(primary_key=True, serialize=False),
),
]
Empty file.
10 changes: 10 additions & 0 deletions tests/apps/idempotency_add_auto_field_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models


class TestTable(models.Model):
test_field_int = models.IntegerField()
test_field_str = models.CharField(max_length=10)


class RelatedTestTable(models.Model):
test_field_int = models.AutoField(primary_key=True)
Empty file.
42 changes: 42 additions & 0 deletions tests/apps/idempotency_add_check_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="RelatedTestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField(null=True)),
],
),
migrations.CreateModel(
name="TestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField()),
("test_field_str", models.CharField(max_length=10)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

dependencies = [
("idempotency_add_check_app", "0001_initial"),
]

operations = [
migrations.AddConstraint(
model_name="relatedtesttable",
constraint=models.CheckConstraint(
check=models.Q(("test_field_int__gt", 0)),
name="idempotency_add_check_app_relatedtesttable_check",
),
),
]
Empty file.
18 changes: 18 additions & 0 deletions tests/apps/idempotency_add_check_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models


class TestTable(models.Model):
test_field_int = models.IntegerField()
test_field_str = models.CharField(max_length=10)


class RelatedTestTable(models.Model):
test_field_int = models.IntegerField(null=True)

class Meta:
constraints = [
models.CheckConstraint(
check=models.Q(test_field_int__gt=0),
name="idempotency_add_check_app_relatedtesttable_check",
)
]
Empty file.
42 changes: 42 additions & 0 deletions tests/apps/idempotency_add_column_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="RelatedTestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField(null=True)),
],
),
migrations.CreateModel(
name="TestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField()),
("test_field_str", models.CharField(max_length=10)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

dependencies = [
("idempotency_add_column_app", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="relatedtesttable",
name="test_field_str",
field=models.CharField(max_length=10, null=True),
),
]
Empty file.
11 changes: 11 additions & 0 deletions tests/apps/idempotency_add_column_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models


class TestTable(models.Model):
test_field_int = models.IntegerField()
test_field_str = models.CharField(max_length=10)


class RelatedTestTable(models.Model):
test_field_int = models.IntegerField(null=True)
test_field_str = models.CharField(max_length=10, null=True)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="RelatedTestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField(null=True)),
],
),
migrations.CreateModel(
name="TestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField()),
("test_field_str", models.CharField(max_length=10)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

dependencies = [
("idempotency_add_column_foreign_key_app", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="relatedtesttable",
name="test_model",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="idempotency_add_column_foreign_key_app.testtable",
),
),
]
Empty file.
11 changes: 11 additions & 0 deletions tests/apps/idempotency_add_column_foreign_key_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import models


class TestTable(models.Model):
test_field_int = models.IntegerField()
test_field_str = models.CharField(max_length=10)


class RelatedTestTable(models.Model):
test_field_int = models.IntegerField(null=True)
test_model = models.ForeignKey(TestTable, null=True, on_delete=models.CASCADE)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="RelatedTestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField(null=True)),
],
),
migrations.CreateModel(
name="TestTable",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("test_field_int", models.IntegerField()),
("test_field_str", models.CharField(max_length=10)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
atomic = False

dependencies = [
("idempotency_add_column_one_to_one_app", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="relatedtesttable",
name="test_model",
field=models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="idempotency_add_column_one_to_one_app.testtable",
),
),
]
Empty file.
Loading
Loading