Skip to content

Commit

Permalink
fix: restrict intection delete
Browse files Browse the repository at this point in the history
  • Loading branch information
99cloud committed Jan 30, 2023
1 parent 370f562 commit 3dae25d
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 28 deletions.
2 changes: 2 additions & 0 deletions apitest/edge_node_rsu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tests:
esn: "RSU_ESN02"
areaCode: '320115'
location: {}
intersectionCode: "32011501"
response_json_paths:
$.name: 'RSU_NAME_02'
$.esn: "RSU_ESN02"
Expand All @@ -69,6 +70,7 @@ tests:
esn: "RSU_ESN03"
areaCode: '320115'
location: { }
intersectionCode: "32011501"
response_json_paths:
$.name: 'RSU_NAME_03'
$.esn: "RSU_ESN03"
Expand Down
20 changes: 12 additions & 8 deletions dandelion/alembic/versions/5a9e7ac60afb_rse_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("camera", sa.Column("intersection_code", sa.String(length=64), nullable=True))
op.add_column("camera", sa.Column("intersection_code", sa.String(length=64), nullable=False))
op.create_foreign_key(
"camera_fk_intersection_code", "camera", "intersection", ["intersection_code"], ["code"]
"camera_fk_intersection_code", "camera", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)
op.add_column("radar", sa.Column("intersection_code", sa.String(length=64), nullable=True))
op.add_column("radar", sa.Column("intersection_code", sa.String(length=64), nullable=False))
op.create_foreign_key(
"radar_fk_intersection_code", "radar", "intersection", ["intersection_code"], ["code"]
"radar_fk_intersection_code", "radar", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)
op.add_column("spat", sa.Column("intersection_code", sa.String(length=64), nullable=True))
op.add_column("spat", sa.Column("intersection_code", sa.String(length=64), nullable=False))
op.create_foreign_key(
"spat_fk_intersection_code", "spat", "intersection", ["intersection_code"], ["code"]
"spat_fk_intersection_code", "spat", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)
op.add_column("lidar", sa.Column("intersection_code", sa.String(length=64), nullable=True))
op.add_column("lidar", sa.Column("intersection_code", sa.String(length=64), nullable=False))
op.create_foreign_key(
"lidar_fk_intersection_code", "lidar", "intersection", ["intersection_code"], ["code"]
"lidar_fk_intersection_code", "lidar", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)
# ### end Alembic commands ###

Expand Down
20 changes: 12 additions & 8 deletions dandelion/alembic/versions/a04def91cc98_intersection_fk.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,41 @@ def upgrade():

with op.batch_alter_table("map", schema=None) as batch_op:
batch_op.add_column( # type: ignore[attr-defined]
sa.Column("intersection_code", sa.String(length=64), nullable=True))
sa.Column("intersection_code", sa.String(length=64), nullable=False))
batch_op.drop_column("address") # type: ignore[attr-defined]
batch_op.drop_column("area_code") # type: ignore[attr-defined]
batch_op.create_foreign_key( # type: ignore[attr-defined]
"intersection_fk_code_map", "intersection", ["intersection_code"], ["code"]
"intersection_fk_code_map", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)

with op.batch_alter_table("rsi_event", schema=None) as batch_op:
batch_op.add_column( # type: ignore[attr-defined]
sa.Column("intersection_code", sa.String(length=64), nullable=True))
sa.Column("intersection_code", sa.String(length=64), nullable=False))
batch_op.drop_column("address") # type: ignore[attr-defined]
batch_op.drop_column("area_code") # type: ignore[attr-defined]
batch_op.create_foreign_key( # type: ignore[attr-defined]
"intersection_fk_code_rsi_event", "intersection", ["intersection_code"], ["code"]
"intersection_fk_code_rsi_event", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)

with op.batch_alter_table("rsu", schema=None) as batch_op:
batch_op.add_column( # type: ignore[attr-defined]
sa.Column("intersection_code", sa.String(length=64), nullable=True))
sa.Column("intersection_code", sa.String(length=64), nullable=False))
batch_op.drop_column("address") # type: ignore[attr-defined]
batch_op.drop_column("area_code") # type: ignore[attr-defined]
batch_op.create_foreign_key( # type: ignore[attr-defined]
"intersection_fk_code_rsu", "intersection", ["intersection_code"], ["code"]
"intersection_fk_code_rsu", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)

