Skip to main content

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

Project description

Django Rename Table

codecov

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 raname 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.2.tar.gz (3.7 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.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_rename_table-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a543a81c76335511b43242c3ab1fc219745bfc73f287b888f1049459a1a4ac89
MD5 144f16177c6d8865158b4816c5765d0f
BLAKE2b-256 68b749de3de9055277e59cadf81b47b60e1cbe6ea0ae1ef3b518286305a0c4d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_rename_table-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4456415afafe571c1aa83c7be03e4eee187c5b78d0375a9963968bfbde8edaf2
MD5 0d67e88f6355d62e19a64042e3184e8b
BLAKE2b-256 a19924c487d1d3486df29eba37d08a1c911d8ec370de9d3b0d4ada345851453d

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