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
14 changes: 11 additions & 3 deletions src/ext/persistence.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/tasks/entities.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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} }
Expand All @@ -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,
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading