How to simplify migrations in Django 1.7?

2019-01-07 04:24发布

There are already similar questions for South, but I have started my project with Django 1.7 and am not using South.

During development a lot of migrations have been created, however the software is not yet delievered and there exists no database that must be migrated. Therefore I would like to reset the migrations as if my current model was the original one and recreate all databases.

What is the recommended way to do that?

EDIT: As of Django 1.8 there is a new command named squashmigrations which more or less solves the problem described here.

11条回答
Evening l夕情丶
2楼-- · 2019-01-07 05:04

I just had the same problem. Here's my workaround.

#!/bin/sh
echo "Starting ..."

echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete


# Optional
echo ">> Deleting database"
find . -name "db.sqlite3" -delete

echo ">> Running manage.py makemigrations"
python manage.py makemigrations

echo ">> Running manage.py migrate"
python manage.py migrate

echo ">> Done"

The find command: http://unixhelp.ed.ac.uk/CGI/man-cgi?find

查看更多
Lonely孤独者°
3楼-- · 2019-01-07 05:05

If you're in development mode and you just want to reset everything (database, migrations, etc), I use this script based on Abdelhamid Ba's answer. This will wipe the tables of the database (Postgres), delete all migration files, re-run the migrations and load my initial fixtures:

#!/usr/bin/env bash
echo "This will wipe out the database, delete migration files, make and apply migrations and load the intial fixtures."

while true; do
    read -p "Do you wish to continue?" yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo ">> Deleting old migrations"
find ../../src -path "*/migrations/*.py" -not -name "__init__.py" -delete

# Optional
echo ">> Deleting database"
psql -U db_user -d db_name -a -f ./reset-db.sql

echo ">> Running manage.py makemigrations and migrate"
./migrations.sh

echo ">> Loading initial fixtures"
./load_initial_fixtures.sh

echo ">> Done"

reset-db.sql file:

DO $$ DECLARE
    r RECORD;
BEGIN
    -- if the schema you operate on is not "current", you will want to
    -- replace current_schema() in query with 'schematodeletetablesfrom'
    -- *and* update the generate 'DROP...' accordingly.
    FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
    END LOOP;
END $$;

migration.sh file:

#!/usr/bin/env bash
cd ../../src
./manage.py makemigrations
./manage.py migrate

load_initial_fixtures.sh file:

#!/usr/bin/env bash
cd ../../src
./manage.py loaddata ~/path-to-fixture/fixture.json

Just be sure to change the paths to corresponds to your app. I personally have these scripts in a folder called project_root/script/local, and django's sources are in project_root/src.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-07 05:06

cd to src directory cd /path/to/src

delete migration directories rm -rf your_app/migrations/

note that this should be done for each app separately

migrate python3.3 manage.py migrate

if you wish to start again python3.3 manage.py makemigrations your_app

查看更多
神经病院院长
5楼-- · 2019-01-07 05:07

I got this. I just figured this out and it is good.

  • First, to clear migrations table:

    ./manage.py migrate --fake <app-name> zero
    
  • Remove app-name/migrations/ folder or contents.

  • Make the migrations:

    ./manage.py makemigrations <app-name>
    
  • Finally tidy up your migrations without making other database changes:

    ./manage.py migrate --fake <app-name>
    
查看更多
姐就是有狂的资本
6楼-- · 2019-01-07 05:11

A simple way is

Go to every app and delete the migration files.

Then go to the django-migrtaions table in the database and truncate it(delete all entries).

After that you can create migrations once again.

查看更多
男人必须洒脱
7楼-- · 2019-01-07 05:16

In the Django 1.7 version of migrations the reset functionality that used to be in South has been dropped in favor of new functionality for 'squashing' your migrations. This is supposed to be a good way to keep the number of migrations in check.

https://docs.djangoproject.com/en/dev/topics/migrations/#squashing-migrations

If you still want to really start from scratch i assume you still could by emptying the migrations table and removing the migrations after which you would run makemigrations again.

查看更多
登录 后发表回答