I have this Procfile
so when I deploy to Heroku my tables will get created using my and some random data will be seeded. It works.
web: knex migrate:rollback && knex migrate:latest && knex seed:run && node index.js
The problem is that for example when I insert some data via a POST request like this:
router.post('/', async function (req, res) {
await db.insert(req.body).into('customer');
res.send(req.body)
})
I get 200 success message, the data gets saved, but I lose the data after 30 minutes. If I check for the previous posted data is gone. If I do the same without this Procfile
and I create a table for example from the CLI then everything works like it should.
Why does this Procfile
cause my data to be lost?
Don't do maintenance tasks like this as part of your web
process. Dynos restart all the time for a variety of reasons, at least once per day, and your database resetting logic will get called whenever that happens.
If you want to do this only when you deploy a new version, consider doing it as a release phase task:
release: knex migrate:rollback && knex migrate:latest && knex seed:run
web: node index.js
Note however that release phase commands are also triggered by lots of things, including changes to most config vars (environment variables).
If you require finer control than this, maybe you should just run the rollback / migrate / seed commands manually. Alternatively, if you wish to reset your database on some schedule, e.g. daily or weekly, you can schedule that command to run using something like Heroku Scheduler.