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
Expand Up @@ -30,9 +30,6 @@ def up() do
add :spec_project_design_id, references("vf_resource_specification"), null: false
add :spec_project_service_id, references("vf_resource_specification"), null: false
add :spec_project_product_id, references("vf_resource_specification"), null: false
add :spec_dpp_id, references("vf_resource_specification"), null: false
add :spec_machine_id, references("vf_resource_specification"), null: false
add :spec_material_id, references("vf_resource_specification"), null: false
end

create constraint("zf_inst_vars", :one_row, check: "one_row")
Expand All @@ -46,19 +43,13 @@ def up() do
spec_design = ResourceSpecification.Domain.create!(r, %{name: "Design", default_unit_of_resource_id: unit_one.id})
spec_service = ResourceSpecification.Domain.create!(r, %{name: "Service", default_unit_of_resource_id: unit_one.id})
spec_product = ResourceSpecification.Domain.create!(r, %{name: "Product", default_unit_of_resource_id: unit_one.id})
spec_dpp = ResourceSpecification.Domain.create!(r, %{name: "DPP", default_unit_of_resource_id: unit_one.id})
spec_machine = ResourceSpecification.Domain.create!(r, %{name: "Machine", default_unit_of_resource_id: unit_one.id})
spec_material = ResourceSpecification.Domain.create!(r, %{name: "Material", default_unit_of_resource_id: unit_one.id})

r.insert!(%InstVars{
unit_one_id: unit_one.id,
spec_currency_id: spec_currency.id,
spec_project_design_id: spec_design.id,
spec_project_service_id: spec_service.id,
spec_project_product_id: spec_product.id,
spec_dpp_id: spec_dpp.id,
spec_machine_id: spec_machine.id,
spec_material_id: spec_material.id,
})
end)
end
Expand Down
96 changes: 96 additions & 0 deletions priv/repo/migrations/20251217151902_seed_material_resources.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# Zenflows is software that implements the Valueflows vocabulary.
# Zenflows is designed, written, and maintained by srfsh <srfsh@dyne.org>
# Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

defmodule Zenflows.DB.Repo.Migrations.Seed_material_resources do
use Ecto.Migration

alias Zenflows.InstVars
alias Zenflows.VF.{EconomicEvent, Organization}

@materials [
"PLA",
"ABS",
"PETG",
"Nylon",
"TPU",
"Resin",
"Aluminium",
"Steel",
"Stainless Steel",
"Copper",
"Brass",
"Titanium",
"Wood",
"Plywood",
"MDF",
"Bamboo",
"Acrylic",
"Polycarbonate",
"HDPE",
"Carbon Fiber",
"Fiberglass",
"Leather",
"Fabric",
"Rubber",
"Silicone",
"Foam",
"Ceramic",
"Glass",
"Concrete",
"Cork",
]

def up() do
execute(fn ->
r = repo()
inst_vars = r.one!(InstVars)
unit_one_id = inst_vars.unit_one_id
spec_material_id = inst_vars.spec_material_id

# Create a system organization to own the material resources
{:ok, system_org} = Organization.Domain.create(r, %{
name: "System Materials Repository",
note: "System organization that holds the seeded material resources",
})

# Seed each material as an economic resource via a "raise" event
Enum.each(@materials, fn material_name ->
{:ok, _event} = EconomicEvent.Domain.create(%{
action_id: "raise",
provider_id: system_org.id,
receiver_id: system_org.id,
resource_conforms_to_id: spec_material_id,
resource_quantity: %{
has_numerical_value: "1",
has_unit_id: unit_one_id,
},
has_point_in_time: DateTime.utc_now(),
}, %{
name: material_name,
note: "Seeded material resource: #{material_name}",
})
end)
end)
end

def down() do
# Resources created via economic events cannot be easily reversed
# This is intentional - we don't want to delete resources that may have been used
:ok
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# Zenflows is software that implements the Valueflows vocabulary.
# Zenflows is designed, written, and maintained by srfsh <srfsh@dyne.org>
# Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

defmodule Zenflows.DB.Repo.Migrations.Add_new_spec_columns_to_inst_vars do
use Ecto.Migration

alias Zenflows.VF.ResourceSpecification

def up() do
# First, add the columns as nullable
alter table("zf_inst_vars") do
add :spec_dpp_id, references("vf_resource_specification")
add :spec_machine_id, references("vf_resource_specification")
add :spec_material_id, references("vf_resource_specification")
end

flush()

# Create the new ResourceSpecifications and update the inst_vars row
execute(fn ->
r = repo()

# Get the existing unit_one_id from the inst_vars row
[[unit_one_id]] = r.query!("SELECT unit_one_id FROM zf_inst_vars").rows

# Create the new resource specifications
spec_dpp = ResourceSpecification.Domain.create!(r, %{name: "DPP", default_unit_of_resource_id: unit_one_id})
spec_machine = ResourceSpecification.Domain.create!(r, %{name: "Machine", default_unit_of_resource_id: unit_one_id})
spec_material = ResourceSpecification.Domain.create!(r, %{name: "Material", default_unit_of_resource_id: unit_one_id})

# Update the inst_vars row with the new spec IDs
r.query!("UPDATE zf_inst_vars SET spec_dpp_id = $1, spec_machine_id = $2, spec_material_id = $3",
[spec_dpp.id, spec_machine.id, spec_material.id])
end)

flush()

# Now make the columns non-nullable
alter table("zf_inst_vars") do
modify :spec_dpp_id, references("vf_resource_specification"), null: false, from: references("vf_resource_specification")
modify :spec_machine_id, references("vf_resource_specification"), null: false, from: references("vf_resource_specification")
modify :spec_material_id, references("vf_resource_specification"), null: false, from: references("vf_resource_specification")
end
end

def down() do
alter table("zf_inst_vars") do
remove :spec_dpp_id
remove :spec_machine_id
remove :spec_material_id
end
end
end
Loading