Add a script for seeding data into the database
Expects a Data argument which is a directory containing the text for A Study in Scarlet
This commit is contained in:
parent
81bad03ebd
commit
cb70df80e1
2 changed files with 45 additions and 0 deletions
|
|
@ -7,4 +7,7 @@ buildPythonApplication {
|
|||
src = ./.;
|
||||
pyproject = true;
|
||||
build-system = [setuptools];
|
||||
postInstall = ''
|
||||
install -Dm755 scripts/seed.py $out/bin/seed
|
||||
'';
|
||||
}
|
||||
|
|
|
|||
42
scripts/seed.py
Normal file
42
scripts/seed.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from src.infra.publication import (
|
||||
PublicationEntryModel,
|
||||
PublicationSequenceModel,
|
||||
PublicationModel,
|
||||
PublicationOrderModel,
|
||||
)
|
||||
import argparse
|
||||
import os
|
||||
from src.config.database import Database
|
||||
from src.infra.db import get_database
|
||||
|
||||
if __name__ == '__main__':
|
||||
arg_parser = argparse.ArgumentParser('seed.py', 'run to load seed data into a database')
|
||||
arg_parser.add_argument('-u', '--url', help='The URL for the database', required=True)
|
||||
arg_parser.add_argument('-d', '--data', help='the directory containing large content files', required=True)
|
||||
args = arg_parser.parse_args()
|
||||
|
||||
with open(os.path.join(args.data, './a-study-in-scarlet.txt')) as infile:
|
||||
text = infile.read()
|
||||
|
||||
db = get_database(Database(url=args.url, echo=True))
|
||||
with Session(db) as session:
|
||||
entry = PublicationEntryModel(text=text)
|
||||
session.add(entry)
|
||||
publication = PublicationModel(
|
||||
title='Sherlock Holmes Canon',
|
||||
description='A collection of detective mystery novels featuring Sherlock Holmes',
|
||||
by_line='Sir Arthur Conan Doyle',
|
||||
)
|
||||
session.flush()
|
||||
order = PublicationOrderModel(title='Published Timeline', publication_id=publication.id)
|
||||
session.flush()
|
||||
sequence = PublicationSequenceModel(
|
||||
position=0,
|
||||
entry_id=entry.id,
|
||||
order_id=order.id,
|
||||
duration=None,
|
||||
)
|
||||
session.commit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue