Schema

A mwdb.Schema represents a pool of connections to a database. New connections will be spawned as needed. A mwdb.Schema can execite queries within the context of a mwdb.Schema.transaction() or directly via mwdb.Schema.execute().

class mwdb.Schema(engine_or_url, *args, **kwargs)[source]
TABLE_MAP = {'logging_logindex': 'logging', 'logging_userindex': 'logging', 'revision_userindex': 'revision'}

Maps the weird view names on labs back to the table names in the production database.

public_replica = None
bool
True if the schema is part of a public replica with _userindex and _logindex views.
transaction()[source]

Provides a transactional scope around a series of operations on a sqlalchemy.Session through the use of a https://docs.python.org/3/reference/compound_stmts.html#the-with-statement If any exception is raised within the context of a transation session, the changes will be rolled-back. If the transactional session completes without error, the changes will committed.

Example:
>>> import mwdb
>>> enwiki = mwdb.Schema("mysql+pymysql://enwiki.labsdb/enwiki_p" +
...                      "?read_default_file=~/replica.my.cnf")
>>>
>>> with enwiki.transation() as session:
...     print(session.query(enwiki.user)
...           .filter_by(user_name="EpochFail")
...           .first())
...
(6396742, b'EpochFail', b'', None, None, None, None, None, None,
 None, None, None, b'20080208222802', None, 4270, None)
execute(clause, params=None, **kwargs)[source]

Executes a a query and returns the result.

Example:
>>> import mwdb
>>> enwiki = mwdb.Schema("mysql+pymysql://enwiki.labsdb/enwiki_p" +
...                     "?read_default_file=~/replica.my.cnf")
>>>
>>> result = enwiki.execute("SELECT * FROM user " +
...                         "WHERE user_id=:user_id",
...                         {'user_id': 6396742})
>>>
>>> print(result.fetchone())
(6396742, b'EpochFail', b'', None, None, None, None, None, None,
 None, None, None, b'20080208222802', None, 4270, None)
Parameters:
clause : str

The query to execute.

params : dict | list ( dict )

A set of key/value pairs to substitute into the clause. If a list is provided, an executemany() will take place.

**kwargs

Passed on to sqlalchemy