jsonschema2ddl package

Submodules

jsonschema2ddl.models module

class jsonschema2ddl.models.Column(name: str, database_flavor: str = 'postgres', comment: str = <factory>, constraints: typing.Dict = <factory>, jsonschema_type: str = <factory>, jsonschema_fields: typing.Dict = <factory>)[source]

Bases: object

Object to encapsulate a Column.

name

name of the Column.

Type

str

database_flavor

postgres or redshift. Defaults to postgres.

Type

str

comment

comment of the Column. Defaults to None.

Type

str

constraints

other columns constraints (not implemented).

Type

Dict

jsonschema_fields

Original fields in the jsonschema.

Type

Dict

comment: str
constraints: Dict
property data_type: str

Data type of the columns.

It accounts of the mapping of the original type to the db types.

Returns

data type of the column.

Return type

str

database_flavor: str = 'postgres'
static is_fk() bool[source]

Returns true if the column is a foreign key.

Returns

True if it is foreign key

Return type

bool

property is_index: bool

Returns true if the column is a index.

Returns

True if it is index.

Return type

bool

property is_pk: bool
property is_unique: bool

Returns true if the column is a unique.

Returns

True if it is unique.

Return type

bool

jsonschema_fields: Dict
jsonschema_type: str
logger: ClassVar[logging.Logger] = <Logger Column (WARNING)>
property max_lenght: int
name: str
class jsonschema2ddl.models.FKColumn(name: str, database_flavor: str = 'postgres', comment: str = <factory>, constraints: typing.Dict = <factory>, jsonschema_type: str = <factory>, jsonschema_fields: typing.Dict = <factory>, table_ref: typing.Optional[jsonschema2ddl.models.Table] = None)[source]

Bases: jsonschema2ddl.models.Column

Special type of Column object to represent a foreign key

table_ref

Pointer to the foreing table object

Type

Table

property data_type: str

Data type of the foreign key.

Accounts of the data type of the primary key of the foreing table.

Returns

the column data type.

Return type

str

static is_fk() bool[source]

Returns true if the column is a foreign key.

Returns

True if it is foreign key.

Return type

bool

table_ref: jsonschema2ddl.models.Table = None
class jsonschema2ddl.models.Table(ref: str, name: str, database_flavor: str = 'postgres', columns: typing.List[jsonschema2ddl.models.Column] = <factory>, primary_key: typing.Optional[jsonschema2ddl.models.Column] = None, comment: typing.Optional[str] = None, indexes: typing.List[str] = <factory>, unique_columns: typing.List[str] = <factory>, jsonschema_fields: typing.Dict = <factory>, _expanded: bool = False)[source]

Bases: object

Object to encapsulate a Table.

ref

id or reference to the table in the jsonschema.

Type

str

name

name of the table.

Type

str

database_flavor

postgres or redshift. Defaults to postgres.

Type

str

columns

columns of the table.

Type

List[Column]

primary_key

Primary key column of the table.

Type

Column

comment

comment of the table. Defaults to None.

Type

str

indexes

Table indexeses (not implemented).

Type

List[str]

unique_columns

Table unique constraints (not implemented).

Type

List[str]

jsonschema_fields

Original fields in the jsonschema.

Type

Dict

columns: List[jsonschema2ddl.models.Column]
comment: str = None
database_flavor: str = 'postgres'
expand_columns(table_definitions: Dict = {}, columns_definitions: Dict = {}, referenced: bool = False)[source]

Expand the columns definitions of the

Parameters
  • table_definitions (Dict, optional) – Dictionary with the rest of the tables definitions. It is used for recursive calls to get the foreign keys. Defaults to dict().

  • columns_definitions (Dict, optional) – Dictionary with the definition of columns outside the main properties field. Defaults to dict().

  • referenced (bool, optional) – Whether or not the table is referenced by others. Used to make sure there is a Primary Key defined. Defaults to False.

indexes: List[str]
jsonschema_fields: Dict
logger: ClassVar[logging.Logger] = <Logger Table (WARNING)>
name: str
primary_key: jsonschema2ddl.models.Column = None
ref: str
unique_columns: List[str]

jsonschema2ddl.translators module

class jsonschema2ddl.translators.JSONSchemaToDatabase(schema: Dict, database_flavor: str = 'postgres', db_schema_name: Optional[str] = None, abbreviations: Optional[Dict] = None, extra_columns: Optional[List] = None, root_table_name: str = 'root', log_level: str = 'DEBUG')[source]

Bases: object

JSONSchemaToDatabase is the mother class for everything.

Typically you want to instantiate a JSONSchemaToPostgres object, and run create_tables() to create all the tables. Run create_links() to populate all references properly and add foreign keys between tables. Optionally you can run analyze() finally which optimizes the tables.

schema

the schema to translate to tables.

Type

Dict

database_flavor

the flavor of the db. One of Postgres or Redshift.

Type

