sa-metameta


Namesa-metameta JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/Red-HAP/sa-metameta
SummaryDynamic SQLAlchemy Metadata Reflection of One or More SQLALchemy Engines
upload_time2022-12-09 17:11:26
maintainerHAP
docs_urlNone
authorRed Hat, Inc.
requires_python>=3.9
licenseApache-2.0
keywords sqlalchemy asyncio asyncpg
VCS
bugtrack_url
requirements pre-commit asyncpg psycopg2-binary sqlalchemy pytest pytest-asyncio
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![](https://raw.githubusercontent.com/Red-HAP/sa-metameta/main/docs/images/sa-metameta-logo.png)

# sa-metameta

Meta information of SQLAlchemy metadata by engine

## Purpose

This module is designed to have a top-level class instance to which SQLAlchemy `Engine` instances can be registered as a `MetaEngine`. Each `MetaEngine` can then be probed directly to retrieve all tables by user schema. Thus an application can reference multiple databases and use the reflected `Table` instances to create queries for each.

This module has classes that will probe a RDBMS that supports the `information_schema.schema` view to get table information for each schema in a database. Currently, only PostgreSQL is fully supported.

This module will support `psycopg2` and `asyncpg`.

This module requires `SQLAlchemy` >= v1.4 for database probing and `Table` class creation.

This module currently supports only table object creation.

## Overview

After instantiating the MetaMeta class, an engine can be registered. Once that is done, the database can be probed for tables. Once that has completed successfully, the tables can be referenced starting from the MetaMeta instance to the engine, to the schema, and then to the table.

Example:

```python
import sqlalchemy as sa
from sa_metameta import meta

engine = sa.create_engine("postgresql://postgres:pg_passwd@localhost:5432/my_database")

mm = meta.MetaMeta()
# This will use the database name from the URL as the attribute name
mm.register_engine(engine)
# This will probe all schemata in my_database and for each schema, the tables will be reflected.
mm.my_database.discover()

# now we can see what tables have been found by using list()
list(mm.my_database.public)
[
    "table1",
    "table2",
    ...
]
```

The engine, schema, and table can be referenced by dot or subscript notation:

```python
engine = mm["my_database"]
schema = mm["my_database"]["public"]
table1 = mm["my_database"]["public"]["table1"]

# or

engine = mm.my_database
schema = mm.my_database.public
table1 = mm.my_database.public.table1
```

To reference columns, use the Table().c.column syntax.

```python
query = sa.select(table1.c.label).filter(table1.c.quatloos > 200)
db = engine.session()
res = db.execute(query).all()
```


The engine can be exported as yaml or ddl. This will write a `.yaml` or a `.sql` file depending on the method called.

```python
mm.my_database.as_yaml()
# "my_database.yaml"

mm.my_database.as_ddl()
# "my_database.sql"
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Red-HAP/sa-metameta",
    "name": "sa-metameta",
    "maintainer": "HAP",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "hproctor@redhat.com",
    "keywords": "SQLALchemy,asyncio,asyncpg",
    "author": "Red Hat, Inc.",
    "author_email": "info@ansible.com",
    "download_url": "https://files.pythonhosted.org/packages/04/72/7b4fe497bf9c18a2b00dfbc638a672869965e8c716346b6da88f8f1a88e1/sa-metameta-0.0.3.tar.gz",
    "platform": null,
    "description": "![](https://raw.githubusercontent.com/Red-HAP/sa-metameta/main/docs/images/sa-metameta-logo.png)\n\n# sa-metameta\n\nMeta information of SQLAlchemy metadata by engine\n\n## Purpose\n\nThis module is designed to have a top-level class instance to which SQLAlchemy `Engine` instances can be registered as a `MetaEngine`. Each `MetaEngine` can then be probed directly to retrieve all tables by user schema. Thus an application can reference multiple databases and use the reflected `Table` instances to create queries for each.\n\nThis module has classes that will probe a RDBMS that supports the `information_schema.schema` view to get table information for each schema in a database. Currently, only PostgreSQL is fully supported.\n\nThis module will support `psycopg2` and `asyncpg`.\n\nThis module requires `SQLAlchemy` >= v1.4 for database probing and `Table` class creation.\n\nThis module currently supports only table object creation.\n\n## Overview\n\nAfter instantiating the MetaMeta class, an engine can be registered. Once that is done, the database can be probed for tables. Once that has completed successfully, the tables can be referenced starting from the MetaMeta instance to the engine, to the schema, and then to the table.\n\nExample:\n\n```python\nimport sqlalchemy as sa\nfrom sa_metameta import meta\n\nengine = sa.create_engine(\"postgresql://postgres:pg_passwd@localhost:5432/my_database\")\n\nmm = meta.MetaMeta()\n# This will use the database name from the URL as the attribute name\nmm.register_engine(engine)\n# This will probe all schemata in my_database and for each schema, the tables will be reflected.\nmm.my_database.discover()\n\n# now we can see what tables have been found by using list()\nlist(mm.my_database.public)\n[\n    \"table1\",\n    \"table2\",\n    ...\n]\n```\n\nThe engine, schema, and table can be referenced by dot or subscript notation:\n\n```python\nengine = mm[\"my_database\"]\nschema = mm[\"my_database\"][\"public\"]\ntable1 = mm[\"my_database\"][\"public\"][\"table1\"]\n\n# or\n\nengine = mm.my_database\nschema = mm.my_database.public\ntable1 = mm.my_database.public.table1\n```\n\nTo reference columns, use the Table().c.column syntax.\n\n```python\nquery = sa.select(table1.c.label).filter(table1.c.quatloos > 200)\ndb = engine.session()\nres = db.execute(query).all()\n```\n\n\nThe engine can be exported as yaml or ddl. This will write a `.yaml` or a `.sql` file depending on the method called.\n\n```python\nmm.my_database.as_yaml()\n# \"my_database.yaml\"\n\nmm.my_database.as_ddl()\n# \"my_database.sql\"\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Dynamic SQLAlchemy Metadata Reflection of One or More SQLALchemy Engines",
    "version": "0.0.3",
    "split_keywords": [
        "sqlalchemy",
        "asyncio",
        "asyncpg"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "0517be08c605905c10db7f89cc4ddc3a",
                "sha256": "0c3b9dee8adc509c32e72dd6c61616c9cccc5b2a68a4a5451fcca788396df894"
            },
            "downloads": -1,
            "filename": "sa_metameta-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0517be08c605905c10db7f89cc4ddc3a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 14673,
            "upload_time": "2022-12-09T17:11:24",
            "upload_time_iso_8601": "2022-12-09T17:11:24.363240Z",
            "url": "https://files.pythonhosted.org/packages/75/8f/961e127ccfaccfed6358c0f264bfd8e85c0bd31d9e956b885a87625c4ac6/sa_metameta-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "671ceda72043b637277539031797f16d",
                "sha256": "26d21bb71863c36b9d2ea0e14527d28c582c36fe6b9d9c83e6ec9aaca6229329"
            },
            "downloads": -1,
            "filename": "sa-metameta-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "671ceda72043b637277539031797f16d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 13478,
            "upload_time": "2022-12-09T17:11:26",
            "upload_time_iso_8601": "2022-12-09T17:11:26.270708Z",
            "url": "https://files.pythonhosted.org/packages/04/72/7b4fe497bf9c18a2b00dfbc638a672869965e8c716346b6da88f8f1a88e1/sa-metameta-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-09 17:11:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Red-HAP",
    "github_project": "sa-metameta",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "pre-commit",
            "specs": []
        },
        {
            "name": "asyncpg",
            "specs": []
        },
        {
            "name": "psycopg2-binary",
            "specs": []
        },
        {
            "name": "sqlalchemy",
            "specs": [
                [
                    ">=",
                    "1.4"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": []
        },
        {
            "name": "pytest-asyncio",
            "specs": []
        }
    ],
    "lcname": "sa-metameta"
}
        
Elapsed time: 0.01569s