Skip to content

Commit

Permalink
add foreign key constraint on Share user_id field
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbedard committed Oct 3, 2024
1 parent 56bae51 commit 4e3a05e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions apis/paios/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ components:
$ref: '#/components/schemas/uuid4'
user_id:
type: string
format: uuid
nullable: true
expiration_dt:
type: string
Expand Down
2 changes: 1 addition & 1 deletion backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Persona(SQLModelBase, table=True):
class Share(SQLModelBase, table=True):
id: str = Field(primary_key=True) # the short URL tag, eg abcd-efgh-ijkl
resource_id: str = Field(foreign_key="resource.id") # the bot ID
user_id: str | None = Field(default=None) # the user granted access (optional)
user_id: str | None = Field(foreign_key="user.id") # the user granted access (optional)
expiration_dt: datetime | None = Field(default=None) # the link expiration date/time (optional)
is_revoked: bool = Field()

Expand Down
4 changes: 1 addition & 3 deletions backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ class ShareBaseSchema(BaseModel):
expiration_dt: Optional[datetime] = None
is_revoked: Optional[bool] = False

@field_serializer('user_id')
@field_serializer('user_id', when_used='unless-none')
def serialize_user_id(self, user_id: str, _info):
if user_id:
return user_id
else:
return ""

@field_serializer('expiration_dt', when_used='unless-none')
def serialize_expiration_dt(self, dt: datetime, _info):
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/shares.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const shareFilters = [
<ReferenceInput source="resource_id" label="Resource" reference="resources">
<SelectInput optionText="name" />
</ReferenceInput>,
<ReferenceInput source="user_id" label="User" reference="users">
<SelectInput optionText="email" />
</ReferenceInput>,
];

export const ShareList = () => (
Expand All @@ -20,6 +23,9 @@ export const ShareList = () => (
<ReferenceField source="resource_id" reference="resources" link="show">
<TextField source="name" />
</ReferenceField>
<ReferenceField source="user_id" reference="users" link="show">
<TextField source="email" />
</ReferenceField>
<BooleanField source="is_revoked" />
</Datagrid>
</List>
Expand All @@ -32,6 +38,9 @@ export const ShareShow = () => (
<ReferenceField source="resource_id" reference="resources" link="show">
<TextField source="name" />
</ReferenceField>
<ReferenceField source="user_id" reference="users" link="show">
<TextField source="email" />
</ReferenceField>
<DateField source="expiration_dt" label="Expires On" showTime locales="UTC" />
<BooleanField source="is_revoked" />
</SimpleShowLayout>
Expand All @@ -44,6 +53,9 @@ export const ShareEdit = () => (
<ReferenceInput source="resource_id" label="Resource" reference="resources">
<SelectInput optionText="name" />
</ReferenceInput>
<ReferenceInput source="user_id" label="User" reference="users">
<SelectInput optionText="email" />
</ReferenceInput>
<DateTimeInput source="expiration_dt" label="Expires On (Optional)" />
<BooleanInput source="is_revoked" />
</SimpleForm>
Expand All @@ -56,6 +68,9 @@ export const ShareCreate = () => (
<ReferenceInput source="resource_id" label="Resource" reference="resources">
<SelectInput optionText="name" />
</ReferenceInput>
<ReferenceInput source="user_id" label="User" reference="users">
<SelectInput optionText="email" />
</ReferenceInput>
<DateTimeInput source="expiration_dt" label="Expires On (Optional)" />
</SimpleForm>
</Create>
Expand Down
2 changes: 2 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def run_migrations_offline() -> None:
literal_binds=True,
dialect_opts={"paramstyle": "named"},
include_schemas=True,
render_as_batch=True,
)

with context.begin_transaction():
Expand All @@ -81,6 +82,7 @@ def run_migrations_online() -> None:
connection=connection,
target_metadata=target_metadata,
include_schemas=True,
render_as_batch=True,
)

with context.begin_transaction():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""share convert user to FK
Revision ID: 4716615fdc5d
Revises: 187855982332
Create Date: 2024-10-03 12:24:37.618156
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision: str = '4716615fdc5d'
down_revision: Union[str, None] = '187855982332'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
with op.batch_alter_table('share', schema=None) as batch_op:
batch_op.create_foreign_key('user_id', 'user', ['user_id'], ['id'])


def downgrade() -> None:
with op.batch_alter_table('share', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='foreignkey')

0 comments on commit 4e3a05e

Please sign in to comment.