Skip to content

Commit

Permalink
feat: add filename to deployments db
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed Nov 22, 2024
1 parent ce6971d commit d684069
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
14 changes: 8 additions & 6 deletions boa/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def get_session_id():
class Deployment:
contract_address: Address # receipt_dict["createAddress"]
contract_name: str
filename: str
rpc: str
deployer: Address # ostensibly equal to tx_dict["from"]
tx_hash: str
Expand Down Expand Up @@ -90,6 +91,7 @@ def from_sql_tuple(cls, values):
session_id text,
contract_address text,
contract_name text,
filename text,
rpc text,
deployer text,
tx_hash text,
Expand All @@ -112,14 +114,14 @@ def __init__(self, path=":memory:"):
self.db = sqlite3.connect(path)
self.db.execute(_CREATE_CMD)

# Migration for legacy DB without ABI column
if not self._abi_is_in_db():
# Migration for legacy DB without filename column
if not self._filename_is_in_db():
try:
self.db.execute("ALTER TABLE deployments ADD COLUMN abi text;")
self.db.execute("ALTER TABLE deployments ADD COLUMN filename text;")
self.db.commit()
except sqlite3.Error as e:
self.db.rollback()
raise Exception(f"Failed to add 'abi' column: {e}")
raise Exception(f"Failed to add 'filename' column: {e}")

def __del__(self):
self.db.close()
Expand All @@ -135,10 +137,10 @@ def insert_deployment(self, deployment: Deployment):
self.db.execute(insert_cmd, tuple(values.values()))
self.db.commit()

def _abi_is_in_db(self) -> bool:
def _filename_is_in_db(self) -> bool:
cursor = self.db.execute("PRAGMA table_info(deployments);")
columns = [col[1] for col in cursor.fetchall()]
if "abi" in columns:
if "filename" in columns:
return True
return False

Expand Down
2 changes: 2 additions & 0 deletions boa/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ def deploy(

if (deployments_db := get_deployments_db()) is not None:
contract_name = getattr(contract, "contract_name", None)
filename = getattr(contract, "filename", None)
try:
source_bundle = get_verification_bundle(contract)
except Exception as e:
Expand All @@ -419,6 +420,7 @@ def deploy(
deployment_data = Deployment(
create_address,
contract_name,
filename,
self._rpc.name,
sender,
receipt["transactionHash"],
Expand Down
11 changes: 7 additions & 4 deletions tests/integration/network/anvil/test_network_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ def test_deployment_db_overriden_contract_name():
with set_deployments_db(DeploymentsDB(":memory:")) as db:
arg = 5
contract_name = "test_deployment"
filename = "my_filename"

# contract is written to deployments db
contract = boa.loads(code, arg, name=contract_name)
contract = boa.loads(code, arg, name=contract_name, filename=filename)

# test get_deployments()
deployment = next(db.get_deployments())
Expand All @@ -93,6 +94,7 @@ def test_deployment_db_overriden_contract_name():
# sanity check all the fields
assert deployment.contract_address == contract.address
assert deployment.contract_name == contract.contract_name
assert deployment.filename == contract.filename
assert deployment.contract_name == contract_name
assert deployment.deployer == boa.env.eoa
assert deployment.rpc == boa.env._rpc.name
Expand Down Expand Up @@ -121,6 +123,7 @@ def test_deployment_db_no_overriden_name():
# sanity check all the fields
assert deployment.contract_address == contract.address
assert deployment.contract_name == contract.contract_name
assert deployment.filename == "<unknown>"
assert deployment.contract_name != non_contract_name
assert deployment.deployer == boa.env.eoa
assert deployment.rpc == boa.env._rpc.name
Expand All @@ -139,7 +142,7 @@ def temp_legacy_db_path() -> Path:
db_path = temp_dir / "test.db"
conn = sqlite3.connect(db_path)
conn.execute(_CREATE_CMD)
DROP_COLUMN_SQL = "ALTER TABLE deployments DROP COLUMN ABI;"
DROP_COLUMN_SQL = "ALTER TABLE deployments DROP COLUMN filename;"
conn.execute(DROP_COLUMN_SQL)
return db_path

Expand All @@ -148,7 +151,7 @@ def test_deployments_db_migration(temp_legacy_db_path):
sql_db = sqlite3.connect(temp_legacy_db_path)
cursor = sql_db.execute("PRAGMA table_info(deployments);")
columns = [col[1] for col in cursor.fetchall()]
assert "abi" not in columns
assert "filename" not in columns

db = DeploymentsDB(temp_legacy_db_path)
assert db._abi_is_in_db() is True
assert db._filename_is_in_db() is True

0 comments on commit d684069

Please sign in to comment.