Skip to content

heavenshell/py-sqlalchemy_seed

Repository files navigation

sqlalchemy_seed

Python 3 Updates

sqlalchemy_seed is a seed library which provides initial data to database using SQLAlchemy.

sqlalchemy_seed is similar to Django fixtures.

Installation

pip install sqlalchemy_seed

Adding seed

/myapp
  __init__.py
  models.py
  /fixtures
    accounts.yaml

Model file.

# -*- coding: utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
engine = create_engine('sqlite://', convert_unicode=True)

Base = declarative_base()
Base.metadata.bind = engine
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
session = scoped_session(Session)

class Account(Base):
    __tablename__ = 'accounts'

    id = Column(Integer, primary_key=True)
    first_name = Column(String(100), nullable=False)
    last_name = Column(String(100), nullable=False)
    age = Column(Integer(), nullable=True)

Seed code.

# -*- coding: utf-8 -*-

from sqlalchemy_seed import (
    create_table,
    drop_table,
    load_fixtures,
    load_fixture_files,
)
from myapp.models import Base, session


def main():
    path = '/path/to/fixtures'
    fixtures = load_fixture_files(path, ['accounts.yaml'])
    load_fixtures(session, fixtures)


if __name__ == '__main__':
    main()

Seed file.

- model: myapp.models.Account
  id: 1
  fields:
    first_name: John
    last_name: Lennon
    age: 20

- model: myapp.models.Account
  id: 2
  fields:
    first_name: Paul
    last_name: McCartney
    age: 21

Update seed

If you want idempotent, you can describe seed like followings.

Seed file.

- model: myapp.models.Account
  fields:
    id: 1
    first_name: John
    last_name: Lennon
    age: 20

- model: myapp.models.Account
  fields:
    id: 2
    first_name: Paul
    last_name: McCartney
    age: 21

LICENSE

NEW BSD LICENSE.

About

Simple data seeder using SQLAlchemy.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages