15.2.133. camcops_server.cc_modules.cc_sqlalchemy


Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).

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:

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.

  • table_name – table name

  • column_name – column name


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.


dialect_name – SQLAlchemy dialect name

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.


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.


connection – connection to SQLite database via sqlite3 module


the SQL