32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
"""Check and create the CheckConstraint for vehicle.assets table."""
|
|
from app.database import engine_sync
|
|
from sqlalchemy import text
|
|
|
|
with engine_sync.connect() as conn:
|
|
# Check if constraint exists
|
|
result = conn.execute(
|
|
text("""
|
|
SELECT conname, pg_get_constraintdef(oid)
|
|
FROM pg_constraint
|
|
WHERE conrelid = 'vehicle.assets'::regclass
|
|
AND contype = 'c'
|
|
""")
|
|
).fetchall()
|
|
print('Constraints on vehicle.assets:')
|
|
for row in result:
|
|
print(f' {row[0]}: {row[1]}')
|
|
|
|
if not result:
|
|
print('No CheckConstraint found - creating it now...')
|
|
conn.execute(
|
|
text("""
|
|
ALTER TABLE vehicle.assets
|
|
ADD CONSTRAINT ck_asset_vin_or_plate_required
|
|
CHECK (vin IS NOT NULL OR license_plate IS NOT NULL)
|
|
""")
|
|
)
|
|
conn.commit()
|
|
print('CheckConstraint created successfully!')
|
|
else:
|
|
print('CheckConstraint already exists.')
|