sqlcompyre package

Subpackages

Submodules

Module contents

class sqlcompyre.Config(*, ignore_tables: list[str] = <factory>, ignore_table_columns: dict[str, list[str]] = <factory>)[source]

Bases: BaseModel

Attributes:
model_extra

Get extra fields set during validation.

model_fields_set

Returns the set of fields that have been explicitly set on this model instance.

Methods

copy(*[, include, exclude, update, deep])

Returns a copy of the model.

model_construct([_fields_set])

Creates a new instance of the Model class with validated data.

model_copy(*[, update, deep])

Usage docs: https://docs.pydantic.dev/2.10/concepts/serialization/#model_copy

model_dump(*[, mode, include, exclude, ...])

Usage docs: https://docs.pydantic.dev/2.10/concepts/serialization/#modelmodel_dump

model_dump_json(*[, indent, include, ...])

Usage docs: https://docs.pydantic.dev/2.10/concepts/serialization/#modelmodel_dump_json

model_json_schema([by_alias, ref_template, ...])

Generates a JSON schema for a model class.

model_parametrized_name(params)

Compute the class name for parametrizations of generic classes.

model_post_init(_BaseModel__context)

Override this method to perform additional initialization after __init__ and model_construct.

model_rebuild(*[, force, raise_errors, ...])

Try to rebuild the pydantic-core schema for the model.

model_validate(obj, *[, strict, ...])

Validate a pydantic model instance.

model_validate_json(json_data, *[, strict, ...])

Usage docs: https://docs.pydantic.dev/2.10/concepts/json/#json-parsing

model_validate_strings(obj, *[, strict, context])

Validate the given object with string data against the Pydantic model.

construct

dict

from_orm

json

parse_file

parse_obj

parse_raw

schema

schema_json

update_forward_refs

validate

ignore_table_columns: dict[str, list[str]]
ignore_tables: list[str]
model_config: ClassVar[ConfigDict] = {}

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

sqlcompyre.compare_schemas(engine: Engine, left: str, right: str, include_views: bool = False, float_precision: float = 2.220446049250313e-16, collation: str | None = None, ignore_casing: bool = False) SchemaComparison[source]

Compare all tables from two schemas in the database. For multi-part schemas (e.g. for MSSQL), it is possible to only specify the first part of the schema and compare tables from all schemas. In MSSQL, this allows comparing entire databases.

Currently, the comparison only compares tables with the same name (and the same schema for multi-part schema comparisons).

Args:

engine: The engine to use to access the database. left: The name of the “left” schema from which to use tables. For multi-part schemas,

the schema may be specified as <database>.* which then references all schemas (and tables) in <database>. Note that this notation is merely a convention and general regex/glob notation is not supported.

right: The name of the “right” schema whose tables to compare to those of the “left”

one. The naming convention follows the convention for the “left” schema.

include_views: Whether to include views in the comparison. float_precision: The precision of floating point comparisons. Values with an absolute

difference below the precision are considered equal.

collation: An optional collation that is used to compare strings. Useful for making

case-sensitive comparisons even if a table’s column uses a case-insensitive collation.

ignore_casing: Whether casing (e.g. capitalization) should be ignored when matching

table names. This is valuable if only interacting with the database through case-insensitive tools (e.g. SQL).

Returns:

A schema comparison object.

sqlcompyre.compare_tables(engine: Engine, left: Select | FromClause | str, right: Select | FromClause | str, join_columns: list[str] | None = None, ignore_columns: list[str] | None = None, column_name_mapping: dict[str, str] | None = None, float_precision: float = 2.220446049250313e-16, collation: str | None = None, ignore_casing: bool = False, infer_primary_keys: bool = False) TableComparison[source]

Compare two tables in the database.

Args:

engine: The engine to use to access the database. left: The “left” database table for the comparison. The table can optionally be

specified with schema (and database) name. For MSSQL, the table name can be specified as [[<database>.]<schema>.]<table> depending on the “default” database of the provided engine and the database’s default schema. If provided as a SQLAlchemy table, the name is extracted automatically.

right: The “right” database table to compare to the “left” table. The naming convention

follows the convention for the “left” table.

join_columns: The columns to join the tables on in order to compare column values. If

not provided, defaults to the union of primary keys. The corresponding primary key(s) of the right table are determined via column_name_mapping.

ignore_columns: Columns to ignore to evaluate equality. These column names should

reference the left table: corresponding columns in the right table are determined via column_name_mapping. Primary key columns passed here are ignored.

column_name_mapping: A mapping from column names in the left table to column names in

the right table. If not provided, defaults to mapping columns with the same names.

float_precision: The precision of floating point comparisons. Values with an absolute

difference below the precision are considered equal.

collation: An optional collation that is used to compare strings. Useful for making

case-sensitive comparisons even if a table’s column uses a case-insensitive collation.

ignore_casing: Whether casing (e.g. capitalization) should be ignored when matching

column names. This is valuable if only interacting with the database through case-insensitive tools (e.g. SQL).

infer_primary_keys: Allows SQLCompyre to build a primary key from all matching columns

automatically and use it to match tables even if they do not have a primary key.

Returns:

A table comparison object that can be used to explore the differences in the tables.

sqlcompyre.inspect(engine: Engine, query: Select | FromClause) QueryInspection[source]

Inspect the results of a query in the database.

Args:

engine: The engine to use to access the database. query: The query whose results to inspect. This can either be a SQLAlchemy SELECT

statement or a FROM clause (which includes plain sqlalchemy.Table objects).

Returns:

A query inspection object that can be used to easily gain insights into the query result.

See also:

inspect_table() if you want to inspect the results of SELECT * FROM table and specify the table as a string.

sqlcompyre.inspect_table(engine: Engine, table: Table | str) QueryInspection[source]

Inspect a table in the database.

Args:

engine: The engine to use to access the database. table: The database table to inspect. When specified as string, the table can optionally

be specified with schema (and database) name. For MSSQL, the table name can be specified as [[<database>.]<schema>.]<table> depending on the “default” database of the provided engine and the database’s default schema.

Returns:

A query inspection object that can be used to easily gain insights into the table.

See also:

inspect() if you want to inspect only a subset of a table or have a sqlalchemy.Table object. This method should preferably only be used when specifying the table name as a string.