with op.batch_alter_table("edge_node_rsu", schema=None) as batch_op:
batch_op.add_column( # type: ignore[attr-defined]
sa.Column("intersection_code", sa.String(length=64), nullable=True))
sa.Column("intersection_code", sa.String(length=64), nullable=False))
batch_op.drop_column("area_code") # type: ignore[attr-defined]
batch_op.create_foreign_key( # type: ignore[attr-defined]
"intersection_fk_code_edge_node_rsu", "intersection", ["intersection_code"], ["code"]
"intersection_fk_code_edge_node_rsu", "intersection", ["intersection_code"], ["code"],
onupdate='CASCADE', ondelete='RESTRICT'
)
# mypy: end ignore
# ### end Alembic commands ###
Expand Down
16 changes: 14 additions & 2 deletions dandelion/api/api_v1/endpoints/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create(
db: Session = Depends(deps.get_db),
current_user: models.User = Depends(deps.get_current_user),
) -> schemas.Intersection:
if crud.intersection.get_by_code_and_area(
if crud.intersection.get_by_name_and_area(
db=db, name=intersection_in.name, area_code=intersection_in.area_code
):
raise HTTPException(
Expand Down Expand Up @@ -101,6 +101,11 @@ def delete(
try:
crud.intersection.remove(db, id=intersection_id)
except sql_exc.IntegrityError as ex:
if eval(re.findall(r"\(pymysql.err.IntegrityError\) (.*)", ex.args[0])[0])[0] == 1048:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Intersection [id: {intersection_id}] cannot delete",
)
raise error_handle(ex, "id", str(intersection_id))
return Response(content=None, status_code=status.HTTP_204_NO_CONTENT)

Expand Down Expand Up @@ -182,7 +187,7 @@ def get_all(
)


@router.patch(
@router.put(
"/{intersection_id}",
response_model=schemas.Intersection,
status_code=status.HTTP_200_OK,
Expand Down Expand Up @@ -216,6 +221,13 @@ def update(
area_code = (
intersection_in.area_code if intersection_in.area_code else intersection_in_db.area_code
)
if crud.intersection.get_by_code_and_id(
db=db, code=str(intersection_in.code), intersection_id=intersection_id
):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Intersection [code: {intersection_in.code}] already exist",
)
try:
new_intersection_in_db = crud.intersection.update(
db, db_obj=intersection_in_db, obj_in=intersection_in
Expand Down
14 changes: 13 additions & 1 deletion dandelion/crud/crud_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_multi_with_total(
def get_by_code(self, db: Session, code: str) -> Optional[Intersection]:
return db.query(self.model).filter(self.model.code == code).first()

def get_by_code_and_area(
def get_by_name_and_area(
self,
db: Session,
name: str,
Expand All @@ -70,5 +70,17 @@ def get_by_code_and_area(
.first()
)

def get_by_code_and_id(
self,
db: Session,
code: str,
intersection_id: int,
) -> Optional[Intersection]:
return (
db.query(self.model)
.filter(self.model.code == code, self.model.id != intersection_id)
.first()
)


intersection = CRUDIntersection(Intersection)
6 changes: 5 additions & 1 deletion dandelion/models/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class Camera(Base, DandelionBase):
desc = Column(String(255), nullable=False, default="")
enabled = Column(Boolean, nullable=True, default=True)

intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)

def __repr__(self) -> str:
return f"<Camera(sn='{self.sn}', name='{self.name}')>"
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/edge_rsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class EdgeNodeRSU(Base, DandelionBase):
__tablename__ = "edge_node_rsu"

edge_node_id = Column(Integer, ForeignKey("edge_node.id"))
intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)
name = Column(String(64), nullable=False, index=True)
esn = Column(String(64), nullable=False, index=True)
location = Column(JSON, nullable=False)
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class Lidar(Base, DandelionBase):
desc = Column(String(255), nullable=False, default="")
ws_url = Column(String(50), nullable=False, default="")

intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)

def __repr__(self) -> str:
return f"<Lidar (sn='{self.sn}', name='{self.name}')>"
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class Map(Base, DandelionBase):
__tablename__ = "map"

name = Column(String(64), nullable=False, index=True, unique=True)
intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)
desc = Column(String(255), nullable=False, default="")
lat = Column(Float, nullable=False)
lng = Column(Float, nullable=False)
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ class Radar(Base, DandelionBase):
desc = Column(String(255), nullable=False, default="")
enabled = Column(Boolean, nullable=True, default=True)

intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)

def __repr__(self) -> str:
return f"<Radar(sn='{self.sn}', name='{self.name}')>"
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/rsi_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class RSIEvent(Base, DandelionBase):
__tablename__ = "rsi_event"

rsu_id = Column(Integer, ForeignKey("rsu.id"))
intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)
alert_id = Column(String(64), nullable=True, default="")
duration = Column(Integer, nullable=True)
event_status = Column(Boolean, nullable=True, default=True)
Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/rsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class RSU(Base, DandelionBase):
config = Column(JSON, nullable=False)
online_status = Column(Boolean, index=True, nullable=False, default=False)
rsu_model_id = Column(Integer, ForeignKey("rsu_model.id"))
intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)
desc = Column(String(255), nullable=True, default="")
log_id = Column(Integer, ForeignKey("rsu_log.id"))

Expand Down
6 changes: 5 additions & 1 deletion dandelion/models/spat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class Spat(Base, DandelionBase):
timing = Column(DateTime, nullable=False, default=lambda: datetime.utcnow())
desc = Column(String(255), nullable=False, default="")

intersection_code = Column(String(64), ForeignKey("intersection.code"))
intersection_code = Column(
String(64),
ForeignKey("intersection.code", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False,
)

__table_args__ = (UniqueConstraint("intersection_id", "phase_id"),)

Expand Down
1 change: 1 addition & 0 deletions dandelion/schemas/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class IntersectionCreate(IntersectionBase):
class IntersectionUpdate(BaseModel):
""""""

code: Optional[str] = Field(None, alias="code", description="Intersection code")
name: Optional[str] = Field(None, alias="name", description="Intersection name")
lat: Optional[str] = Field(None, alias="lat", description="Intersection latitude")
lng: Optional[str] = Field(None, alias="lng", description="Intersection longitude")
Expand Down
2 changes: 1 addition & 1 deletion tools/datainit.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def init_db() -> None:
rsu1.version = "v1"
rsu1.rsu_status = "Normal"
rsu1.online_status = False
rsu1.location = {"lon": 118.8213963998263, "lat": 31.934846637757847}
rsu1.location = {"lon": 118.8213963998, "lat": 31.9348466377}
rsu1.config = {}
rsu1.rsu_model_id = rsu_model1.id
rsu1.intersection_code = "32011501"
Expand Down

0 comments on commit 3dae25d

Please sign in to comment.