diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index 4f6dc8f8c5..f8067aebbf 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -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; diff --git a/mathesar_ui/src/components/breadcrumb/SchemaSelector.svelte b/mathesar_ui/src/components/breadcrumb/SchemaSelector.svelte index fb7a15ad54..b7cfe908de 100644 --- a/mathesar_ui/src/components/breadcrumb/SchemaSelector.svelte +++ b/mathesar_ui/src/components/breadcrumb/SchemaSelector.svelte @@ -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'; diff --git a/mathesar_ui/src/pages/database/schemas/SchemasSection.svelte b/mathesar_ui/src/pages/database/schemas/SchemasSection.svelte index a911f24042..fb061b9c9b 100644 --- a/mathesar_ui/src/pages/database/schemas/SchemasSection.svelte +++ b/mathesar_ui/src/pages/database/schemas/SchemasSection.svelte @@ -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 { diff --git a/mathesar_ui/src/stores/schemas.ts b/mathesar_ui/src/stores/schemas.ts index a3a231a1c9..0c7bd8f3d3 100644 --- a/mathesar_ui/src/stores/schemas.ts +++ b/mathesar_ui/src/stores/schemas.ts @@ -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'; @@ -175,6 +179,26 @@ export const schemas = collapse( }), ); +function sortSchemas(_schemas: Iterable): 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 = derived( [currentSchemaId, schemas], ([$currentSchemaId, $schemas]) =>