str

db_schema_name

the name of the schema in the database to create the tables.

Type

str

root_table_name

Name of the root table for the schema.

Type

str

abbreviations

Dictionary of abbreviations for columns.

Type

Dict

extra_columns

List of extra columns.

Type

List[Dict]

log_level

Log level of the deployment. Default ‘DEBUG’.

Type

str

analyze(conn)[source]

Runs analyze on each table. This improves performance.

See the Postgres documentation for Analyze

Parameters

conn (psocopg2.connection) – connection object.

Adds foreign keys between tables.

Parameters
  • conn (psocopg2.connection) – connection object.

  • auto_commit (bool, Optional) – Defaults to False.

create_tables(conn, drop_schema: bool = False, drop_tables: bool = False, drop_cascade: bool = True, auto_commit: bool = False)[source]

Create the tables for the schema

Parameters
  • conn (psocopg2.connection) – Connection object to the db.

  • drop_schema (bool, optional) – Whether or not drop the schema if exists. Defaults to False.

  • drop_tables (bool, optional) – Whether or not drop the tables if exists. Defaults to False.

  • drop_cascade (bool, optional) – Execute drops with cascade. Defaults to True.

  • auto_commit (bool, optional) – autocomit after finishing. Defaults to False.

logger: logging.Logger = <Logger JSONSchemaToDatabase (WARNING)>
class jsonschema2ddl.translators.JSONSchemaToPostgres(*args, **kwargs)[source]

Bases: jsonschema2ddl.translators.JSONSchemaToDatabase

Shorthand for JSONSchemaToDatabase(…, database_flavor=’postgres’)

class jsonschema2ddl.translators.JSONSchemaToRedshift(*args, **kwargs)[source]

Bases: jsonschema2ddl.translators.JSONSchemaToDatabase

Shorthand for JSONSchemaToDatabase(…, database_flavor=’redshift’)

jsonschema2ddl.types module

jsonschema2ddl.utils module

jsonschema2ddl.utils.db_column_name(col_name: str) str[source]
jsonschema2ddl.utils.db_table_name(table_name: str, schema_name: Optional[str] = None) str[source]
jsonschema2ddl.utils.get_one_schema(object_schema) Dict[source]

Get one schema from a list of ‘allOf’, ‘anyOf’, ‘oneOf’.

Module contents

class jsonschema2ddl.JSONSchemaToDatabase(schema: Dict, database_flavor: str = 'postgres', db_schema_name: Optional[str] = None, abbreviations: Optional[Dict] = None, extra_columns: Optional[List] = None, root_table_name: str = 'root', log_level: str = 'DEBUG')[source]

Bases: object

JSONSchemaToDatabase is the mother class for everything.

Typically you want to instantiate a JSONSchemaToPostgres object, and run create_tables() to create all the tables. Run create_links() to populate all references properly and add foreign keys between tables. Optionally you can run analyze() finally which optimizes the tables.

schema

the schema to translate to tables.

Type

Dict

database_flavor

the flavor of the db. One of Postgres or Redshift.

Type

str

db_schema_name

the name of the schema in the database to create the tables.

Type

str

root_table_name

Name of the root table for the schema.

Type

str

abbreviations

Dictionary of abbreviations for columns.

Type

Dict

extra_columns

List of extra columns.

Type

List[Dict]

log_level

Log level of the deployment. Default ‘DEBUG’.

Type

str

analyze(conn)[source]

Runs analyze on each table. This improves performance.

See the Postgres documentation for Analyze

Parameters

conn (psocopg2.connection) – connection object.

Adds foreign keys between tables.

Parameters
  • conn (psocopg2.connection) – connection object.

  • auto_commit (bool, Optional) – Defaults to False.

create_tables(conn, drop_schema: bool = False, drop_tables: bool = False, drop_cascade: bool = True, auto_commit: bool = False)[source]

Create the tables for the schema

Parameters
  • conn (psocopg2.connection) – Connection object to the db.

  • drop_schema (bool, optional) – Whether or not drop the schema if exists. Defaults to False.

  • drop_tables (bool, optional) – Whether or not drop the tables if exists. Defaults to False.

  • drop_cascade (bool, optional) – Execute drops with cascade. Defaults to True.

  • auto_commit (bool, optional) – autocomit after finishing. Defaults to False.

logger: logging.Logger = <Logger JSONSchemaToDatabase (WARNING)>
class jsonschema2ddl.JSONSchemaToPostgres(*args, **kwargs)[source]

Bases: jsonschema2ddl.translators.JSONSchemaToDatabase

Shorthand for JSONSchemaToDatabase(…, database_flavor=’postgres’)

class jsonschema2ddl.JSONSchemaToRedshift(*args, **kwargs)[source]

Bases: jsonschema2ddl.translators.JSONSchemaToDatabase

Shorthand for JSONSchemaToDatabase(…, database_flavor=’redshift’)