Skip to content

Commit

Permalink
add idempotent mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tbicr committed Apr 25, 2024
1 parent 3209249 commit f41984b
Show file tree
Hide file tree
Showing 41 changed files with 1,604 additions and 60 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# django-pg-zero-downtime-migrations changelog

## 0.15
- added idempotent mode and `ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL` setting

## 0.14
- fixed deferred sql errors
- added django 5.0 support
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
363 changes: 307 additions & 56 deletions django_zero_downtime_migrations/backends/postgres/schema.py

Large diffs are not rendered by default.

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,25 @@
# Generated by Django 5.0 on 2024-04-22 17:47

import django.db.models.deletion
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_model",
field=models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="idempotency_add_column_app.testtable",
),
),
]
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_model = models.OneToOneField(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,26 @@
# Generated by Django 5.0 on 2024-04-23 10:59

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

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

operations = [
migrations.AlterField(
model_name="relatedtesttable",
name="test_field_int",
field=models.OneToOneField(
db_column="test_field_int",
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="idempotency_add_foreign_key_app.testtable",
),
),
]
Empty file.
15 changes: 15 additions & 0 deletions tests/apps/idempotency_add_foreign_key_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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.OneToOneField(
TestTable,
null=True,
on_delete=models.CASCADE,
db_column="test_field_int",
)
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()),
],
),
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 @@
from django.db import migrations, models


class Migration(migrations.Migration):

atomic = False

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

operations = [
migrations.RemoveField(
model_name="relatedtesttable",
name="id",
),
migrations.AlterField(
model_name="relatedtesttable",
name="test_field_int",
field=models.IntegerField(primary_key=True, serialize=False),
),
]
Empty file.
10 changes: 10 additions & 0 deletions tests/apps/idempotency_add_primary_key_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.IntegerField(primary_key=True)
Empty file.
Loading

0 comments on commit f41984b

Please sign in to comment.