-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create new databases context file * Move the database events to its folder
- Loading branch information
Showing
35 changed files
with
554 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
lib/trento/databases/events/database_instance_deregistered.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
lib/trento/databases/events/database_instance_health_changed.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
lib/trento/databases/events/database_instance_marked_absent.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
lib/trento/databases/events/database_instance_marked_present.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
lib/trento/databases/events/database_instance_registered.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
lib/trento/databases/events/database_instance_system_replication_changed.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.