question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create initial revision from migration

See original GitHub issue

Hi, should the project document a way to make migrations file to make initial revision instead of running them manually on each server instance?

from __future__ import unicode_literals
from django.db import migrations
from django.core import management


def update_all_contenttypes():
    from django.apps import apps
    from django.contrib.contenttypes.management import create_contenttypes

    for app_config in apps.get_app_configs():
        create_contenttypes(app_config)


def get_models():
    return (
        'my_app.MyModel',
        'my_app.MyModel2',
    )


def init_reversion():
    management.call_command(
        'createinitialrevisions',
        *get_models(),
        comment="Initial revision.",
        verbosity=1)


def clean_reversion():
    management.call_command(
        'deleterevisions',
        *get_models(),
        verbosity=1)


def forward(apps, schema_editor):
    update_all_contenttypes()
    init_reversion()


def backward(apps, schema_editor):
    clean_reversion()


class Migration(migrations.Migration):

    dependencies = [
        ('reversion', "0001_squashed_0004_auto_20160611_1202"),
        ('my_app', '0018_auto_20181230_0025'),
        ('contenttypes', '0002_remove_content_type_name'),
    ]

    operations = [
        migrations.RunPython(forward, backward),
    ]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
Christophe31commented, Dec 12, 2018

if it make sense, a small tool may allow simpler migration:

from __future__ import unicode_literals

from django.db import migrations
from django.core import management


def update_all_contenttypes():
    from django.apps import apps
    from django.contrib.contenttypes.management import create_contenttypes

    for app_config in apps.get_app_configs():
        create_contenttypes(app_config)


def init_reversion(models, comment):
    management.call_command(
        'createinitialrevisions',
        *models,
        comment=comment,
        verbosity=1)


def clean_reversion(models):
    management.call_command(
        'deleterevisions',
        *models,
        verbosity=1)


def reversion_migration(models, update_content_type=True, comment="Initial revision."):
    def forward(apps, schema_editor):
        if update_content_type:
            update_all_contenttypes()
        init_reversion(models, comment=comment)

    def backward(apps, schema_editor):
        clean_reversion(models)

    return migrations.RunPython(forward, backward)

which then would be used:

from django.db import migrations
from reversion.migrations import reversion_migration

class Migration(migrations.Migration):

    dependencies = [
        ('reversion', "0001_squashed_0004_auto_20160611_1202"),
        ('contenttypes', '0002_remove_content_type_name'),
        ('my_app', '0018_auto_20181230_0025'),
    ]

    operations = [
        reversion_migration(
             ("my_app.Model",)),
    ]
1reaction
mastacheatacommented, Apr 27, 2020

Sorry, I simply didn’t get the models part into my head. Had a discussion with a colleague and then it made click.
Reversion must use the model definitions from the Model class files at the time of executing the command, even if the migrations and current database state don’t reflect the state of that class yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code First Migrations with an existing database - EF6
Run the Update-Database command in Package Manager Console. This will apply the InitialCreate migration to the database. Since the actual ...
Read more >
Developers - Create initial revision from migration - - Bountysource
Hi, should the project document a way to make migrations file to make initial revision instead of running them manually on each server...
Read more >
Tutorial — Alembic 1.9.0 documentation
Create a Migration Script¶ ... The file contains some header information, identifiers for the current revision and a “downgrade” revision, an import of...
Read more >
Creating "zero state" migration for existing db with sqlalchemy ...
alembic revision --autogenerate inspects the state of the connected database and the state of the target metadata and then creates a ...
Read more >
Safely Test and Apply Changes to Your Database
2. Creating migrations ... In the first function we'll put the code we want to execute, for example creating a schema. In the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found