Skip to content

Added recoverable table #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
asyncpg==0.30.0
streamflow==0.2.0.dev12
streamflow @ git+https://github.com/alpha-unito/streamflow@master
22 changes: 19 additions & 3 deletions streamflow/plugins/unito/postgresql/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,17 @@ async def add_target(
)

async def add_token(
self, tag: str, type: type[Token], value: Any, port: int | None = None
self,
tag: str,
type: type[Token],
value: Any,
port: int | None = None,
recoverable: bool = False,
) -> int:
async with self.pool as pool:
async with pool.acquire() as conn:
async with conn.transaction():
return await conn.fetchval(
token_id = await conn.fetchval(
"INSERT INTO token(port, type, tag, value) "
"VALUES($1, $2, $3, $4) "
"RETURNING id",
Expand All @@ -256,6 +261,11 @@ async def add_token(
tag,
bytearray(value, "utf-8"),
)
if recoverable:
await conn.fetchval(
"INSERT INTO recoverable(id) VALUES($1)", token_id
)
return token_id

async def add_workflow(
self,
Expand Down Expand Up @@ -460,7 +470,13 @@ async def get_target(self, target_id: int) -> MutableMapping[str, Any]:
async def get_token(self, token_id: int) -> MutableMapping[str, Any]:
async with self.pool as pool:
async with pool.acquire() as conn:
row = await conn.fetchrow("SELECT * FROM token WHERE id = $1", token_id)
row = await conn.fetchrow(
"SELECT *, "
"EXISTS(SELECT 1 FROM recoverable AS r WHERE r.id =$1) AS recoverable "
"FROM token "
"WHERE id =$1",
token_id,
)
return {
k: bytearray(v) if isinstance(v, memoryview) else v
for k, v in row.items()
Expand Down
7 changes: 7 additions & 0 deletions streamflow/plugins/unito/postgresql/schemas/postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ CREATE TABLE IF NOT EXISTS token
);


CREATE TABLE IF NOT EXISTS recoverable
(
id SERIAL PRIMARY KEY,
FOREIGN KEY (id) REFERENCES token (id)
);


CREATE TABLE IF NOT EXISTS provenance
(
dependee INTEGER,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ async def test_local_target(context: StreamFlowContext):


@pytest.mark.asyncio
async def test_token(context: StreamFlowContext):
@pytest.mark.parametrize("recoverable", ["recoverable", "unrecoverable"])
async def test_token(context: StreamFlowContext, recoverable: str):
"""Test saving and loading Token from database"""
token = Token(value=["test", "token"])
token = Token(
value=["test", "token"], tag="0.0", recoverable=recoverable == "recoverable"
)
await save_load_and_test(token, context)


Expand Down
Loading