How to check if database is present and migrated in Elastic Beanstalk? - django

I have been trying to troubleshoot 500 error, on a Django REST framework + ReactJS app. I think the migrate is not executing correctly.
The db-migrate.config file:
container_commands:
01_migrate:
command: "source $PYTHONPATH/activate pipenv run python manage.py migrate"
leader_only: true
DB_SETTINGS: eventually I want to change this to RDS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
eb deploy is successful.
I still run into the 500 error, on localmachine the same endpoint works perfectly.
How can I check whether the database on Elastic Beanstalk has the necessary migrations? Any other causes I should be looking into?

Related

MongoDB connection with Django i am getting below error after configuration

raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: 'django' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
The best way to use MongoDB with Django is by using DJONGO package. It is a kind of django package developed only for the purpose to make Django compatible with MongoDB.
How to install djongo
pip install djongo
Also, Addi it requirement.txt to capture requirements for next time.
Update database settings as
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'CLIENT': {
'host': 'your-db-host',
}
}
}
Run migrations
python manage.py makemigrations <app_name>
and then run migrate changes to DB
python manage.py migrate
djongo is compatible with Python >=3.6 and MongoDB>=3.4.
After followed above steps i install python -m pip install pymongo[srv] it works i can run django project.
After applying default migrations i was getting errors in console so i followed below step.
Why I am getting "Not Implemented Error: Database objects do not implement truth value testing or bool()." while running makemigration cmd in django
Now i am not getting any kind of error and i am able to run django project with MongoDB successfully.

Beanstalk not running makemigrations

I am trying to deploy my Django server to Amazon through Beanstalk, and so far it's been ok except I've made a few changes to my models and when I deployed the instance on Aws is not updating accordingly.
I have followed the guide from amazon and created a file named db-migrate.config with the content
container_commands:
01_migrate:
command: "django-admin.py migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: myAppName.settings
but obviously it doesn't seem to be working. I tried to access my django instance on Aws with
eb ssh myAppEnv
but when I enter I saw nothing and I couldn't find the code for my django server anywhere, thus i am unable to debug and manually run makemigrations also.
Anyone can help me with this?
Only way i was able to fix this was by specifying the exact applications i wanted to makemigrate and migrate.
02_migrateapps:
command: "source /opt/python/run/venv/bin/activate && python3 manage.py makemigrations organisations shows media exhibitors && python3 manage.py migrate --noinput"
leader_only: true
It's a real pain but each time i make a new app i'll need to add it to the list of makemigrations.
Hope this helps.

Issue with Django migrations when deploying in Prod via AWS Beanstalk

I am struggling with Django migrations when deploying into several environments. I have the following ones:
Dev locally on my laptop with sqlite3 database
Test in AWS (deployed via Beanstalk) connected to AWS RDS Production Database
Prod in AWS (deployed via Beanstalk) connected to AWS RDS Production Database.
My workflow is once I have done my dev locally, I deploy to the Test instance, which should make the backward compatible changes to the prod database, where I run my tests, before deploying to the Prod instance.
In my .gitignore, I have excluded all the migrations folder as when I used to apply the same migrations I created in dev, it lead to some inconsistencies and errors during deployment. I thought it would be cleaner to recreate the migrations on the test or prod servers when deploying.
In my .ebextensions I then have the following config that are executed when deploying the application:
01_makemigration:
command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations --noinput"
leader_only: true
02_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: true
However when I deploy to the Test platform after having made changes to my models, the migrations don't seem to happen. I have tried to run those commands manually but it tells me that everything is up to date and no migrations are to be applied.
I have also tried to "rebase" the migrations using the following sequence:
On the Database:
> delete from django_migrations;
On the App:
> rm -rf calc/migrations/
> source /opt/python/run/venv/bin/activate
> python manage.py migrate --fake
> python manage.py makemigrations calc
> python manage.py migrate --fake-initial
But it does not seem to work either.
Would someone be able to advise what is the right way of applying migrations in that type of scenario? I would prefer to avoid committing dev migrations and create new and clean ones on the test environment (and the prod database) but I dont seem to find the right way to do it.
Thank you
Regards
Yann
You must not exclude your migrations from git. They are a part of your code base and need to be deployed with your app. You shouldn't be running makemigrations in prod, only migrate.

Django app deployed on elastic beanstalk with MySQL RDS - tables never get created

I have followed every guide, and tutorial, on setting up RDS MySQL for my Django app. Everything works fine locally. When I deploy, I get no errors. My website runs. But when I try to access the restful framework I have setup, or I try to log into the admin page, I get a ProgrammingError that the table doesn't exist (none of the tables exist).
Access to the database doesn't seem to be the problem, all of my RDS environment variables are set up, the security group is set up, etc. I can even access the database from my local mysql client and I can see that the database has no tables in it.
I have commands set up in my config file to run 'django-admin.py makemigrations' and 'django-admin.py migrate', I even tried changing this to 'python manage.py ...' etc., it seems like the commands never work.
When I ssh into my elastic beanstalk environment, I can get into the mysql using 'mysql -u my_username'. However, if I navigate to the folder where my app is, and try to run 'python manage.py makemigrations' manually, I get this: django.db.utils.OperationalError: (1045, "Access denied for user 'my_username'#'localhost' (using password: YES)")
Here is my config...
requirements.txt
virtualenv==15.0.1
Django==1.9.5
django-compressor==2.0
django-model-utils==2.4
djangorestframework==3.3.2
dj-database-url==0.4.0
gunicorn==19.4.5
MySQL-python==1.2.5
jsonfield==1.0.3
.ebextentions/01_packages.config
packages:
yum:
git: []
gcc: []
mysql: []
mysql-devel: []
python-devel: []
.ebextentions/project.config
container_commands:
01_makemigrations:
command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations --noinput"
leader_only: true
02_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
leader_only: true
03_createsu:
command: "source /opt/python/run/venv/bin/activate && python manage.py createsu"
leader_only: true
04_collectstatic:
command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
Please help. Thank you!
See if the RDS security group is configured in EC2 security groups for your elastic beanstalk. It should look like this:
EC2 security groups: your-rds-security-group-name, your-Elastic-Beanstalk-Security-Group
Make sure that the same settings are used when migrating and running!
I had a similar problem but I figured out that I used local settings (from manage.py) when migrating. But for running i used production settings (defined in wsgi.py). That meant that a local database was migrated but a production one was used but was never migrated, thus tables were never created even if migration command returning a success. I had to change my django.config:
container_commands:
01_makemigrations:
command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations"
leader_only: true
02_migrate:
command: "source /opt/python/run/venv/bin/activate && python manage.py migrate"
leader_only: true
to:
container_commands:
01_migrate:
command: "django-admin migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: fund.productionSettings
as recommended here.

Django-Cms 2.4 South Migration on AWS Beanstalk

I have a Django app (still in development) built with Django-Cms 2.4 and its other dependencies, the problem i'm having is that during deployment to AWS Beanstalk Environment, the 01_syncdb command below fails whenever i add a new app to INSTALLED_APPS in settings.py.
In the .config file
I have the in the container commands:
01_syncdb:
command: "django-admin.py syncdb --noinput"
leader_only = True
02_migrate:
command: "django-admin.py migrate --noinput"
leader_only = True
log
2013-08-20 10:21:46,812 [DEBUG] (19029 MainThread) [commandWrapper.py-60] [root commandWrapper main] Command result: {'status': 'FAILURE', 'results': [{'status': 'FAILURE', 'config_set': u'Infra-EmbeddedPostBuild', 'returncode': 1, 'events': [], 'msg': 'Error occurred during build: Command 01_syncdb failed\n'}], 'api_version': '1.0'}
What could be wrong? Thanks
To others that encounter this problem, you might want to check if you have mysql-python (or whatever database driver compatible with the database your project is using) included in your requirements.txt so that the system will know that you need this installed in your environment. Django won't be able to communicate with the database without this package.
I also predict that your second command (02_migrate) will fail since the migrate command will not be recognized by django-admin.py. Use the following instead
02_migrate:
command: "python manage.py migrate --noinput"