Skip to content

[Bug] Cannot Revert Chain Shard Change Due to Duplicate Key Constraint #6196

@0x19dG87

Description

@0x19dG87

Description

When changing the shard for a chain using graphman chain change-shard, the existing chain is correctly renamed to <chain>-old and persists in the database.

However, if a subsequent attempt is made to revert the chain's shard back to the original shard (which would cause the currently active chain to be renamed to <chain>-old again), the operation fails due to a unique constraint violation on the chains table. The error indicates that the key value (<chain>-old) already exists, as it was created during the initial shard change.

Steps to Reproduce

  • Start with chain A on shard S1 (e.g., matic on polygon_chain).
  • Change the shard for chain A to S2 (e.g., primary):
graphman chain change-shard A S2
# Result: Successful. A is on S2. A-old (the original chain) is on S1.
  • Attempt to change the shard for chain A back to the original shard S1:
graphman chain change-shard A S1

Expected Behavior

The shard change should complete successfully. The current chain A (on S2) should be renamed to A-old (or a more uniquely named backup), and the old chain A-old (on S1) should be re-promoted/renamed to A.

Actual Behavior

The command fails with a database constraint violation:

Error: database constraint violated: duplicate key value violates unique constraint "chains_name_key": Key (name)=(matic-old) already exists.

Additional

The current logic for change-shard unconditionally attempts to rename the current chain to <chain>-old. This approach is fragile because it doesn't account for the persistence of a previously archived <chain>-old entity. The failure occurs because the database enforces a unique constraint on the name column.

Relevant log output

root@b5c46359fc62:/# graphman chain change-shard matic primary
Changing block cache shard for matic to primary
Current shard: polygon_chain
Error: database constraint violated: duplicate key value violates unique constraint "chains_name_key": Key (name)=(matic-old) already exists.

IPFS hash

No response

Subgraph name or link to explorer

No response

Some information to help us out

  • Tick this box if this bug is caused by a regression found in the latest release.
  • Tick this box if this bug is specific to the hosted service.
  • I have searched the issue tracker to make sure this issue is not a duplicate.

OS information

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions