Make reply to hash nullable (#1057)

* Make reply to hash column nullable

* Add itest for null reply_to field
This commit is contained in:
Jonathan Zernik 2021-08-24 17:39:49 -07:00 committed by GitHub
parent 641b81ac38
commit e78c2560bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 5 deletions

View file

@ -90,6 +90,8 @@ def test_make_squeak(admin_stub, signing_profile_id):
assert get_squeak_display_entry.HasField("author")
assert len(
get_squeak_display_entry.author.profile_image) > 0
assert not get_squeak_display_entry.is_reply
assert not bool(get_squeak_display_entry.reply_to)
# Block time should be within the past hour
block_time = datetime.datetime.fromtimestamp(

View file

@ -23,13 +23,13 @@ logger = logging.getLogger(__name__)
DEFAULT_PROFILE_IMAGE = load_default_profile_image()
# TODO: Remove this after reply is nullable column
EMPTY_HASH = b'\x00' * 32
# EMPTY_HASH = b'\x00' * 32
def squeak_entry_to_message(squeak_entry_with_profile: SqueakEntryWithProfile) -> squeak_admin_pb2.SqueakDisplayEntry:
squeak_entry = squeak_entry_with_profile.squeak_entry
squeak_profile = squeak_entry_with_profile.squeak_profile
is_reply = squeak_entry.reply_to != EMPTY_HASH
is_reply = bool(squeak_entry.reply_to)
reply_to = squeak_entry.reply_to.hex() if squeak_entry.reply_to else None
is_author_known = False
profile_msg = None

View file

@ -0,0 +1,36 @@
"""Make reply to hash nullable
Revision ID: adec68bd0785
Revises: a35d180bf020
Create Date: 2021-08-24 17:10:21.009027
"""
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision = 'adec68bd0785'
down_revision = 'a35d180bf020'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('squeak', schema=None) as batch_op:
batch_op.alter_column('hash_reply_sqk',
existing_type=sa.VARCHAR(length=64),
nullable=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('squeak', schema=None) as batch_op:
batch_op.alter_column('hash_reply_sqk',
existing_type=sa.VARCHAR(length=64),
nullable=False)
# ### end Alembic commands ###

View file

@ -63,7 +63,7 @@ class Models:
Column("created", TZDateTime,
server_default=func.now(), nullable=False),
Column("squeak", Binary, nullable=False),
Column("hash_reply_sqk", String(64), nullable=False),
Column("hash_reply_sqk", String(64), nullable=True),
Column("hash_block", String(64), nullable=False),
Column("n_block_height", Integer, nullable=False),
Column("n_time", Integer, nullable=False),

View file

@ -167,7 +167,7 @@ class SqueakDb:
ins = self.squeaks.insert().values(
hash=get_hash(squeak).hex(),
squeak=squeak.serialize(),
hash_reply_sqk=squeak.hashReplySqk.hex(),
hash_reply_sqk=squeak.hashReplySqk.hex() if squeak.is_reply else None,
hash_block=squeak.hashBlock.hex(),
n_block_height=squeak.nBlockHeight,
n_time=squeak.nTime,
@ -1103,6 +1103,8 @@ class SqueakDb:
def _parse_squeak_entry(self, row) -> SqueakEntry:
secret_key_column = row["secret_key"]
is_locked = bool(secret_key_column)
reply_to = bytes.fromhex(
row["hash_reply_sqk"]) if row["hash_reply_sqk"] else None
liked_time = row["liked_time"]
liked_time_s = int(liked_time.timestamp()) if liked_time else None
return SqueakEntry(
@ -1111,7 +1113,7 @@ class SqueakDb:
block_height=row["n_block_height"],
block_hash=bytes.fromhex(row["hash_block"]),
block_time=row["block_time"],
reply_to=bytes.fromhex(row["hash_reply_sqk"]),
reply_to=reply_to,
is_unlocked=is_locked,
liked_time=liked_time_s,
content=row["content"],