Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do database migration using python script?

Tags:

python

django

is it possible to do migration from python script?

I am trying to use django on Heliohost where there is no shell but I can use python script.

something like

from django import shell
shell.main(['mysite/manage.py', 'migrate'])

Edit

Using input from @Shadow, I tried putting this code in views.py file

def migrate(request):
    django.setup()
    from django.core.management import call_command
    call_command("migrate", interactive=False)
    return HttpResponse("Final Migration Successful")

Then visited the url mapped to migrate which returned "Final Migration Successful"

then I tried the database like

from .models import Question


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

Gives me error:

(1146, "Table 'usr_mydb.polls_question' doesn't exist")

From Django Tutorial:

Bypassing manage.py

If you’d rather not use manage.py, no problem. Just set the DJANGO_SETTINGS_MODULE environment variable to mysite.settings, start a plain Python shell, and set up Django:

>>> import django
>>> django.setup()

If this raises an AttributeError, you’re probably using a version of Django that doesn’t match this tutorial version. You’ll want to either switch to the older tutorial or the newer Django version.

You must run python from the same directory manage.py is in, or ensure that directory is on the Python path, so that import mysite works.

For more information on all of this, see the django-admin documentation.

But how to do python manage.py <command> using the django.setup() is nowhere in the documentation.

like image 339
Rahul Avatar asked Oct 12 '17 04:10

Rahul


People also ask

What is data migration in Python?

I think the idea of data migration is similar to git commit. Every migration is a descriptive file that documents changes you've made to the database, such as adding new columns or tables. Let's dive right into it! # python version python==3.10 # requirements.txt alembic==1.7.6 sqlalchemy==1.4.25 python-dotenv==0.19.0

What are the different types of migration scripts?

Migration scripts can be forward or “up” migrations that go to a newer version of a database, or backward, or “down” migrations to fall back to a previous version of a database. Any manual migration script is generally used to move between consecutive versions of a database.

What is a database migration?

Migrations are mainly for keeping the data model of you database up-to-date, but a database is more than just a data model. Most notably, it’s also a large collection of data. So any discussion of database migrations wouldn’t be complete without also talking about data migrations.

Can I use migrations to load data?

While you could use migrations to load data, they are mainly about migrating data and/or data models.


Video Answer


1 Answers

You can use call_command to run django manage commands.

Here is an example of using it to call migrate;

from django.core.management import call_command
call_command("migrate", interactive=False)

I have interactive=False because this runs on a continual integration server - but depending on your usage you may wish to omit it.

like image 120
Shadow Avatar answered Sep 20 '22 20:09

Shadow