Skip to content

Commit

Permalink
Merge pull request #3897 from mathesar-foundation/handle-caching
Browse files Browse the repository at this point in the history
Reduces frontend caching, fixes a few bugs
  • Loading branch information
pavish authored Oct 1, 2024
2 parents 47222d0 + 98e48ea commit a41e2fb
Show file tree
Hide file tree
Showing 28 changed files with 486 additions and 512 deletions.
23 changes: 8 additions & 15 deletions mathesar/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
from mathesar.rpc.schemas import list_ as schemas_list
from mathesar.rpc.servers.configured import list_ as get_servers_list
from mathesar.rpc.tables import list_with_metadata as tables_list
from mathesar.api.serializers.databases import TypeSerializer
from mathesar.api.serializers.tables import TableSerializer
from mathesar.api.serializers.queries import QuerySerializer
from mathesar.api.ui.serializers.users import UserSerializer
from mathesar.api.utils import is_valid_uuid_v4
from mathesar.database.types import UIType
from mathesar.models.shares import SharedTable, SharedQuery
from mathesar.state import reset_reflection
from mathesar import __version__
Expand Down Expand Up @@ -45,19 +43,15 @@ def get_table_list(request, database_id, schema_oid):
return []


def get_queries_list(request, database_id, schema_id):
return explorations_list(request=request, database_id=database_id, schema_oid=schema_id)


def get_ui_type_list(request, database_id):
if database_id is None:
def get_queries_list(request, database_id, schema_oid):
if database_id is not None and schema_oid is not None:
return explorations_list(
request=request,
database_id=database_id,
schema_oid=schema_oid
)
else:
return []
type_serializer = TypeSerializer(
UIType,
many=True,
context={'request': request}
)
return type_serializer.data


def get_user_data(request):
Expand All @@ -84,7 +78,6 @@ def _get_internal_db_meta():

