Phase 7A: wire ADR-001 asset position contract surface
Lock the position-resolution columns from ADR-001 in code so
resolve_asset_position's relationship walk activates.
Schema
- Asset.mapleft -> Asset.mapx, Asset.maptop -> Asset.mapy
- Location.mapx / Location.mapy added (fallback for priority 3 of the
ADR-001 resolution chain)
- AssetRelationship.label (free-text nuance per ADR-001)
- AssetRelationship.inheritsposition (bool, server_default true, controls
whether the resolved-position walk follows the edge)
- RelationshipType.propagatesthroughid (self-FK; sibling-propagation rail)
Seeds
- Three canonical ADR-001 relationship types created idempotently:
partof, controls, connectedto
- controls.propagatesthroughid wired to partof (partof + connectedto stay
null per ADR-001 table). Both via Alembic migration AND CLI seed command
so a fresh test fixture and a sister-site deploy both end up correct.
- Legacy connection types (Serial Cable, Direct Ethernet, USB, WiFi,
Dualpath) retained for backward compat with pre-1.0 relationship rows.
Resolver
- shopdb.api.resolve_asset_position now walks inheritsposition=true edges
of type partof (then controls), recursively, depth-capped at 3 with
visited-set cycle protection. Inactive edges + non-inheritable types
are skipped. Falls through to the existing location fallback when the
walk yields nothing.
Tests
- 11 new test_api_namespace cases cover: partof walk, controls-after-
partof ordering, connectedto skipped, inheritsposition=false skipped,
recursion, cycle break, depth-3 cap, self-beats-related, related-beats-
location, inactive-edge skip.
- 111 tests pass. Naming/style check green.
Migration
- migrations/versions/7a01_adr001_position_contract.py:
- alter_column renames on assets (no data loss)
- add_column on locations + relationshiptypes + assetrelationships
- idempotent seed of three ADR types + propagation FK wire-up
- downgrade reverses + best-effort deletion of seeded types that have
no FK refs
Backend rename (mapleft/maptop -> mapx/mapy)
- shopdb/core/api/assets.py
- plugins/{computers,equipment,network,printers}/api/...
- scripts/migration/migrate_assets.py
- Legacy Machine model + machines API + import_from_mysql.py UNCHANGED
(per ADR-001 Machine retires; not part of the asset contract)
Frontend rename
- frontend/src/components/ShopFloorMap.vue
- frontend/src/views/{MapEditor.vue, pcs/{PCDetail,PCForm}.vue,
printers/{PrinterDetail,PrinterForm}.vue,
machines/{MachineDetail,MachineForm}.vue,
network/NetworkDeviceForm.vue}
- Form field labels + v-model bindings + computed flags switched in
lockstep with the backend.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
133
migrations/versions/7a01_adr001_position_contract.py
Normal file
133
migrations/versions/7a01_adr001_position_contract.py
Normal file
@@ -0,0 +1,133 @@
|
||||
"""ADR-001 position contract surface
|
||||
|
||||
Rename Asset.mapleft -> Asset.mapx, Asset.maptop -> Asset.mapy. Add
|
||||
Location.mapx / Location.mapy. Add AssetRelationship.label,
|
||||
AssetRelationship.inheritsposition, RelationshipType.propagatesthroughid.
|
||||
Seed the three canonical relationship types (partof, controls, connectedto)
|
||||
with controls.propagatesthroughid -> partof.
|
||||
|
||||
Revision ID: 7a01_adr001_position
|
||||
Revises: 68b3947ae14f
|
||||
Create Date: 2026-05-30
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision = '7a01_adr001_position'
|
||||
down_revision = '68b3947ae14f'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ---- Asset.mapleft/maptop -> mapx/mapy --------------------------------
|
||||
with op.batch_alter_table('assets') as batch_op:
|
||||
batch_op.alter_column('mapleft', new_column_name='mapx',
|
||||
existing_type=sa.Integer(), existing_nullable=True,
|
||||
comment='X coordinate on floor map (ADR-001)')
|
||||
batch_op.alter_column('maptop', new_column_name='mapy',
|
||||
existing_type=sa.Integer(), existing_nullable=True,
|
||||
comment='Y coordinate on floor map (ADR-001)')
|
||||
|
||||
# ---- Location: add mapx/mapy for the fallback path --------------------
|
||||
with op.batch_alter_table('locations') as batch_op:
|
||||
batch_op.add_column(sa.Column('mapx', sa.Integer(), nullable=True,
|
||||
comment='Default X coordinate for assets at this location'))
|
||||
batch_op.add_column(sa.Column('mapy', sa.Integer(), nullable=True,
|
||||
comment='Default Y coordinate for assets at this location'))
|
||||
|
||||
# ---- RelationshipType: add propagatesthroughid self-FK ----------------
|
||||
with op.batch_alter_table('relationshiptypes') as batch_op:
|
||||
batch_op.add_column(sa.Column('propagatesthroughid', sa.Integer(),
|
||||
nullable=True,
|
||||
comment='Sibling-propagation rail per ADR-001'))
|
||||
batch_op.create_foreign_key(
|
||||
'fk_relationshiptype_propagation',
|
||||
'relationshiptypes',
|
||||
['propagatesthroughid'],
|
||||
['relationshiptypeid'],
|
||||
)
|
||||
|
||||
# ---- AssetRelationship: add label + inheritsposition ------------------
|
||||
with op.batch_alter_table('assetrelationships') as batch_op:
|
||||
batch_op.add_column(sa.Column('label', sa.String(length=200), nullable=True,
|
||||
comment='Free-text relationship description (ADR-001)'))
|
||||
batch_op.add_column(sa.Column('inheritsposition', sa.Boolean(),
|
||||
nullable=False, server_default='1',
|
||||
comment='If true, resolved-position walk follows this edge (ADR-001)'))
|
||||
|
||||
# ---- Seed three canonical relationship types --------------------------
|
||||
# Idempotent: insert only if name is not already present. Then update
|
||||
# controls.propagatesthroughid to point at partof.
|
||||
relationshiptypes = sa.table(
|
||||
'relationshiptypes',
|
||||
sa.column('relationshiptype', sa.String),
|
||||
sa.column('description', sa.Text),
|
||||
sa.column('propagatesthroughid', sa.Integer),
|
||||
sa.column('isactive', sa.Boolean),
|
||||
)
|
||||
|
||||
conn = op.get_bind()
|
||||
for rt in (
|
||||
('partof', 'Composition / sub-assembly (ADR-001)'),
|
||||
('controls', 'Operational authority over another asset (ADR-001)'),
|
||||
('connectedto', 'Network or data link without authority (ADR-001)'),
|
||||
):
|
||||
existing = conn.execute(sa.text(
|
||||
"SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = :n"
|
||||
), {'n': rt[0]}).first()
|
||||
if not existing:
|
||||
conn.execute(relationshiptypes.insert().values(
|
||||
relationshiptype=rt[0],
|
||||
description=rt[1],
|
||||
propagatesthroughid=None,
|
||||
isactive=True,
|
||||
))
|
||||
|
||||
partof_row = conn.execute(sa.text(
|
||||
"SELECT relationshiptypeid FROM relationshiptypes WHERE relationshiptype = 'partof'"
|
||||
)).first()
|
||||
if partof_row:
|
||||
conn.execute(sa.text(
|
||||
"UPDATE relationshiptypes SET propagatesthroughid = :p WHERE relationshiptype = 'controls'"
|
||||
), {'p': partof_row[0]})
|
||||
|
||||
|
||||
def downgrade():
|
||||
# Revert in reverse order. Drops the seeded ADR types if no rows
|
||||
# reference them; otherwise leaves them in place to avoid FK violations.
|
||||
conn = op.get_bind()
|
||||
|
||||
with op.batch_alter_table('assetrelationships') as batch_op:
|
||||
batch_op.drop_column('inheritsposition')
|
||||
batch_op.drop_column('label')
|
||||
|
||||
with op.batch_alter_table('relationshiptypes') as batch_op:
|
||||
batch_op.drop_constraint('fk_relationshiptype_propagation', type_='foreignkey')
|
||||
batch_op.drop_column('propagatesthroughid')
|
||||
|
||||
with op.batch_alter_table('locations') as batch_op:
|
||||
batch_op.drop_column('mapy')
|
||||
batch_op.drop_column('mapx')
|
||||
|
||||
with op.batch_alter_table('assets') as batch_op:
|
||||
batch_op.alter_column('mapx', new_column_name='mapleft',
|
||||
existing_type=sa.Integer(), existing_nullable=True)
|
||||
batch_op.alter_column('mapy', new_column_name='maptop',
|
||||
existing_type=sa.Integer(), existing_nullable=True)
|
||||
|
||||
# Best-effort: drop the seeded types if nothing references them.
|
||||
for name in ('connectedto', 'controls', 'partof'):
|
||||
try:
|
||||
conn.execute(sa.text(
|
||||
"DELETE FROM relationshiptypes "
|
||||
"WHERE relationshiptype = :n "
|
||||
"AND relationshiptypeid NOT IN ("
|
||||
" SELECT relationshiptypeid FROM assetrelationships "
|
||||
" UNION SELECT relationshiptypeid FROM machinerelationships"
|
||||
")"
|
||||
), {'n': name})
|
||||
except Exception:
|
||||
pass
|
||||
Reference in New Issue
Block a user