()
| 849 | * safe to share with OneUptime for diagnostics. |
| 850 | */ |
| 851 | async function getPostgresSchema(): Promise<JSONObject> { |
| 852 | const result: JSONObject = { |
| 853 | connected: false, |
| 854 | serverVersion: null, |
| 855 | databaseSizeInBytes: null, |
| 856 | tableCount: 0, |
| 857 | tables: [], |
| 858 | }; |
| 859 | |
| 860 | try { |
| 861 | const dataSource: ReturnType<typeof PostgresAppInstance.getDataSource> = |
| 862 | PostgresAppInstance.getDataSource(); |
| 863 | |
| 864 | if (!dataSource) { |
| 865 | return result; |
| 866 | } |
| 867 | |
| 868 | result["connected"] = true; |
| 869 | |
| 870 | const versionRows: Array<{ server_version: string }> = |
| 871 | await dataSource.query("SHOW server_version"); |
| 872 | result["serverVersion"] = versionRows?.[0]?.server_version || null; |
| 873 | |
| 874 | const sizeRows: Array<{ size: string }> = await dataSource.query( |
| 875 | "SELECT pg_database_size(current_database()) AS size", |
| 876 | ); |
| 877 | result["databaseSizeInBytes"] = toNumberOrNull(sizeRows?.[0]?.size); |
| 878 | |
| 879 | const columnRows: Array<{ |
| 880 | table_name: string; |
| 881 | column_name: string; |
| 882 | data_type: string; |
| 883 | is_nullable: string; |
| 884 | column_default: string | null; |
| 885 | character_maximum_length: number | null; |
| 886 | }> = await dataSource.query( |
| 887 | `SELECT table_name, column_name, data_type, is_nullable, column_default, character_maximum_length |
| 888 | FROM information_schema.columns |
| 889 | WHERE table_schema = 'public' |
| 890 | ORDER BY table_name ASC, ordinal_position ASC`, |
| 891 | ); |
| 892 | |
| 893 | // Group the flat column list into one entry per table, preserving order. |
| 894 | const tableMap: Map<string, JSONArray> = new Map(); |
| 895 | |
| 896 | for (const row of columnRows) { |
| 897 | const columns: JSONArray = tableMap.get(row.table_name) || []; |
| 898 | columns.push({ |
| 899 | name: row.column_name, |
| 900 | type: row.data_type, |
| 901 | nullable: row.is_nullable === "YES", |
| 902 | default: redactDefaultExpression(row.column_default), |
| 903 | maxLength: toNumberOrNull(row.character_maximum_length), |
| 904 | }); |
| 905 | tableMap.set(row.table_name, columns); |
| 906 | } |
| 907 | |
| 908 | const tables: JSONArray = []; |
no test coverage detected