From 49fcc6c5d85e2afe4d0cc7b8752c30368eb54931 Mon Sep 17 00:00:00 2001 From: Brooke Kuhlmann Date: Mon, 8 Sep 2025 15:41:50 -0600 Subject: [PATCH] Updated device logs table as device log Necessary to reduce confusion and be consistent with existing tables. Milestone: minor --- app/relations/device.rb | 2 +- app/relations/device_log.rb | 2 +- app/repositories/device_log.rb | 28 +++++++++---------- .../20250908153842_rename_device_logs.rb | 3 ++ config/db/structure.sql | 25 +++++++++-------- spec/support/factories/device_log.rb | 2 +- 6 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 config/db/migrate/20250908153842_rename_device_logs.rb diff --git a/app/relations/device.rb b/app/relations/device.rb index a37d6f54..ba630fd1 100644 --- a/app/relations/device.rb +++ b/app/relations/device.rb @@ -8,7 +8,7 @@ module Terminus associations do belongs_to :model, relation: :model belongs_to :playlist, relation: :playlist - has_many :device_logs, as: :logs + has_many :device_logs, relation: :device_log, as: :logs end end end diff --git a/app/relations/device_log.rb b/app/relations/device_log.rb index 31a93a8d..577a60c9 100644 --- a/app/relations/device_log.rb +++ b/app/relations/device_log.rb @@ -4,7 +4,7 @@ module Terminus module Relations # The device log relation. class DeviceLog < DB::Relation - schema :device_logs, infer: true do + schema :device_log, infer: true do associations { belongs_to :device, relation: :device } end end diff --git a/app/repositories/device_log.rb b/app/repositories/device_log.rb index a28a60ca..2832414f 100644 --- a/app/repositories/device_log.rb +++ b/app/repositories/device_log.rb @@ -3,7 +3,7 @@ module Terminus module Repositories # The device log repository. - class DeviceLog < DB::Repository[:device_logs] + class DeviceLog < DB::Repository[:device_log] commands :create, delete: :by_pk commands update: :by_pk, @@ -11,28 +11,28 @@ module Terminus plugins_options: {timestamps: {timestamps: :updated_at}} def all - device_logs.combine(:device) - .order { created_at.desc } - .to_a + device_log.combine(:device) + .order { created_at.desc } + .to_a end - def find(id) = (device_logs.combine(:device).by_pk(id).one if id) + def find(id) = (device_log.combine(:device).by_pk(id).one if id) - def delete_by_device(device_id, id) = device_logs.where(device_id:, id:).delete + def delete_by_device(device_id, id) = device_log.where(device_id:, id:).delete - def delete_all_by_device(device_id) = device_logs.where(device_id:).command(:delete).call + def delete_all_by_device(device_id) = device_log.where(device_id:).command(:delete).call def search(key, value, **) - device_logs.where(**) - .where(Sequel.ilike(key, "%#{value}%")) - .order { created_at.asc } - .to_a + device_log.where(**) + .where(Sequel.ilike(key, "%#{value}%")) + .order { created_at.asc } + .to_a end def where(**) - device_logs.where(**) - .order { created_at.desc } - .to_a + device_log.where(**) + .order { created_at.desc } + .to_a end end end diff --git a/config/db/migrate/20250908153842_rename_device_logs.rb b/config/db/migrate/20250908153842_rename_device_logs.rb new file mode 100644 index 00000000..f0f163e8 --- /dev/null +++ b/config/db/migrate/20250908153842_rename_device_logs.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +ROM::SQL.migration { change { rename_table :device_logs, :device_log } } diff --git a/config/db/structure.sql b/config/db/structure.sql index 5d947239..2c8d45db 100644 --- a/config/db/structure.sql +++ b/config/db/structure.sql @@ -138,10 +138,10 @@ CREATE TABLE public.device ( -- --- Name: device_logs; Type: TABLE; Schema: public; Owner: - +-- Name: device_log; Type: TABLE; Schema: public; Owner: - -- -CREATE TABLE public.device_logs ( +CREATE TABLE public.device_log ( id integer NOT NULL, device_id integer NOT NULL, external_id integer NOT NULL, @@ -168,7 +168,7 @@ CREATE TABLE public.device_logs ( -- Name: device_logs_id_seq; Type: SEQUENCE; Schema: public; Owner: - -- -ALTER TABLE public.device_logs ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY ( +ALTER TABLE public.device_log ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY ( SEQUENCE NAME public.device_logs_id_seq START WITH 1 INCREMENT BY 1 @@ -361,10 +361,10 @@ ALTER TABLE public.screen ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY ( -- --- Name: device_logs device_logs_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- Name: device_log device_logs_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public.device_logs +ALTER TABLE ONLY public.device_log ADD CONSTRAINT device_logs_pkey PRIMARY KEY (id); @@ -484,28 +484,28 @@ ALTER TABLE ONLY public.screen -- Name: device_logs_device_id_index; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX device_logs_device_id_index ON public.device_logs USING btree (device_id); +CREATE INDEX device_logs_device_id_index ON public.device_log USING btree (device_id); -- -- Name: device_logs_special_function_index; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX device_logs_special_function_index ON public.device_logs USING btree (special_function); +CREATE INDEX device_logs_special_function_index ON public.device_log USING btree (special_function); -- -- Name: device_logs_wake_reason_index; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX device_logs_wake_reason_index ON public.device_logs USING btree (wake_reason); +CREATE INDEX device_logs_wake_reason_index ON public.device_log USING btree (wake_reason); -- -- Name: device_logs_wifi_status_index; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX device_logs_wifi_status_index ON public.device_logs USING btree (wifi_status); +CREATE INDEX device_logs_wifi_status_index ON public.device_log USING btree (wifi_status); -- @@ -579,10 +579,10 @@ CREATE INDEX screen_name_index ON public.screen USING btree (name); -- --- Name: device_logs device_logs_device_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - +-- Name: device_log device_logs_device_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: - -- -ALTER TABLE ONLY public.device_logs +ALTER TABLE ONLY public.device_log ADD CONSTRAINT device_logs_device_id_fkey FOREIGN KEY (device_id) REFERENCES public.device(id) ON UPDATE CASCADE ON DELETE CASCADE; @@ -664,4 +664,5 @@ INSERT INTO schema_migrations (filename) VALUES ('20250821094444_add_model_kind_column.rb'), ('20250904100039_drop_model_scale_factor.rb'), ('20250908152041_rename_devices.rb'), -('20250908153411_rename_firmwares.rb'); +('20250908153411_rename_firmwares.rb'), +('20250908153842_rename_device_logs.rb'); diff --git a/spec/support/factories/device_log.rb b/spec/support/factories/device_log.rb index c0cb4404..0e34b27b 100644 --- a/spec/support/factories/device_log.rb +++ b/spec/support/factories/device_log.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -Factory.define :device_log do |factory| +Factory.define :device_log, relation: :device_log do |factory| factory.association :device factory.sequence(:external_id) { it } factory.message "Danger!"