Skip to content

Commit

Permalink
Add seed script
Browse files Browse the repository at this point in the history
  • Loading branch information
sandenbergmelo committed Jan 9, 2025
1 parent afe9566 commit 789f6f3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"pythonpath",
"sqlalchemy",
"taskipy",
"testcontainers"
"testcontainers",
"todostate"
],
"[python]": {
"editor.formatOnSave": true,
Expand Down
Empty file added fast_zero/db/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions fast_zero/db/seed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import sys
from pathlib import Path

import alembic
import alembic.command
from alembic.config import Config
from rich import print
from sqlalchemy import text

sys.path.append(str(Path(__file__).resolve().parents[2]))

import factory
import factory.fuzzy

from fast_zero.db.connection import get_session
from fast_zero.db.models import Todo, TodoState, User
from fast_zero.helpers.security import get_password_hash


class UserFactory(factory.Factory):
class Meta:
model = User

username = factory.Sequence(lambda n: f'test{n}')
email = factory.LazyAttribute(lambda obj: f'{obj.username}@test.com')
password = factory.LazyAttribute(
lambda obj: get_password_hash(f'{obj.username}+password')
)


class TodoFactory(factory.Factory):
class Meta:
model = Todo

title = factory.Faker('text')
description = factory.Faker('text', max_nb_chars=50)
state = factory.fuzzy.FuzzyChoice(TodoState)
user_id = factory.fuzzy.FuzzyInteger(1, 5)


alembic_config = Config(Path(__file__).parent.parent.parent / 'alembic.ini')
session = next(get_session())

# Delete all tables
print('[bold yellow]Dropping database...[/]')
alembic.command.downgrade(alembic_config, 'base')
session.execute(text('DROP TYPE IF EXISTS todostate'))
session.commit()
print('[bold green]Database dropped![/]')

# Create all tables
print('[bold yellow]Creating database...[/]')
alembic.command.upgrade(alembic_config, 'head')
print('[bold green]Database created![/]')

users = UserFactory.create_batch(5)
todos = TodoFactory.create_batch(10)

users.insert(
0,
User(
username='admin',
password=get_password_hash('admin'),
email='[email protected]',
),
)

print('[bold yellow]Seeding database...[/]')
session.add_all(users)
session.commit()
session.add_all(todos)
session.commit()
print('[bold green]Database seeded![/]')
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ migrate_generate = 'alembic revision --autogenerate -m'
migrate = 'task migrate_generate'
post_migrate = 'task migrate_upgrade'

seed = 'python fast_zero/db/seed.py'

coverage = 'coverage html'

lint = 'ruff check . && ruff check . --diff'
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 789f6f3

Please sign in to comment.