Skip to content

Commit

Permalink
PROTO-1767: rm old reactions columns and modify indexers to not use it (
Browse files Browse the repository at this point in the history
  • Loading branch information
alecsavvy authored May 13, 2024
1 parent bd0550a commit a8f42e7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ begin

if new.reaction_value != 0 then
INSERT INTO notification
(slot, user_ids, timestamp, type, specifier, group_id, data)
(user_ids, timestamp, type, specifier, group_id, data)
VALUES
(
new.slot,
ARRAY [tip_sender_user_id],
new.timestamp,
'reaction',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- drop slot and tx_signature column in reactions table
begin;

lock table reactions in exclusive mode;

-- drop unused and legacy columns
alter table reactions drop column if exists slot;
alter table reactions drop column if exists tx_signature;

-- add default block number
alter table reactions add column blocknumber integer default 1;

-- cascade delete on block revert
alter table reactions drop constraint if exists reactions_blocknumber_fkey;
alter table reactions add constraint reactions_blocknumber_fkey foreign key (blocknumber) references blocks(number) on delete cascade;

commit;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sqlalchemy import asc

from integration_tests.utils import populate_mock_db
from integration_tests.utils import populate_mock_db, populate_mock_db_blocks
from src.models.notifications.notification import Notification
from src.utils.db_session import get_db

Expand All @@ -21,6 +21,7 @@ def test_reaction_notification(app):
entities = {
"users": [{"user_id": i + 1, "wallet": "0x" + str(i)} for i in range(4)],
}
populate_mock_db_blocks(db, 0, 1)
populate_mock_db(db, entities)

entities = {
Expand Down Expand Up @@ -56,7 +57,7 @@ def test_reaction_notification(app):
notifications: List[Notification] = (
session.query(Notification)
.filter(Notification.type == "reaction")
.order_by(asc(Notification.slot))
.order_by(asc(Notification.timestamp))
.all()
)

Expand All @@ -72,7 +73,6 @@ def test_reaction_notification(app):
== "reaction:reaction_to:sig_2:reaction_type:tip:reaction_value:2"
)
assert notifications[0].type == "reaction"
assert notifications[0].slot == 1
assert notifications[0].blocknumber == None
assert notifications[0].data == {
"reacted_to": "sig_1",
Expand All @@ -86,7 +86,6 @@ def test_reaction_notification(app):
assert notifications[0].user_ids == [2]

assert notifications[1].type == "reaction"
assert notifications[1].slot == 2
assert notifications[1].blocknumber == None
assert notifications[1].data == {
"reacted_to": "sig_2",
Expand All @@ -102,7 +101,7 @@ def test_reaction_notification(app):
notifications: List[Notification] = (
session.query(Notification)
.filter(Notification.type == "tip_receive")
.order_by(asc(Notification.slot))
.order_by(asc(Notification.timestamp))
.all()
)
assert len(notifications) == 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,13 @@ def get_events_side_effect(_, tx_receipt):
assert reaction1.reaction_type == "tip"
assert reaction1.reaction_value == 1
assert reaction1.sender_wallet == "user1wallet"
assert reaction1.slot == 0

reaction2 = all_tip_reactions[1]
assert reaction2.id == 2
assert reaction2.reacted_to == "user_1_tip_3"
assert reaction2.reaction_type == "tip"
assert reaction2.reaction_value == 3
assert reaction2.sender_wallet == "user1wallet"
assert reaction2.slot == 1


def test_identity_and_discovery_tip_reactions(app, mocker):
Expand Down Expand Up @@ -318,13 +316,11 @@ def get_reactions_from_identity_mock(_) -> List[ReactionResponse]:
discovery_reaction = all_tip_reactions[1]

assert 1 == identity_reaction.id
assert 0 == identity_reaction.slot
assert 1 == identity_reaction.reaction_value
assert "user_1_tip_2_1" == identity_reaction.reacted_to
assert "user1wallet" == identity_reaction.sender_wallet

assert 2 == discovery_reaction.id
assert 1 == discovery_reaction.slot
assert 1 == discovery_reaction.reaction_value
assert "user_1_tip_2_2" == discovery_reaction.reacted_to
assert "user1wallet" == discovery_reaction.sender_wallet
2 changes: 0 additions & 2 deletions packages/discovery-provider/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,11 @@ def populate_mock_db(db, entities, block_offset=None):
for i, reaction in enumerate(reactions):
reaction = Reaction(
id=reaction.get("id", i),
slot=reaction.get("slot", i),
reaction_value=reaction.get("reaction_value", 1),
sender_wallet=reaction.get("sender_wallet", "0x"),
reaction_type=reaction.get("reaction_type", "type"),
reacted_to=reaction.get("reacted_to", "reaction_to"),
timestamp=reaction.get("timestamp", datetime.now()),
tx_signature=reaction.get("tx_signature", str(i)),
)
session.add(reaction)
for i, user_bank_tx in enumerate(user_bank_txs):
Expand Down
3 changes: 1 addition & 2 deletions packages/discovery-provider/src/models/social/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class Reaction(Base, RepresentableMixin):
Integer,
primary_key=True,
)
slot = Column(Integer, nullable=False, index=True)
reaction_value = Column(Integer, nullable=False)
sender_wallet = Column(String, nullable=False)
reaction_type = Column(String, nullable=False)
reacted_to = Column(String, nullable=False)
timestamp = Column(DateTime, nullable=False)
tx_signature = Column(String)
blocknumber = Column(Integer, nullable=False)
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,15 @@ def tip_reaction(params: ManageEntityParameters):
reactor_user_id = params.user_id

session = params.session
tip = (
session.query(UserTip.slot, UserTip.sender_user_id)
sender_user_id = (
session.query(UserTip.sender_user_id)
.filter(
UserTip.signature == reacted_to,
UserTip.receiver_user_id == reactor_user_id,
)
.one_or_none()
)

if not tip:
raise IndexingValidationError(
f"tip_reactions.py | reactor {reactor_user_id} reacted to a tip {reacted_to} that doesn't exist"
)

slot, sender_user_id = tip

sender = (
session.query(User).filter(User.user_id == sender_user_id).one_or_none()
)
Expand All @@ -93,10 +86,10 @@ def tip_reaction(params: ManageEntityParameters):
reaction = Reaction(
reacted_to=reacted_to,
reaction_value=reaction_value,
slot=slot,
sender_wallet=sender_wallet,
reaction_type=reaction_type,
timestamp=datetime.now(),
blocknumber=params.block_number,
)

params.add_record(reacted_to, reaction)
Expand Down
2 changes: 0 additions & 2 deletions packages/discovery-provider/src/tasks/index_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ def reaction_dict_to_model(reaction: ReactionResponse) -> Union[Reaction, None]:

try:
reaction_model = Reaction(
slot=reaction["slot"],
reaction_value=reaction["reactionValue"],
sender_wallet=reaction["senderWallet"],
reaction_type=reaction["reactionType"],
reacted_to=reaction["reactedTo"],
timestamp=cast(datetime, reaction["createdAt"]),
tx_signature=None, # no tx_signature for now
)
return reaction_model
except Exception as e:
Expand Down

0 comments on commit a8f42e7

Please sign in to comment.