Skip to content

Commit

Permalink
Ignore failed nodes in unique indexes
Browse files Browse the repository at this point in the history
Fixes: #877

Signed-off-by: Nils Philippsen <[email protected]>
  • Loading branch information
nphilipp committed Jul 27, 2023
1 parent 2694f24 commit a4d5a2f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""ignore failed nodes in unique indexes
Revision ID: 36cf064c0481
Revises: ce2e575cb800
Create Date: 2023-07-27 11:29:37.405520
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "36cf064c0481"
down_revision = "ce2e575cb800"
branch_labels = None
depends_on = None


def upgrade():
op.drop_index("active_hostname_index")
op.create_index(
"active_hostname_index",
"nodes",
["hostname"],
unique=True,
postgresql_where=sa.text(
"retired_at IS NULL AND state <> 'provisioning'::node_state_enum"
+ " AND state <> 'failed'::node_state_enum"
),
sqlite_where=sa.text(
"retired_at IS NULL AND state != 'provisioning' AND state != 'failed'"
),
)
op.drop_index("active_ipaddr_index")
op.create_index(
"active_ipaddr_index",
"nodes",
["ipaddr"],
unique=True,
postgresql_where=sa.text(
"retired_at IS NULL AND state <> 'provisioning'::node_state_enum"
+ " AND state <> 'failed'::node_state_enum"
),
sqlite_where=sa.text(
"retired_at IS NULL AND state != 'provisioning' AND state != 'failed'"
),
)


def downgrade():
op.drop_index("active_hostname_index")
op.create_index(
"active_hostname_index",
"nodes",
["hostname"],
unique=True,
postgresql_where=sa.text("retired_at IS NULL AND state <> 'provisioning'::node_state_enum"),
sqlite_where=sa.text("retired_at IS NULL AND state != 'provisioning'"),
)
op.drop_index("active_ipaddr_index")
op.create_index(
"active_ipaddr_index",
"nodes",
["ipaddr"],
unique=True,
postgresql_where=sa.text("retired_at IS NULL AND state <> 'provisioning'::node_state_enum"),
sqlite_where=sa.text("retired_at IS NULL AND state != 'provisioning'"),
)
2 changes: 1 addition & 1 deletion duffy/database/model/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from ..util import CreatableMixin, RetirableMixin
from .session import Session


INDEX_UNIQUENESS_CLAUSE = and_(
Column("retired_at") == None, # noqa: E711
Column("state") != "provisioning",
Column("state") != "failed",
)


Expand Down

0 comments on commit a4d5a2f

Please sign in to comment.