package store import ( "context" "database/sql" ) // RemoveMember removes the member with id and, in the same transaction, the // Oficina ID links that name its number, as connect requires: a link left // behind would hold its Oficina account so it could not link again. The // number itself stays taken, so it can never name anyone else. With Oficina // ID off the links table may not exist, and there is nothing to delete. func RemoveMember(ctx context.Context, db *sql.DB, id string) error { tx, err := db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() var links int if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'home_identity_links'`).Scan(&links); err != nil { return err } if links > 0 { if _, err := tx.ExecContext(ctx, `DELETE FROM home_identity_links WHERE person_id IN (SELECT number FROM member_numbers WHERE member_id = ?)`, id); err != nil { return err } } if _, err := tx.ExecContext(ctx, `DELETE FROM members WHERE id = ?`, id); err != nil { return err } return tx.Commit() }