Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

makemigrations or migrate while server is runnig

Tags:

django

I'm wondering how we can handle database migration in django while the site in production as while developing we stop the server then make changes in database then rerun the server I think it may be stupid question but I am learning by myself and can't figure it out thanks in advance.

like image 882
Ahmed Elshorbagy Avatar asked Jun 26 '17 14:06

Ahmed Elshorbagy


People also ask

What is difference between migrate and Makemigrations?

migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

When should you run Makemigrations?

When to run makemigrations. You'll want to run python manage.py makemigrations w henever you make a change to a model, even if it is updating the description on a field. Adding or updating functions inside of a model does not need a migration, but you can still run makemigrations just in case.

What happens if I delete migrations?

Deleting migration files means losing your history. This historical info is recorded in the django_migrations table in your database. if you delete migration files, you will get dependency errors. So Don't try to lose your history by deleting your migration files.


1 Answers

You can connect to the server using ssh and run commands to migrate without stopping the server and once you are done, you restart the server.

python manage.py makemigrations

and then

python manage.py migrate

and then restart the server.

for example: in case of nginx and gunicorn

sudo service gunicorn restart
sudo service nginx restart
like image 81
Astik Anand Avatar answered Nov 02 '22 23:11

Astik Anand