From a96517caf157e690efde62703b22aec28831f8f0 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 5 Aug 2025 22:06:34 +0200 Subject: [PATCH] Add device model with relations --- CHANGELOG.md | 5 +++++ app/models/device.rb | 6 ++++++ app/models/point.rb | 3 ++- app/models/user.rb | 1 + app/serializers/point_serializer.rb | 2 +- db/migrate/20250805184854_create_devices.rb | 14 ++++++++++++++ .../20250805184855_add_device_id_to_points.rb | 9 +++++++++ db/schema.rb | 15 ++++++++++++++- spec/factories/devices.rb | 9 +++++++++ spec/factories/points.rb | 1 + spec/models/device_spec.rb | 13 +++++++++++++ spec/models/point_spec.rb | 4 ++++ spec/models/user_spec.rb | 1 + spec/services/photos/importer_spec.rb | 3 ++- 14 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 app/models/device.rb create mode 100644 db/migrate/20250805184854_create_devices.rb create mode 100644 db/migrate/20250805184855_add_device_id_to_points.rb create mode 100644 spec/factories/devices.rb create mode 100644 spec/models/device_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f554a6..aaf648f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Possibly fixed a bug where visits were no suggested correctly. #984 - Scratch map is now working correctly. +## Added + +- Internal data structure for separate devices in a single user account. + - [ ] Immich and Photoprism integrations should fill all possible fields in points table + # [0.30.7] - 2025-08-01 diff --git a/app/models/device.rb b/app/models/device.rb new file mode 100644 index 00000000..a1e8b6ac --- /dev/null +++ b/app/models/device.rb @@ -0,0 +1,6 @@ +class Device < ApplicationRecord + belongs_to :user + + validates :name, presence: true + validates :identifier, presence: true, uniqueness: { scope: :user_id } +end diff --git a/app/models/point.rb b/app/models/point.rb index ef00e99b..97513112 100644 --- a/app/models/point.rb +++ b/app/models/point.rb @@ -9,10 +9,11 @@ class Point < ApplicationRecord belongs_to :user belongs_to :country, optional: true belongs_to :track, optional: true + belongs_to :device, optional: true validates :timestamp, :lonlat, presence: true validates :lonlat, uniqueness: { - scope: %i[timestamp user_id], + scope: %i[timestamp user_id device_id], message: 'already has a point at this location and time for this user', index: true } diff --git a/app/models/user.rb b/app/models/user.rb index 4c61d98e..13c5cee6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,6 +15,7 @@ class User < ApplicationRecord has_many :places, through: :visits has_many :trips, dependent: :destroy has_many :tracks, dependent: :destroy + has_many :devices, dependent: :destroy after_create :create_api_key after_commit :activate, on: :create, if: -> { DawarichSettings.self_hosted? } diff --git a/app/serializers/point_serializer.rb b/app/serializers/point_serializer.rb index 580dd574..091b7fc5 100644 --- a/app/serializers/point_serializer.rb +++ b/app/serializers/point_serializer.rb @@ -3,7 +3,7 @@ class PointSerializer EXCLUDED_ATTRIBUTES = %w[ created_at updated_at visit_id id import_id user_id raw_data lonlat - reverse_geocoded_at country_id + reverse_geocoded_at country_id device_id ].freeze def initialize(point) diff --git a/db/migrate/20250805184854_create_devices.rb b/db/migrate/20250805184854_create_devices.rb new file mode 100644 index 00000000..6ad17766 --- /dev/null +++ b/db/migrate/20250805184854_create_devices.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CreateDevices < ActiveRecord::Migration[8.0] + def change + create_table :devices do |t| + t.string :name, null: false + t.references :user, null: false, foreign_key: true + t.string :identifier, null: false + + t.timestamps + end + add_index :devices, :identifier + end +end diff --git a/db/migrate/20250805184855_add_device_id_to_points.rb b/db/migrate/20250805184855_add_device_id_to_points.rb new file mode 100644 index 00000000..ff415193 --- /dev/null +++ b/db/migrate/20250805184855_add_device_id_to_points.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddDeviceIdToPoints < ActiveRecord::Migration[8.0] + disable_ddl_transaction! + + def change + add_reference :points, :device, null: true, index: { algorithm: :concurrently } + end +end diff --git a/db/schema.rb b/db/schema.rb index feac06e4..2021122b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_07_28_191359) do +ActiveRecord::Schema[8.0].define(version: 2025_08_05_184855) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "postgis" @@ -80,6 +80,16 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_28_191359) do create_table "data_migrations", primary_key: "version", id: :string, force: :cascade do |t| end + create_table "devices", force: :cascade do |t| + t.string "name", null: false + t.bigint "user_id", null: false + t.string "identifier", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["identifier"], name: "index_devices_on_identifier" + t.index ["user_id"], name: "index_devices_on_user_id" + end + create_table "exports", force: :cascade do |t| t.string "name", null: false t.string "url" @@ -187,6 +197,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_28_191359) do t.bigint "country_id" t.bigint "track_id" t.string "country_name" + t.bigint "device_id" t.index ["altitude"], name: "index_points_on_altitude" t.index ["battery"], name: "index_points_on_battery" t.index ["battery_status"], name: "index_points_on_battery_status" @@ -195,6 +206,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_28_191359) do t.index ["country"], name: "index_points_on_country" t.index ["country_id"], name: "index_points_on_country_id" t.index ["country_name"], name: "index_points_on_country_name" + t.index ["device_id"], name: "index_points_on_device_id" t.index ["external_track_id"], name: "index_points_on_external_track_id" t.index ["geodata"], name: "index_points_on_geodata", using: :gin t.index ["import_id"], name: "index_points_on_import_id" @@ -300,6 +312,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_28_191359) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "areas", "users" + add_foreign_key "devices", "users" add_foreign_key "notifications", "users" add_foreign_key "place_visits", "places" add_foreign_key "place_visits", "visits" diff --git a/spec/factories/devices.rb b/spec/factories/devices.rb new file mode 100644 index 00000000..ac850fe7 --- /dev/null +++ b/spec/factories/devices.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :device do + name { SecureRandom.uuid } + user + identifier { SecureRandom.uuid } + end +end diff --git a/spec/factories/points.rb b/spec/factories/points.rb index acc097e9..a4baf1f6 100644 --- a/spec/factories/points.rb +++ b/spec/factories/points.rb @@ -31,6 +31,7 @@ FactoryBot.define do lonlat { "POINT(#{FFaker::Geolocation.lng} #{FFaker::Geolocation.lat})" } user country_id { nil } + device # Add transient attribute to handle country strings transient do diff --git a/spec/models/device_spec.rb b/spec/models/device_spec.rb new file mode 100644 index 00000000..a4cf3b4e --- /dev/null +++ b/spec/models/device_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Device, type: :model do + describe 'validations' do + subject { build(:device) } + + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_presence_of(:identifier) } + it { is_expected.to validate_uniqueness_of(:identifier).scoped_to(:user_id) } + end +end diff --git a/spec/models/point_spec.rb b/spec/models/point_spec.rb index eaf3d4ba..c2511406 100644 --- a/spec/models/point_spec.rb +++ b/spec/models/point_spec.rb @@ -9,11 +9,15 @@ RSpec.describe Point, type: :model do it { is_expected.to belong_to(:country).optional } it { is_expected.to belong_to(:visit).optional } it { is_expected.to belong_to(:track).optional } + it { is_expected.to belong_to(:device).optional } end describe 'validations' do + subject { build(:point, timestamp: Time.current, lonlat: 'POINT(1.0 2.0)') } + it { is_expected.to validate_presence_of(:timestamp) } it { is_expected.to validate_presence_of(:lonlat) } + it { is_expected.to validate_uniqueness_of(:lonlat).scoped_to(%i[timestamp user_id device_id]).with_message('already has a point at this location and time for this user') } end describe 'callbacks' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3caba416..21410e7f 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -15,6 +15,7 @@ RSpec.describe User, type: :model do it { is_expected.to have_many(:places).through(:visits) } it { is_expected.to have_many(:trips).dependent(:destroy) } it { is_expected.to have_many(:tracks).dependent(:destroy) } + it { is_expected.to have_many(:devices).dependent(:destroy) } end describe 'enums' do diff --git a/spec/services/photos/importer_spec.rb b/spec/services/photos/importer_spec.rb index 567898a3..1425f67d 100644 --- a/spec/services/photos/importer_spec.rb +++ b/spec/services/photos/importer_spec.rb @@ -9,6 +9,7 @@ RSpec.describe Photos::Importer do let(:user) do create(:user, settings: { 'immich_url' => 'http://immich.app', 'immich_api_key' => '123456' }) end + let(:device) { create(:device, user:) } let(:immich_data) do JSON.parse(File.read(Rails.root.join('spec/fixtures/files/immich/geodata.json'))) @@ -44,7 +45,7 @@ RSpec.describe Photos::Importer do context 'when there are points with the same coordinates' do let!(:existing_point) do - create(:point, lonlat: 'POINT(30.0000 59.0000)', timestamp: 978_296_400, user:) + create(:point, lonlat: 'POINT(30.0000 59.0000)', timestamp: 978_296_400, user:, device:) end it 'creates only new points' do