django-db-call
==============

.. image:: https://travis-ci.org/paetzke/django-db-call.png?branch=master
  :target: https://travis-ci.org/paetzke/django-db-call
.. image:: https://coveralls.io/repos/paetzke/django-db-call/badge.png?branch=master
  :target: https://coveralls.io/r/paetzke/django-db-call?branch=master

Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com)
All rights reserved.

Django-db-call creates a database configuration for a django project from command line input.

It supports MySQL and PostgreSQL⁽¹⁾ right now.

It comes with two functions:

* ``from_call()``
* ``from_calls()``

You can install ``django-db-call`` via ``pip``.

.. code:: bash

    $ pip install django-db-call

An usage example:

.. code:: python

    from django_db_call import from_call
    
    DATABASES = from_call('mysql -h localhost -u root -p my_sweet_secret -D my_db')

That will become:

.. code:: python

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'PORT': '',
            'PASSWORD': 'my_sweet_secret',
            'HOST': 'localhost',
            'NAME': 'my_db',
            'USER': 'root'
        }
    }

You can change the connection name by passing a name for it:

.. code:: python

    DATABASES = from_call('mysql', connection='not_default')

And that is equivalent to:

.. code:: python

    DATABASES = {
        'not_default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '',
            'NAME': '',
            'PASSWORD': '',
            'PORT': '',
            'USER': ''
        }
    }

You can also pass additional arguments to be used in ``OPTIONS``.

.. code:: python

    DATABASES = from_call('mysql', autocommit=True)

And that will give you:

.. code:: python

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
            'OPTIONS': {
                'autocommit': True,
            },
        }
    }

For creating multiple databases connections you can use ``from_calls()``.

.. code:: python

    from django_db_call import from_calls
    
    DATABASES = from_calls(
        [
            ['mysql -h localhost -u root -p my_sweet_secret -D my_db'],
            ['mysql -h localhost -u root -p my_sweet_secret -D archive',
                {'connection': 'archive'}],
        ])

And that will result in:

.. code:: python

    DATABASES = {
        'default': {
            'PASSWORD': 'my_sweet_secret',
            'ENGINE': 'django.db.backends.mysql',
            'PORT': '',
            'USER': 'root',
            'HOST': 'localhost',
            'NAME': 'my_db'
        },
        'archive': {
            'PASSWORD': 'my_sweet_secret',
            'ENGINE': 'django.db.backends.mysql',
            'PORT': '',
            'USER': 'root',
            'HOST': 'localhost',
            'NAME': 'archive'
        }
    }

⁽¹⁾ It seems it is not possible to pass the password to use a specific database on ``psql``. You have pass it explicitly.

.. code:: python

    DATABASES = from_call('psql', password='my_sweet_secret')

.. image:: https://d2weczhvl823v0.cloudfront.net/paetzke/django-db-call/trend.png

