Skip to main content

Synchronization of data from different sources into one database using SQLAlchemy as an ORM.

Project description

SQLAlchemy sync data

Synchronization of data from different sources into one database using SQLAlchemy.

Examples

# models.py
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String

CommonBase = declarative_base()


class User(CommonBase):
    __tablename__ = "old_users"

    id = Column(Integer, autoincrement=True, unique=True, primary_key=True, nullable=False)
    first_name = Column(String(255), nullable=False)
    last_name = Column(String(255), nullable=False)
    username = Column(String(255), nullable=False)


# getters.py

from sqlalchemy_sync_data.getters import SQLiteGetter


class UserGetter(SQLiteGetter):
    template_query = """select id, first_name, last_name, email from users"""
    connection_settings = {"database": "sqlalchemy_sync_data.sqlite"}

# handlers.py

from sqlalchemy_sync_data.handlers import BaseHandler
from .getters import UserGetter
from .models import User

class UserHandler(BaseHandler):
    model = User
    db_fields_to_model_mapping = {
        "id": "id",
        "first_name": "first_name",
        "last_name": "last_name",
        "email": "username",
    }
    field_name_as_external_id = 'id'
    getter_class = UserGetter

# synchronizators.py

from sqlalchemy_sync_data.synchronizator import BaseSyncronizator
from .handlers import UserHandler

class UserSyncronizator(BaseSyncronizator):
    handler_classes = (UserHandler,)

# Run sync data
from .synchronizators import UserSyncronizator

syncronizator = UserSyncronizator()
syncronizator.run()

Environment variables

SQLALCHEMY_SYNC_DATA_LOCAL_TIMEZONE - setting time zone (default value "UTC").

Required

  • python >=3.11, <4.0
  • fastapi >=0.100.0, <1.0
  • SQLAlchemy >=1.4.36, <2.1.0
  • requests >=2.32.3
  • psycopg2 >=2.9.5
  • pytz >=2020.1
  • environs >=9.3.1

Installation

pip install sqlalchemy-sync-data

Contribution

You can run tests with pytest.

pip install poetry
poetry install
pytest

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

sqlalchemy_sync_data-0.0.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_sync_data-0.0.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_sync_data-0.0.1.tar.gz.

File metadata

  • Download URL: sqlalchemy_sync_data-0.0.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

File hashes

Hashes for sqlalchemy_sync_data-0.0.1.tar.gz
Algorithm Hash digest
SHA256 960c0182536146f80f61b1fd3d935961aa5eb04deb7a8d8f3c6ae29eedcbbcb3
MD5 2fba8334ef699f146d2a43fbce6b89ce
BLAKE2b-256 f5b16bc35e9b663136a3296c3d8b8cc687c6af08c81c756e6825a586b4f0de83

See more details on using hashes here.

File details

Details for the file sqlalchemy_sync_data-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: sqlalchemy_sync_data-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1015-azure

File hashes

Hashes for sqlalchemy_sync_data-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9dec67820935257caf3abadb0026c1d5f4d8acb66b7d6265557d29748985af2a
MD5 bd7632439d24415cec7ebc1928c2a7f7
BLAKE2b-256 e5283d2c406329816bb379830a1fd3d8b9e49271b4c90bcc96b7faa7232e19df

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