db migration for better uniqueness constraints
This commit is contained in:
parent
9d4123a734
commit
aa2bc5a447
@ -45,15 +45,17 @@ def upgrade() -> None:
|
|||||||
sa.UniqueConstraint('name', name='pillar_tool_host_unique_name')
|
sa.UniqueConstraint('name', name='pillar_tool_host_unique_name')
|
||||||
)
|
)
|
||||||
op.create_table('pillar_tool_pillar_value',
|
op.create_table('pillar_tool_pillar_value',
|
||||||
|
sa.Column('id', sa.UUID(), nullable=False),
|
||||||
sa.Column('pillar_id', sa.UUID(), nullable=False),
|
sa.Column('pillar_id', sa.UUID(), nullable=False),
|
||||||
sa.Column('host_id', sa.UUID(), nullable=False),
|
sa.Column('host_id', sa.UUID(), nullable=True),
|
||||||
sa.Column('host_group_id', sa.UUID(), nullable=False),
|
sa.Column('host_group_id', sa.UUID(), nullable=True),
|
||||||
sa.Column('type', sa.String(), nullable=False),
|
sa.Column('type', sa.String(), nullable=False),
|
||||||
sa.Column('value', sa.String(), nullable=False),
|
sa.Column('value', sa.String(), nullable=False),
|
||||||
sa.ForeignKeyConstraint(['host_group_id'], ['pillar_tool_host_group.id'], ),
|
sa.ForeignKeyConstraint(['host_group_id'], ['pillar_tool_host_group.id'], ),
|
||||||
sa.ForeignKeyConstraint(['host_id'], ['pillar_tool_host.id'], ),
|
sa.ForeignKeyConstraint(['host_id'], ['pillar_tool_host.id'], ),
|
||||||
sa.ForeignKeyConstraint(['pillar_id'], ['pillar_tool_pillar.id'], ),
|
sa.ForeignKeyConstraint(['pillar_id'], ['pillar_tool_pillar.id'], ),
|
||||||
sa.PrimaryKeyConstraint('pillar_id', 'host_id', 'host_group_id')
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('pillar_id', 'host_id', 'host_group_id', name='pillar_value_unique_pillar_value')
|
||||||
)
|
)
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
"""better uniqueness contraints
|
||||||
|
|
||||||
|
Revision ID: c6fe061ad732
|
||||||
|
Revises: 678356102624
|
||||||
|
Create Date: 2025-12-30 10:09:17.004215
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'c6fe061ad732'
|
||||||
|
down_revision: Union[str, Sequence[str], None] = '678356102624'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
"""Upgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint(op.f('pillar_tool_host_group_unique_name'), 'pillar_tool_host_group', type_='unique')
|
||||||
|
op.create_unique_constraint('pillar_tool_host_group_unique_name_parent', 'pillar_tool_host_group', ['name', 'parent_id'])
|
||||||
|
op.create_unique_constraint('pillar_parent_unique_name_parent', 'pillar_tool_pillar', ['parent_id', 'name'])
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
"""Downgrade schema."""
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint('pillar_parent_unique_name_parent', 'pillar_tool_pillar', type_='unique')
|
||||||
|
op.drop_constraint('pillar_tool_host_group_unique_name_parent', 'pillar_tool_host_group', type_='unique')
|
||||||
|
op.create_unique_constraint(op.f('pillar_tool_host_group_unique_name'), 'pillar_tool_host_group', ['name'], postgresql_nulls_not_distinct=False)
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -9,15 +9,24 @@ class Pillar(Base):
|
|||||||
name = Column(String, nullable=False)
|
name = Column(String, nullable=False)
|
||||||
parent_id = Column(UUID, ForeignKey('pillar_tool_pillar.id'), nullable=True)
|
parent_id = Column(UUID, ForeignKey('pillar_tool_pillar.id'), nullable=True)
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
UniqueConstraint('parent_id', 'name', name="pillar_parent_unique_name_parent"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PillarValue(Base):
|
class PillarValue(Base):
|
||||||
__tablename__ = 'pillar_tool_pillar_value'
|
__tablename__ = 'pillar_tool_pillar_value'
|
||||||
pillar_id = Column(UUID, ForeignKey('pillar_tool_pillar.id'), primary_key=True)
|
id = Column(UUID, primary_key=True)
|
||||||
host_id = Column(UUID, ForeignKey('pillar_tool_host.id'), primary_key=True)
|
pillar_id = Column(UUID, ForeignKey('pillar_tool_pillar.id'), nullable=False)
|
||||||
host_group_id = Column(UUID, ForeignKey('pillar_tool_host_group.id'), primary_key=True)
|
host_id = Column(UUID, ForeignKey('pillar_tool_host.id'), nullable=True)
|
||||||
|
host_group_id = Column(UUID, ForeignKey('pillar_tool_host_group.id'), nullable=True)
|
||||||
type = Column(String, nullable=False)
|
type = Column(String, nullable=False)
|
||||||
value = Column(String, nullable=False)
|
value = Column(String, nullable=False)
|
||||||
|
|
||||||
|
__table_args__ = (
|
||||||
|
UniqueConstraint('pillar_id', 'host_id', 'host_group_id', name='pillar_value_unique_pillar_value'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Host(Base):
|
class Host(Base):
|
||||||
__tablename__ = 'pillar_tool_host'
|
__tablename__ = 'pillar_tool_host'
|
||||||
@ -27,7 +36,6 @@ class Host(Base):
|
|||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
UniqueConstraint('name', name='pillar_tool_host_unique_name'),
|
UniqueConstraint('name', name='pillar_tool_host_unique_name'),
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -38,6 +46,6 @@ class HostGroup(Base):
|
|||||||
parent_id = Column(UUID, ForeignKey('pillar_tool_host_group.id'), nullable=True)
|
parent_id = Column(UUID, ForeignKey('pillar_tool_host_group.id'), nullable=True)
|
||||||
|
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
UniqueConstraint('name', name='pillar_tool_host_group_unique_name'),
|
UniqueConstraint('name', 'parent_id', name='pillar_tool_host_group_unique_name_parent'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user