Forums

After pushing my code from the production server to a gitlab repository, I have a problem with postgresql and migrations in my django webapp

Now maybe I'm getting this wrong but here's my story :

Today, i decide to get the code that I've been writing in an uncontrolled manner on the production server to a gitlab repository.

So I create the repository on gitlab and push my code using

git init
git add .
git commit -m "message"
git remote add origin https://gitlab.com/xxxxxx
git branch -M master
git push -uf origin master

A few hours later I notice my django webapp is down and when i check in the error logs, it coincides roughly with the work I was doing on gitlab

The error message I get is

2023-08-08 21:26:31,218: Error running WSGI application
2023-08-08 21:26:31,226: django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

I reinstalled the module with

pip3 install psycopg2

Then i get the error message

psycopg2.errors.UndefinedTable: relation "app_pageview" does not exist
LINE 1: ..."app_pageview"."url", "app_pageview"."count" FROM "app_pagev...

pageview is a middleware I create to log the number of pageviews per page to the database

I have a look in the database with

python3 manage.py dbshell
\dt

and I see that there are no tables relating to the pageviews in the database

So I think , maybe if I see what is going on with the migrations

python3 manage.py migrate --plan

and this gives an error message

django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency app.0001_initial on database 'default'

And at this point I say, time to ask questions before I cause some serious damage as I don't really know what I'm doing

You can see the error message on https://www.clare-energy.ie

many thanks if you have some pointers

/ Colm

I don't think that pushing your code to a remote repository could break your web app. Did you introduce any other changes? Just to start with the missing module error -- if you use a virtual env, check if that's a correct one and it has all correct packages installed.

I didn't introduce any other changes, thats the strange thing.

When i opened the bash terminal using the "Start a console in this virtualenv" link on the "Web" page and did

pip2 list I could see that psycopg2 was no longer installed, which it had been previously. I reinstalled and now everything seems fine from the modules perspective

(env) 07:21 ~/boilerplate-code-django (master)$ pip3 list
Package             Version
------------------- ---------
asgiref             3.3.4
autopep8            1.5.6
certifi             2022.6.15
charset-normalizer  2.1.0
dj-database-url     0.5.0
Django              2.2.10
django-filter       2.4.0
django-tables2      2.4.0
djangorestframework 3.12.4
gunicorn            20.1.0
idna                3.3
importlib-metadata  4.8.1
Markdown            3.3.4
pip                 21.0.1
psycopg2            2.9.7
pycodestyle         2.7.0
python-decouple     3.4
pytz                2021.1
requests            2.28.1
setuptools          56.0.0
sqlparse            0.4.1
toml                0.10.2
typing-extensions   3.7.4.3
Unipath             1.1
urllib3             1.26.9
wheel               0.36.2
whitenoise          5.2.0
zipp                3.5.0

I think I'll remove the middleware for pageview and see if that improves anything.

This SO post helped me out, by doing some psql stuff I could rollback migrations

https://stackoverflow.com/questions/38996599/django-manage-py-migration-applied-before-its-dependency

i definitely did something funny with git which seems to have messed up the psql database tables somehow

I wouldn't expect anything in git to affect the database; the only way I can think of that it might cause problems would be if you somehow got your code into a state where it was no longer compatible with the database's current state, or where the migrations got changed somehow.