Skip to content

Commit

Permalink
Preload database instances during database restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Jul 31, 2023
1 parent baf724e commit 160f81b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
9 changes: 8 additions & 1 deletion assets/js/state/databases.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSlice } from '@reduxjs/toolkit';
import { maybeUpdateInstanceHealth } from './instances';
import { filterByInstances, maybeUpdateInstanceHealth } from './instances';

const initialState = {
loading: false,
Expand Down Expand Up @@ -30,6 +30,12 @@ export const databasesListSlice = createSlice({
appendDatabaseInstance: (state, action) => {
state.databaseInstances = [...state.databaseInstances, action.payload];
},
upsertDatabaseInstances: (state, action) => {
state.databaseInstances = filterByInstances(
state.databaseInstances,
action.payload
).concat(action.payload);
},
removeDatabase: (state, { payload: { id } }) => {
state.databases = state.databases.filter(
(database) => database.id !== id
Expand Down Expand Up @@ -118,6 +124,7 @@ export const {
removeDatabase,
removeDatabaseInstance,
appendDatabaseInstance,
upsertDatabaseInstances,
updateDatabaseHealth,
updateDatabaseInstanceHealth,
updateDatabaseInstanceSystemReplication,
Expand Down
26 changes: 26 additions & 0 deletions assets/js/state/databases.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import databaseReducer, {
removeDatabase,
removeDatabaseInstance,
upsertDatabaseInstances,
} from '@state/databases';
import {
databaseFactory,
Expand Down Expand Up @@ -47,4 +48,29 @@ describe('Databases reducer', () => {

expect(databaseReducer(initialState, action)).toEqual(expectedState);
});

it('should upsert database instances', () => {
const changedIndex = 0;

const initialState = {
databaseInstances: databaseInstanceFactory.buildList(2),
};

const updatedInstance = {
...initialState.databaseInstances[changedIndex],
instance_hostname: 'my_name_has_changed',
};
const newInstance = databaseInstanceFactory.build();
const newInstances = [updatedInstance, newInstance];

const action = upsertDatabaseInstances(newInstances);

const expectedState = {
databaseInstances: initialState.databaseInstances
.filter((_instance, index) => index !== changedIndex)
.concat(newInstances),
};

expect(databaseReducer(initialState, action)).toEqual(expectedState);
});
});
2 changes: 2 additions & 0 deletions assets/js/state/sagas/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DATABASE_INSTANCE_SYSTEM_REPLICATION_CHANGED,
appendDatabase,
appendDatabaseInstance,
upsertDatabaseInstances,
updateDatabaseHealth,
updateDatabaseInstanceHealth,
updateDatabaseInstanceSystemReplication,
Expand Down Expand Up @@ -73,6 +74,7 @@ export function* databaseDeregistered({ payload }) {

export function* databaseRestored({ payload }) {
yield put(appendDatabase(payload));
yield put(upsertDatabaseInstances(payload.database_instances));
yield put(
notify({
text: `The database ${payload.sid} has been restored.`,
Expand Down
2 changes: 2 additions & 0 deletions assets/js/state/sagas/databases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
databaseRestored,
} from '@state/sagas/databases';
import {
upsertDatabaseInstances,
removeDatabase,
removeDatabaseInstance,
appendDatabase,
Expand Down Expand Up @@ -70,6 +71,7 @@ describe('SAP Systems sagas', () => {

expect(dispatched).toEqual([
appendDatabase(database),
upsertDatabaseInstances(database.database_instances),
notify({
text: `The database ${database.sid} has been restored.`,
icon: 'ℹ️',
Expand Down
2 changes: 1 addition & 1 deletion lib/trento/application/projectors/database_projector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ defmodule Trento.DatabaseProjector do
database =
DatabaseReadModel
|> Repo.get!(sap_system_id)
|> Repo.preload([:tags])
|> Repo.preload([:tags, :database_instances])

TrentoWeb.Endpoint.broadcast(
@databases_topic,
Expand Down
1 change: 0 additions & 1 deletion lib/trento_web/views/v1/sap_system_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ defmodule TrentoWeb.V1.SapSystemView do
database
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:database_instances)
end

def render("database_health_changed.json", %{health: health}), do: health
Expand Down
10 changes: 8 additions & 2 deletions test/trento/application/projectors/database_projector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ defmodule Trento.DatabaseProjectorTest do
health: :critical
)

insert(:database_instance_without_host, sap_system_id: sap_system_id)
|> Map.from_struct()
|> Map.delete(:__meta__)
|> Map.delete(:host)

insert_list(5, :tag, resource_id: sap_system_id)

event = %DatabaseRestored{
Expand All @@ -327,11 +332,11 @@ defmodule Trento.DatabaseProjectorTest do

ProjectorTestHelper.project(DatabaseProjector, event, "database_projector")

%{tags: tags} =
%{tags: tags, database_instances: database_instances} =
projection =
DatabaseReadModel
|> Repo.get(sap_system_id)
|> Repo.preload([:tags])
|> Repo.preload([:tags, :database_instances])

assert nil == projection.deregistered_at
assert :passing == projection.health
Expand All @@ -341,6 +346,7 @@ defmodule Trento.DatabaseProjectorTest do
health: :passing,
id: ^sap_system_id,
sid: "NWD",
database_instances: ^database_instances,
tags: ^tags
},
1000
Expand Down

0 comments on commit 160f81b

Please sign in to comment.