pastesrht: Use canonical user IDs
Update user IDs across paste.sr.ht to match those of meta.sr.ht.
This commit is contained in:
parent
2eca3bd693
commit
dede02bda9
|
@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/paste.sr.ht/api
|
|||
go 1.17
|
||||
|
||||
require (
|
||||
git.sr.ht/~sircmpwn/core-go v0.0.0-20220530120843-d0bf1153ada4
|
||||
git.sr.ht/~sircmpwn/core-go v0.0.0-20221025082458-3e69641ef307
|
||||
github.com/99designs/gqlgen v0.17.20
|
||||
github.com/Masterminds/squirrel v1.5.0
|
||||
github.com/go-chi/chi v4.1.2+incompatible
|
||||
|
|
|
@ -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=
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
"""Use canonical user ID
|
||||
|
||||
Revision ID: 5db7585a816b
|
||||
Revises: 6d6af372fc35
|
||||
Create Date: 2022-07-14 15:25:05.083188
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '5db7585a816b'
|
||||
down_revision = '6d6af372fc35'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# These tables all have a column referencing "user"(id)
|
||||
tables = [
|
||||
("gql_user_wh_sub", "user_id"),
|
||||
("oauthtoken", "user_id"),
|
||||
("paste", "user_id"),
|
||||
]
|
||||
|
||||
def upgrade():
|
||||
# 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;
|
||||
""")
|
||||
|
||||
|
||||
def downgrade():
|
||||
# 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;
|
||||
""")
|
|
@ -0,0 +1,54 @@
|
|||
"""Add user.remote_id
|
||||
|
||||
Revision ID: 6d6af372fc35
|
||||
Revises: fbec2ccf782e
|
||||
Create Date: 2022-07-14 15:22:49.698064
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '6d6af372fc35'
|
||||
down_revision = 'fbec2ccf782e'
|
||||
|
||||
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"]
|
Loading…
Reference in New Issue