todosrht: Use canonical user IDs

Update user IDs across todo.sr.ht to match those of meta.sr.ht.
This commit is contained in:
Adnan Maolood 2022-08-18 22:16:03 -04:00 committed by Drew DeVault
parent 30084360aa
commit a26fc1fbfd
4 changed files with 163 additions and 3 deletions

View File

@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/todo.sr.ht/api
go 1.15
require (
git.sr.ht/~sircmpwn/core-go v0.0.0-20220530120843-d0bf1153ada4
git.sr.ht/~sircmpwn/core-go v0.0.0-20221025082458-3e69641ef307
git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3
github.com/99designs/gqlgen v0.17.20
github.com/Masterminds/squirrel v1.4.0

View File

@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220530120843-d0bf1153ada4 h1:pyGHngvO7qIANeI5pmqzfVc+nLKSTTuwq2OA66nxEGw=
git.sr.ht/~sircmpwn/core-go v0.0.0-20220530120843-d0bf1153ada4/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc=
git.sr.ht/~sircmpwn/core-go v0.0.0-20221025082458-3e69641ef307 h1:KYkDsSLoJZ+LshT7ab/8PMhWyq8X+43+FkT1hR3ee1c=
git.sr.ht/~sircmpwn/core-go v0.0.0-20221025082458-3e69641ef307/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc=
git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 h1:9WCv5cK67s2SiY/R4DWT/OchEsFnfYDz3lbevKxZ4QI=
git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw=
git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk=

View File

@ -0,0 +1,106 @@
"""Use canonical user ID
Revision ID: 4c70fa9bc46f
Revises: b5f503ac3ae9
Create Date: 2022-06-16 11:38:50.325723
"""
# revision identifiers, used by Alembic.
revision = '4c70fa9bc46f'
down_revision = 'b5f503ac3ae9'
from alembic import op
import sqlalchemy as sa
# These tables all have a column referencing "user"(id)
tables = [
("event_notification", "user_id"),
("gql_ticket_wh_sub", "user_id"),
("gql_tracker_wh_sub", "user_id"),
("gql_user_wh_sub", "user_id"),
("oauthtoken", "user_id"),
("participant", "user_id"),
("ticket_assignee", "assignee_id"),
("ticket_assignee", "assigner_id"),
("ticket_label", "user_id"),
("ticket_webhook_subscription", "user_id"),
("tracker", "owner_id"),
("tracker_webhook_subscription", "user_id"),
("user_access", "user_id"),
("user_webhook_subscription", "user_id"),
]
def upgrade():
# Drop unique constraints
op.execute("""
ALTER TABLE participant DROP CONSTRAINT participant_user_id_key;
ALTER TABLE tracker DROP CONSTRAINT tracker_owner_id_name_unique;
""")
# Drop foreign key constraints and update user IDs
for (table, col) in tables:
op.execute(f"""
ALTER TABLE {table} DROP CONSTRAINT {table}_{col}_fkey;
UPDATE {table} t SET {col} = u.remote_id FROM "user" u WHERE u.id = t.{col};
""")
# Update primary key
op.execute("""
ALTER TABLE "user" DROP CONSTRAINT user_pkey;
ALTER TABLE "user" DROP CONSTRAINT user_remote_id_key;
ALTER TABLE "user" RENAME COLUMN id TO old_id;
ALTER TABLE "user" RENAME COLUMN remote_id TO id;
ALTER TABLE "user" ADD PRIMARY KEY (id);
ALTER TABLE "user" ADD UNIQUE (old_id);
""")
# Add foreign key constraints
for (table, col) in tables:
op.execute(f"""
ALTER TABLE {table} ADD CONSTRAINT {table}_{col}_fkey FOREIGN KEY ({col}) REFERENCES "user"(id) ON DELETE CASCADE;
""")
# Add unique constraints
op.execute("""
ALTER TABLE participant ADD CONSTRAINT participant_user_id_key UNIQUE (user_id);
ALTER TABLE tracker ADD CONSTRAINT tracker_owner_id_name_unique UNIQUE (owner_id, name);
""")
def downgrade():
# Drop unique constraints
op.execute("""
ALTER TABLE participant DROP CONSTRAINT participant_user_id_key;
ALTER TABLE tracker DROP CONSTRAINT tracker_owner_id_name_unique;
""")
# Drop foreign key constraints and update user IDs
for (table, col) in tables:
op.execute(f"""
ALTER TABLE {table} DROP CONSTRAINT {table}_{col}_fkey;
UPDATE {table} t SET {col} = u.old_id FROM "user" u WHERE u.id = t.{col};
""")
# Update primary key
op.execute("""
ALTER TABLE "user" DROP CONSTRAINT user_pkey;
ALTER TABLE "user" DROP CONSTRAINT user_old_id_key;
ALTER TABLE "user" RENAME COLUMN id TO remote_id;
ALTER TABLE "user" RENAME COLUMN old_id TO id;
ALTER TABLE "user" ADD PRIMARY KEY (id);
ALTER TABLE "user" ADD UNIQUE (remote_id);
""")
# Add foreign key constraints
for (table, col) in tables:
op.execute(f"""
ALTER TABLE {table} ADD CONSTRAINT {table}_{col}_fkey FOREIGN KEY ({col}) REFERENCES "user"(id) ON DELETE CASCADE;
""")
# Add unique constraints
op.execute("""
ALTER TABLE participant ADD CONSTRAINT participant_user_id_key UNIQUE (user_id);
ALTER TABLE tracker ADD CONSTRAINT tracker_owner_id_name_unique UNIQUE (owner_id, name);
""")

View File

@ -0,0 +1,54 @@
"""Add user.remote_id
Revision ID: b5f503ac3ae9
Revises: e1e2e901be0c
Create Date: 2022-06-16 10:11:00.727683
"""
# revision identifiers, used by Alembic.
revision = 'b5f503ac3ae9'
down_revision = 'e1e2e901be0c'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from srht.crypto import internal_anon
from srht.database import db
from srht.graphql import exec_gql
Base = declarative_base()
class User(Base):
__tablename__ = "user"
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(sa.Unicode(256), index=True, unique=True)
remote_id = sa.Column(sa.Integer, unique=True)
def upgrade():
engine = op.get_bind()
session = scoped_session(sessionmaker(
autocommit=False,
autoflush=False,
bind=engine))
Base.query = session.query_property()
op.execute("""ALTER TABLE "user" ADD COLUMN remote_id integer UNIQUE""")
for user in User.query:
user.remote_id = fetch_user_id(user.username)
print(f"~{user.username} id: {user.id} -> {user.remote_id}")
session.commit()
op.execute("""ALTER TABLE "user" ALTER COLUMN remote_id SET NOT NULL""")
def downgrade():
op.drop_column("user", "remote_id")
def fetch_user_id(username):
resp = exec_gql("meta.sr.ht",
"query($username: String!) { user(username: $username) { id } }",
user=internal_anon,
username=username)
return resp["user"]["id"]