feat: add organization-related models and update schemas for organization management
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
"""backfill_invite_access
|
||||
|
||||
Revision ID: 050c16fde00e
|
||||
Revises: 2c7b1c4d9e10
|
||||
Create Date: 2026-02-08 20:07:14.621575
|
||||
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '050c16fde00e'
|
||||
down_revision = '2c7b1c4d9e10'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
now = datetime.utcnow()
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT
|
||||
m.id AS member_id,
|
||||
iba.board_id AS board_id,
|
||||
iba.can_read AS can_read,
|
||||
iba.can_write AS can_write
|
||||
FROM organization_invites i
|
||||
JOIN organization_invite_board_access iba
|
||||
ON iba.organization_invite_id = i.id
|
||||
JOIN organization_members m
|
||||
ON m.user_id = i.accepted_by_user_id
|
||||
AND m.organization_id = i.organization_id
|
||||
WHERE i.accepted_at IS NOT NULL
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
|
||||
for row in rows:
|
||||
can_write = bool(row.can_write)
|
||||
can_read = bool(row.can_read or row.can_write)
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO organization_board_access (
|
||||
id,
|
||||
organization_member_id,
|
||||
board_id,
|
||||
can_read,
|
||||
can_write,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
:id,
|
||||
:member_id,
|
||||
:board_id,
|
||||
:can_read,
|
||||
:can_write,
|
||||
:now,
|
||||
:now
|
||||
)
|
||||
ON CONFLICT (organization_member_id, board_id) DO UPDATE
|
||||
SET
|
||||
can_read = organization_board_access.can_read OR EXCLUDED.can_read,
|
||||
can_write = organization_board_access.can_write OR EXCLUDED.can_write,
|
||||
updated_at = EXCLUDED.updated_at
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": uuid.uuid4(),
|
||||
"member_id": row.member_id,
|
||||
"board_id": row.board_id,
|
||||
"can_read": can_read,
|
||||
"can_write": can_write,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
pass
|
||||
259
backend/alembic/versions/1f2a3b4c5d6e_add_organizations.py
Normal file
259
backend/alembic/versions/1f2a3b4c5d6e_add_organizations.py
Normal file
@@ -0,0 +1,259 @@
|
||||
"""add organizations
|
||||
|
||||
Revision ID: 1f2a3b4c5d6e
|
||||
Revises: 9f0c4fb2a7b8
|
||||
Create Date: 2026-02-07
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "1f2a3b4c5d6e"
|
||||
down_revision = "9f0c4fb2a7b8"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
DEFAULT_ORG_NAME = "Personal"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"organizations",
|
||||
sa.Column("id", sa.UUID(), primary_key=True, nullable=False),
|
||||
sa.Column("name", sa.String(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.UniqueConstraint("name", name="uq_organizations_name"),
|
||||
)
|
||||
op.create_index("ix_organizations_name", "organizations", ["name"])
|
||||
|
||||
op.create_table(
|
||||
"organization_members",
|
||||
sa.Column("id", sa.UUID(), primary_key=True, nullable=False),
|
||||
sa.Column("organization_id", sa.UUID(), nullable=False),
|
||||
sa.Column("user_id", sa.UUID(), nullable=False),
|
||||
sa.Column("role", sa.String(), nullable=False, server_default="member"),
|
||||
sa.Column("all_boards_read", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("all_boards_write", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"], name="fk_org_members_org"),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"], name="fk_org_members_user"),
|
||||
sa.UniqueConstraint(
|
||||
"organization_id",
|
||||
"user_id",
|
||||
name="uq_organization_members_org_user",
|
||||
),
|
||||
)
|
||||
op.create_index("ix_org_members_org", "organization_members", ["organization_id"])
|
||||
op.create_index("ix_org_members_user", "organization_members", ["user_id"])
|
||||
op.create_index("ix_org_members_role", "organization_members", ["role"])
|
||||
|
||||
op.create_table(
|
||||
"organization_board_access",
|
||||
sa.Column("id", sa.UUID(), primary_key=True, nullable=False),
|
||||
sa.Column("organization_member_id", sa.UUID(), nullable=False),
|
||||
sa.Column("board_id", sa.UUID(), nullable=False),
|
||||
sa.Column("can_read", sa.Boolean(), nullable=False, server_default=sa.text("true")),
|
||||
sa.Column("can_write", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["organization_member_id"],
|
||||
["organization_members.id"],
|
||||
name="fk_org_board_access_member",
|
||||
),
|
||||
sa.ForeignKeyConstraint(["board_id"], ["boards.id"], name="fk_org_board_access_board"),
|
||||
sa.UniqueConstraint(
|
||||
"organization_member_id",
|
||||
"board_id",
|
||||
name="uq_org_board_access_member_board",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_org_board_access_member",
|
||||
"organization_board_access",
|
||||
["organization_member_id"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_org_board_access_board",
|
||||
"organization_board_access",
|
||||
["board_id"],
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"organization_invites",
|
||||
sa.Column("id", sa.UUID(), primary_key=True, nullable=False),
|
||||
sa.Column("organization_id", sa.UUID(), nullable=False),
|
||||
sa.Column("invited_email", sa.String(), nullable=False),
|
||||
sa.Column("token", sa.String(), nullable=False),
|
||||
sa.Column("role", sa.String(), nullable=False, server_default="member"),
|
||||
sa.Column("all_boards_read", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("all_boards_write", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("created_by_user_id", sa.UUID(), nullable=True),
|
||||
sa.Column("accepted_by_user_id", sa.UUID(), nullable=True),
|
||||
sa.Column("accepted_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"], name="fk_org_invites_org"),
|
||||
sa.ForeignKeyConstraint(["created_by_user_id"], ["users.id"], name="fk_org_invites_creator"),
|
||||
sa.ForeignKeyConstraint(["accepted_by_user_id"], ["users.id"], name="fk_org_invites_acceptor"),
|
||||
sa.UniqueConstraint("token", name="uq_org_invites_token"),
|
||||
)
|
||||
op.create_index("ix_org_invites_org", "organization_invites", ["organization_id"])
|
||||
op.create_index("ix_org_invites_email", "organization_invites", ["invited_email"])
|
||||
op.create_index("ix_org_invites_token", "organization_invites", ["token"])
|
||||
|
||||
op.create_table(
|
||||
"organization_invite_board_access",
|
||||
sa.Column("id", sa.UUID(), primary_key=True, nullable=False),
|
||||
sa.Column("organization_invite_id", sa.UUID(), nullable=False),
|
||||
sa.Column("board_id", sa.UUID(), nullable=False),
|
||||
sa.Column("can_read", sa.Boolean(), nullable=False, server_default=sa.text("true")),
|
||||
sa.Column("can_write", sa.Boolean(), nullable=False, server_default=sa.text("false")),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.ForeignKeyConstraint(
|
||||
["organization_invite_id"],
|
||||
["organization_invites.id"],
|
||||
name="fk_org_invite_access_invite",
|
||||
),
|
||||
sa.ForeignKeyConstraint(["board_id"], ["boards.id"], name="fk_org_invite_access_board"),
|
||||
sa.UniqueConstraint(
|
||||
"organization_invite_id",
|
||||
"board_id",
|
||||
name="uq_org_invite_board_access_invite_board",
|
||||
),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_org_invite_access_invite",
|
||||
"organization_invite_board_access",
|
||||
["organization_invite_id"],
|
||||
)
|
||||
op.create_index(
|
||||
"ix_org_invite_access_board",
|
||||
"organization_invite_board_access",
|
||||
["board_id"],
|
||||
)
|
||||
|
||||
op.add_column("boards", sa.Column("organization_id", sa.UUID(), nullable=True))
|
||||
op.add_column("board_groups", sa.Column("organization_id", sa.UUID(), nullable=True))
|
||||
op.add_column("gateways", sa.Column("organization_id", sa.UUID(), nullable=True))
|
||||
|
||||
op.create_index("ix_boards_organization_id", "boards", ["organization_id"])
|
||||
op.create_index("ix_board_groups_organization_id", "board_groups", ["organization_id"])
|
||||
op.create_index("ix_gateways_organization_id", "gateways", ["organization_id"])
|
||||
|
||||
op.create_foreign_key(
|
||||
"fk_boards_organization_id",
|
||||
"boards",
|
||||
"organizations",
|
||||
["organization_id"],
|
||||
["id"],
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_board_groups_organization_id",
|
||||
"board_groups",
|
||||
"organizations",
|
||||
["organization_id"],
|
||||
["id"],
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_gateways_organization_id",
|
||||
"gateways",
|
||||
"organizations",
|
||||
["organization_id"],
|
||||
["id"],
|
||||
)
|
||||
|
||||
bind = op.get_bind()
|
||||
now = datetime.utcnow()
|
||||
org_id = uuid.uuid4()
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"INSERT INTO organizations (id, name, created_at, updated_at) VALUES (:id, :name, :now, :now)"
|
||||
),
|
||||
{"id": org_id, "name": DEFAULT_ORG_NAME, "now": now},
|
||||
)
|
||||
|
||||
bind.execute(
|
||||
sa.text("UPDATE boards SET organization_id = :org_id"),
|
||||
{"org_id": org_id},
|
||||
)
|
||||
bind.execute(
|
||||
sa.text("UPDATE board_groups SET organization_id = :org_id"),
|
||||
{"org_id": org_id},
|
||||
)
|
||||
bind.execute(
|
||||
sa.text("UPDATE gateways SET organization_id = :org_id"),
|
||||
{"org_id": org_id},
|
||||
)
|
||||
|
||||
user_rows = list(bind.execute(sa.text("SELECT id FROM users")))
|
||||
for row in user_rows:
|
||||
user_id = row[0]
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
INSERT INTO organization_members
|
||||
(id, organization_id, user_id, role, all_boards_read, all_boards_write, created_at, updated_at)
|
||||
VALUES
|
||||
(:id, :org_id, :user_id, :role, :all_read, :all_write, :now, :now)
|
||||
"""
|
||||
),
|
||||
{
|
||||
"id": uuid.uuid4(),
|
||||
"org_id": org_id,
|
||||
"user_id": user_id,
|
||||
"role": "owner",
|
||||
"all_read": True,
|
||||
"all_write": True,
|
||||
"now": now,
|
||||
},
|
||||
)
|
||||
|
||||
op.alter_column("boards", "organization_id", nullable=False)
|
||||
op.alter_column("board_groups", "organization_id", nullable=False)
|
||||
op.alter_column("gateways", "organization_id", nullable=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_constraint("fk_gateways_organization_id", "gateways", type_="foreignkey")
|
||||
op.drop_constraint("fk_board_groups_organization_id", "board_groups", type_="foreignkey")
|
||||
op.drop_constraint("fk_boards_organization_id", "boards", type_="foreignkey")
|
||||
|
||||
op.drop_index("ix_gateways_organization_id", table_name="gateways")
|
||||
op.drop_index("ix_board_groups_organization_id", table_name="board_groups")
|
||||
op.drop_index("ix_boards_organization_id", table_name="boards")
|
||||
|
||||
op.drop_column("gateways", "organization_id")
|
||||
op.drop_column("board_groups", "organization_id")
|
||||
op.drop_column("boards", "organization_id")
|
||||
|
||||
op.drop_index("ix_org_invite_access_board", table_name="organization_invite_board_access")
|
||||
op.drop_index("ix_org_invite_access_invite", table_name="organization_invite_board_access")
|
||||
op.drop_table("organization_invite_board_access")
|
||||
|
||||
op.drop_index("ix_org_invites_token", table_name="organization_invites")
|
||||
op.drop_index("ix_org_invites_email", table_name="organization_invites")
|
||||
op.drop_index("ix_org_invites_org", table_name="organization_invites")
|
||||
op.drop_table("organization_invites")
|
||||
|
||||
op.drop_index("ix_org_board_access_board", table_name="organization_board_access")
|
||||
op.drop_index("ix_org_board_access_member", table_name="organization_board_access")
|
||||
op.drop_table("organization_board_access")
|
||||
|
||||
op.drop_index("ix_org_members_role", table_name="organization_members")
|
||||
op.drop_index("ix_org_members_user", table_name="organization_members")
|
||||
op.drop_index("ix_org_members_org", table_name="organization_members")
|
||||
op.drop_table("organization_members")
|
||||
|
||||
op.drop_index("ix_organizations_name", table_name="organizations")
|
||||
op.drop_table("organizations")
|
||||
24
backend/alembic/versions/2c7b1c4d9e10_merge_heads.py
Normal file
24
backend/alembic/versions/2c7b1c4d9e10_merge_heads.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""merge heads
|
||||
|
||||
Revision ID: 2c7b1c4d9e10
|
||||
Revises: 1f2a3b4c5d6e, af403671a8c4
|
||||
Create Date: 2026-02-07
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "2c7b1c4d9e10"
|
||||
down_revision = ("1f2a3b4c5d6e", "af403671a8c4")
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
pass
|
||||
@@ -0,0 +1,70 @@
|
||||
"""add active organization to users
|
||||
|
||||
Revision ID: 6e1c9b2f7a4d
|
||||
Revises: 050c16fde00e
|
||||
Create Date: 2026-02-08
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "6e1c9b2f7a4d"
|
||||
down_revision = "050c16fde00e"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column(
|
||||
"users",
|
||||
sa.Column("active_organization_id", sa.UUID(), nullable=True),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_users_active_organization_id",
|
||||
"users",
|
||||
["active_organization_id"],
|
||||
)
|
||||
op.create_foreign_key(
|
||||
"fk_users_active_organization",
|
||||
"users",
|
||||
"organizations",
|
||||
["active_organization_id"],
|
||||
["id"],
|
||||
)
|
||||
|
||||
bind = op.get_bind()
|
||||
rows = bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
SELECT user_id, organization_id
|
||||
FROM organization_members
|
||||
ORDER BY user_id, created_at ASC
|
||||
"""
|
||||
)
|
||||
).fetchall()
|
||||
seen: set[str] = set()
|
||||
for row in rows:
|
||||
user_id = str(row.user_id)
|
||||
if user_id in seen:
|
||||
continue
|
||||
seen.add(user_id)
|
||||
bind.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE users
|
||||
SET active_organization_id = :org_id
|
||||
WHERE id = :user_id
|
||||
AND active_organization_id IS NULL
|
||||
"""
|
||||
),
|
||||
{"org_id": row.organization_id, "user_id": row.user_id},
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_constraint("fk_users_active_organization", "users", type_="foreignkey")
|
||||
op.drop_index("ix_users_active_organization_id", table_name="users")
|
||||
op.drop_column("users", "active_organization_id")
|
||||
Reference in New Issue
Block a user