Skip to content

Commit

Permalink
Add missing timestamps to readmodels
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Feb 26, 2024
1 parent 14d7749 commit f721437
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/trento/clusters/cluster_enrichment_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Trento.Clusters.ClusterEnrichmentData do
@primary_key {:cluster_id, :binary_id, autogenerate: false}
schema "clusters_enrichment_data" do
field :cib_last_written, :string

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/clusters/projections/cluster_read_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ defmodule Trento.Clusters.Projections.ClusterReadModel do
field :cib_last_written, :string, virtual: true

field :deregistered_at, :utc_datetime_usec

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/heartbeats/heartbeat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule Trento.Heartbeats.Heartbeat do
@primary_key {:agent_id, :string, autogenerate: false}
schema "heartbeats" do
field :timestamp, :utc_datetime_usec

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
1 change: 1 addition & 0 deletions lib/trento/hosts/projections/host_read_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule Trento.Hosts.Projections.HostReadModel do
field :last_heartbeat_timestamp, :utc_datetime_usec, virtual: true

field :deregistered_at, :utc_datetime_usec
timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 1 addition & 1 deletion lib/trento/hosts/projections/host_telemetry_read_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Trento.Hosts.Projections.HostTelemetryReadModel do

field :provider, Ecto.Enum, values: Provider.values()

timestamps()
timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Trento.Hosts.Projections.SlesSubscriptionReadModel do
field :starts_at, :string
field :expires_at, :string

timestamps()
timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ defmodule Trento.SapSystems.Projections.ApplicationInstanceReadModel do
references: :host_id,
foreign_key: :id,
where: [deregistered_at: nil]

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ defmodule Trento.SapSystems.Projections.DatabaseInstanceReadModel do
references: :host_id,
foreign_key: :id,
where: [deregistered_at: nil]

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/sap_systems/projections/database_read_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ defmodule Trento.SapSystems.Projections.DatabaseReadModel do
preload_order: [asc: :instance_number, asc: :host_id]

field :deregistered_at, :utc_datetime_usec

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/sap_systems/projections/sap_system_read_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ defmodule Trento.SapSystems.Projections.SapSystemReadModel do
has_many :tags, Tag, foreign_key: :resource_id

field :deregistered_at, :utc_datetime_usec

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/settings/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule Trento.Settings.Settings do
@primary_key {:installation_id, :binary_id, autogenerate: false}
schema "settings" do
field :eula_accepted, :boolean

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 2 additions & 0 deletions lib/trento/tags/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Trento.Tags.Tag do
field :value, :string
field :resource_id, Ecto.UUID
field :resource_type, Ecto.Enum, values: [:host, :cluster, :sap_system, :database]

timestamps(type: :utc_datetime_usec)
end

@spec changeset(t() | Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
Expand Down
2 changes: 1 addition & 1 deletion lib/trento/users/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Trento.Users.User do
schema "users" do
pow_user_fields()

timestamps()
timestamps(type: :utc_datetime_usec)
end

def changeset(user, attrs) do
Expand Down
39 changes: 39 additions & 0 deletions priv/repo/migrations/20240221134250_add_missing_timestamps.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
defmodule Trento.Repo.Migrations.AddMissingTimestamps do
use Ecto.Migration

def change do
[
:heartbeats,
:hosts,
:clusters,
:database_instances,
:sap_systems,
:application_instances,
:databases,
:tags,
:settings,
:clusters_enrichment_data
]
|> Enum.each(fn table ->
alter table(table) do
# We add the default value to deal with already existing records, otherwise the not-null constraint of timestamps is triggered
timestamps(default: "NOW()", type: :utc_datetime_usec)
end
end)

[
:users,
:projection_versions,
:sles_subscriptions,
:discovery_events,
:hosts_telemetry,
:discarded_discovery_events
]
|> Enum.each(fn table ->
alter table(table) do
modify :inserted_at, :utc_datetime_usec, default: "NOW()"
modify :updated_at, :utc_datetime_usec, default: "NOW()"
end
end)
end
end

0 comments on commit f721437

Please sign in to comment.