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.
- constraints
other columns constraints (not implemented).
- Type
Dict
- jsonschema_fields
Original fields in the jsonschema.
- Type
Dict
- 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
- static is_fk() bool [source]
Returns true if the column is a foreign key.
- Returns
True if it is foreign key
- Return type
- property is_index: bool
Returns true if the column is a index.
- Returns
True if it is index.
- Return type
- property is_unique: bool
Returns true if the column is a unique.
- Returns
True if it is unique.
- Return type
- jsonschema_fields: Dict
- logger: ClassVar[logging.Logger] = <Logger Column (WARNING)>
- 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
- 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
- static is_fk() bool [source]
Returns true if the column is a foreign key.
- Returns
True if it is foreign key.
- Return type
- 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.
- jsonschema_fields
Original fields in the jsonschema.
- Type
Dict
- columns: List[jsonschema2ddl.models.Column]
- 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.
- jsonschema_fields: Dict
- logger: ClassVar[logging.Logger] = <Logger Table (WARNING)>
- primary_key: jsonschema2ddl.models.Column = None
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. Runcreate_links()
to populate all references properly and add foreign keys between tables. Optionally you can runanalyze()
finally which optimizes the tables.- schema
the schema to translate to tables.
- Type
Dict
- abbreviations
Dictionary of abbreviations for columns.
- Type
Dict
- extra_columns
List of extra columns.
- Type
List[Dict]
- analyze(conn)[source]
Runs analyze on each table. This improves performance.
See the Postgres documentation for Analyze
- Parameters
conn (psocopg2.connection) – connection object.
- create_links(conn, auto_commit: bool = True)[source]
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
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. Runcreate_links()
to populate all references properly and add foreign keys between tables. Optionally you can runanalyze()
finally which optimizes the tables.- schema
the schema to translate to tables.
- Type
Dict
- abbreviations
Dictionary of abbreviations for columns.
- Type
Dict
- extra_columns
List of extra columns.
- Type
List[Dict]
- analyze(conn)[source]
Runs analyze on each table. This improves performance.
See the Postgres documentation for Analyze
- Parameters
conn (psocopg2.connection) – connection object.
- create_links(conn, auto_commit: bool = True)[source]
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’)