Skip to main content

Django migration operations for renaming tables with aliases and managing view aliases.

Project description

Django Rename Table

codecov Package version Supported Python versions

This package provides the ability to create (and remove) an alias of a database table.

You may wish to do this when renaming database tables and want to avoid downtime.

Renaming tables on a live system can be problematic. If you run the migration to rename the table first then any previous running versions of the code will start to error as they can no longer find the model's table.

Deploying the updated code first and then migrating would also fail for similar reasons.

A solution to this is to use this tool and a multistep process.

Instructions

1) Rename the table

This renames the table and create an alias with the original name.

from django.db import migrations
from django_rename_table.operations import RenameTableWithAlias

class Migration(migrations.Migration):
    dependencies = [
        ("myapp", "0001_initial"),
    ]

    operations = [
        RenameTableWithAlias("old_table_name", "new_table_name"),
    ]

2) Apply the migration

python manage.py migrate

3) Update model to point at renamed table

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=1000)

    class Meta:
        db_table = "new_table_name"

4) Delete alias when you're happy no deployed code is using the old table

from django.db import migrations
from django_rename_table.operations import RemoveAlias

class Migration(migrations.Migration):
    dependencies = [
        ("myapp", "0002_rename_and_create_alias"),
    ]

    operations = [
        RemoveAlias("old_table_name"),
    ]

Questions

It is possible to perform write operations on a table alias?

Yes, however there are limitations. Postgres support this (as far as I can see from 9.3 onwards).

See Updatable Views in the docs - https://www.postgresql.org/docs/current/sql-createview.html.

The test tests/test_migrations.py (test_crud_on_renamed_model_with_alias_table) runs through some basic CRUD operations on a model which references a table alias.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to report bugs or suggest features.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_rename_table-0.1.3.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rename_table-0.1.3-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django_rename_table-0.1.3.tar.gz.

File metadata

  • Download URL: django_rename_table-0.1.3.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for django_rename_table-0.1.3.tar.gz
Algorithm Hash digest
SHA256 53ad315d7091b3c4c1ac3b1215759f947235b21a642f4f10918ccdb380f04537
MD5 893a5819b8c4b22582272b6fe6f38bff
BLAKE2b-256 5c34dcccd3b1e9d31926cd1b4806793b4195f4e9f7487a89a04b46d3d841fe8f

See more details on using hashes here.

File details

Details for the file django_rename_table-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rename_table-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f163b15f425635289cf0fe7d4ca8a3f11a3b423b5d80506b7670f0cf522bb95e
MD5 223a0f6fccf25464554396375b049b7b
BLAKE2b-256 e5596b7df33e7e444133b90635ca723c6a41d956c8dc569f1ecd8ad210f47896

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page