sqlalchemy-pglogical


Namesqlalchemy-pglogical JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/brandfolder/sqlalchemy-pglogical
Summary
upload_time2023-11-30 18:20:10
maintainer
docs_urlNone
authorBen Berry
requires_python>=3.8,<=3.12
licenseMIT
keywords pglogical postgres sqlalchemy alembic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqlalchemy-pglogical

`sqlalchemy-pglogical` is a sqlalchemy extension to automatically send DDL through 
`pglogical.replicate_ddl_command`

## Who's this for?

`sqlalchemy-pglogical` may be for you if:
- you use `sqlalchemy` and/or `alembic` for DDL, and 
- you use `pglogical` for logical replication

## How do I use it?

There are two keys to using `sqlalchemy-pglogical`:

1. `import sqlalchemy_pglogical` - because of the way extending
   `sqlalchemy` works, you can do this in pretty much any file that 
   is loaded before DDL is called
2. Explicitly define your schema. If you're using `sqlalchemy`'s declaritive
   syntax, you define your schema by adding `__table_args__` to each table:
   ```python
   Base = declarative_base()
   
   
   class User(Base):
       __tablename__ = "users"
       __table_args__ = {"schema": "public"}
   ```
   if you're using `sqlalchemy` core and using `MetaData` to define your tables, 
   add `schema` as a keyword arg to your `MetaData` initialization:
   ```python
   metadata = Metadata(schema="public")
   ```

