property value as Union type
This commit is contained in:
parent
feb747bd21
commit
6cce36b26e
10 changed files with 87 additions and 34 deletions
17
Justfile
17
Justfile
|
|
@ -40,3 +40,20 @@ build-docker-container:
|
||||||
# This is meant for debugging the container build process only.
|
# This is meant for debugging the container build process only.
|
||||||
run-docker-container: build-docker-container
|
run-docker-container: build-docker-container
|
||||||
podman run -e "SECRET_KEY_BASE=ahK8BeiDaibaige1ahkooS0chie9lo7the7uuzar0eeBeeCh2iereteshee2Oosu" -e='DATABASE_URL=postgres://postgres@localhost:5432/mv_dev' -e='PORT=4040' -e='PHX_HOST=localhost' --network=host mitgliederverwaltung
|
podman run -e "SECRET_KEY_BASE=ahK8BeiDaibaige1ahkooS0chie9lo7the7uuzar0eeBeeCh2iereteshee2Oosu" -e='DATABASE_URL=postgres://postgres@localhost:5432/mv_dev' -e='PORT=4040' -e='PHX_HOST=localhost' --network=host mitgliederverwaltung
|
||||||
|
|
||||||
|
regen-migrations migration_name:
|
||||||
|
#!/bin/bash
|
||||||
|
set -euo pipefail
|
||||||
|
# Get count of untracked migrations
|
||||||
|
N_MIGRATIONS=$(git ls-files --others priv/repo/migrations | wc -l)
|
||||||
|
# Rollback untracked migrations
|
||||||
|
mix ash_postgres.rollback -n $N_MIGRATIONS
|
||||||
|
# Delete untracked migrations and snapshots
|
||||||
|
git ls-files --others priv/repo/migrations | xargs rm
|
||||||
|
git ls-files --others priv/resource_snapshots | xargs rm
|
||||||
|
# Regenerate migrations
|
||||||
|
mix ash.codegen --name {{migration_name}}
|
||||||
|
# Run migrations if flag
|
||||||
|
if echo $* | grep -e "-m" -q; then
|
||||||
|
mix ash.migrate
|
||||||
|
fi
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,16 @@ defmodule Mv.Membership.Property do
|
||||||
attributes do
|
attributes do
|
||||||
uuid_primary_key :id
|
uuid_primary_key :id
|
||||||
|
|
||||||
attribute :value, :string,
|
attribute :value, :union,
|
||||||
description: "Speichert den Wert, Typ-Interpretation per property_type.typ"
|
constraints: [
|
||||||
|
storage: :type_and_value,
|
||||||
|
types: [
|
||||||
|
boolean: [type: :boolean],
|
||||||
|
date: [type: :date],
|
||||||
|
integer: [type: :integer],
|
||||||
|
string: [type: :string]
|
||||||
|
]
|
||||||
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
relationships do
|
relationships do
|
||||||
|
|
@ -25,4 +33,8 @@ defmodule Mv.Membership.Property do
|
||||||
|
|
||||||
belongs_to :property_type, Mv.Membership.PropertyType
|
belongs_to :property_type, Mv.Membership.PropertyType
|
||||||
end
|
end
|
||||||
|
|
||||||
|
calculations do
|
||||||
|
calculate :value_to_string, :string, expr(value[:value] <> "")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ defmodule Mv.Membership.PropertyType do
|
||||||
|
|
||||||
actions do
|
actions do
|
||||||
defaults [:create, :read, :update, :destroy]
|
defaults [:create, :read, :update, :destroy]
|
||||||
default_accept [:name, :type, :description, :immutable, :required]
|
default_accept [:name, :value_type, :description, :immutable, :required]
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes do
|
attributes do
|
||||||
|
|
@ -18,7 +18,8 @@ defmodule Mv.Membership.PropertyType do
|
||||||
|
|
||||||
attribute :name, :string, allow_nil?: false, public?: true
|
attribute :name, :string, allow_nil?: false, public?: true
|
||||||
|
|
||||||
attribute :type, :string,
|
attribute :value_type, :atom,
|
||||||
|
constraints: [one_of: [:string, :integer, :boolean, :date]],
|
||||||
allow_nil?: false,
|
allow_nil?: false,
|
||||||
description: "Definies the datatype `Property.value` is interpreted as"
|
description: "Definies the datatype `Property.value` is interpreted as"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,11 @@ defmodule MvWeb.MemberLive.FormComponent do
|
||||||
Enum.map(property_types, fn pt ->
|
Enum.map(property_types, fn pt ->
|
||||||
%{
|
%{
|
||||||
"property_type_id" => pt.id,
|
"property_type_id" => pt.id,
|
||||||
"value" => nil
|
"value" => %{
|
||||||
|
"type" => pt.value_type,
|
||||||
|
"value" => nil,
|
||||||
|
"_union_type" => Atom.to_string(pt.value_type)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
@ -34,7 +38,9 @@ defmodule MvWeb.MemberLive.FormComponent do
|
||||||
>
|
>
|
||||||
<.inputs_for :let={f_property} field={@form[:properties]}>
|
<.inputs_for :let={f_property} field={@form[:properties]}>
|
||||||
<% type = Enum.find(@property_types, &(&1.id == f_property[:property_type_id].value)) %>
|
<% type = Enum.find(@property_types, &(&1.id == f_property[:property_type_id].value)) %>
|
||||||
<.input field={f_property[:value]} label={type && type.name} />
|
<.inputs_for :let={value_form} field={f_property[:value]}>
|
||||||
|
<.input field={value_form[:value]} label={type && type.name} />
|
||||||
|
</.inputs_for>
|
||||||
<input
|
<input
|
||||||
type="hidden"
|
type="hidden"
|
||||||
name={f_property[:property_type_id].name}
|
name={f_property[:property_type_id].name}
|
||||||
|
|
@ -95,12 +101,27 @@ defmodule MvWeb.MemberLive.FormComponent do
|
||||||
not Enum.member?(existing_properties, Map.get(i, "property_type_id"))
|
not Enum.member?(existing_properties, Map.get(i, "property_type_id"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
params = %{
|
||||||
|
"properties" =>
|
||||||
|
Enum.map(member.properties, fn prop ->
|
||||||
|
%{
|
||||||
|
"property_type_id" => prop.property_type_id,
|
||||||
|
"value" => %{
|
||||||
|
"_union_type" => Atom.to_string(prop.value.type),
|
||||||
|
"type" => prop.value.type,
|
||||||
|
"value" => prop.value.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
}
|
||||||
|
|
||||||
form =
|
form =
|
||||||
AshPhoenix.Form.for_update(
|
AshPhoenix.Form.for_update(
|
||||||
member,
|
member,
|
||||||
:update_member,
|
:update_member,
|
||||||
api: Mv.Membership,
|
api: Mv.Membership,
|
||||||
as: "member",
|
as: "member",
|
||||||
|
params: params,
|
||||||
forms: [auto?: true]
|
forms: [auto?: true]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ defmodule Mv.Repo.Migrations.InitialMigration do
|
||||||
create table(:property_types, primary_key: false) do
|
create table(:property_types, primary_key: false) do
|
||||||
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
|
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
|
||||||
add :name, :text, null: false
|
add :name, :text, null: false
|
||||||
add :type, :text, null: false
|
add :value_type, :text, null: false
|
||||||
add :description, :text
|
add :description, :text
|
||||||
add :immutable, :boolean, null: false, default: false
|
add :immutable, :boolean, null: false, default: false
|
||||||
add :required, :boolean, null: false, default: false
|
add :required, :boolean, null: false, default: false
|
||||||
|
|
@ -21,7 +21,7 @@ defmodule Mv.Repo.Migrations.InitialMigration do
|
||||||
|
|
||||||
create table(:properties, primary_key: false) do
|
create table(:properties, primary_key: false) do
|
||||||
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
|
add :id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true
|
||||||
add :value, :text
|
add :value, :map
|
||||||
add :member_id, :uuid
|
add :member_id, :uuid
|
||||||
add :property_type_id, :uuid
|
add :property_type_id, :uuid
|
||||||
end
|
end
|
||||||
|
|
@ -15,14 +15,35 @@ alias Mv.Membership
|
||||||
for attrs <- [
|
for attrs <- [
|
||||||
%{
|
%{
|
||||||
name: "Vorname",
|
name: "Vorname",
|
||||||
type: "string",
|
value_type: :string,
|
||||||
description: "Vorname des Mitglieds",
|
description: "Vorname des Mitglieds",
|
||||||
immutable: true,
|
immutable: true,
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
|
%{
|
||||||
|
name: "Nachname",
|
||||||
|
value_type: :string,
|
||||||
|
description: "Nachname des Mitglieds",
|
||||||
|
immutable: true,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
name: "Geburtsdatum",
|
||||||
|
value_type: :date,
|
||||||
|
description: "Geburtsdatum des Mitglieds",
|
||||||
|
immutable: true,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
name: "Bezahlt",
|
||||||
|
value_type: :boolean,
|
||||||
|
description: "Status des Mitgliedsbeitrages des Mitglieds",
|
||||||
|
immutable: true,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
%{
|
%{
|
||||||
name: "Email",
|
name: "Email",
|
||||||
type: "string",
|
value_type: :string,
|
||||||
description: "Email-Adresse des Mitglieds",
|
description: "Email-Adresse des Mitglieds",
|
||||||
immutable: true,
|
immutable: true,
|
||||||
required: true
|
required: true
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
"custom_indexes": [],
|
"custom_indexes": [],
|
||||||
"custom_statements": [],
|
"custom_statements": [],
|
||||||
"has_create_action": true,
|
"has_create_action": true,
|
||||||
"hash": "A0402269CB456075B81CA4CB3A2135A2C88D8B7FD51CD7A23084AA5264FEE344",
|
"hash": "35D45214D6D344B0AF6CFCB69B8682FCB3D382D85883D3D3AAC1AEE7F54FD89A",
|
||||||
"identities": [],
|
"identities": [],
|
||||||
"multitenancy": {
|
"multitenancy": {
|
||||||
"attribute": null,
|
"attribute": null,
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
"references": null,
|
"references": null,
|
||||||
"size": null,
|
"size": null,
|
||||||
"source": "value",
|
"source": "value",
|
||||||
"type": "text"
|
"type": "map"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"allow_nil?": true,
|
"allow_nil?": true,
|
||||||
|
|
@ -84,7 +84,7 @@
|
||||||
"custom_indexes": [],
|
"custom_indexes": [],
|
||||||
"custom_statements": [],
|
"custom_statements": [],
|
||||||
"has_create_action": true,
|
"has_create_action": true,
|
||||||
"hash": "F2A42A3427E0428637F465E4F357A3BE21B33231F94CF77B4843084128F6BDA5",
|
"hash": "8CF241CB9E8239511914EDEC96186BB7879529372BD8A4162431CCE9961F4F1B",
|
||||||
"identities": [],
|
"identities": [],
|
||||||
"multitenancy": {
|
"multitenancy": {
|
||||||
"attribute": null,
|
"attribute": null,
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
"primary_key?": false,
|
"primary_key?": false,
|
||||||
"references": null,
|
"references": null,
|
||||||
"size": null,
|
"size": null,
|
||||||
"source": "type",
|
"source": "value_type",
|
||||||
"type": "text"
|
"type": "text"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -66,7 +66,7 @@
|
||||||
"custom_indexes": [],
|
"custom_indexes": [],
|
||||||
"custom_statements": [],
|
"custom_statements": [],
|
||||||
"has_create_action": true,
|
"has_create_action": true,
|
||||||
"hash": "47210108DE1E7B2A20A67205E875B3440526941E61AB95B166976E8CD8AA0955",
|
"hash": "F98A723AE0D20005FBE4205E46ABEE09A88DFF9334C85BADC1FBEEF100F3E25B",
|
||||||
"identities": [
|
"identities": [
|
||||||
{
|
{
|
||||||
"all_tenants?": false,
|
"all_tenants?": false,
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Get count of untracked migrations
|
|
||||||
N_MIGRATIONS=$(git ls-files --others priv/repo/migrations | wc -l)
|
|
||||||
|
|
||||||
# Rollback untracked migrations
|
|
||||||
mix ash_postgres.rollback -n $N_MIGRATIONS
|
|
||||||
|
|
||||||
# Delete untracked migrations and snapshots
|
|
||||||
git ls-files --others priv/repo/migrations | xargs rm
|
|
||||||
git ls-files --others priv/resource_snapshots | xargs rm
|
|
||||||
|
|
||||||
# Regenerate migrations
|
|
||||||
mix ash.codegen --name $1
|
|
||||||
|
|
||||||
# Run migrations if flag
|
|
||||||
if echo $* | grep -e "-m" -q; then
|
|
||||||
mix ash.migrate
|
|
||||||
fi
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue