GithubHelp home page GithubHelp logo

takehaya / peewee_seed Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 4.0 16 KB

peewee_seed is a seed library which provides initial data to database using peewee.

Python 97.98% Makefile 2.02%
peewee peewee-orm seed-database seed

peewee_seed's Introduction

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

  • file envs
/myapp
  __init__.py
  seeds_entry.py
  models.py
  /fixtures
    accounts.yaml
# accounts.yaml
- model : myapp.models.Account
  id: 1
  fields:
    first_name: John
    last_name: Smith
    age: 20
# models.py
from peewee import CharField
from peewee import IntegerField
from peewee import Model
from peewee import SqliteDatabase

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
# 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()
  • Run command
python seeds_entroy.py

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)

peewee_seed's People

Contributors

ossbn-mhotta avatar takehaya avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

peewee_seed's Issues

ユニークキーを削除する時にMySQLの構文をディフォルトにしてるのでそれを治す

これはなに

ユニークキーを削除する時

self.db.execute_sql("SET FOREIGN_KEY_CHECKS=0;")

と書いているのでMySQL前提のコードになってしまっている。SQLiteであることなどを渡して修正する必要がある。

今後

とりあえず治す。
それとそもそもSQLiteであるという情報などは受け渡してそれように書いておく必要があると感じている

Readme error

seeds entry file example:
from peewee_seeds import PeeweeSeed
should be
from peewee_seed import PeeweeSeed

Issue loading from yaml fixtures

When running seeds.create_table_all() using yaml fixtures, I get the following error:

Traceback (most recent call last):
  File "seed.py", line 13, in <module>
    main()
  File "seed.py", line 8, in main
    seeds.create_table_all()
  File "/usr/local/lib/python3.6/site-packages/peewee_seed/__init__.py", line 41, in create_table_all
    tables_list = self.get_tables(models_path_list)
  File "/usr/local/lib/python3.6/site-packages/peewee_seed/__init__.py", line 30, in get_tables
    _, models_path_list = self.load_fixtures(fixture_data)
  File "/usr/local/lib/python3.6/site-packages/peewee_seed/__init__.py", line 117, in load_fixtures
    fixture_data = self.load_fixture_files(self.fixture_files)
  File "/usr/local/lib/python3.6/site-packages/peewee_seed/__init__.py", line 82, in load_fixture_files
    data = yaml.load(f)
TypeError: load() missing 1 required positional argument: 'Loader'

I see this issue has already been resolved in https://github.com/takehaya/peewee_seed/commit/f13e311cf208b92bb5fce692ad86df904176b1c2

However this change is not yet present in the latest release on pypi (0.1.8)
Would it be possible to create a new release for this and push it out on pypi,
so as to prevent people from having to patch manually?

Current workaround is to install using VCS notation:

pip install git+https://github.com/takehaya/peewee_seed.git@51bdf5145937f0e5d7032e88702ef91f10bfc3ef#egg=peewee_seed

Allow optional conflict ignoring

Hello,
thank you for creating this simple, yet useful tool.

I came across a situation when I would like to apply fixtures on already filled database (by previously applied fixtures or migrations) to add new data, but in that case it ends up with IntegrityError due to duplicate key (actually the whole record is same).

For such case, I would like to have an option to either ignore conflicts or update record (in eventual case of changed previously applied fixtures). I think it could be seamlessly done similar way as ignoring foreign key checks is implemented.

If you don't mind, I could prepare pull request for that ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.