From 58077cfe989591cec1cf584f6ce79881b2ac77e3 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Wed, 16 Apr 2025 10:59:22 +0800 Subject: [PATCH] fix(entities): fix + upgrade to pg-orm v2 --- shard.lock | 10 +++++----- src/ext/persistence.cr | 14 +++++++++++--- src/tasks/entities.cr | 14 +++++++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/shard.lock b/shard.lock index 20b7dbf..52f8797 100644 --- a/shard.lock +++ b/shard.lock @@ -15,11 +15,11 @@ shards: awscr-s3: git: https://github.com/taylorfinnell/awscr-s3.git - version: 0.8.3 + version: 0.9.0 awscr-signer: git: https://github.com/taylorfinnell/awscr-signer.git - version: 0.8.2 + version: 0.9.0 azblob: git: https://github.com/spider-gazelle/azblob.cr.git @@ -127,7 +127,7 @@ shards: office365: git: https://github.com/placeos/office365.git - version: 1.25.5 + version: 1.25.6 openssl_ext: git: https://github.com/spider-gazelle/openssl_ext.git @@ -147,7 +147,7 @@ shards: pg-orm: git: https://github.com/spider-gazelle/pg-orm.git - version: 1.1.2+git.commit.f14936937ad0787a75d04d9fed23ffab11e4c42d + version: 2.0.1 place_calendar: git: https://github.com/placeos/calendar.git @@ -159,7 +159,7 @@ shards: placeos-models: git: https://github.com/placeos/models.git - version: 9.64.1 + version: 9.65.0 pool: git: https://github.com/ysbaddaden/pool.git diff --git a/src/ext/persistence.cr b/src/ext/persistence.cr index 4e839cf..19b31e9 100644 --- a/src/ext/persistence.cr +++ b/src/ext/persistence.cr @@ -1,16 +1,24 @@ # nodoc: module PgORM::Persistence private def __create(**options) - builder = Query::Builder.new(table_name, primary_key.to_s) + builder = Query::Builder.new(table_name, primary_key.first.to_s) adapter = Database.adapter(builder) Database.transaction do raise PgORM::Error::RecordInvalid.new(self) unless valid? attributes = self.persistent_attributes - attributes.delete(primary_key) unless self.id? + keys = primary_key + vals = self.id? + case vals + when Nil + keys.each { |key| attributes.delete(key) } + when Enumerable + primary_key.each_with_index { |key, index| attributes.delete(key) if vals[index].nil? } + end + begin adapter.insert(attributes) do |rid| - set_primary_key_after_create(rid) unless self.id? + set_primary_key_after_create(rid) clear_changes_information self.new_record = false end diff --git a/src/tasks/entities.cr b/src/tasks/entities.cr index e6b4d0b..af07f33 100644 --- a/src/tasks/entities.cr +++ b/src/tasks/entities.cr @@ -20,7 +20,7 @@ module PlaceOS::Tasks::Entities def create_authority( name : String, domain : String, - config : Hash(String, JSON::Any) + config : Hash(String, JSON::Any), ) upsert_document(Model::Authority.find_by_domain(domain)) do Log.info { {message: "creating Authority", name: name, domain: domain} } @@ -41,7 +41,7 @@ module PlaceOS::Tasks::Entities description : String, uri : String, branch : String, - commit_hash : String = "HEAD" + commit_hash : String = "HEAD", ) upsert_document(Model::Repository.where( repo_type: Model::Repository::Type::Interface.value, @@ -77,7 +77,7 @@ module PlaceOS::Tasks::Entities email : String? = nil, password : String? = nil, sys_admin : Bool = false, - support : Bool = false + support : Bool = false, ) authority = Model::Authority.find!(authority) if authority.is_a?(String) name = "PlaceOS Support (#{authority.name})" if name.nil? @@ -109,7 +109,7 @@ module PlaceOS::Tasks::Entities name : String, base : String, redirect_uri : String? = nil, - scope : String? = nil + scope : String? = nil, ) authority = Model::Authority.find!(authority) if authority.is_a?(String) authority_id = authority.id.as(String) @@ -260,7 +260,11 @@ module PlaceOS::Tasks::Entities existing = query.is_a?(Iterator) || query.is_a?(Enumerable) ? query.first? : query if existing.nil? model = yield - model.save! + model.run_create_callbacks do + model.run_save_callbacks do + model.save! + end + end Log.info { "created #{model.class}<#{model.id}>" } model else