Skip to content

Commit

Permalink
applied requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Armolas committed Jul 18, 2024
1 parent 3a1de68 commit 2697e0b
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 50 deletions.
2 changes: 1 addition & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = sqlite:///./database.db
sqlalchemy.url =


[post_write_hooks]
Expand Down
7 changes: 6 additions & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from decouple import config as decouple_config
from api.v1.models.user import User
from api.v1.models.org import Organisation
from api.v1.models.org import Organization
from api.v1.models.profile import Profile
from api.v1.models.product import Product
from api.v1.models.base import Base
Expand All @@ -17,6 +18,10 @@
if config.config_file_name is not None:
fileConfig(config.config_file_name)

database_url = decouple_config('DATABASE_URL')

# Set the SQLAlchemy URL dynamically
config.set_main_option('sqlalchemy.url', database_url)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Initial migrations
"""initial migration
Revision ID: 9670771a11f9
Revision ID: 5a2dc9cc9735
Revises:
Create Date: 2024-07-18 20:08:23.466008
Create Date: 2024-07-19 00:08:59.055052
"""
from typing import Sequence, Union
Expand All @@ -12,47 +12,47 @@


# revision identifiers, used by Alembic.
revision: str = '9670771a11f9'
revision: str = '5a2dc9cc9735'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('organisations',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
op.create_table('organizations',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(length=50), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('products',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('price', sa.Numeric(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('username', sa.String(length=50), nullable=False),
sa.Column('email', sa.String(length=100), nullable=False),
sa.Column('password', sa.String(length=255), nullable=False),
sa.Column('first_name', sa.String(length=50), nullable=True),
sa.Column('last_name', sa.String(length=50), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
sa.Column('created_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.Column('updated_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('username')
)
op.create_table('profiles',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('bio', sa.Text(), nullable=True),
sa.Column('phone_number', sa.String(length=50), nullable=True),
sa.Column('avatar_url', sa.String(length=100), nullable=True),
Expand All @@ -61,21 +61,21 @@ def upgrade() -> None:
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('user_organisation',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('organisation_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['organisation_id'], ['organisations.id'], ),
op.create_table('user_organization',
sa.Column('user_id', sa.UUID(), nullable=False),
sa.Column('organization_id', sa.UUID(), nullable=False),
sa.ForeignKeyConstraint(['organization_id'], ['organizations.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('user_id', 'organisation_id')
sa.PrimaryKeyConstraint('user_id', 'organization_id')
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('user_organisation')
op.drop_table('user_organization')
op.drop_table('profiles')
op.drop_table('users')
op.drop_table('products')
op.drop_table('organisations')
op.drop_table('organizations')
# ### end Alembic commands ###
30 changes: 30 additions & 0 deletions alembic/versions/ba7a518767c3_initial_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""initial migration
Revision ID: ba7a518767c3
Revises: 5a2dc9cc9735
Create Date: 2024-07-19 00:15:15.551494
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'ba7a518767c3'
down_revision: Union[str, None] = '5a2dc9cc9735'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
8 changes: 4 additions & 4 deletions api/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

def get_db_engine():

DB_TYPE = config("DB_TYPE", "")
DB_NAME = config("DB_NAME", "")
DB_TYPE = config("DB_TYPE", "postgresql")
DB_NAME = config("DB_NAME", "hng_fast_api")
DB_USER = config("DB_USER", "")
DB_PASSWORD = config("DB_PASSWORD", "")
DB_HOST = config("DB_HOST", "")
DB_PORT = config("DB_PORT", "")
DB_HOST = config("DB_HOST", "localhost")
DB_PORT = config("DB_PORT", "5432")
MYSQL_DRIVER = config("MYSQL_DRIVER", "")
DATABASE_URL = ""

Expand Down
7 changes: 4 additions & 3 deletions api/v1/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime
from sqlalchemy.dialects.postgresql import UUID


Base = declarative_base()

user_organisation_association = Table('user_organisation', Base.metadata,
Column('user_id', Integer, ForeignKey('users.id'), primary_key=True),
Column('organisation_id', Integer, ForeignKey('organisations.id'), primary_key=True)
user_organization_association = Table('user_organization', Base.metadata,
Column('user_id', UUID(as_uuid=True), ForeignKey('users.id'), primary_key=True),
Column('organization_id', UUID(as_uuid=True), ForeignKey('organizations.id'), primary_key=True)
)
16 changes: 9 additions & 7 deletions api/v1/models/org.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
""" The Organisation model
""" The Organization model
"""
from sqlalchemy import (
Column,
Expand All @@ -14,22 +14,24 @@
)
from sqlalchemy.orm import relationship
from datetime import datetime
from api.v1.models.base import Base, user_organisation_association
from api.v1.models.base import Base, user_organization_association
from sqlalchemy.dialects.postgresql import UUID
from uuid_extensions import uuid7


class Organisation(Base):
__tablename__ = 'organisations'
class Organization(Base):
__tablename__ = 'organizations'

id = Column(Integer, primary_key=True, autoincrement=True)
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
name = Column(String(50), unique=True, nullable=False)
description = Column(Text, nullable=True)
created_at = Column(DateTime, server_default=func.now())
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())

users = relationship(
"User",
secondary=user_organisation_association,
back_populates="organisations"
secondary=user_organization_association,
back_populates="organizations"
)

def __str__(self):
Expand Down
4 changes: 3 additions & 1 deletion api/v1/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
)
from datetime import datetime
from api.v1.models.base import Base
from sqlalchemy.dialects.postgresql import UUID
from uuid_extensions import uuid7


class Product(Base):
__tablename__ = 'products'

id = Column(Integer, primary_key=True, autoincrement=True)
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
name = Column(String, nullable=False)
description = Column(Text, nullable=True)
price = Column(Numeric, nullable=False)
Expand Down
6 changes: 4 additions & 2 deletions api/v1/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
func,
)
from sqlalchemy.orm import relationship
from sqlalchemy.dialects.postgresql import UUID
from datetime import datetime
from api.v1.models.base import Base
from uuid_extensions import uuid7


class Profile(Base):
__tablename__ = 'profiles'

id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
user_id = Column(UUID(as_uuid=True), ForeignKey('users.id'), nullable=False)
bio = Column(Text, nullable=True)
phone_number = Column(String(50), nullable=True)
avatar_url = Column(String(100), nullable=True)
Expand Down
13 changes: 8 additions & 5 deletions api/v1/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
)
from sqlalchemy.orm import relationship
from datetime import datetime
from api.v1.models.base import Base, user_organisation_association
from api.v1.models.base import Base, user_organization_association
import bcrypt
from uuid_extensions import uuid7
from sqlalchemy.dialects.postgresql import UUID


def hash_password(password: str) -> bytes:
""" Hashes the user password for security
Expand All @@ -30,7 +33,7 @@ def hash_password(password: str) -> bytes:
class User(Base):
__tablename__ = 'users'

id = Column(Integer, primary_key=True, autoincrement=True)
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid7)
username = Column(String(50), unique=True, nullable=False)
email = Column(String(100), unique=True, nullable=False)
password = Column(String(255), nullable=False)
Expand All @@ -40,9 +43,9 @@ class User(Base):
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())

profile = relationship("Profile", uselist=False)
organisations = relationship(
"Organisation",
secondary=user_organisation_association,
organizations = relationship(
"Organization",
secondary=user_organization_association,
back_populates="users"
)

Expand Down
10 changes: 5 additions & 5 deletions db_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from api.db.database import create_database, get_db
from api.v1.models.user import User
from api.v1.models.org import Organisation
from api.v1.models.org import Organization
from api.v1.models.profile import Profile
from api.v1.models.product import Product

Expand All @@ -16,9 +16,9 @@

db.add_all([user_1, user_2, user_3])

org_1 = Organisation(name="Python Org", description="An organisation for python develoers")
org_2 = Organisation(name="Django Org", description="An organisation of django devs")
org_3 = Organisation(name="FastAPI Devs", description="An organisation of Fast API devs")
org_1 = Organization(name="Python Org", description="An organization for python develoers")
org_2 = Organization(name="Django Org", description="An organization of django devs")
org_3 = Organization(name="FastAPI Devs", description="An organization of Fast API devs")

db.add_all([org_1, org_2, org_3])

Expand All @@ -34,7 +34,7 @@

db.add_all([product_1, product_2])
db.commit()
users = db.query(Organisation).first().users
users = db.query(Organization).first().users
for user in users:
print(user.password)
print(profile_1.user_id)
Expand Down

0 comments on commit 2697e0b

Please sign in to comment.