Skip to content

Commit

Permalink
✨ add product_name to folders table (#6124)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrei Neagu <[email protected]>
  • Loading branch information
GitHK and Andrei Neagu authored Aug 1, 2024
1 parent 56764b3 commit 48e2791
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""added product name to folders
Revision ID: 617e0ecaf602
Revises: 21699ee569a7
Create Date: 2024-07-31 08:26:46.073511+00:00
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "617e0ecaf602"
down_revision = "21699ee569a7"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("folders", sa.Column("product_name", sa.String(), nullable=False))
op.create_foreign_key(
"fk_folders_to_products_name",
"folders",
"products",
["product_name"],
["name"],
onupdate="CASCADE",
ondelete="CASCADE",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("fk_folders_to_products_name", "folders", type_="foreignkey")
op.drop_column("folders", "product_name")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
server_default="",
doc="user provided description for the folder",
),
sa.Column(
"product_name",
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
name="fk_folders_to_products_name",
),
nullable=False,
doc="product identifier",
),
sa.Column(
"created_by",
sa.BigInteger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .models.folders import folders, folders_access_rights, folders_to_projects
from .models.groups import GroupType, groups

_ProductName: TypeAlias = str
_ProjectID: TypeAlias = uuid.UUID
_GroupID: TypeAlias = PositiveInt
_FolderID: TypeAlias = PositiveInt
Expand Down Expand Up @@ -68,7 +69,9 @@ class FolderAccessError(FoldersError):


class FolderNotFoundError(FolderAccessError):
msg_template = "no entry for folder_id={folder_id} found"
msg_template = (
"no entry found for folder_id={folder_id} and product_name={product_name}"
)


class FolderNotSharedWithGidError(FolderAccessError):
Expand All @@ -84,9 +87,7 @@ class BaseCreateFolderError(FoldersError):


class FolderAlreadyExistsError(BaseCreateFolderError):
msg_template = (
"A folder='{folder}' with parent='{parent}' for group='{gid}' already exists"
)
msg_template = "A folder='{folder}' with parent='{parent}' for group='{gid}' in product_name={product_name} already exists"


class ParentFolderIsNotWritableError(BaseCreateFolderError):
Expand Down Expand Up @@ -400,6 +401,7 @@ def _get_permissions_where_clause() -> ColumnElement | bool:

async def _check_folder_and_access(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -413,10 +415,12 @@ async def _check_folder_and_access(
InsufficientPermissionsError
"""
folder_entry: int | None = await connection.scalar(
sa.select([folders.c.id]).where(folders.c.id == folder_id)
sa.select([folders.c.id])
.where(folders.c.id == folder_id)
.where(folders.c.product_name == product_name)
)
if not folder_entry:
raise FolderNotFoundError(folder_id=folder_id)
raise FolderNotFoundError(folder_id=folder_id, product_name=product_name)

# check if folder was shared
resolved_access_rights_without_permissions = await _get_resolved_access_rights(
Expand Down Expand Up @@ -454,6 +458,7 @@ async def _check_folder_and_access(

async def folder_create(
connection: SAConnection,
product_name: _ProductName,
name: str,
gid: _GroupID,
*,
Expand Down Expand Up @@ -484,16 +489,19 @@ async def folder_create(
)
)
.where(folders.c.name == name)
# .where(folders_access_rights.c.gid == gid)
.where(folders.c.product_name == product_name)
.where(folders_access_rights.c.original_parent_id == parent)
)
if entry_exists:
raise FolderAlreadyExistsError(folder=name, parent=parent, gid=gid)
raise FolderAlreadyExistsError(
product_name=product_name, folder=name, parent=parent, gid=gid
)

if parent:
# check if parent has permissions
await _check_folder_and_access(
connection,
product_name,
folder_id=parent,
gid=gid,
permissions=required_permissions,
Expand All @@ -504,7 +512,12 @@ async def folder_create(
try:
folder_id = await connection.scalar(
sa.insert(folders)
.values(name=name, description=description, created_by=gid)
.values(
name=name,
description=description,
created_by=gid,
product_name=product_name,
)
.returning(folders.c.id)
)

Expand All @@ -528,6 +541,7 @@ async def folder_create(

async def folder_share_or_update_permissions(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
sharing_gid: _GroupID,
*,
Expand All @@ -547,6 +561,7 @@ async def folder_share_or_update_permissions(
async with connection.begin():
await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=sharing_gid,
permissions=required_permissions,
Expand Down Expand Up @@ -577,6 +592,7 @@ async def folder_share_or_update_permissions(

async def folder_update(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -595,6 +611,7 @@ async def folder_update(
async with connection.begin():
await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=gid,
permissions=required_permissions,
Expand All @@ -619,6 +636,7 @@ async def folder_update(

async def folder_delete(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -637,6 +655,7 @@ async def folder_delete(
async with connection.begin():
await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=gid,
permissions=required_permissions,
Expand All @@ -656,7 +675,7 @@ async def folder_delete(

# first remove all childeren
for child_folder_id in childern_folder_ids:
await folder_delete(connection, child_folder_id, gid)
await folder_delete(connection, product_name, child_folder_id, gid)

# as a last step remove the folder per se
async with connection.begin():
Expand All @@ -665,6 +684,7 @@ async def folder_delete(

async def folder_move(
connection: SAConnection,
product_name: _ProductName,
source_folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -686,6 +706,7 @@ async def folder_move(
async with connection.begin():
source_access_entry = await _check_folder_and_access(
connection,
product_name,
folder_id=source_folder_id,
gid=gid,
permissions=required_permissions_source,
Expand All @@ -703,6 +724,7 @@ async def folder_move(
if destination_folder_id:
await _check_folder_and_access(
connection,
product_name,
folder_id=destination_folder_id,
gid=gid,
permissions=required_permissions_destination,
Expand All @@ -724,6 +746,7 @@ async def folder_move(

async def folder_add_project(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -742,6 +765,7 @@ async def folder_add_project(
async with connection.begin():
await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=gid,
permissions=required_permissions,
Expand Down Expand Up @@ -771,6 +795,7 @@ async def folder_add_project(

async def folder_remove_project(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID,
gid: _GroupID,
*,
Expand All @@ -788,6 +813,7 @@ async def folder_remove_project(
async with connection.begin():
await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=gid,
permissions=required_permissions,
Expand All @@ -803,6 +829,7 @@ async def folder_remove_project(

async def folder_list(
connection: SAConnection,
product_name: _ProductName,
folder_id: _FolderID | None,
gid: _GroupID,
*,
Expand All @@ -828,6 +855,7 @@ async def folder_list(
# this one provides the set of access rights
resolved_access_rights = await _check_folder_and_access(
connection,
product_name,
folder_id=folder_id,
gid=gid,
permissions=required_permissions,
Expand Down
Loading

0 comments on commit 48e2791

Please sign in to comment.