Skip to content

Commit

Permalink
complete the "Field Report" migration ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰
Browse files Browse the repository at this point in the history
This moves over the persistence layer from the old "Incident Report"
name, and this is the last of that extensive migration.

#1469
  • Loading branch information
srabraham committed Jan 2, 2025
1 parent 3064db8 commit 00feaf3
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 62 deletions.
6 changes: 3 additions & 3 deletions src/ims/store/_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ def _fetchIncidents(
loads(str(row["INCIDENT_TYPES"])) if row["INCIDENT_TYPES"] else []
)
fieldReportNumbers = []
if row["INCIDENT_REPORT_NUMBERS"]:
if row["FIELD_REPORT_NUMBERS"]:
fieldReportNumbers = [
int(val) for val in loads(str(row["INCIDENT_REPORT_NUMBERS"]))
int(val) for val in loads(str(row["FIELD_REPORT_NUMBERS"]))
]
incidentNumber = cast(int, row["NUMBER"])
results.append(
Expand Down Expand Up @@ -1456,7 +1456,7 @@ def _fetchFieldReports(
# field report number -> report entry
reports = defaultdict[int, list[ReportEntry]](list)
for row in txn.fetchall():
fieldReportNumber = cast(int, row["INCIDENT_REPORT_NUMBER"])
fieldReportNumber = cast(int, row["FIELD_REPORT_NUMBER"])
reports[fieldReportNumber].append(
ReportEntry(
created=self.fromDateTimeValue(row["CREATED"]),
Expand Down
34 changes: 17 additions & 17 deletions src/ims/store/mysql/_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"""

template_setFieldReportAttribute = f"""
update INCIDENT_REPORT set {{column}} = %(value)s
update FIELD_REPORT set {{column}} = %(value)s
where EVENT = ({query_eventID}) and NUMBER = %(fieldReportNumber)s
"""

Expand Down Expand Up @@ -152,7 +152,7 @@
select AUTHOR, TEXT, CREATED, GENERATED from REPORT_ENTRY
where
ID not in (select REPORT_ENTRY from INCIDENT__REPORT_ENTRY) and
ID not in (select REPORT_ENTRY from INCIDENT_REPORT__REPORT_ENTRY)
ID not in (select REPORT_ENTRY from FIELD_REPORT__REPORT_ENTRY)
""",
),
incident=Query(
Expand Down Expand Up @@ -237,10 +237,10 @@
) as INCIDENT_TYPES,
(
select json_arrayagg(irep.NUMBER)
from INCIDENT_REPORT irep
from FIELD_REPORT irep
where i.EVENT = irep.EVENT
and i.NUMBER = irep.INCIDENT_NUMBER
) as INCIDENT_REPORT_NUMBERS,
) as FIELD_REPORT_NUMBERS,
(
select json_arrayagg(ir.RANGER_HANDLE)
from INCIDENT__RANGER ir
Expand Down Expand Up @@ -392,7 +392,7 @@
fieldReport=Query(
"look up field report",
f"""
select CREATED, SUMMARY, INCIDENT_NUMBER from INCIDENT_REPORT
select CREATED, SUMMARY, INCIDENT_NUMBER from FIELD_REPORT
where EVENT = ({query_eventID}) and NUMBER = %(fieldReportNumber)s
""",
),
Expand All @@ -401,24 +401,24 @@
f"""
select AUTHOR, TEXT, CREATED, GENERATED from REPORT_ENTRY
where ID in (
select REPORT_ENTRY from INCIDENT_REPORT__REPORT_ENTRY
select REPORT_ENTRY from FIELD_REPORT__REPORT_ENTRY
where
EVENT = ({query_eventID}) and
INCIDENT_REPORT_NUMBER = %(fieldReportNumber)s
FIELD_REPORT_NUMBER = %(fieldReportNumber)s
)
""",
),
fieldReportNumbers=Query(
"look up field report numbers for event",
f"""
select NUMBER from INCIDENT_REPORT
select NUMBER from FIELD_REPORT
where EVENT = ({query_eventID})
""",
),
maxFieldReportNumber=Query(
"look up maximum field report number",
"""
select max(NUMBER) from INCIDENT_REPORT
select max(NUMBER) from FIELD_REPORT
""",
),
fieldReports=Query(
Expand All @@ -430,7 +430,7 @@
SUMMARY,
INCIDENT_NUMBER
from
INCIDENT_REPORT
FIELD_REPORT
where
EVENT = ({query_eventID})
""",
Expand All @@ -439,14 +439,14 @@
"look up all field report report entries for an event",
f"""
select
irre.INCIDENT_REPORT_NUMBER,
irre.FIELD_REPORT_NUMBER,
re.AUTHOR,
re.CREATED,
re.GENERATED,
re.ID,
re.TEXT
from
INCIDENT_REPORT__REPORT_ENTRY irre
FIELD_REPORT__REPORT_ENTRY irre
join REPORT_ENTRY re
on irre.REPORT_ENTRY = re.ID
where
Expand All @@ -457,7 +457,7 @@
createFieldReport=Query(
"create field report",
f"""
insert into INCIDENT_REPORT (
insert into FIELD_REPORT (
EVENT, NUMBER, CREATED, SUMMARY, INCIDENT_NUMBER
)
values (
Expand All @@ -472,8 +472,8 @@
attachReportEntryToFieldReport=Query(
"add report entry to field report",
f"""
insert into INCIDENT_REPORT__REPORT_ENTRY (
EVENT, INCIDENT_REPORT_NUMBER, REPORT_ENTRY
insert into FIELD_REPORT__REPORT_ENTRY (
EVENT, FIELD_REPORT_NUMBER, REPORT_ENTRY
)
values (({query_eventID}), %(fieldReportNumber)s, %(reportEntryID)s)
""",
Expand All @@ -489,14 +489,14 @@
detachedFieldReportNumbers=Query(
"look up detached field report numbers",
f"""
select NUMBER from INCIDENT_REPORT
select NUMBER from FIELD_REPORT
where EVENT = ({query_eventID}) and INCIDENT_NUMBER is null
""",
),
attachedFieldReportNumbers=Query(
"look up attached field report numbers",
f"""
select NUMBER from INCIDENT_REPORT
select NUMBER from FIELD_REPORT
where
EVENT = ({query_eventID}) and
INCIDENT_NUMBER = %(incidentNumber)s
Expand Down
2 changes: 1 addition & 1 deletion src/ims/store/mysql/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class DataStore(DatabaseStore):

_log: ClassVar[Logger] = Logger()

schemaVersion: ClassVar[int] = 7
schemaVersion: ClassVar[int] = 8
schemaBasePath: ClassVar[Path] = Path(__file__).parent / "schema"
sqlFileExtension: ClassVar[str] = "mysql"

Expand Down
8 changes: 8 additions & 0 deletions src/ims/store/mysql/schema/8-from-7.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* this migration is about renaming INCIDENT_REPORT to FIELD_REPORT */

alter table `INCIDENT_REPORT__REPORT_ENTRY` rename column `INCIDENT_REPORT_NUMBER` to `FIELD_REPORT_NUMBER`, rename to `FIELD_REPORT__REPORT_ENTRY`;
alter table `INCIDENT_REPORT` rename to `FIELD_REPORT`;

/* Update schema version */

update `SCHEMA_INFO` set `VERSION` = 8;
160 changes: 160 additions & 0 deletions src/ims/store/mysql/schema/8.mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
create table SCHEMA_INFO (
VERSION smallint not null
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

insert into SCHEMA_INFO (VERSION) values (8);


create table EVENT (
ID integer not null auto_increment,
NAME varchar(128) not null,

primary key (ID),
unique key (NAME)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table CONCENTRIC_STREET (
EVENT integer not null,
ID varchar(16) not null,
NAME varchar(128) not null,

primary key (EVENT, ID)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table INCIDENT_TYPE (
ID integer not null auto_increment,
NAME varchar(128) not null,
HIDDEN boolean not null,

primary key (ID),
unique key (NAME)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

insert into INCIDENT_TYPE (NAME, HIDDEN) values ('Admin', 0);
insert into INCIDENT_TYPE (NAME, HIDDEN) values ('Junk' , 0);


create table REPORT_ENTRY (
ID integer not null auto_increment,
AUTHOR varchar(64) not null,
TEXT text not null,
CREATED double not null,
GENERATED boolean not null,
STRICKEN boolean not null,

-- FIXME: AUTHOR is an external non-primary key.
-- Primary key is DMS Person ID.

primary key (ID)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table INCIDENT (
EVENT integer not null,
NUMBER integer not null,
CREATED double not null,
PRIORITY tinyint not null,

STATE enum(
'new', 'on_hold', 'dispatched', 'on_scene', 'closed'
) not null,

SUMMARY varchar(1024),

LOCATION_NAME varchar(1024),
LOCATION_CONCENTRIC varchar(64),
LOCATION_RADIAL_HOUR tinyint,
LOCATION_RADIAL_MINUTE tinyint,
LOCATION_DESCRIPTION varchar(1024),

foreign key (EVENT) references EVENT(ID),

foreign key (EVENT, LOCATION_CONCENTRIC)
references CONCENTRIC_STREET(EVENT, ID),

primary key (EVENT, NUMBER)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table INCIDENT__RANGER (
EVENT integer not null,
INCIDENT_NUMBER integer not null,
RANGER_HANDLE varchar(64) not null,

foreign key (EVENT) references EVENT(ID),
foreign key (EVENT, INCIDENT_NUMBER) references INCIDENT(EVENT, NUMBER),

-- FIXME: RANGER_HANDLE is an external non-primary key.
-- Primary key is DMS Person ID.

primary key (EVENT, INCIDENT_NUMBER, RANGER_HANDLE)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table INCIDENT__INCIDENT_TYPE (
EVENT integer not null,
INCIDENT_NUMBER integer not null,
INCIDENT_TYPE integer not null,

foreign key (EVENT) references EVENT(ID),
foreign key (EVENT, INCIDENT_NUMBER) references INCIDENT(EVENT, NUMBER),
foreign key (INCIDENT_TYPE) references INCIDENT_TYPE(ID),

primary key (EVENT, INCIDENT_NUMBER, INCIDENT_TYPE)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table INCIDENT__REPORT_ENTRY (
EVENT integer not null,
INCIDENT_NUMBER integer not null,
REPORT_ENTRY integer not null,

foreign key (EVENT) references EVENT(ID),
foreign key (EVENT, INCIDENT_NUMBER) references INCIDENT(EVENT, NUMBER),
foreign key (REPORT_ENTRY) references REPORT_ENTRY(ID),

primary key (EVENT, INCIDENT_NUMBER, REPORT_ENTRY)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table EVENT_ACCESS (
EVENT integer not null,
EXPRESSION varchar(128) not null,

MODE enum ('read', 'write', 'report') not null,

foreign key (EVENT) references EVENT(ID),

primary key (EVENT, EXPRESSION)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table FIELD_REPORT (
EVENT integer not null,
NUMBER integer not null,
CREATED double not null,

SUMMARY varchar(1024),
INCIDENT_NUMBER integer,

foreign key (EVENT) references EVENT(ID),
foreign key (EVENT, INCIDENT_NUMBER) references INCIDENT(EVENT, NUMBER),

primary key (EVENT, NUMBER)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


create table FIELD_REPORT__REPORT_ENTRY (
EVENT integer not null,
FIELD_REPORT_NUMBER integer not null,
REPORT_ENTRY integer not null,

foreign key (EVENT) references EVENT(ID),
foreign key (EVENT, FIELD_REPORT_NUMBER)
references FIELD_REPORT(EVENT, NUMBER),
foreign key (REPORT_ENTRY) references REPORT_ENTRY(ID),

primary key (EVENT, FIELD_REPORT_NUMBER, REPORT_ENTRY)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
22 changes: 11 additions & 11 deletions src/ims/store/mysql/test/test_store_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async def test_printSchema(self) -> None:
self.assertEqual(
dedent(
"""
Version: 7
Version: 8
CONCENTRIC_STREET:
1: EVENT(int) not null
2: ID(varchar(16)) not null
Expand All @@ -156,6 +156,16 @@ async def test_printSchema(self) -> None:
1: EVENT(int) not null
2: EXPRESSION(varchar(128)) not null
3: MODE(enum(6)) not null
FIELD_REPORT:
1: EVENT(int) not null
2: NUMBER(int) not null
3: CREATED(double) not null
4: SUMMARY(varchar(1024)) := NULL
5: INCIDENT_NUMBER(int) := NULL
FIELD_REPORT__REPORT_ENTRY:
1: EVENT(int) not null
2: FIELD_REPORT_NUMBER(int) not null
3: REPORT_ENTRY(int) not null
INCIDENT:
1: EVENT(int) not null
2: NUMBER(int) not null
Expand All @@ -168,16 +178,6 @@ async def test_printSchema(self) -> None:
9: LOCATION_RADIAL_HOUR(tinyint) := NULL
10: LOCATION_RADIAL_MINUTE(tinyint) := NULL
11: LOCATION_DESCRIPTION(varchar(1024)) := NULL
INCIDENT_REPORT:
1: EVENT(int) not null
2: NUMBER(int) not null
3: CREATED(double) not null
4: SUMMARY(varchar(1024)) := NULL
5: INCIDENT_NUMBER(int) := NULL
INCIDENT_REPORT__REPORT_ENTRY:
1: EVENT(int) not null
2: INCIDENT_REPORT_NUMBER(int) not null
3: REPORT_ENTRY(int) not null
INCIDENT_TYPE:
1: ID(int) not null
2: NAME(varchar(128)) not null
Expand Down
Loading

0 comments on commit 00feaf3

Please sign in to comment.