यदि आप विकास मोड में हैं और आप बस सब कुछ (डेटाबेस, माइग्रेशन, आदि) रीसेट करना चाहते हैं, तो मैं अब्देलहामिद बा के उत्तर के आधार पर इस स्क्रिप्ट का उपयोग करता हूं। यह डेटाबेस (पोस्टग्रेज) की तालिकाओं को मिटा देगा, सभी माइग्रेशन फ़ाइलों को हटा देगा, माइग्रेशनों को फिर से चलाएगा और मेरे नए फिक्स्चर लोड करेगा:
#!/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"
रीसेट-db.sql फ़ाइल:
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 $$;
प्रवास.श फ़ाइल:
#!/usr/bin/env bash
cd ../../src
./manage.py makemigrations
./manage.py migrate
load_initial_fixtures.sh फ़ाइल:
#!/usr/bin/env bash
cd ../../src
./manage.py loaddata ~/path-to-fixture/fixture.json
बस अपने ऐप से मेल खाते रास्तों को बदलना सुनिश्चित करें। मेरे पास निजी रूप से Project_root / script / local नामक फ़ोल्डर में ये स्क्रिप्ट्स हैं, और django के स्रोत project_root / src में हैं।