refactor: remove Vendor.owner_user_id column

- Remove owner_user_id column and owner relationship from Vendor model
- Update User model ownership checks to use company relationship
- Add migration to drop owner_user_id column from vendors table

Ownership is now determined solely via vendor.company.owner_user_id

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-02 19:39:08 +01:00
parent 14054bfd6d
commit 5ba4603ac9
4 changed files with 72 additions and 246 deletions

View File

@@ -0,0 +1,60 @@
"""remove_vendor_owner_user_id_column
Revision ID: 9f3a25ea4991
Revises: 5818330181a5
Create Date: 2025-12-02 17:58:45.663338
This migration removes the owner_user_id column from the vendors table.
Architecture Change:
- OLD: Each vendor had its own owner (vendor.owner_user_id)
- NEW: Vendors belong to a company, company has one owner (company.owner_user_id)
The vendor ownership is now determined via the company relationship:
- vendor.company.owner_user_id contains the owner
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '9f3a25ea4991'
down_revision: Union[str, None] = '5818330181a5'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""
Remove owner_user_id column from vendors table.
Ownership is now determined via the company relationship.
Note: SQLite batch mode recreates the table without the column,
so we don't need to explicitly drop constraints.
"""
with op.batch_alter_table('vendors', schema=None) as batch_op:
# Drop the column - batch mode handles constraints automatically
batch_op.drop_column('owner_user_id')
def downgrade() -> None:
"""
Re-add owner_user_id column to vendors table.
WARNING: This will add the column back but NOT restore the data.
You will need to manually populate owner_user_id from company.owner_user_id
if reverting this migration.
"""
with op.batch_alter_table('vendors', schema=None) as batch_op:
batch_op.add_column(
sa.Column('owner_user_id', sa.Integer(), nullable=True)
)
batch_op.create_foreign_key(
'vendors_owner_user_id_fkey',
'users',
['owner_user_id'],
['id']
)