Skip to content

Commit

Permalink
Move database events (#2452)
Browse files Browse the repository at this point in the history
* Create new databases context file

* Move the database events to its folder
  • Loading branch information
arbulu89 committed Apr 2, 2024
1 parent 157dc6e commit 03c3ac7
Show file tree
Hide file tree
Showing 35 changed files with 554 additions and 360 deletions.
68 changes: 68 additions & 0 deletions lib/trento/databases.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Trento.Databases do
@moduledoc """
Provides a set of functions to interact with databases.
"""

import Ecto.Query

alias Trento.SapSystems.Projections.{
DatabaseInstanceReadModel,
DatabaseReadModel
}

alias Trento.Support.DateService

alias Trento.Databases.Commands.DeregisterDatabaseInstance

alias Trento.Repo

@spec get_all_databases :: [DatabaseReadModel.t()]
def get_all_databases do
DatabaseReadModel
|> where([d], is_nil(d.deregistered_at))
|> order_by(asc: :sid)
|> Repo.all()
|> Repo.preload([
:database_instances,
:tags
])
end

@spec get_database_instances_by_host_id(String.t()) :: [DatabaseInstanceReadModel.t()]
def get_database_instances_by_host_id(host_id) do
DatabaseInstanceReadModel
|> where([d], d.host_id == ^host_id)
|> Repo.all()
end

@spec deregister_database_instance(Ecto.UUID.t(), Ecto.UUID.t(), String.t(), DateService) ::
:ok | {:error, :instance_present} | {:error, :database_instance_not_registered}
def deregister_database_instance(
database_id,
host_id,
instance_number,
date_service \\ DateService
) do
case Repo.get_by(DatabaseInstanceReadModel,
sap_system_id: database_id,
host_id: host_id,
instance_number: instance_number
) do
%DatabaseInstanceReadModel{absent_at: nil} ->
{:error, :instance_present}

_ ->
commanded().dispatch(
DeregisterDatabaseInstance.new!(%{
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
deregistered_at: date_service.utc_now()
})
)
end
end

defp commanded,
do: Application.fetch_env!(:trento, Trento.Commanded)[:adapter]
end
30 changes: 15 additions & 15 deletions lib/trento/databases/database.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Trento.Databases.Database do
RegisterDatabaseInstance
}

