# python-owsmysql
This package makes MySQL connection usage simple and unified, 
with minumum additional code in your application to support 
database connection in code or tests.

To use connection you have to call the "configure" method with 
environment variable and DB configuration.

How to use:

In config.py:
```python
DATABASES = {
    'default': {
        'user': 'kvolkov',
        'password': 'password',
        'host': 'digital.db.devorch.com',
        'database': 'art_relations',
    },
}
```
in application.py:
```python
from owsmysql import db
import config

db.configure('dev', config.DATABASES)
```
in code:
```python
from owsmysql import db

with db.default.session() as session:
    ...  # work with session as usial
```
Also it supports multiple databases. Change config:
```python
DATABASES = {
    'AR': {
        'user': 'kvolkov',
        'password': 'password',
        'host': 'digital.db.devorch.com',
        'database': 'art_relations',
    },
    'SS': {
        'user': 'kvolkov',
        'password': 'password',
        'host': 'digital.db.devorch.com',
        'database': 'sale_sheets',
    }
}
```
Note that you can combine it with "default" approach, to have 
default and additional database. Usage in code:
```python
from owsmysql import db

with db.connections.AR.session() as session:
    ...  # work with session as usial
```
or you can create shortcuts to access connections in code. 
connections.py:
```python
from owsmysql import db

ar_db = db.connections.AR
```
in code:
```python
from connections import ar_db

with ar_db.session() as session:
    ...  # work with session as usial
```
More interesting is how it can be used to write tests. Add this to 
conftest.py:
```python
from owsmysql import db

db.configure('test', None)
```
In all your tests now will be used sqlite database for all 
connections. But you can still use mysql for tests by providing 
environment variables (if present, it will be used to access DB
 in tests):
```python
TEST_DB_NAME = 'test_db'
TEST_DB_USER = 'test_user'
TEST_DB_PASSWORD = 'test_password'
TEST_DB_HOST = '127.0.0.1'
```
That is finally good idea as mysql and sqlite have a few known 
differences and it is much better to run tests on local mysql 
database (however it is much slower on schema creation). 

Also you can add configuration directly:
```python
import config

db.configure('test', config.DATABASES['test'])
```
format is same as for normal connection.

It your tests use:
```python
from owsmysql.utils import testdb

@testdb.empty
def test_smth():
    pass
```
That will create database and empty tables for all your models 
automatically. If you want to use fixtures:
```python
fixture1 = model()
fixture2 = model()
fixture3 = model()
fixture_set = (fixture2, fixture3)

@testdb.with_fixtures(fixture1, fixture_set)
def test_smth():
    pass
```
This will create database with fixtures. Fixtures must be created 
using factoryboy package. After that you can easily access fixtures
by names, combine them – decorator accepts plain models and any kind 
of iterables with models as well.. You can place fixtures in a 
separate module to make them available from different test modules.

## Connection pooling
Some recommendations on how to configure a connection pool can be found [here](https://github.com/theorchard/docs/blob/master/patterns/flask-eb/README.md)

