2
  1. I have a locally running Django 1.7 app with some tests, connecting to MySQL
  2. I configured Travis CI with this repo

Question:

  • I want to have to have a separate database for Travis , that is different from the one I use for development.

  • I tried adding separate settings in settings.py : default (for use with tests) and development (for use in dev boxes); and thought .travis.xml would use the 'default' when it ran migrate tasks.

But Travis CI errors out with the error : django.db.utils.OperationalError: (1045, "Access denied for user 'sajay'@'localhost' (using password: YES)")

I have no idea why it is trying to access my development db settings? I checked django1.7 docs, googled around but no luck.

Appreciate any help, Thanks

My settings.py database section looks like the below :

DATABASES = {
'default': {
    'ENGINE':'django.db.backends.mysql',
    'NAME':'expenses_db',
    'USER':'root',
    'PASSWORD':'',
    'HOST':'127.0.0.1',
    'PORT':'3306',
},
#    'development': {
#        'ENGINE':'django.db.backends.mysql',
#        'NAME':'myapp_db',
#        'USER':'sajay',
#        'PASSWORD':'secret',
#        'HOST':'127.0.0.1',
#        'PORT':'3306',
#    },
}

Note : When the 'development' section is commented, Travis CI build is green

My .travis.yml is pasted below:

language: python

services:
- mysql

python:
- "2.7"

env:
- DJANGO_VERSION=1.7 DB=mysql

install:
- pip install -r requirements.txt
- pip install mysql-python

before_script:
- mysql -e 'create database IF NOT EXISTS myapp_db;' -uroot
- mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';" -uroot
- python manage.py migrate


script:
- python manage.py test
Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
sajay
  • 21
  • 4
  • Refered to the below link for django1.7 multiple db settings : https://docs.djangoproject.com/en/1.7/topics/db/multi-db/ – sajay Oct 20 '14 at 14:30

2 Answers2

3

The problem you are getting is because you haven't got the right database name and settings for Travis CI. First you will need to separate out your settings between Travis and your project. To do that I use an environment variable called BUILD_ON_TRAVIS (alternatively you can use a different settings file if you prefer).

settings.py:

import os

#Use the following live settings to build on Travis CI
if os.getenv('BUILD_ON_TRAVIS', None):
    SECRET_KEY = "SecretKeyForUseOnTravis"
    DEBUG = False
    TEMPLATE_DEBUG = True

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'travis_ci_db',
            'USER': 'travis',
            'PASSWORD': '',
            'HOST': '127.0.0.1',
        }
    }
else:
    #Non-travis DB configuration goes here

Then in your .travis.yml file in the before_script sections you will need to use the same database name as in the DATABASES settings. We then just have to set the environment variable in the .travis.yml file like so:

env:
  global:
     - BUILD_ON_TRAVIS=true
  matrix:
     - DJANGO_VERSION=1.7 DB=mysql

EDIT:

There is now an environment variable set by default when building on Travis. Using this environment variable we can more simply solve the problem:

settings.py:

import os

#Use the following live settings to build on Travis CI
if os.getenv('TRAVIS', None):
    #Travis DB configuration goes here
else:
    #Non-Travis DB configuration goes here

Doing it this way is preferable as we no longer have to define the environment variable ourselves in the .travis.yml file.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Note `BUILD_ON_TRAVIS` is not a valid environment var according to https://docs.travis-ci.com/user/environment-variables/ . Should be just `TRAVIS`. – shacker May 23 '16 at 22:53
  • @shacker, I'm defining the environment var in `.travis.yml` file before using it. Thanks for pointing out that there's an environment var `TRAVIS` that is now defined as it makes solving this problem easier. I edited to reflect this. – shuttle87 May 24 '16 at 15:11
0

Followed the approach suggested in http://www.slideshare.net/jacobian/the-best-and-worst-of-django to separate the settings for different environments

I checked out How to manage local vs production settings in Django? but did not get a clear answer. Thanks

Community
  • 1
  • 1
sajay
  • 21
  • 4