Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- +micrate Up
-- SQL in section 'Up' is executed when this migration is applied

-- 1. Add `locatable` with default TRUE and NOT NULL in one step
-- 2. Add `photo_upload_id` with an inline FK that sets null on delete
ALTER TABLE "user"
ADD COLUMN locatable BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN photo_upload_id TEXT REFERENCES uploads(id) ON DELETE SET NULL;

-- 3. Add `tags` to `uploads` as a non-null TEXT[] with an empty‐array default
ALTER TABLE uploads
ADD COLUMN tags TEXT[] NOT NULL DEFAULT '{}';

CREATE INDEX idx_uploads_tags
ON uploads
USING GIN (tags);

-- +micrate Down
-- SQL section 'Down' is executed when this migration is rolled back

-- 1. Drop `photo_upload_id` (this also removes the FK constraint automatically)
-- 2. Drop `locatable`
ALTER TABLE "user"
DROP COLUMN photo_upload_id,
DROP COLUMN locatable;

-- 3. Drop `tags`
ALTER TABLE uploads
DROP COLUMN tags;
4 changes: 4 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ dependencies:
cron_parser:
github: kostya/cron_parser

# Upload signer
upload-signer:
github: spider-gazelle/upload-signer

development_dependencies:
ameba:
github: crystal-ameba/ameba
Expand Down
15 changes: 15 additions & 0 deletions src/placeos-models/upload.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "json"
require "upload-signer"

require "./base/model"
require "./storage"
Expand Down Expand Up @@ -34,6 +35,10 @@ module PlaceOS::Model
attribute part_data : Hash(String, JSON::Any) = {} of String => JSON::Any
attribute upload_complete : Bool = false

# so we can tag which system the upload belongs to
# allowing us to filter for relevancy
attribute tags : Array(String) = -> { [] of String }

belongs_to Storage
belongs_to User, foreign_key: "uploaded_by", association_name: "user"

Expand All @@ -58,5 +63,15 @@ module PlaceOS::Model
def part_data_changed(flag = true)
@part_data_changed = flag
end

before_destroy :delete_data

protected def delete_data
cloud_fs = self.storage rescue nil
return unless cloud_fs

signer = UploadSigner.signer(UploadSigner::StorageType.from_value(cloud_fs.storage_type.value), cloud_fs.access_key, cloud_fs.decrypt_secret, cloud_fs.region, endpoint: cloud_fs.endpoint)
signer.delete_file(cloud_fs.bucket_name, self.object_key, self.resumable_id)
end
end
end
4 changes: 3 additions & 1 deletion src/placeos-models/user.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module PlaceOS::Model

attribute sys_admin : Bool = false, mass_assignment: false
attribute support : Bool = false, mass_assignment: false
attribute locatable : Bool = true

attribute login_count : Int64 = 0
attribute last_login : Time? = nil, converter: Time::EpochConverterOptional, type: "integer", format: "Int64"
Expand All @@ -77,6 +78,7 @@ module PlaceOS::Model
################################################################################################

belongs_to Authority
belongs_to Upload, foreign_key: :photo_upload_id

has_many(
child_class: UserAuthLookup,
Expand Down Expand Up @@ -291,7 +293,7 @@ module PlaceOS::Model
:email_digest, :email, :nickname, :name, :first_name, :last_name, :groups,
:country, :building, :image, :created_at, :authority_id, :deleted,
:department, :preferred_language, :staff_id, :phone, :work_preferences,
:work_overrides, :login_count, :last_login,
:work_overrides, :login_count, :last_login, :photo_upload_id, :locatable,
]

{% begin %}
Expand Down