Comments (5)
I use next definition for safe migration: no select, insert, update, delete operation will be affected (fail or timeout) during migration and deployment.
In case of ADD COLUMN DEFAULT NULL
:
- old code works fine with old schema
- old code will insert nulls to new schema, no fail there
- new code works fine with new schema
NULL
means column values can be nullable that semantically correct, so ADD COLUMN DEFAULT NULL
should be considered as safe migration.
In case of ADD COLUMN DEFAULT NOT NULL
:
- old code works fine with old schema
- old code will try to insert nulls to new schema and will fail
- new code works fine with new schema
So ADD COLUMN DEFAULT NOT NULL
is unsafe migration.
Looks previously this two cases were considered as same and was handled same way, so they should be separated.
You correct that in postgres 11+ no table rewrite happens and only schema changes is safe operation, this library documentation should be fixed.
from django-pg-zero-downtime-migrations.
Add column with default looks next https://github.com/tbicr/django-pg-zero-downtime-migrations/blob/master/tests/unit/test_schema.py#L241-L242:
ALTER TABLE "tests_model" ADD COLUMN "field" varchar(40) DEFAULT 'test' NULL;
ALTER TABLE "tests_model" ALTER COLUMN "field" DROP DEFAULT;
Lets consider two cases:
-
add column with default and null
in this case inserts between migration and deployment will be reason of null values that can be not expected -
add column with default and not null
in this case all inserts between migration and deployment will be reason of fails that can be not accepted
so this is reason as in this lib adding column with default considered unsafe
in django 5 you can use https://docs.djangoproject.com/en/5.0/ref/models/fields/#db-default that is better way to use default, I'd strongly recommend
default explanation mentioned there https://github.com/tbicr/django-pg-zero-downtime-migrations/tree/master?tab=readme-ov-file#create-column-with-default
however option keeping default after migration and considered this operation as safe is interesting option
from django-pg-zero-downtime-migrations.
Thank you for your quick and thorough response - I understand it better now.
I have 2 follow-up questions:
- I am wondering if the operation could still be considered safe, from a downtime prespective? Yes - there is still the possibility of NULL values in the code (in the case of between migration and deployment) but would it still be unsafe with respect to downtime? Or, is somehow having NULL values considered unsafe and could cause downtime?
- The reason in row 8 for why it is unsafe:
because you spend time in migration to populate all values in table
Is this still true? I was thinking that since Postgres 11, it had been addressed:
adding a column with a constant default value no longer means that each row of the table needs to be updated when the
ALTER TABLE
statement is executed.
So since each row doesn't need to be updated when ALTER TABLE
is executed, this would mean that the migration that generates this SQL would not need time to populate the values in the table, during the migration:
Instead, the default value will be returned the next time the row is accessed, and applied when the table is rewritten
Thank you in advance for your time and response!
from django-pg-zero-downtime-migrations.
Ok - great. The explanation provided makes sense to me, thank you.
I can leave this issue open, and the update can be created against it to track? Or if a new issue is preferred, feel free to close this out.
Thank you again for your response and explanations.
from django-pg-zero-downtime-migrations.
defaults was reworked in 0.16
from django-pg-zero-downtime-migrations.
Related Issues (15)
- Postgis support HOT 3
- migrations.DeleteModel locks DB when it has ForeignKey field HOT 5
- Unchanged `db_table` in `RenameModel` raises `Unsafe.ALTER_TABLE_RENAME`
- Publish release to pypi? HOT 2
- Do you have a workflow for running `prettier` on markdown files? HOT 7
- Django 4.1: `'DatabaseSchemaEditor'` has no attribute `'sql_create_sequence'` HOT 2
- Flag for deeming CREATE INDEX operation unsafe HOT 8
- DatabaseSchemaEditorMixin._alter_column_type_sql() takes 5 positional arguments but 7 were given HOT 1
- Drop support for Python 3.6 HOT 4
- When migrating DB from scratch, early ran deferred SQL queries crash HOT 2
- Django 2.2 and PostgreSQL 11 HOT 6
- AttributeError on django v3.2 HOT 4
- Is package really still beta? HOT 4
- Using django-pg-zero-downtime-migrations our table is locked HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-pg-zero-downtime-migrations.