{"id":17283242,"url":"https://github.com/schettino72/sqla_yaml_fixtures","last_synced_at":"2025-04-14T10:11:21.200Z","repository":{"id":53486615,"uuid":"94335536","full_name":"schettino72/sqla_yaml_fixtures","owner":"schettino72","description":"Load YAML data fixtures for SQLAlchemy","archived":false,"fork":false,"pushed_at":"2025-02-11T06:20:02.000Z","size":71,"stargazers_count":14,"open_issues_count":7,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T23:23:47.637Z","etag":null,"topics":["database","fixtures","python","sql","sqlalchemy","yaml"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/schettino72.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":"schettino72","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-06-14T13:44:29.000Z","updated_at":"2025-02-11T05:55:11.000Z","dependencies_parsed_at":"2024-10-15T09:50:59.083Z","dependency_job_id":"c45c4f26-7660-4150-887d-f2e59e2be9a0","html_url":"https://github.com/schettino72/sqla_yaml_fixtures","commit_stats":{"total_commits":61,"total_committers":3,"mean_commits":"20.333333333333332","dds":0.3278688524590164,"last_synced_commit":"2aba7d7c11855019e66267cea937f6deadc2194d"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schettino72%2Fsqla_yaml_fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schettino72%2Fsqla_yaml_fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schettino72%2Fsqla_yaml_fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schettino72%2Fsqla_yaml_fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schettino72","download_url":"https://codeload.github.com/schettino72/sqla_yaml_fixtures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860213,"owners_count":21173342,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","fixtures","python","sql","sqlalchemy","yaml"],"created_at":"2024-10-15T09:50:51.742Z","updated_at":"2025-04-14T10:11:21.161Z","avatar_url":"https://github.com/schettino72.png","language":"Python","funding_links":["https://ko-fi.com/schettino72","https://ko-fi.com/A0A23ZL4A"],"categories":[],"sub_categories":[],"readme":"sqla\\_yaml\\_fixtures\n====================\n\nLoad YAML data fixtures for SQLAlchemy\n\n.. image:: https://img.shields.io/pypi/v/sqla_yaml_fixtures.svg\n   :target: https://pypi.python.org/pypi/sqla_yaml_fixtures\n\n.. image:: https://img.shields.io/pypi/l/sqla_yaml_fixtures.svg\n   :target: https://pypi.python.org/pypi/sqla_yaml_fixtures\n\n.. image:: https://img.shields.io/pypi/pyversions/sqla_yaml_fixtures.svg\n   :target: https://pypi.python.org/pypi/sqla_yaml_fixtures\n\n.. image:: https://github.com/schettino72/sqla_yaml_fixtures/workflows/test/badge.svg\n   :target: https://github.com/schettino72/sqla_yaml_fixtures/actions?query=workflow%3Atest\n\n.. image:: https://ko-fi.com/img/githubbutton_sm.svg\n   :target: https://ko-fi.com/A0A23ZL4A\n\n\nThis package allows you to define some data in YAML and load them into a\nDB. The yaml data should correspond to SQLAlchemy declarative mappers.\n\nExample:\n\n.. code:: yaml\n\n    - User:\n      - __key__: joey\n        username: joey\n        email: joey@example.com\n        profile:\n          name: Jeffrey\n\n      - __key__: dee\n        username: deedee\n        email: deedee@example.com\n\n    - Profile:\n      - user: dee\n        name: Douglas\n\n    - Group:\n      - name: Ramones\n        members: [joey.profile, dee.profile]\n\n-  The root of YAML contains a *sequence* of ``mapper names`` e.g. ``- User``, ``- Profile`` etc\n-  The order of these names should follow relationship dependencies\n-  Every name should contain a *sequence* of instances\n-  Each instance is a *mapping* of *attribute* -\u003e *value*\n-  the attributes are taken from the mapper ``__init__()`` (usually an\n   attributes maps to a column)\n-  The special field ``__key__`` can be used to identify this instnace\n   in a relationship reference e.g. The ``Profile.user``\n-  Note that any ``__key__`` MUST be globally **unique**\n-  In a *to-one* relationship the data can be directly nested in the\n   parent data definition\n-  References can access attributes using a *dot* notation, e.g.\n   ``joey.profile``\n-  *to-many* relationships can be added as a list of references\n\nThe mapper definition for this example is in the `test file`_.\n\nInstallation\n------------\n\n``pip install sqla-yaml-fixtures``\n\n\nAPI\n---\n\nORM\n++++\n\n``def load(ModelBase, session, fixture_text, loader=None)``\n\nWhere:\n\n-  ``ModelBase`` is SQLAlchemy declarative base\n-  ``session`` is SQLAlchemy session\n-  ``fixture_text`` is a string containg the YAML fixtures\n\n.. code:: python\n\n    from sqlalchemy import create_engine\n    from sqlalchemy import Column, Integer, String\n    from sqlalchemy.ext.declarative import declarative_base\n    from sqlalchemy.orm import Session\n\n    import sqla_yaml_fixtures\n\n    BaseModel = declarative_base()\n\n    class User(BaseModel):\n        __tablename__ = 'user'\n        id = Column(Integer, primary_key=True)\n        username = Column(String(150), nullable=False, unique=True)\n        email = Column(String(254), unique=True)\n\n\n    def main():\n        engine = create_engine('sqlite://')\n        BaseModel.metadata.create_all(engine)\n        connection = engine.connect()\n        session = Session(bind=connection)\n\n        fixture = \"\"\"\n        - User:\n          - username: deedee\n            email: deedee@example.com\n          - username: joey\n            email: joey@example.commit\n        \"\"\"\n        sqla_yaml_fixtures.load(BaseModel, session, fixture)\n\n        print('\\n'.join(u.username for u in session.query(User).all()))\n\n    if __name__ == '__main__':\n        main()\n\n\nNote: the `load()` function performs a `session.commit()`.\n\n`load()` returns an instance of `Store`. Using this object `get()` method you can passing a `key` as argument you get a reference to the object added into the database. This is useful to easily get attributes that are generated by the database.\n\n.. code:: python\n\n     store = sqla_yaml_fixtures.load(BaseModel, session, fixture)\n     my_obj = store.get('dee')\n     print('Created object id: {}'.format(my_obj.id))\n\n\n.. warning::\n\n   By default YAML is loaded using `yaml.FullLoader`, this is insecure when\n   loading unstrusted input. It is possible to overwrite the loaded by setting\n   `loader` param in the `load()` function.\n\n\nCore / Non-ORM\n+++++++++++++++\n\n``async def load_core(metadata, connection, fixture_text, loader=None)``\n\n\nCommand Line\n------------\n\nFor basic usage there is also command line. Example::\n\n  $ python -m sqla_yaml_fixtures --db-url sqlite:///dev.db --db-base mypkg.models:Base --reset-db --alembic-stamp fixture.yaml\n\n\nAll available options::\n\n  $ python -m sqla_yaml_fixtures --help\n  usage: sqla_yaml_fixtures [-h] --db-base DB_BASE --db-url DB_URL [--yes]\n                            [--reset-db] [--alembic-stamp] [--jinja2]\n                            FILE [FILE ...]\n\n  load fixtures from yaml file into DB\n\n  positional arguments:\n    FILE               YAML file with DB fixtures\n\n  optional arguments:\n    -h, --help         show this help message and exit\n    --db-base DB_BASE  SQLAlchemy Base class with schema metadata in the format\n                       my_package.my_module:MyClass\n    --db-url DB_URL    Database URL in the format\n                       dialect+driver://username:password@host:port/database\n    --yes              Do NOT ask for confirmation before applying fixtures\n    --reset-db         Drop DB schema and data and re-create schema before\n                       loading fixtures\n    --alembic-stamp    Perform `alembic stamp head`\n    --jinja2           load fixture files as jinja2 templates\n\n\n\n.. _test file: https://github.com/schettino72/sqla_yaml_fixtures/blob/master/tests/test_sqla_yaml_fixtures.py\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschettino72%2Fsqla_yaml_fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschettino72%2Fsqla_yaml_fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschettino72%2Fsqla_yaml_fixtures/lists"}