def _get_base_data_all_routes(request, database_id=None, schema_id=None):
return {
'abstract_types': get_ui_type_list(request, database_id),
'current_database': int(database_id) if database_id else None,
'current_schema': int(schema_id) if schema_id else None,
'current_release_tag_name': __version__,
Expand Down
18 changes: 10 additions & 8 deletions mathesar_ui/src/contexts/DatabaseSettingsRouteContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ export class DatabaseSettingsRouteContext {
(cr) => cr.name,
);
if (isStable && loginRoles && configuredRoles) {
return [...loginRoles.values()].map((role) => ({
const allRoles = [...loginRoles.values()].map((role) => ({
name: role.name,
role,
configuredRole: configuredRoles.get(role.name),
}));
const allLoginRoleNames = new Set(allRoles.map((ar) => ar.name));
const configuredRolesNotInAllRoles = [...configuredRoles.values()]
.filter((cr) => !allLoginRoleNames.has(cr.name))
.map((configuredRole) => ({
name: configuredRole.name,
configuredRole,
}));
return [...allRoles, ...configuredRolesNotInAllRoles];
}
if ($configuredRoles.isStable && configuredRoles) {
return [...configuredRoles.values()].map((configuredRole) => ({
Expand Down Expand Up @@ -129,20 +137,14 @@ export class DatabaseSettingsRouteContext {
this.collaborators.updateResolvedValue((collaborators) =>
collaborators.with(newCollaborator.id, newCollaborator),
);
return newCollaborator;
}

async deleteCollaborator(collaborator: Collaborator) {
await collaborator.delete();
this.collaborators.updateResolvedValue((c) => c.without(collaborator.id));
}

async deleteRoleAndResetDependents(role: Role) {
await this.databaseRouteContext.deleteRole(role);
// When a role is deleted, both Collaborators & ConfiguredRoles need to be reset
this.configuredRoles.reset();
this.collaborators.reset();
}

static construct(databaseRouteContext: DatabaseRouteContext) {
return setRouteContext(
contextKey,
Expand Down
2 changes: 2 additions & 0 deletions mathesar_ui/src/i18n/languages/en/dict.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"click_save_button_to_save_changes": "Click Save button to save changes.",
"collaborator": "Collaborator",
"collaborator_added_successfully": "Collaborator added successfully",
"collaborator_removed_successfully": "Collaborator removed successfully",
"collaborator_role_help": "This role determines permissions for this collaborator. Mathesar will use this PostgreSQL role for all database interactions by this user.",
"collaborator_role_updated_successfully": "Role updated successfully for collaborator.",
"collaborators": "Collaborators",
Expand Down Expand Up @@ -531,6 +532,7 @@
"select_columns_cells_view_properties": "Select one or more columns or cells to view the associated column properties and actions.",
"select_columns_extract": "Select the columns you want to extract",
"select_columns_extract_into_table": "Select the columns you want to extract into [targetTableName]",
"select_columns_for_exploration_help": "Select the columns that will be used for the exploration. Columns are limited to those from the base table and its linked tables.",
"select_columns_move": "Select the columns you want to move",
"select_columns_move_into_table": "Select the columns you want to move into [targetTableName]",
"select_columns_to_hide": "Select Columns to Hide",
Expand Down
2 changes: 1 addition & 1 deletion mathesar_ui/src/models/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Database {
return new AsyncRpcApiStore(api.roles.get_current_role, {
postProcess: (currentRole) => ({
currentRoleOid: currentRole.current_role.oid,
parentRoleOids: currentRole.parent_roles.map((pr) => pr.oid),
parentRoleOids: new Set(currentRole.parent_roles.map((pr) => pr.oid)),
}),
});
}
Expand Down
2 changes: 1 addition & 1 deletion mathesar_ui/src/pages/database/DatabasePageWrapper.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
const databaseRouteContext = DatabaseRouteContext.get();
$: ({ database, underlyingDatabase } = $databaseRouteContext);
$: void underlyingDatabase.run({ database_id: database.id });
$: void underlyingDatabase.runConservatively({ database_id: database.id });
const commonData = preloadCommonData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
};
function getAsyncStoresForPermissions() {
void AsyncRpcApiStore.runBatched([
void AsyncRpcApiStore.runBatchConservatively([
databasePrivileges.batchRunner({ database_id: database.id }),
roles.batchRunner({ database_id: database.id }),
underlyingDatabase.batchRunner({ database_id: database.id }),
Expand Down
10 changes: 3 additions & 7 deletions mathesar_ui/src/pages/database/schemas/SchemasSection.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
import { RichText } from '@mathesar/components/rich-text';
import { DatabaseRouteContext } from '@mathesar/contexts/DatabaseRouteContext';
import { iconAddNew } from '@mathesar/icons';
import type { Database } from '@mathesar/models/Database';
import type { Schema } from '@mathesar/models/Schema';
import { confirmDelete } from '@mathesar/stores/confirmation';
import { modal } from '@mathesar/stores/modal';
import {
type DBSchemaStoreData,
type SchemaStoreData,
deleteSchema as deleteSchemaAPI,
schemas as schemasStore,
} from '@mathesar/stores/schemas';
import { removeTablesStore } from '@mathesar/stores/tables';
import AddEditSchemaModal from '@mathesar/systems/schemas/AddEditSchemaModal.svelte';
import {
Button,
Expand All @@ -31,7 +29,7 @@
const databaseRouteContext = DatabaseRouteContext.get();
$: ({ database, underlyingDatabase } = $databaseRouteContext);
$: void underlyingDatabase.run({ database_id: database.id });
$: void underlyingDatabase.runConservatively({ database_id: database.id });
$: schemasMap = $schemasStore.data;
$: schemasRequestStatus = $schemasStore.requestStatus;
$: isLoading =
Expand All @@ -44,7 +42,7 @@
let targetSchema: Schema | undefined;
function filterSchemas(
schemaData: DBSchemaStoreData['data'],
schemaData: SchemaStoreData['data'],
filter: string,
): Schema[] {
const filtered: Schema[] = [];
Expand Down Expand Up @@ -75,8 +73,6 @@
body: [$_('schema_delete_warning'), $_('are_you_sure_to_proceed')],
onProceed: async () => {
await deleteSchemaAPI(schema);
// TODO: Create common util to handle data clearing & sync between stores
removeTablesStore(schema);
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
export let configuredRolesMap: ImmutableMap<number, ConfiguredRole>;
export let usersMap: ImmutableMap<number, User>;
export let collaboratorsMap: ImmutableMap<number, Collaborator>;
export let onAdd: (collaborator: Collaborator) => void;
const userId = requiredField<number | undefined>(undefined);
const configuredRoleId = requiredField<number | undefined>(undefined);
Expand All @@ -41,7 +42,11 @@
async function addCollaborator() {
if ($userId && $configuredRoleId) {
await $routeContext.addCollaborator($userId, $configuredRoleId);
const collaborator = await $routeContext.addCollaborator(
$userId,
$configuredRoleId,
);
onAdd(collaborator);
controller.close();
toast.success($_('collaborator_added_successfully'));
form.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import { iconDeleteMajor, iconEdit } from '@mathesar/icons';
import type { Collaborator } from '@mathesar/models/Collaborator';
import { confirmDelete } from '@mathesar/stores/confirmation';
import { toast } from '@mathesar/stores/toast';
import { getUserProfileStoreFromContext } from '@mathesar/stores/userProfile';
import { Button, SpinnerButton } from '@mathesar-component-library';
export let collaborator: Collaborator;
export let editRoleForCollaborator: (collaborator: Collaborator) => void;
export let onClickEditRole: (collaborator: Collaborator) => void;
export let onDelete: (collaborator: Collaborator) => void;
const routeContext = DatabaseSettingsRouteContext.get();
$: ({ configuredRoles, users } = $routeContext);
Expand All @@ -23,6 +25,12 @@
$: configuredRoleId = collaborator.configuredRoleId;
$: configuredRole = $configuredRoles.resolvedValue?.get($configuredRoleId);
$: userName = user ? user.full_name || user.username : '';
async function deleteCollaborator() {
await $routeContext.deleteCollaborator(collaborator);
onDelete(collaborator);
toast.success($_('collaborator_removed_successfully'));
}
</script>

<GridTableCell>
Expand All @@ -47,7 +55,7 @@
<div class="actions">
<Button
appearance="secondary"
on:click={() => editRoleForCollaborator(collaborator)}
on:click={() => onClickEditRole(collaborator)}
disabled={!isMathesarAdmin}
>
<Icon {...iconEdit} size="0.8em" />
Expand All @@ -62,7 +70,7 @@
identifierName: userName,
identifierType: $_('collaborator'),
})}
onClick={() => $routeContext.deleteCollaborator(collaborator)}
onClick={deleteCollaborator}
icon={{ ...iconDeleteMajor, size: '0.8em' }}
label=""
appearance="secondary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type { Collaborator } from '@mathesar/models/Collaborator';
import AsyncRpcApiStore from '@mathesar/stores/AsyncRpcApiStore';
import { modal } from '@mathesar/stores/modal';
import { fetchSchemasForCurrentDatabase } from '@mathesar/stores/schemas';
import { getUserProfileStoreFromContext } from '@mathesar/stores/userProfile';
import {
Button,
Expand All @@ -30,13 +31,19 @@
const userProfileStore = getUserProfileStoreFromContext();
$: ({ isMathesarAdmin } = $userProfileStore);
$: ({ database, configuredRoles, collaborators, users } = $routeContext);
$: ({
database,
configuredRoles,
collaborators,
users,
databaseRouteContext,
} = $routeContext);
$: void AsyncRpcApiStore.runBatched([
$: void AsyncRpcApiStore.runBatchConservatively([
collaborators.batchRunner({ database_id: database.id }),
configuredRoles.batchRunner({ server_id: database.server.id }),
]);
$: void users.runIfNotInitialized();
$: void users.runConservatively();
$: isLoading =
$collaborators.isLoading || $configuredRoles.isLoading || $users.isLoading;
$: isSuccess = $collaborators.isOk && $configuredRoles.isOk && $users.isOk;
Expand All @@ -52,6 +59,18 @@
targetCollaborator = collaborator;
editCollaboratorRoleModal.open();
}
function checkAndHandleSideEffects(collaborator: Collaborator) {
if (collaborator.userId === $userProfileStore.id) {
void AsyncRpcApiStore.runBatch([
databaseRouteContext.underlyingDatabase.batchRunner({
database_id: database.id,
}),
databaseRouteContext.roles.batchRunner({ database_id: database.id }),
]);
void fetchSchemasForCurrentDatabase();
}
}
</script>

<SettingsContentLayout>
Expand Down Expand Up @@ -79,7 +98,11 @@
<GridTableCell header>{$_('role')}</GridTableCell>
<GridTableCell header>{$_('actions')}</GridTableCell>
{#each [...($collaborators.resolvedValue?.values() ?? [])] as collaborator (collaborator.id)}
<CollaboratorRow {collaborator} {editRoleForCollaborator} />
<CollaboratorRow
{collaborator}
onClickEditRole={editRoleForCollaborator}
onDelete={checkAndHandleSideEffects}
/>
{/each}
</GridTable>
</div>
Expand All @@ -94,6 +117,7 @@
usersMap={$users.resolvedValue}
configuredRolesMap={$configuredRoles.resolvedValue}
collaboratorsMap={$collaborators.resolvedValue}
onAdd={checkAndHandleSideEffects}
/>
{/if}

Expand All @@ -103,6 +127,7 @@
usersMap={$users.resolvedValue}
controller={editCollaboratorRoleModal}
configuredRolesMap={$configuredRoles.resolvedValue}
onUpdateRole={checkAndHandleSideEffects}
/>
{/if}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
export let collaborator: Collaborator;
export let configuredRolesMap: ImmutableMap<number, ConfiguredRole>;
export let usersMap: ImmutableMap<number, User>;
export let onUpdateRole: (collaborator: Collaborator) => void;
$: savedConfiguredRoleId = collaborator.configuredRoleId;
$: configuredRoleId = requiredField<number>($savedConfiguredRoleId);
Expand All @@ -33,6 +34,7 @@
async function updateRoleForCollaborator() {
await collaborator.setConfiguredRole($configuredRoleId);
onUpdateRole(collaborator);
controller.close();
toast.success($_('collaborator_role_updated_successfully'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$: ({ database, databaseRouteContext, configuredRoles, combinedLoginRoles } =
$routeContext);
$: ({ roles } = databaseRouteContext);
$: void AsyncRpcApiStore.runBatched([
$: void AsyncRpcApiStore.runBatchConservatively([
configuredRoles.batchRunner({ server_id: database.server.id }),
roles.batchRunner({ database_id: database.id }),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
login: false,
});
}
toast.success('role_created_successfully');
toast.success($_('role_created_successfully'));
controller.close();
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
export let controller: ModalController;
export let parentRole: Role;
export let rolesMap: ImmutableMap<number, Role>;
export let onSave: (props: { parentRole: Role }) => void;
$: savedMembers = parentRole.members;
$: savedMemberOids = new Set([...$savedMembers.keys()]);
Expand All @@ -51,6 +52,7 @@
async function saveMembers() {
await parentRole.setMembers(new Set($memberOids));
onSave({ parentRole });
controller.close();
toast.success($_('child_roles_saved_successfully'));
}
Expand Down
Loading

0 comments on commit a41e2fb

Please sign in to comment.