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