14.2.82. camcops_server.cc_modules.cc_sqlalchemy

camcops_server/cc_modules/cc_sqlalchemy.py


Copyright (C) 2012-2019 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 <http://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
    
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.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_debug_sqlite_engine(echo: bool = True) → sqlalchemy.engine.base.Engine[source]

Create an SQLAlchemy Engine for an in-memory SQLite database, optionally switching on the SQL echo feature for logging.

camcops_server.cc_modules.cc_sqlalchemy.make_memory_sqlite_engine() → sqlalchemy.engine.base.Engine[source]

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