2

I am using sqlalchemy to connect to my database for my python API like so:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/dbname'

My problem is that other developers in this project have different database username/passwords. Whenever we push pull with git we have to reset the values to accommodate the developers database. Is there a way we can have sqlalchemy pull from a file? This way we could just add it to .gitignore.

Or is there an easier way to achieve this?

3
  • os.environ.get("DB_STRING",'mysql://username:password@localhost/dbname') then you just set an environmental variable ... Commented Jan 29, 2016 at 18:18
  • 1
    you can use instance folder: exploreflask.com/configuration.html#instance-folder Commented Jan 29, 2016 at 18:39
  • @rmn; didn't know about the instance folder; this is great. Commented Jan 29, 2016 at 19:12

2 Answers 2

3

You want to set an environment variable for this. Since each environment is unique to the machine it's running on, each team member will have their own connection string picked up from their respective environment. I'll show an example for Zsh shell, but Bash or other shells should be equivalent.

Create a file (if one doesn't exist already) called ~/.zshenv

In this file, add the line:

export DB_CONNECTION_STRING="mysql://username:password@localhost/dbname"

Note that there should be no spaces between the variable definition and its value (namely the equal sign has no spaces around it). This was an issue for me on Zsh.

To apply this variable run source ~/.zshenv. It will automatically load on restart, but we don't want to logout/login every time when changing an environment varialble. Verify the variable was defined by running printenv | grep DB_CONNECTION_STRING.

In flask, simply define the following (remembering to import os on top):

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_CONNECTION_STRING')

Using environment variables is really the way to go. I used to hard-code stuff, which is bad practice and as you can see, would be a problem for even something trivial like multiple users connecting to the same database.

EDIT: please see @rmn's comment about instance folder flask functionality; seems like a solid alternative to environment variables (of course, this is limited to the Flask application, environment variables can be used for anything)

Sign up to request clarification or add additional context in comments.

2 Comments

you should provide a default value for the os.environ.get as this will raise an exception if the key is not present ... (or at least print a more helpful error message than keyerror) +1 , I also think an environmental variable is the right way to go for this...
@JoranBeasley, I see your point, but what happens when you set a default is that there may be another database (local) that's not the same as the one you want to connect to; I've gotten in trouble for this in the past, but your suggestion is absolutely valid.
0

Alternative approach - each developer can have local settings in the file ignored by git, for example, local_config.py.

Then you use it like this:

try:
    import local_config
    app.config['SQLALCHEMY_DATABASE_URI'] = local_config.DB_URI
except ImportError:
    print "No Local config, use defaults"
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/dbname'

This can be useful if there are many parameters to set.

You can keep something like local_config.py.example in the repository where all the parameters are listed. Then everyone can rename it to the local_config.py and customize the parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.