Skip to main content

Validate SQL database Schemas using Python

Project description

SQL Judge, a tool for validation Relational Databases Schemas (SQL)

Also avaliable in PT-BR

Goals

SQL Judge has a goal to allow writing database schema validations that are easily testable and portable between different Database implementations (Postgres, MariaDB, MySQL, etc.)

It achieves the first by making the validations pure python functions that are executed as-is in the validation, needing only an decorator to being recognized as one, making the function testable through regular python unit testing tools (such as unittest and pytest).

The second is achieved by utilizing a independent, user-provided way to access the database (see Adapters)

Install

pip install sql_judge

How to Use

In order to use the tool, the user must pass a configuration file in JSON format. For example, if you have the configuration in a file called config.json, it would execute the tool with:

python -m sql_judge config.json

Currently, all configuration options must be passed through a configuration file. You can, however, pass multiple files, like this:

python -m sql_judge config.json another_config.json

In case of conflict between the two files, the latter has preference.

Setting up a configuration file

The configuration file must be formatted correctly ni order to the program to execute properly. Here is a guide in how to fill it properly:

The SQL Judge configuration has the following format, with a more detailed information below it:

{
  "adapter": {
    "module": "adapter_module",
    "class": "AdapterClass",
    "params": ["posarg1"],
    "named_params": { "namedarg1": "value1"}  
  },
  "validations": {
    "module": "validations_module"
  },
  "ignore_tables": ["ignored_table"],
  "export": {
    "format": "\"CLI\" or \"CSV\""
  }
}

adapter: options regarding the adapter(see Adapters for more info), it has the following options:

  • module (mandatory): The python module containing the adapter
  • class (mandatory): The Adapter class presented in the module
  • params (optional): unnamed parameters passed to the Adapter constuctor
  • named_params (optional): named parameters passed to the Adapter constructor

validations: Options regarding the schema validations:

  • module (mandatory): The module containing the validations functions

ignore_tables (optional, default: []): Pass which tables should not be included in the module validation, dicriminated by the name and name only(i.e does not support pattern matching yet).

export: The generated report format. Currently it has no option but show te report in the stdout, not passing to a file

  • format (optional, options: [CLI, CSV], default: CLI)

OBS: Since the tool deals with python modules instead of pure text files, the modules passed to the configuration file must be in the sys.PATH. If the files are in the same folder that the tool is invoked should cause no problems.

Adapters

Adapters are the way SQL Judge has access to the database schema. The user provides through the configuration file a class that acts as it. This class must implement the same interface that AbstractAdapter present in src/sql_judge/adapter.py has. More information in how to implement such class is presented in the class and methods docstrings.

The idea is that, in the future, these adapters will be able to be passed through plug-ins.

Writing Validations

In order to have the validations tested against the schema, the user must create a python module containing the validations, and which entities they should validate against. A validation is recognized by the program when a function is decorated with validates. The program assumes each validation follow this behavior:

  • A validation must be decorated with validates, passing the entity it will be validated against(ex.: Tables, Columns)
    • Obs.: Currently, it needs to pass the entity capitalized and in plural - Tables and Indexes instead of table and index, for example.
  • The given function must have one, and only one, parameter, that will represent the entity to be validated.
  • When given entity follows the rule present in the validation, the function must return None
  • When given entity does not follow the rule present in the validation, the function must return a string, preferably detailing the reason it failed, since the string will be passed to the report

Here's an example of a validation module

from sql_judge import validates # Decorator that marks functions as validations

def not_a_validation(): # Since it does not have the decorator, it is not recognized as a validation

@validates('Tables') # Makes the function a validation of tables
def table_must_start_with_tbl(table):
  if table.name[0:4] != 'tbl_':
    return None # Return None if the entity follows the rule

  # Return a string containing the error when it does not follow the rule
  return 'Table must start with "tbl_"'

Entities API

SQL Judge's Schema builder supports the following databases entities:

  • Tables;
  • Functions;
  • Procedures;
  • Columns;
  • Triggers;
  • Indexes;
  • Constraints;
  • Sequences;

By default, each entity provides its name and its relation to others entities (columns associated to a table, for example). You can pass another details about a given entity through the adapter. The properties can be accessed by calling by the key provided in the adapter. So, if you passed in the columns an aditional key 'type', you would acces it by using column.type. Note that all the properties are accessed through properties, not methods. Following, the minimal interface that each entity has that exists by default:

Schema

Property Return Type Description
tables List[Table] Schema Tables
functions List[Entity] Schema Functions
procedures List[Entity] Schema Procedures
sequences List[Entity] Schema sequences
columns List[Column] Schema Table Columns
triggers List[TableEntity] Schema Table Triggers
indexes List[ColumnEntity] Schema Column Indexes
constraints List[ColumnEntity] Schema Column constraints

Properties commom to all Entities

Property Return Type Description
name str Entity name
schema Schema Main Object, able to access every schema entity

Table

Property Return Type Description
columns List[Column] Table Columns
triggers List[TableEntity] Table Triggers
primary key Column Primary Key Column

Properties commom to Column, Trigger, Constraint and Index

Property Return Type Description
table Table Table that contains the entity

Column

Property Return Type Description
primary_key boolean True if the column is the primary key. False if not
references Table If column has a foreign key constraint, returns the table it references. None if not
indexes List[ColumnIndex] Column Indexes
constraints List[ColumnEntity] Column Constraints

Properties commom to Index and Constraint

Property Return Type Description
column Column Column that contains the Entity

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql_judge-0.1.0.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql_judge-0.1.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file sql_judge-0.1.0.tar.gz.

File metadata

  • Download URL: sql_judge-0.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.3

File hashes

Hashes for sql_judge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a56f22b0c16869cf4add0b026dda4ced812f73c18c5412f24d11fa25e93f0139
MD5 5b2168365da48ada925f7061cf7a55dd
BLAKE2b-256 b084bde941a351606d5b49dd41f6ce7c5bbbbaf282ca437b89083b3fc7d53858

See more details on using hashes here.

File details

Details for the file sql_judge-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sql_judge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.3

File hashes

Hashes for sql_judge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36277cb5f3cd2e7ee21cd6a4a013bb3f1e9a26e94047a569b2a30f7edc23c9b5
MD5 d2f98336eecd952224359c3244686548
BLAKE2b-256 ec09a68f03bd45e97886630986b3336ff3d11dab6b50e727e5f57f2bb1887979

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page