API: Rig up mutation { delete }

This commit is contained in:
Drew DeVault 2021-09-21 14:41:42 +02:00
parent f54633537b
commit 3bbdbf08de
3 changed files with 58 additions and 2 deletions

View File

@ -206,7 +206,25 @@ func (r *mutationResolver) Update(ctx context.Context, id string, visibility mod
}
func (r *mutationResolver) Delete(ctx context.Context, id string) (*model.Paste, error) {
panic(fmt.Errorf("not implemented"))
var paste model.Paste
if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
row := tx.QueryRowContext(ctx, `
DELETE FROM paste
WHERE sha = $1 AND user_id = $2
RETURNING
id, sha, created, user_id, visibility;`,
id, auth.ForContext(ctx).UserID)
return row.Scan(&paste.PKID, &paste.ID,
&paste.Created, &paste.UserID, &paste.RawVisibility)
}); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return &paste, nil
}
func (r *pasteResolver) Files(ctx context.Context, obj *model.Paste) ([]*model.File, error) {

View File

@ -0,0 +1,37 @@
"""Add cascade to paste_file
Revision ID: a9cfba1cedba
Revises: 72db8bd163a7
Create Date: 2021-09-21 14:39:05.297771
"""
# revision identifiers, used by Alembic.
revision = 'a9cfba1cedba'
down_revision = '72db8bd163a7'
from alembic import op
import sqlalchemy as sa
def upgrade():
op.execute("""
ALTER TABLE paste_file
DROP CONSTRAINT paste_file_paste_id_fkey;
ALTER TABLE paste_file
ADD CONSTRAINT paste_file_paste_id_fkey
FOREIGN KEY (paste_id) REFERENCES paste(id)
ON DELETE CASCADE;
""")
def downgrade():
op.execute("""
ALTER TABLE paste_file
DROP CONSTRAINT paste_file_paste_id_fkey;
ALTER TABLE paste_file
ADD CONSTRAINT paste_file_paste_id_fkey
FOREIGN KEY (paste_id) REFERENCES paste(id);
""")

View File

@ -44,7 +44,8 @@ class PasteFile(Base):
blob = sa.orm.relationship("Blob")
paste_id = sa.Column(sa.Integer,
sa.ForeignKey('paste.id'), nullable=False)
sa.ForeignKey('paste.id', ondelete="CASCADE"),
nullable=False)
paste = sa.orm.relationship("Paste",
backref=sa.orm.backref("files",
cascade="save-update, merge, delete"))