Skip to content

Commit

Permalink
Merge pull request #4050 from mathesar-foundation/sort_schemas
Browse files Browse the repository at this point in the history
Sort schemas in Database page and Breadcrumb selector
  • Loading branch information
seancolsen authored Dec 6, 2024
2 parents 5552df8 + 0a65135 commit f8647ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion db/sql/00_msar.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,8 @@ LEFT JOIN pg_catalog.pg_class c ON c.relnamespace = s.oid AND c.relkind = 'r'
GROUP BY
s.oid,
s.nspname,
s.nspowner;
s.nspowner
ORDER BY s.nspname;
-- Filter on relkind so that we only count tables. This must be done in the ON clause so that
-- we still get a row for schemas with no tables.
$$ LANGUAGE SQL STABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { getSchemaPageUrl } from '@mathesar/routes/urls';
import {
currentSchemaId,
schemas as schemasStore,
sortedSchemas as schemasStore,
} from '@mathesar/stores/schemas';
import BreadcrumbSelector from './BreadcrumbSelector.svelte';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import {
type SchemaStoreData,
deleteSchema as deleteSchemaAPI,
schemas as schemasStore,
sortedSchemas as schemasStore,
} from '@mathesar/stores/schemas';
import AddEditSchemaModal from '@mathesar/systems/schemas/AddEditSchemaModal.svelte';
import {
Expand Down
26 changes: 25 additions & 1 deletion mathesar_ui/src/stores/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import type { Database } from '@mathesar/models/Database';
import { Schema } from '@mathesar/models/Schema';
import { getErrorMessage } from '@mathesar/utils/errors';
import { preloadCommonData } from '@mathesar/utils/preloadData';
import { type CancellablePromise, collapse } from '@mathesar-component-library';
import {
type CancellablePromise,
collapse,
unite,
} from '@mathesar-component-library';

import { databasesStore } from './databases';

Expand Down Expand Up @@ -175,6 +179,26 @@ export const schemas = collapse(
}),
);

function sortSchemas(_schemas: Iterable<Schema>): Schema[] {
return [..._schemas].sort((a, b) => get(a.name).localeCompare(get(b.name)));
}

export const schemaNames = collapse(
derived(schemas, ($schemas) =>
unite([...$schemas.data.values()].map((s) => s.name)),
),
);

export const sortedSchemas = derived([schemas, schemaNames], ([$schemas]) => {
const schemasMap = new Map(
sortSchemas($schemas.data.values()).map((s) => [s.oid, s]),
);
return {
...$schemas,
data: schemasMap,
};
});

export const currentSchema: Readable<Schema | undefined> = derived(
[currentSchemaId, schemas],
([$currentSchemaId, $schemas]) =>
Expand Down

0 comments on commit f8647ae

Please sign in to comment.