/django-drifter

A small Django utility to make it easier to revert and redo migrations or to recreate your database.

Primary LanguagePythonApache License 2.0Apache-2.0

Django Drifter Project

Currently only supports PostgreSQL

Overview

The Drifter project provides custom Django management commands to manage database migrations. It includes commands to revert and redo migrations for a specified app or the entire project.

These commands are most useful during development and so cannot be run in production (DEBUG = False).

Features

  • Revert Migration: Reverts one or more migrations, optionally for a specified app.
  • Redo Migration: Reverts and re-applies the last migration, optionally for a specified app.
  • Reset Database: Drops all tables and runs all migrations.

Installation

  1. Install the package:
    pip install django-drifter
  2. Add drifter to the INSTALLED_APPS setting in your Django project's settings.py file:
    INSTALLED_APPS = [
        "drifter",
        ...,
    ]

Usage

Revert Migration

The revert_migration command reverts the last migration for a specified app.

python manage.py revert_migration [app_name] [--num N]
  • app_name: The name of the app whose migration you want to revert.
  • --num N: (Optional) The number of migrations to revert. Defaults to 1.

Redo Migration

The redo_migration command undoes and redoes the last migration for a specified app.

python manage.py redo_migration [--app app_name]
  • --app app_name: (Optional) The name of the app whose migration you want to redo.

Reset Database

The reset_database command drops all tables and runs all migrations.

python manage.py reset_database [--yes]
  • --yes: (Optional) Skips the confirmation prompt.

Running Tests

Before running the tests, start a local Postgres database:

docker run --name drifter-postgres -e POSTGRES_USER=django -e POSTGRES_PASSWORD=django -p 5432:5432 -d polls

To run the tests, use the following command:

pytest

Example

Revert Migration Example

python manage.py revert_migration polls --num 2

This command reverts the last two migrations for the polls app.

Redo Migration Example

python manage.py redo_migration --app polls

This command redoes the last migration for the polls app.

Reset Database Example

python manage.py reset_database

This command drops all tables and runs all migrations.

Contributing

Contributions are more than welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Commit your changes (git commit -am 'Add new feature').
  5. Push to the branch (git push origin feature-branch).
  6. Create a new Pull Request.

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for more details.