I have two boxes: dev and production. I need a very simple method for working on the dev machine and then pushing to production.
Environment
Use direnv
to maintain environment based on directory.
Use two seperate .envrc
files on dev and production. Don’t check-in to version control.
Testing and Syncing Databases
Create a test case:
# phisaverweb/tests.py
class TestEmails(TestCase):
fixtures = ['phisaverweb/fixtures/email-tests.json']
def test_send(self):
email_weekly_summary()
print ("hi")
Tests use a seperate database. To get data to test against, it’s best to programmatically create it as part of test setup.
But to play with the production database, you want to grab it’s current data. First, dump the current database. If you get django test fixtures contenttypes.ContentType unique constraint duplicate key
type error, I don’t understand why but “natural primary” and “natural foreign” (see below) fixes it. Also some background.
# original server
python manage.py dumpdata --indent 4 --natural-primary --natural-foreign > dump.json
# new server
scp server1.com:dump.json .
# psql> create database phisaver if required
manage.py migrate # create database schema
manage loaddata dump.json
Then run the server to play locally or run the tests via python manage.py tests
. In VSCode you can make a launch task:
{
"name": "Django Tests",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"test",
"--noinput",
"--keepdb", # don't destroy db on exit
"phisaverweb.tests" # run just these tests
],
"django": true,
"env": {"PYTHONPATH":"${workspaceFolder}/.."}
},