alias Trento.SapSystems.Events.{
alias Trento.Databases.Events.{
DatabaseDeregistered,
DatabaseHealthChanged,
DatabaseInstanceDeregistered,
Expand Down Expand Up @@ -84,12 +84,12 @@ defmodule Trento.Databases.Database do
) do
[
%DatabaseRegistered{
sap_system_id: database_id,
database_id: database_id,
sid: sid,
health: health
},
%DatabaseInstanceRegistered{
sap_system_id: database_id,
database_id: database_id,
sid: sid,
tenant: tenant,
instance_number: instance_number,
Expand Down Expand Up @@ -139,7 +139,7 @@ defmodule Trento.Databases.Database do
when not is_nil(deregistered_at) do
[
%DatabaseInstanceRegistered{
sap_system_id: database_id,
database_id: database_id,
sid: sid,
tenant: tenant,
instance_number: instance_number,
Expand All @@ -154,7 +154,7 @@ defmodule Trento.Databases.Database do
health: health
},
%DatabaseRestored{
sap_system_id: database_id,
database_id: database_id,
health: health
}
]
Expand Down Expand Up @@ -206,7 +206,7 @@ defmodule Trento.Databases.Database do
%DatabaseInstanceMarkedAbsent{
instance_number: instance_number,
host_id: host_id,
sap_system_id: database_id,
database_id: database_id,
absent_at: absent_at
}

Expand Down Expand Up @@ -245,7 +245,7 @@ defmodule Trento.Databases.Database do
def apply(
%Database{database_id: nil},
%DatabaseRegistered{
sap_system_id: database_id,
database_id: database_id,
sid: sid,
health: health
}
Expand Down Expand Up @@ -426,7 +426,7 @@ defmodule Trento.Databases.Database do
}
) do
%DatabaseInstanceRegistered{
sap_system_id: database_id,
database_id: database_id,
sid: sid,
tenant: tenant,
instance_number: instance_number,
Expand Down Expand Up @@ -460,7 +460,7 @@ defmodule Trento.Databases.Database do
%DatabaseInstanceMarkedPresent{
instance_number: instance_number,
host_id: host_id,
sap_system_id: database_id
database_id: database_id
}
end

Expand All @@ -482,7 +482,7 @@ defmodule Trento.Databases.Database do
when system_replication != new_system_replication or
system_replication_status != new_system_replication_status do
%DatabaseInstanceSystemReplicationChanged{
sap_system_id: database_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
system_replication: new_system_replication,
Expand All @@ -505,7 +505,7 @@ defmodule Trento.Databases.Database do
)
when health != new_health do
%DatabaseInstanceHealthChanged{
sap_system_id: database_id,
database_id: database_id,
host_id: host_id,
instance_number: instance_number,
health: new_health
Expand All @@ -528,7 +528,7 @@ defmodule Trento.Databases.Database do

if new_health != health do
%DatabaseHealthChanged{
sap_system_id: database_id,
database_id: database_id,
health: new_health
}
end
Expand All @@ -555,7 +555,7 @@ defmodule Trento.Databases.Database do

_ ->
%DatabaseInstanceDeregistered{
sap_system_id: database_id,
database_id: database_id,
instance_number: instance_number,
host_id: host_id,
deregistered_at: deregistered_at
Expand All @@ -571,7 +571,7 @@ defmodule Trento.Databases.Database do
},
deregistered_at
) do
%DatabaseDeregistered{sap_system_id: database_id, deregistered_at: deregistered_at}
%DatabaseDeregistered{database_id: database_id, deregistered_at: deregistered_at}
end

defp maybe_emit_database_deregistered_event(
Expand All @@ -594,7 +594,7 @@ defmodule Trento.Databases.Database do

if has_secondary? and !has_primary? do
%DatabaseDeregistered{
sap_system_id: database_id,
database_id: database_id,
deregistered_at: deregistered_at
}
end
Expand Down
12 changes: 12 additions & 0 deletions lib/trento/databases/events/database_deregistered.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Trento.Databases.Events.DatabaseDeregistered do
@moduledoc """
This event is emitted once all database instances belonging to a HANA database have been deregistered (decommissioned).
"""

use Trento.Support.Event

defevent do
field :database_id, Ecto.UUID
field :deregistered_at, :utc_datetime_usec
end
end
14 changes: 14 additions & 0 deletions lib/trento/databases/events/database_health_changed.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Databases.Events.DatabaseHealthChanged do
@moduledoc """
This event is emitted when a database health has changed.
"""

use Trento.Support.Event

require Trento.Enums.Health, as: Health

defevent do
field :database_id, Ecto.UUID
field :health, Ecto.Enum, values: Health.values()
end
end
14 changes: 14 additions & 0 deletions lib/trento/databases/events/database_instance_deregistered.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Databases.Events.DatabaseInstanceDeregistered do
@moduledoc """
This event is emitted when a database instance is deregistered (decommissioned).
"""

use Trento.Support.Event

defevent do
field :instance_number, :string
field :host_id, Ecto.UUID
field :database_id, Ecto.UUID
field :deregistered_at, :utc_datetime_usec
end
end
16 changes: 16 additions & 0 deletions lib/trento/databases/events/database_instance_health_changed.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Trento.Databases.Events.DatabaseInstanceHealthChanged do
@moduledoc """
This event is emitted when a database instance health has changed.
"""

use Trento.Support.Event

require Trento.Enums.Health, as: Health

defevent do
field :database_id, Ecto.UUID
field :host_id, Ecto.UUID
field :instance_number, :string
field :health, Ecto.Enum, values: Health.values()
end
end
14 changes: 14 additions & 0 deletions lib/trento/databases/events/database_instance_marked_absent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Databases.Events.DatabaseInstanceMarkedAbsent do
@moduledoc """
This event is emitted when a database instance is marked as absent.
"""

use Trento.Support.Event

defevent do
field :instance_number, :string
field :host_id, Ecto.UUID
field :database_id, Ecto.UUID
field :absent_at, :utc_datetime_usec
end
end
13 changes: 13 additions & 0 deletions lib/trento/databases/events/database_instance_marked_present.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Trento.Databases.Events.DatabaseInstanceMarkedPresent do
@moduledoc """
This event is emitted when a database instance is marked as present.
"""

use Trento.Support.Event

defevent do
field :instance_number, :string
field :host_id, Ecto.UUID
field :database_id, Ecto.UUID
end
end
25 changes: 25 additions & 0 deletions lib/trento/databases/events/database_instance_registered.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Trento.Databases.Events.DatabaseInstanceRegistered do
@moduledoc """
This event is emitted when a database instance is registered.
"""

use Trento.Support.Event

require Trento.Enums.Health, as: Health

defevent do
field :database_id, Ecto.UUID
field :sid, :string
field :tenant, :string
field :host_id, Ecto.UUID
field :instance_number, :string
field :instance_hostname, :string
field :features, :string
field :http_port, :integer
field :https_port, :integer
field :start_priority, :string
field :system_replication, :string
field :system_replication_status, :string
field :health, Ecto.Enum, values: Health.values()
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Trento.Databases.Events.DatabaseInstanceSystemReplicationChanged do
@moduledoc """
This event is emitted when a database instance system replication has changed.
"""

use Trento.Support.Event

defevent do
field :database_id, Ecto.UUID
field :host_id, Ecto.UUID
field :instance_number, :string
field :system_replication, :string
field :system_replication_status, :string
end
end
15 changes: 15 additions & 0 deletions lib/trento/databases/events/database_registered.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Trento.Databases.Events.DatabaseRegistered do
@moduledoc """
This event is emitted when a database is registered.
"""

use Trento.Support.Event

require Trento.Enums.Health, as: Health

defevent do
field :database_id, Ecto.UUID
field :sid, :string
field :health, Ecto.Enum, values: Health.values()
end
end
14 changes: 14 additions & 0 deletions lib/trento/databases/events/database_restored.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule Trento.Databases.Events.DatabaseRestored do
@moduledoc """
This event is emitted when a database is restored.
"""

use Trento.Support.Event

require Trento.Enums.Health, as: Health

defevent do
field :database_id, Ecto.UUID
field :health, Ecto.Enum, values: Health.values()
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseDeregistered do

use Trento.Support.Event

defevent do
defevent superseded_by: Trento.Databases.Events.DatabaseDeregistered do
field :sap_system_id, Ecto.UUID
field :deregistered_at, :utc_datetime_usec
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseHealthChanged do

require Trento.Enums.Health, as: Health

defevent do
defevent superseded_by: Trento.Databases.Events.DatabaseHealthChanged do
field :sap_system_id, Ecto.UUID
field :health, Ecto.Enum, values: Health.values()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceDeregistered do

use Trento.Support.Event

defevent do
defevent superseded_by: Trento.Databases.Events.DatabaseInstanceDeregistered do
field :instance_number, :string
field :host_id, Ecto.UUID
field :sap_system_id, Ecto.UUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Trento.SapSystems.Events.DatabaseInstanceHealthChanged do

require Trento.Enums.Health, as: Health

defevent do
defevent superseded_by: Trento.Databases.Events.DatabaseInstanceHealthChanged do
field :sap_system_id, Ecto.UUID
field :host_id, Ecto.UUID
field :instance_number, :string
Expand Down
Loading

0 comments on commit 03c3ac7

Please sign in to comment.