It probably makes the most sense to import it wherever you create your engine
to be sure it's always applied. For most apps, this is probably most important
for your migration toolchain (e.g. `alembic`), and less likely to be needed 
for your running application. If you're relying on `alembic` to build your 
`sqlalchemy.engine` (e.g, it's only defined in your alembic.ini), then you
should probably add this to your migration mako template.

```python
from sqlalchemy import create_engine
import sqlalchemy_pglogical
```

## Known limitations

### Only one publication

We currently assume you're using the `default_ddl` publication and only publish to that publication.

### CREATE INDEX CONCURRENTLY

`pglogical` can't propagate `{CREATE, DROP} INDEX CONCURRENTLY` statements. `sqlachemy-pglogical` makes
no attempt to catch the `CONCURRENTLY` keyword in `INDEX` statements, so they will likely fail.


## How does it work?

DDL operations are represented in SQLAlchemy 1.x with a subclass of `DDLElement`. To
extend a DDL type, you can do this:

```python
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.ddl import CreateTable

@compiles(CreateTable)
def visit_create_table(element, compiler, **kwargs) -> str:
    normal_ddl_sql: str = compiler.visit_create_table(element, **kwargs)
    modified: str = some_modification_we_define_elsewhere(normal_ddl_sql)
    return modified
```

We use this to extend _all_ subclasses of `DDLElement` and wrap them in `pglogical.replicate_ddl_command`



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/brandfolder/sqlalchemy-pglogical",
    "name": "sqlalchemy-pglogical",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<=3.12",
    "maintainer_email": "",
    "keywords": "pglogical,postgres,sqlalchemy,alembic",
    "author": "Ben Berry",
    "author_email": "benjamin.berry@smartsheet.com",
    "download_url": "https://files.pythonhosted.org/packages/34/e6/66c5f0269d850e3d13aabd7b86ab32a522ab6c26d0b020c0052569791a2c/sqlalchemy_pglogical-0.1.0.tar.gz",
    "platform": null,
    "description": "# sqlalchemy-pglogical\n\n`sqlalchemy-pglogical` is a sqlalchemy extension to automatically send DDL through \n`pglogical.replicate_ddl_command`\n\n## Who's this for?\n\n`sqlalchemy-pglogical` may be for you if:\n- you use `sqlalchemy` and/or `alembic` for DDL, and \n- you use `pglogical` for logical replication\n\n## How do I use it?\n\nThere are two keys to using `sqlalchemy-pglogical`:\n\n1. `import sqlalchemy_pglogical` - because of the way extending\n   `sqlalchemy` works, you can do this in pretty much any file that \n   is loaded before DDL is called\n2. Explicitly define your schema. If you're using `sqlalchemy`'s declaritive\n   syntax, you define your schema by adding `__table_args__` to each table:\n   ```python\n   Base = declarative_base()\n   \n   \n   class User(Base):\n       __tablename__ = \"users\"\n       __table_args__ = {\"schema\": \"public\"}\n   ```\n   if you're using `sqlalchemy` core and using `MetaData` to define your tables, \n   add `schema` as a keyword arg to your `MetaData` initialization:\n   ```python\n   metadata = Metadata(schema=\"public\")\n   ```\n\nIt probably makes the most sense to import it wherever you create your engine\nto be sure it's always applied. For most apps, this is probably most important\nfor your migration toolchain (e.g. `alembic`), and less likely to be needed \nfor your running application. If you're relying on `alembic` to build your \n`sqlalchemy.engine` (e.g, it's only defined in your alembic.ini), then you\nshould probably add this to your migration mako template.\n\n```python\nfrom sqlalchemy import create_engine\nimport sqlalchemy_pglogical\n```\n\n## Known limitations\n\n### Only one publication\n\nWe currently assume you're using the `default_ddl` publication and only publish to that publication.\n\n### CREATE INDEX CONCURRENTLY\n\n`pglogical` can't propagate `{CREATE, DROP} INDEX CONCURRENTLY` statements. `sqlachemy-pglogical` makes\nno attempt to catch the `CONCURRENTLY` keyword in `INDEX` statements, so they will likely fail.\n\n\n## How does it work?\n\nDDL operations are represented in SQLAlchemy 1.x with a subclass of `DDLElement`. To\nextend a DDL type, you can do this:\n\n```python\nfrom sqlalchemy.ext.compiler import compiles\nfrom sqlalchemy.sql.ddl import CreateTable\n\n@compiles(CreateTable)\ndef visit_create_table(element, compiler, **kwargs) -> str:\n    normal_ddl_sql: str = compiler.visit_create_table(element, **kwargs)\n    modified: str = some_modification_we_define_elsewhere(normal_ddl_sql)\n    return modified\n```\n\nWe use this to extend _all_ subclasses of `DDLElement` and wrap them in `pglogical.replicate_ddl_command`\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/brandfolder/sqlalchemy-pglogical/issues",
        "Documentation": "https://github.com/brandfolder/sqlalchemy-pglogical/blob/main/README.md",
        "Homepage": "https://github.com/brandfolder/sqlalchemy-pglogical",
        "Repository": "https://github.com/brandfolder/sqlalchemy-pglogical",
        "changelog": "https://github.com/brandfolder/sqlalchemy-pglogical/blob/main/CHANGELOG.md"
    },
    "split_keywords": [
        "pglogical",
        "postgres",
        "sqlalchemy",
        "alembic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e05032e67a71b3d247a38a135da6fc9650c1f5b73492e4284656c917dc0e3f10",
                "md5": "b5bd3362d89d002dadb4251f79921bad",
                "sha256": "f936a314119d5481d40cff08602bf00cf230e6b05f97f7b53701eae6e9970f04"
            },
            "downloads": -1,
            "filename": "sqlalchemy_pglogical-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b5bd3362d89d002dadb4251f79921bad",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<=3.12",
            "size": 5174,
            "upload_time": "2023-11-30T18:20:08",
            "upload_time_iso_8601": "2023-11-30T18:20:08.897461Z",
            "url": "https://files.pythonhosted.org/packages/e0/50/32e67a71b3d247a38a135da6fc9650c1f5b73492e4284656c917dc0e3f10/sqlalchemy_pglogical-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "34e666c5f0269d850e3d13aabd7b86ab32a522ab6c26d0b020c0052569791a2c",
                "md5": "17c831edbcfc6ffd09d617a1efe9a554",
                "sha256": "0efbe01ee0b85fc95109debdc345566370c9fc8932b9f89dc04b83b121780ae0"
            },
            "downloads": -1,
            "filename": "sqlalchemy_pglogical-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "17c831edbcfc6ffd09d617a1efe9a554",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<=3.12",
            "size": 4003,
            "upload_time": "2023-11-30T18:20:10",
            "upload_time_iso_8601": "2023-11-30T18:20:10.784124Z",
            "url": "https://files.pythonhosted.org/packages/34/e6/66c5f0269d850e3d13aabd7b86ab32a522ab6c26d0b020c0052569791a2c/sqlalchemy_pglogical-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-30 18:20:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "brandfolder",
    "github_project": "sqlalchemy-pglogical",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlalchemy-pglogical"
}
        
Elapsed time: 0.21296s