takehaya / peewee_seed

peewee_seed is a seed library which provides initial data to database using peewee.
2 stars 4 forks source link
peewee peewee-orm seed seed-database

peewee_seed

peewee_seed is a seed library which provides initial data to database using peewee. With reference to the Django fixture

CircleCI PyPI version

usage

simple seeds

database = SqliteDatabase(":memory:", pragmas={"foregin_keys": 1})

class BaseModel(Model): class Meta(object): database = database

class Account(BaseModel): first_name = CharField(null=False) last_name = CharField(null=False) age = IntegerField(null=True)

- seeds entry file
``` python
# seeds_entry.py
from peewee_seeds import PeeweeSeed
from myapp.models import database

def main():
    path = '/path/to/fixtures'

    # seeds instance
    seeds = PeeweeSeed(database, path, ['accounts.yaml'])

    # load fixture for create table
    seeds.create_table_all()

    # load fixture for db input
    seeds.db_data_input()

if __name__ == '__main__':
    main()

other example

from peewee_seed import PeeweeSeed
from myapp.models import database

# seeds instance
seeds = PeeweeSeed()

# set path
seeds.set_path('/path/to/fixtures')

# set use fixture name
seeds.set_fixture_files(['accounts.yaml', 'pictures.yaml'])

# loading fixture file data
fixtures_row_data = seeds.load_fixture_files()

# fixture  purification
fields, models_namelist = seeds.load_fixture(fixtures_row_data[0])

# fixtures purification
fields, models_namelist = seeds.load_fixtures(fixtures_row_data)

# set database session
seeds.set_database(database)

# base on fixtures, create tables
seeds.create_table_all()

# fixtures data to db input
seeds.db_data_input(fixtures_row_data)

# base on fixtures, drop tables 
seeds.drop_table_all()

direct inputdata seed

# body is dict data
# create & seed
seed = PeeweeSeed(db)
_, models = seed.load_fixtures([body])
seed.create_table_all(models)

seed.db_data_input([body])

# body is modelpath(same to fixtures)
# drop
seed = PeeweeSeed(db)

models = body["models"]
seed.drop_table_all(models)

error: Foreign key constraint


# seed
seed.db_data_input([body], foreign_key_checks=True)

# db drop
seed.drop_table_all(models, foreign_key_checks=True)