stress test applied
This commit is contained in:
38
runner/app/__init__.py
Normal file
38
runner/app/__init__.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""
|
||||
PostgreSQL Database Tester Application
|
||||
"""
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, scoped_session
|
||||
import os
|
||||
|
||||
# Get database connection string from environment variable or use default
|
||||
DATABASE_URL = os.environ.get(
|
||||
"DATABASE_URL",
|
||||
"postgresql://postgres:password@localhost:5432/postgres"
|
||||
)
|
||||
|
||||
# Create SQLAlchemy engine
|
||||
engine = create_engine(DATABASE_URL)
|
||||
|
||||
# Create session factory
|
||||
session_factory = sessionmaker(bind=engine)
|
||||
Session = scoped_session(session_factory)
|
||||
|
||||
# Create base class for declarative models
|
||||
Base = declarative_base()
|
||||
Base.query = Session.query_property()
|
||||
|
||||
def init_db():
|
||||
"""Initialize the database by creating all tables"""
|
||||
# Import all models to ensure they're registered with Base
|
||||
from app.models import user, post
|
||||
|
||||
# Create all tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
print("Database tables created successfully!")
|
||||
|
||||
def get_session():
|
||||
"""Get a database session"""
|
||||
return Session()
|
||||
7
runner/app/models/__init__.py
Normal file
7
runner/app/models/__init__.py
Normal file
@@ -0,0 +1,7 @@
|
||||
"""
|
||||
Models package initialization
|
||||
"""
|
||||
from app.models.user import User
|
||||
from app.models.post import Post
|
||||
|
||||
__all__ = ['User', 'Post']
|
||||
25
runner/app/models/post.py
Normal file
25
runner/app/models/post.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""
|
||||
Post model for the PostgreSQL database
|
||||
"""
|
||||
from sqlalchemy import Column, String, Text, Boolean, DateTime, ForeignKey, func, UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
import uuid
|
||||
from app import Base
|
||||
|
||||
class Post(Base):
|
||||
"""Post model representing a blog post or article in the system"""
|
||||
__tablename__ = 'posts'
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
user_id = Column(UUID(as_uuid=True), ForeignKey('users.id', ondelete='CASCADE'), nullable=False, index=True)
|
||||
title = Column(String(200), nullable=False)
|
||||
content = Column(Text)
|
||||
is_published = Column(Boolean, default=False)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="posts")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Post(title='{self.title}', user_id='{self.user_id}')>"
|
||||
27
runner/app/models/user.py
Normal file
27
runner/app/models/user.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
User model for the PostgreSQL database
|
||||
"""
|
||||
from sqlalchemy import Column, String, Boolean, DateTime, func, UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
import uuid
|
||||
from app import Base
|
||||
|
||||
class User(Base):
|
||||
"""User model representing a user in the system"""
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
username = Column(String(50), unique=True, nullable=False, index=True)
|
||||
email = Column(String(100), unique=True, nullable=False, index=True)
|
||||
password_hash = Column(String(100), nullable=False)
|
||||
first_name = Column(String(50))
|
||||
last_name = Column(String(50))
|
||||
is_active = Column(Boolean, default=True)
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
posts = relationship("Post", back_populates="user", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User(username='{self.username}', email='{self.email}')>"
|
||||
Reference in New Issue
Block a user