Skip to content

Commit 9b0e5e5

Browse files
committed
Internal: make file size column not nullable
Problem: the size column of the `files` table is nullable for no reason. Solution: make it non-nullable.
1 parent ef2bfdd commit 9b0e5e5

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""file size not null
2+
3+
Revision ID: 039c56d3b33e
4+
Revises: daa92b500049
5+
Create Date: 2023-04-13 17:13:01.353182
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = "039c56d3b33e"
14+
down_revision = "daa92b500049"
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.alter_column("files", "size", existing_type=sa.BIGINT(), nullable=False)
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade() -> None:
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.alter_column("files", "size", existing_type=sa.BIGINT(), nullable=True)
28+
# ### end Alembic commands ###

src/aleph/chains/chaindata.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import asyncio
22
import json
3-
from typing import Dict, Optional, List, Any, Mapping, Set, Type, cast
3+
from typing import Dict, Optional, List, Any, Mapping, Set, cast
44

55
from aleph_message.models import StoreContent, ItemType, Chain, MessageType
66
from pydantic import ValidationError
77

88
from aleph.chains.common import LOGGER
99
from aleph.config import get_config
1010
from aleph.db.accessors.chains import upsert_chain_tx
11-
from aleph.db.accessors.files import upsert_tx_file_pin, upsert_stored_file
11+
from aleph.db.accessors.files import upsert_tx_file_pin, upsert_file
1212
from aleph.db.accessors.pending_txs import upsert_pending_tx
13-
from aleph.db.models import ChainTxDb, MessageDb, StoredFileDb
13+
from aleph.db.models import ChainTxDb, MessageDb
1414
from aleph.exceptions import (
1515
InvalidContent,
1616
AlephStorageException,
1717
ContentCurrentlyUnavailable,
1818
)
1919
from aleph.schemas.chains.indexer_response import MessageEvent, GenericMessageEvent
20+
from aleph.schemas.chains.tezos_indexer_response import (
21+
MessageEventPayload as TezosMessageEventPayload,
22+
)
2023
from aleph.storage import StorageService
2124
from aleph.toolkit.timestamp import utc_now
2225
from aleph.types.chain_sync import ChainSyncProtocol
2326
from aleph.types.db_session import DbSessionFactory, DbSession
2427
from aleph.types.files import FileType
2528
from aleph.utils import get_sha256
26-
from aleph.schemas.chains.tezos_indexer_response import MessageEventPayload as TezosMessageEventPayload
2729

2830
INCOMING_MESSAGE_AUTHORIZED_FIELDS = [
2931
"item_hash",
@@ -140,12 +142,12 @@ async def _get_tx_messages_off_chain_protocol(
140142
with self.session_factory() as session:
141143
# Some chain data files are duplicated, and can be treated in parallel,
142144
# hence the upsert.
143-
stored_file = StoredFileDb(
144-
hash=sync_file_content.hash,
145-
type=FileType.FILE,
145+
upsert_file(
146+
session=session,
147+
file_hash=sync_file_content.hash,
148+
file_type=FileType.FILE,
146149
size=len(sync_file_content.raw_value),
147150
)
148-
upsert_stored_file(session=session, file=stored_file)
149151
upsert_tx_file_pin(
150152
session=session,
151153
file_hash=file_hash,

src/aleph/db/accessors/files.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ def get_address_files_for_api(
147147
return session.execute(select_stmt).all()
148148

149149

150-
def upsert_file(
151-
session: DbSession, file_hash: str, size: Optional[int], file_type: FileType
152-
):
150+
def upsert_file(session: DbSession, file_hash: str, size: int, file_type: FileType):
153151
upsert_file_stmt = (
154152
insert(StoredFileDb)
155153
.values(hash=file_hash, size=size, type=file_type)
@@ -158,15 +156,6 @@ def upsert_file(
158156
session.execute(upsert_file_stmt)
159157

160158

161-
def upsert_stored_file(
162-
session: DbSession,
163-
file: StoredFileDb,
164-
) -> None:
165-
upsert_file(
166-
session=session, file_hash=file.hash, size=file.size, file_type=file.type
167-
)
168-
169-
170159
def get_file(session: DbSession, file_hash: str) -> Optional[StoredFileDb]:
171160
select_stmt = select(StoredFileDb).where(StoredFileDb.hash == file_hash)
172161
return session.execute(select_stmt).scalar_one_or_none()

src/aleph/db/models/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StoredFileDb(Base):
3737

3838
# size: int = Column(BigInteger, nullable=False)
3939
# TODO: compute the size from local storage
40-
size: Optional[int] = Column(BigInteger, nullable=True)
40+
size: int = Column(BigInteger, nullable=False)
4141
type: FileType = Column(ChoiceType(FileType), nullable=False)
4242

4343
pins: List["FilePinDb"] = relationship("FilePinDb", back_populates="file")

src/aleph/handlers/content/store.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
insert_message_file_pin,
2424
get_file_tag,
2525
upsert_file_tag,
26-
upsert_stored_file,
2726
delete_file_pin,
2827
refresh_file_tag,
2928
is_pinned_file,
3029
get_message_file_pin,
30+
upsert_file,
3131
)
32-
from aleph.db.models import MessageDb, StoredFileDb
32+
from aleph.db.models import MessageDb
3333
from aleph.exceptions import AlephStorageException, UnknownHashError
3434
from aleph.handlers.content.content_handler import ContentHandler
3535
from aleph.storage import StorageService
@@ -121,7 +121,9 @@ async def fetch_related_content(
121121

122122
ipfs_enabled = config.ipfs.enabled.value
123123
do_standard_lookup = True
124-
size = None
124+
125+
# Sentinel value, the code below always sets a value but mypy does not see it.
126+
size: int = -1
125127

126128
if engine == ItemType.ipfs and ipfs_enabled:
127129
if item_type_from_hash(item_hash) != ItemType.ipfs:
@@ -190,12 +192,12 @@ async def fetch_related_content(
190192

191193
size = len(file_content)
192194

193-
stored_file = StoredFileDb(
194-
hash=item_hash,
195-
type=FileType.DIRECTORY if is_folder else FileType.FILE,
195+
upsert_file(
196+
session=session,
197+
file_hash=item_hash,
198+
file_type=FileType.DIRECTORY if is_folder else FileType.FILE,
196199
size=size,
197200
)
198-
upsert_stored_file(session=session, file=stored_file)
199201

200202
async def check_dependencies(self, session: DbSession, message: MessageDb) -> None:
201203
content = _get_store_content(message)

tests/db/test_files.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
@pytest.mark.asyncio
1919
async def test_is_pinned_file(session_factory: DbSessionFactory):
2020
def is_pinned(_session_factory, _file_hash) -> bool:
21-
with session_factory() as session:
22-
return is_pinned_file(session=session, file_hash=_file_hash)
21+
with _session_factory() as _session:
22+
return is_pinned_file(session=_session, file_hash=_file_hash)
2323

2424
file = StoredFileDb(
2525
hash="QmTm7g1Mh3BhrQPjnedVQ5g67DR7cwhyMN3MvFt1JPPdWd",
26+
size=27,
2627
type=FileType.FILE,
2728
)
2829

@@ -49,10 +50,12 @@ def is_pinned(_session_factory, _file_hash) -> bool:
4950
async def test_upsert_file_tag(session_factory: DbSessionFactory):
5051
original_file = StoredFileDb(
5152
hash="QmTm7g1Mh3BhrQPjnedVQ5g67DR7cwhyMN3MvFt1JPPdWd",
53+
size=32,
5254
type=FileType.FILE,
5355
)
5456
new_version = StoredFileDb(
5557
hash="QmTm7g1Mh3BhrQPjnedVQ5g67DR7cwhyMN3MvFt1JPPdWe",
58+
size=413,
5659
type=FileType.FILE,
5760
)
5861

@@ -122,10 +125,12 @@ async def test_refresh_file_tag(session_factory: DbSessionFactory):
122125
files = [
123126
StoredFileDb(
124127
hash="QmTm7g1Mh3BhrQPjnedVQ5g67DR7cwhyMN3MvFt1JPPdWd",
128+
size=123,
125129
type=FileType.FILE,
126130
),
127131
StoredFileDb(
128132
hash="QmTm7g1Mh3BhrQPjnedVQ5g67DR7cwhyMN3MvFt1JPPdWe",
133+
size=678,
129134
type=FileType.FILE,
130135
),
131136
]

0 commit comments

Comments
 (0)