Skip to content

Instantly share code, notes, and snippets.

@ekardon
Created January 26, 2022 00:09
Show Gist options
  • Save ekardon/8239f49b5067ec4cd3da5911bfaae3f6 to your computer and use it in GitHub Desktop.
Save ekardon/8239f49b5067ec4cd3da5911bfaae3f6 to your computer and use it in GitHub Desktop.
This script will let you easily undone migrations, remove migration files, re-migrate and createsuperuser in one go.
<#
This script will let you easily undone migrations, removing migration files, re-migrate and createsuperuser.
It's cumbursome to do this all the time by hand. Well, normally you wouldn't need to do this very frequently,
but for beginners like me, it is convinient to have a script to automate this.
#>
# Find manage.py and set project folder accordingly
$manager = Get-Item -Path ".\*", ".\*\*" | Where-Object {$_.name -match "manage.py"}
$project_folder = $manager.DirectoryName
$migration_folders = $project_folder + "\*\migrations\*"
Write-Host Project Folder: $project_folder
# Get all app names which has migration
py $manager showmigrations | Where-Object {$_ -Match "^[a-z]+"} -OutVariable apps | Out-Null
Foreach ($app in $apps) {
Write-Host $($apps.IndexOf($app) + 1) - $app
}
# Let user select which apps migration to undone
$user_selected_apps = Read-Host "Enter the migration apps you would like to clean seperated by comma or leave blank for all (1,4,6)"
If ($user_selected_apps -eq "") {
$apps_to_undone = $apps
} Else {
Foreach ($app_id in $user_selected_apps.Split(",")) {
$apps_to_undone += $apps[[int]$app_id - 1]
}
}
# Print apps to undone
Write-Host "Following migration apps will be cleaned out!"
Write-Host $apps_to_undone
# Clean all migrations for each given app from database
Foreach ($app in $apps_to_undone) {
Write-Host Cleaning $app
py $manager migrate $app zero
}
# Remove all migration files that has been done so far
# TODO: Remove last migration files based on git stage?
Write-Host "Removing all migration files based on given migration app!"
$files_to_remove = Get-ChildItem -Path $migration_folders | Where-Object {$_.name -Match "[0-9]{4}_.*\.py"}
foreach ($file in $files_to_remove) {
Write-Host Removing: $file
Remove-Item $file
}
# Create new migrations for apps
py $manager makemigrations
# Apply migrations
py $manager migrate
# Create superuser
# Unfortunately we cannot create superuser with manage.py createsuperuser command, because --password parameter
# does not exist. Maybe it can be implemented as a standalone app for later
# TODO: Create a custom createsuperuser command with --password parameter [https://stackoverflow.com/a/42491469]
$create_superuser = @(
"from django.contrib.auth import get_user_model;"
"User = get_user_model();"
# My custom user model takes email and passwords only!
# Keep in mind in default user model, it also requires username!
"User.objects.create_superuser('[email protected]', '123456')"
) -join " "
# Cannot pass above python code with -c parameter, because django requires some environmental variables to be set.
# Thus, "manage.py shell -c" is needed.
py $manager shell -c $create_superuser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment