Django Web App on Azure, does not pull remote sqlite3 database - django

I have a django web app up and running with Azure. Now, when there are changes in the remote database, I cannot pull it so that it is local (It doesn't detect any changes even though the sqlite3 file is included in the project and the commits). And if I try to sync or push, the remote database gets reset/overwritten with my local database. I cannot simply do the git ignore trick because I need to push the database after I created new models (I'm assuming the remote database does not have my local migrations) Does anyone know how to fix this issue? Here are my database settings in the settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': path.join(PROJECT_ROOT, 'db.sqlite3'),
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
My git ignore file:
# Python cache
__pycache__/
*.pyc
# PTVS analysis
.ptvs/
# Build results
/bin/
/obj/
# User-specific files
*.suo
*.user
*.sln.docstates
# Auto-generated virtual environment
/env/
# Auto-generated web.config
/web.config
# Auto-collected static files folder
/static/
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac desktop service store files
.DS_Store

This is surely a very bad idea. Your remote database will surely be updated all the time, with live data created by the users of your website. Why would you want to override that with stale data from dev?
And this:
I'm assuming the remote database does not have my local migrations
is not only false, it's quite literally missing the whole point of migrations, which is that you run the same code in dev and prod to keep the tables in sync.
Finally, sqlite isn't really suitable for production use in anything but a toy site. Use a proper db like Postgres.

Related

Heroku Postgres - How come my production database is working, even though not listed in settings.py - Django?

I configured my database in Heroku several months ago so don't remember exact steps I took. I'm using the Heroku-Postgres add-on: https://devcenter.heroku.com/articles/heroku-postgresql
I have a DATABASE_PASS listed as a config var in Heroku. And I have a config var for DATABASE_URL
In my settings.py file I only have the following as it relates to my database. Why is my app still working in production on Heroku if DATABASES variable is referring to localhost only?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_dev',
'USER': 'postgres',
'PASSWORD': os.environ.get('DATABASE_PASS'),
'HOST': 'localhost',
'PORT': '5410',
}
}
The Heroku-Postgres documentation states the following:
The value of your app’s DATABASE_URL config var might change at any time. You should not rely on this value either inside or outside your
Heroku app.
Am I doing something wrong? Should I not rely on DATABASE_URL as a config var?
Additional Detail - I am using the django-heroku pip package.
Why is my app still working in production on Heroku if DATABASES variable is referring to localhost only?
Additional Detail - I am using the django-heroku pip package.
django-heroku does quite a lot, including setting up your database from the DATABASE_URL environment variable automatically:
This will automatically configure DATABASE_URL, ALLOWED_HOSTS, WhiteNoise (for static assets), Logging, and Heroku CI for your application.

Django sphinx documentation does not read environment variables in settings file

I want to document my cookiecutter django project with sphinx. The problem is that when running make html sphinx gives me problems reading the config file. It says django.core.exceptions.ImproperlyConfigured: Set the USE_DOCKER environment variable
When not calling django.setup() it also throws me an error with my envs: django.core.exceptions.ImproperlyConfigured: Set the POSTGRES_DB environment variable When I hardcode them, the error goes on to complain about the next environment variable. I can't hardcode them all into the config file, that is not an option.
My environment variables are properly configured. When I print them out running my localhost they are there. It seems that somehow sphinx cannot process them. I am also using docker, so maybe that could interfere but I don't know. Here are parts of my sphinx config:
sys.path.insert(0, os.path.abspath('..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings.local'
Here are parts of my local settings:
# ------------------------------------------------------------------------------
# https://django-extensions.readthedocs.io/en/latest/installation_instructions.html#configuration
INSTALLED_APPS += ['django_extensions'] # noqa F405
# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
# 'default': env.db('DATABASE_URL'), # This was the default value, but modification below seemed necessary
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': env("POSTGRES_DB"),
'USER': env("POSTGRES_USER"),
'PASSWORD': env("POSTGRES_PASSWORD"),
'HOST': env("POSTGRES_HOST"),
'PORT': env("POSTGRES_PORT"),
}
}
Grateful for any kind of help. Thanks in advance!
So it turned out that apparantly some applications can't use environ to read from settings from file. Another problem was that docker alpine image doesn't come with make pre installed.
I got it to work by installing make in my docker image (apk add make) and building the doc while spinning up the container with docker-compose -f local.yml run django make -C ./docs html. Thanks goes out to uzi0espil for leading me there.
For more information see:
https://github.com/pydanny/cookiecutter-django/issues/1747
https://github.com/cookiecutter/cookiecutter/issues/1251
I had this same issue, related to the RTD build passing, but not rendering the whole thing properly.
Issue
My code had:
os.environ['MY_ENV_VAR']
The RTD build would pass, but not render any autodoc elements. The imports failed due to not being able to identify the key of ['MY_ENV_VAR'], raising a KeyError(key).
This error can be found by viewing the RAW output of build logs at your RTD account homepage under 'Builds'.
Solution
To resolve this, you can add the required environment keys ("POSTGRES_DB" for example), in your RTD account at Admin/Environment Variables. Here you can add the required keys, and in my experience, you can add nonsense values for the value. The RTD build will now fully pass as it can identify the Key(s) that you passed when calling os.environ[].

synchronize the local database with deployed project in Heroku

When I logged in the project deployed on Heroku with a registered account, Learning Log, it starts freshly without any data I have entered in the local project.
I'm aware that Heroku employed Postgres while my local database is python's contributed sqlite.
Additionally, I ignored the *.sqlite3 in git repository following the tutorial.(When undo the ignore, it not works)
I prefer to work locally, push local data to remote and fetch remote to local.
How could I synchronize remote with local?
You can connect your database for your server in local runserver.
Just change your database setting for your server ip. (first you have to open 5432 port in your for your local ip)
like below
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_server_db_name',
'USER': 'your_server_user',
'PASSWORD': 'your_server_db_pw',
'HOST': 'your_server_ip',
'PORT': '5432',
}
}
Then the db is synchronized. However, I don't recommend this way. Whenever you migrate / or do sth concerned with db, it must be crashed because you can't modify code both.
So if you want more active database from real db, you just use pg_dump for dumping sql and add it to your local database.

Production and development database in Django 1.8

How do I seperate production and development database in Django 1.8?
For now I'm doing the naive way using the same database for both dev and production. When deploying, the dev database are copied over production (SQLite).
What's the correct way to do this in Django 1.8? Additionally, how can I update production tables without losing previous data?
Well, first off, I would really recommend you to not use sqlite in production. If you insist, what I would do would be to make a copy of the sqlite file outside of the project structure, and use an additional settings file for live only, such as settings_live.py and override the DATABASES setting with the correct path:
from myproject.settings import *
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ..., # the correct path file name here
}
}
Make sure that you specify the correct settings file in your production environment. For example, for wsgi:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', wsgi_app + '.settings_live')
Update
Marina Mele has written a thorough article on the dual environment setup "thing".
In my setup, I have 2 or 3 different virtualenvs.
I also have a main settings.py with the basic parameters and different settings that inherit from the main and cater for each virtualenv.
The idea is described here and here.
In my .virtualenvs/bin/activate file I append the path to the relevant settings file:
DJANGO_SETTINGS_MODULE=air.settings.settingst
export DJANGO_SETTINGS_MODULE
This way, I have a separate database for each environment for the same project.

Unable to sync database in Django

I'm trying to create my first web application using Django.
I'm following the instructions given in the official Django documentation. (The first tutorial: https://docs.djangoproject.com/en/1.5/intro/tutorial01/)
The server started successfully, but when I run the following command (from command prompt):
python manage.py syncdb
I'm getting the following error
ORA-12560: TNS: protocol adapter error
I'm able to access Oracle homepage by going to 127.0.0.1:8080/apex/
I have Oracle 10g database express edition installed on my system, along with the Oracle instant client. After doing a Google search for the above error code, I've been presented with a variety of solutions — about adding ORACLE_HOME in environment variables and pointing that to
E:\oraclexe\app\oracle\product\10.2.0\server\bin;
But that doesn't fix the issue.
I have also added
E:\instantclient;E:\oraclexe\app\oracle\product\10.2.0\server;
to the PATH variable.
I have Python 2.7 installed along with cx_Oracle. I have successfully checked the installation of cx_Oracle by importing it in python. Some of the solutions speak about an ORACLE_SID. I'm unable to find that.
As you can see, I'm a complete newbie to stackoverflow and programming. It's really discouraging to come across such a problem in my very first program. So please help me solve this issue.
To use SQLite3, you just need to modify your settings.py DATABASES field to something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/path/to/mysite/database.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Yo do not need to download SQLite explicitly. Django has inbuilt SQLite3 database engine.