15.2.156. camcops_server.cc_modules.cc_sqlalchemy

camcops_server/cc_modules/cc_sqlalchemy.py


Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.


SQLAlchemy helper functions and constants.

We define our metadata Base here, and things like our index naming convention and MySQL table formats.

A few random notes:

  • SQLAlchemy will automatically warn about clashing columns:

    SAWarning: On class 'Thing', Column object 'b' named directly multiple
    times, only one will be used: b, c. Consider using orm.synonym instead
    
class camcops_server.cc_modules.cc_sqlalchemy.DeclarativeAndABCMeta(name, bases, namespace, **kwargs)[source]

Metaclass for classes that want to inherit from Base and also ABC:

class camcops_server.cc_modules.cc_sqlalchemy.MutableDict[source]

Source: https://docs.sqlalchemy.org/en/14/orm/extensions/mutable.html

classmethod coerce(key: str, value: Any) Any[source]

Convert plain dictionaries to MutableDict.

camcops_server.cc_modules.cc_sqlalchemy.assert_constraint_name_ok(table_name: str, column_name: str) None[source]

Checks that the automatically generated name of a constraint isn’t too long for specific databases.

Parameters
  • table_name – table name

  • column_name – column name

Raises

AssertionError, if something will break

camcops_server.cc_modules.cc_sqlalchemy.get_all_ddl(dialect_name: str = 'mysql') str[source]

Returns the DDL (data definition language; SQL CREATE TABLE commands) for our SQLAlchemy metadata.

Parameters

dialect_name – SQLAlchemy dialect name

camcops_server.cc_modules.cc_sqlalchemy.get_table_ddl(table: sqlalchemy.sql.schema.Table, dialect_name: str = 'mysql') str[source]

Returns the DDL (data definition language; SQL CREATE TABLE commands) for a specific table.

Parameters
  • table – Table to dump.

  • dialect_name – SQLAlchemy dialect name.

https://stackoverflow.com/questions/2128717/sqlalchemy-printing-raw-sql-from-create

camcops_server.cc_modules.cc_sqlalchemy.hack_pendulum_into_pymysql() None[source]

Hack in support for pendulum.DateTime into the pymysql database interface.

See https://pendulum.eustace.io/docs/#limitations.

camcops_server.cc_modules.cc_sqlalchemy.log_all_ddl(dialect_name: str = 'mysql') None[source]

Send the DDL for our SQLAlchemy metadata to the Python log.

Parameters

dialect_name – SQLAlchemy dialect name

camcops_server.cc_modules.cc_sqlalchemy.make_file_sqlite_engine(filename: str, echo: bool = False) sqlalchemy.engine.base.Engine[source]

Create an SQLAlchemy Engine for an on-disk SQLite database.

camcops_server.cc_modules.cc_sqlalchemy.make_memory_sqlite_engine(echo: bool = False) sqlalchemy.engine.base.Engine[source]

Create an SQLAlchemy Engine for an in-memory SQLite database.

camcops_server.cc_modules.cc_sqlalchemy.sql_from_sqlite_database(connection: sqlite3.Connection) str[source]

Returns SQL to describe an SQLite database.

Parameters

connection – connection to SQLite database via sqlite3 module

Returns

the SQL