Skip to content

Instantly share code, notes, and snippets.

@nick-brady
Last active July 28, 2020 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nick-brady/60e141c48a388655134e1590e7e312c2 to your computer and use it in GitHub Desktop.
Save nick-brady/60e141c48a388655134e1590e7e312c2 to your computer and use it in GitHub Desktop.
Blow away all Django migrations in Postgres and start over - the easy way
# This file is not meant to be executed. Merely a reference for doing this step by step.
# dump the postgres database (DATA ONLY)
pg_dump --data-only dbname > dump.sql
# Create a new database that will replace your old one
psql -U pguser --password
=# CREATE DATABASE newdb;
# typically, I would just delete from within vscode or on the command line manually. Here's a fancy way
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
# Create new migration files now that old ones aren't there, will only create 1 per app
python manage.py makemigrations
# Update the database in your settings file
vim settings.py
python manage.py migrate
# load the data back into the now structured databae
psql newdb < dump.sql
# Note this works since psql will just give an error for the data migrate has already written. For example
# ERROR: duplicate key value violates unique constraint "django_content_type_pkey"
# DETAIL: Key (id)=(1) already exists.
# CONTEXT: COPY django_content_type, line 1
# ERROR: duplicate key value violates unique constraint "auth_permission_pkey"
# DETAIL: Key (id)=(1) already exists.
# CONTEXT: COPY auth_permission, line 1
# ERROR: duplicate key value violates unique constraint "django_migrations_pkey"
# DETAIL: Key (id)=(1) already exists.
# CONTEXT: COPY django_migrations, line 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment