Skip to content

Commit

Permalink
feat:add migration solution_id
Browse files Browse the repository at this point in the history
  • Loading branch information
WSbaikaishui authored and HarukaMa committed Jul 12, 2024
1 parent de5fe78 commit c809225
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion aleo_types/vm_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class AleoID(AleoIDProtocol):

def __init__(self, data: bytes):
if len(self._prefix) != 2:
raise ValueError("locator_prefix must be 2 bytes")
if self._prefix != "solution":
raise ValueError("locator_prefix must be 2 bytes")
self._data = data
self._bech32m = Bech32m(data, self._prefix)

Expand Down Expand Up @@ -100,6 +101,8 @@ class TransactionID(AleoID):
class TransitionID(AleoID):
_prefix = "au"

class SolutionID(AleoID):
_prefix = "solution"

## Saved for reference
# class RecordCiphertext(AleoObject):
Expand Down
37 changes: 37 additions & 0 deletions db/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ async def migrate(self):
(1, self.migrate_1_add_rejected_original_id),
(2, self.migrate_2_set_on_delete_cascade),
(3, self.migrate_3_fix_finalize_operation_function),
(4, self.migrate_4_add_solution_id)
]
async with self.pool.connection() as conn:
async with conn.cursor() as cur:
Expand Down Expand Up @@ -93,3 +94,39 @@ async def migrate_2_set_on_delete_cascade(conn: psycopg.AsyncConnection[DictRow]
@staticmethod
async def migrate_3_fix_finalize_operation_function(conn: psycopg.AsyncConnection[DictRow]):
await conn.execute(cast(LiteralString, open("migration_3.sql").read()))


@staticmethod
async def migrate_4_add_solution_id(conn: psycopg.AsyncConnection[DictRow]):
# add column
try:
async with conn.transaction():
await conn.execute("""
ALTER TABLE solution
ADD COLUMN solution_id TEXT NULL
""")
except Exception as e:
raise RuntimeError(f"Failed to add column 'solution_id' to 'solution': {e}")

async with conn.transaction():
async with conn.cursor() as cur:
await cur.execute(
"SELECT id, epoch_hash, address, counter FROM solution ",
)
rows = await cur.fetchall()
if not rows:
break

for row in rows:
solution_id = SolutionID.load(
BytesIO(aleo_explorer_rust.solution_to_id(
str(row["epoch_hash"]),
str(row["address"]),
int(row["counter"])
)
)
)
await cur.execute(
"UPDATE solution SET solution_id = %s WHERE id = %s",
(str(solution_id), row["id"])
)

0 comments on commit c809225

Please sign in to comment.