Fix chain import to use correct delete syntax and preserve chain_id from imported data
- Remove unused sqlalchemy.func import - Replace select().delete() with delete() for proper SQLModel syntax in import_chain - Add chain_id extraction from first block if not provided in import_data root - Add chain_id field to Block creation during import to ensure consistency - Remove redundant session.close() in export_chain finally block (session_scope handles cleanup)
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from sqlalchemy import func
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
@@ -9,7 +8,7 @@ from datetime import datetime, timedelta
|
|||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, status
|
from fastapi import APIRouter, HTTPException, status
|
||||||
from pydantic import BaseModel, Field, model_validator
|
from pydantic import BaseModel, Field, model_validator
|
||||||
from sqlmodel import select
|
from sqlmodel import select, delete
|
||||||
|
|
||||||
from ..database import session_scope
|
from ..database import session_scope
|
||||||
from ..gossip import gossip_broker
|
from ..gossip import gossip_broker
|
||||||
@@ -739,8 +738,6 @@ async def export_chain(chain_id: str = None) -> Dict[str, Any]:
|
|||||||
"export_data": export_data,
|
"export_data": export_data,
|
||||||
"export_size_bytes": len(json.dumps(export_data))
|
"export_size_bytes": len(json.dumps(export_data))
|
||||||
}
|
}
|
||||||
finally:
|
|
||||||
session.close()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
_logger.error(f"Error exporting chain: {e}")
|
_logger.error(f"Error exporting chain: {e}")
|
||||||
raise HTTPException(status_code=500, detail=f"Failed to export chain: {str(e)}")
|
raise HTTPException(status_code=500, detail=f"Failed to export chain: {str(e)}")
|
||||||
@@ -754,6 +751,10 @@ async def import_chain(import_data: dict) -> Dict[str, Any]:
|
|||||||
accounts = import_data.get("accounts", [])
|
accounts = import_data.get("accounts", [])
|
||||||
transactions = import_data.get("transactions", [])
|
transactions = import_data.get("transactions", [])
|
||||||
|
|
||||||
|
# If chain_id not in import_data, try to get it from first block
|
||||||
|
if not chain_id and blocks:
|
||||||
|
chain_id = blocks[0].get("chain_id")
|
||||||
|
|
||||||
with session_scope() as session:
|
with session_scope() as session:
|
||||||
# Validate import
|
# Validate import
|
||||||
if not blocks:
|
if not blocks:
|
||||||
@@ -773,13 +774,14 @@ async def import_chain(import_data: dict) -> Dict[str, Any]:
|
|||||||
_logger.info(f"Backing up existing chain with {existing_count} blocks")
|
_logger.info(f"Backing up existing chain with {existing_count} blocks")
|
||||||
|
|
||||||
# Clear existing data
|
# Clear existing data
|
||||||
session.execute(select(Block).delete())
|
session.execute(delete(Block))
|
||||||
session.execute(select(Account).delete())
|
session.execute(delete(Account))
|
||||||
session.execute(select(Transaction).delete())
|
session.execute(delete(Transaction))
|
||||||
|
|
||||||
# Import blocks
|
# Import blocks
|
||||||
for block_data in blocks:
|
for block_data in blocks:
|
||||||
block = Block(
|
block = Block(
|
||||||
|
chain_id=chain_id,
|
||||||
height=block_data["height"],
|
height=block_data["height"],
|
||||||
hash=block_data["hash"],
|
hash=block_data["hash"],
|
||||||
parent_hash=block_data["parent_hash"],
|
parent_hash=block_data["parent_hash"],
|
||||||
@@ -793,6 +795,7 @@ async def import_chain(import_data: dict) -> Dict[str, Any]:
|
|||||||
# Import accounts
|
# Import accounts
|
||||||
for account_data in accounts:
|
for account_data in accounts:
|
||||||
account = Account(
|
account = Account(
|
||||||
|
chain_id=chain_id,
|
||||||
address=account_data["address"],
|
address=account_data["address"],
|
||||||
balance=account_data["balance"],
|
balance=account_data["balance"],
|
||||||
nonce=account_data["nonce"]
|
nonce=account_data["nonce"]
|
||||||
|
|||||||
Reference in New Issue
Block a user