mirar.database package

Central module for database-related functions

Subpackages

Submodules

mirar.database.base_model module

Module to define PSQL database tables using sqlalchemy

class mirar.database.base_model.BaseDB[source]

Bases: PydanticBase

Base Database Table model, requiring an associated SQLalchemy table

get_available_unique_keys() list[Column][source]

Get the unique keys of the table which are present in the data

Returns:

unique keys

get_primary_key() str[source]

Get the primary key of the table

Returns:

primary key

get_unique_keys() list[Column][source]

Get the unique key of the table

Returns:

unique keys

insert_entry(duplicate_protocol: str, returning_key_names: str | list[str] | None = None) DataFrame[source]

Insert the pydantic-ified data into the corresponding sql database

Parameters:
  • duplicate_protocol – protocol to follow if duplicate entry is found

  • returning_key_names – names of the keys to return

Returns:

dataframe of the sequence keys

model_config: ClassVar[ConfigDict] = {'extra': 'ignore'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

sql_model: ClassVar[Type[Table]]
classmethod sql_model_exists_check(data: Any) Any[source]

Validator to ensure an sql model has been specified in the child class :param data: data to validate :return: data

update_entry(update_keys=None)[source]

Wrapper to update database entry. Users should override this function.

Parameters:

update_keys – keys to update

Returns:

None

classmethod validate_sql(value: Any, info: ValidationInfo) Any[source]

Validator to ensure that the field names of a pydantic model match the database table

Parameters:
  • value – value

  • info – field info

Returns:

value

exception mirar.database.base_model.DatabaseUpdateError[source]

Bases: ProcessorError

Exception for database update errors

class mirar.database.base_model.PydanticBase[source]

Bases: BaseModel

Base code pydantic model (no extra colunns!)

model_config: ClassVar[ConfigDict] = {'extra': 'ignore'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

mirar.database.base_table module

Module to define PSQL database tables using sqlalchemy

class mirar.database.base_table.BaseTable[source]

Bases: object

Parent class for database tables. Tables should inherit from this and DeclarativeBase.

property db_name

Name of the database. :return: None

get_primary_key() str[source]

Function to get primary key of table Returns: primary key

mirar.database.constants module

Central list of postgres constants

mirar.database.constraints module

Module for DBQueryConstraints to carefully specify postgres query constraints

class mirar.database.constraints.DBQueryConstraints(columns: str | list[str] | None = None, accepted_values: str | int | float | list[str | float | int | list] | None = None, comparison_types: str | list[str] | None = None)[source]

Bases: object

Object containing one or more postgres query constraints

add_constraint(column: str, accepted_values: str | int | float | tuple[float, float] | tuple[int, int], comparison_type: str = '=')[source]

Add a new constraint

Parameters:
  • column – column

  • accepted_values – accepted value for comparison

  • comparison_type – type of comparison, e.g ‘=’

Returns:

None

add_q3c_constraint(ra: float, dec: float, crossmatch_radius_arcsec: float, ra_field_name: str = 'ra', dec_field_name: str = 'dec')[source]

Add a q3c constraint

Parameters:
  • ra – ra of source

  • dec – dec of source

  • crossmatch_radius_arcsec – crossmatch radius in arcsec

  • ra_field_name – ra field name in database

  • dec_field_name – dec field name in database

Returns:

None

parse_constraints() str[source]

Converts the list of constraints to sql

Returns:

sql string

mirar.database.credentials module

This file contains the credential keys for the database.

mirar.database.engine module

Util functions for database interactions

mirar.database.engine.get_engine(db_name: str, db_user: str = None, db_password: str = None, db_hostname: str = '127.0.0.1', db_port: int = 5432, db_schema: str = 'public') Engine[source]

Function to create a postgres engine

Parameters:
  • db_user – User for db

  • db_password – password for db

  • db_name – name of db

  • db_hostname – hostname of db

  • db_port – port of db

  • db_schema – schema of db

Returns:

sqlalchemy engine

mirar.database.errors module

Database errors

exception mirar.database.errors.DataBaseError[source]

Bases: ProcessorError

Error relating to postgres interactions

mirar.database.q3c module

Module to create q3c extension and index on table

mirar.database.q3c.create_q3c_extension(db_name: str, table_name: str, ra_column_name: str, dec_column_name: str)[source]

Function to create q3c extension and index on table

Parameters:
  • db_name – Name of database

  • table_name – Name of table

  • ra_column_name – ra column name

  • dec_column_name – dec column name

Returns:

mirar.database.setup module

Central function to setup database

mirar.database.setup.setup_database(db_base: DeclarativeBase | BaseTable)[source]

Function to setup database

Parameters:

db_base – BaseTable

Returns:

None

mirar.database.utils module

Module for postgres utilities

mirar.database.utils.get_sequence_key_names_from_table(db_table: str, db_name: str) list[source]

Gets sequence keys of db table

Parameters:
  • db_table – database table to use

  • db_name – database name

Returns:

numpy array of keys