{% import "macros.html" as ui %} {% from "database.html" import code_cell, lint_cell %} {# The page of a database object (`item`, from CatalogStore.object): a server, database, schema, table, view, column, function, trigger..., read from a live database or declared by a file (origin:...): the same page, from the same store, the header telling which (read from the live database / declared in file:line). Folding sections from the general to the specific, those that apply to its kind; what is not known of it, or what later steps of DATABASES fill, says "Data unavailable". Empty cells of a table show "-". Names link to their own pages (in the consultation modal too, as the code's pages do). The Universe opens in the page itself, in its own section, on this object; in the modal, its footer has Close, Universe and Open the page, the only one that leaves for another screen. #} {% set kind = item.kind %} {% set source = "file" if item.declared else "database" %}{# who says what the page shows #} {% set data = item.data %} {% set tabular = kind in ("table", "view", "materialized view", "foreign table") %} {% macro link(path, name) -%}{{ name }}{%- endmacro %} {% macro unavailable(reason) -%}

Data unavailable: {{ reason }}

{%- endmacro %} {% macro marks(found) -%} {% for mark in (found.marks or []) %}{% if mark != "null" %}{{ db_marks[mark] }}{% endif %}{% endfor %} {%- if found.nullable is defined and not found.nullable %}not null{% endif %} {%- endmacro %} {# Sample the table again now (a live one's): the page follows by itself once it is read. #} {% macro sample_again(table) -%} {% if item.read %}
{% endif %} {%- endmacro %} {% macro sampled(found) -%}{{ "{:,}".format(found.rows) }} rows taken at random{% if found.read %} · {{ found.read | local_time(seconds=True) }}{% endif %}{%- endmacro %} {% set drawn = kind in ("server", "database", "schema", "table", "view", "materialized view", "foreign table", "column") %} {% set table_list = "/databases/navigate/level?mode=browse&at=" ~ (item.path | urlencode) %} {% call ui.section("description", "Description", divider=true) %} {% if data.comment %}

{{ data.comment }}

{% else %}

The {{ source }} holds no comment on it.

{% endif %} {% endcall %} {% if drawn %} {# The Universe opened on it, drawn only once its section opens (Universe in the header or the footer). #} {% call ui.section("class", "Universe", name="universe", divider=true, open=false) %} {# Drawn from the live databases, or from what this object's file declares. #}
{{ ui.universe_canvas(item.path, "") }}
{% endcall %} {% endif %} {% if kind in ("server", "database", "schema") %} {# What it holds, by kind, each a link. #} {% for group_kind, children in item.children | groupby("kind") %} {% call ui.section("attributes", group_kind ~ "s", count=children | length, name="holds-" ~ group_kind, divider=true) %} {% endcall %} {% endfor %} {% endif %} {% if tabular %} {% set columns = item.children | selectattr("kind", "equalto", "column") | list %} {% call ui.section("attributes", "Columns", count=columns | length, name="columns", divider=true) %} {% if columns %} {% for column in columns %} {% set column_marks = marks(column.data) | trim %} {% endfor %}
NameTypeDefaultMarksComment
{{ link(column.path, column.name) }}{% if column.data.type %}{{ column.data.type }}{% else %}-{% endif %} {% if column.data.default %}{{ column.data.default }}{% elif column.data.computed %}{{ column.data.computed }} computed{% else %}-{% endif %}{{ column_marks if column_marks else "-" }}{{ column.data.comment or "-" }}
{% else %}

No columns read.

{% endif %} {% endcall %} {% if kind != "view" and kind != "materialized view" %} {% call ui.section("attributes", "Keys and constraints", name="keys", divider=true) %}
Primary key
{% if data.primary_key %}{{ data.primary_key.columns | join(", ") }}{% if data.primary_key.name %} {{ data.primary_key.name }}{% endif %}{% else %}none{% endif %}
{% for key in data.foreign_keys or [] %}
Foreign key
{{ key.columns | join(", ") }} → {{ link(key.target, db_place(key.target)) }} ({{ key.target_columns | join(", ") }}) {% for option, value in key.options.items() %} {{ option | replace("on", "ON ") | upper }} {{ value }}{% endfor %}{% if key.name %} {{ key.name }}{% endif %}
{% endfor %} {% for unique in data.unique or [] %}
Unique
{{ unique.columns | join(", ") }}{% if unique.name %} {{ unique.name }}{% endif %}
{% endfor %} {% for check in data.checks or [] %}
Check
{{ check.check }}{% if check.name %} {{ check.name }}{% endif %}
{% endfor %}
{% endcall %} {% call ui.section("attributes", "Indexes", count=(data.indexes or []) | length, name="indexes", divider=true) %} {% if data.indexes %} {% else %}

No indexes.

{% endif %} {% endcall %} {% endif %} {% endif %} {% if kind == "column" %} {% call ui.section("attributes", "Type and constraints", name="type", divider=true) %}
Type
{{ data.type }}
Null
{% if data.nullable %}allowed{% else %}not null{% endif %}
{% if data.default %}
Default
{{ data.default }}
{% endif %} {% if data.computed %}
Computed
{{ data.computed }}
{% endif %} {% if data.identity %}
Identity
its values are generated by the database
{% endif %}
Marks
{{ marks(data) or "none" }}
{% if data.position %}
Position
{{ data.position }}
{% endif %}
{% endcall %} {% endif %} {% if kind not in ("server", "database", "schema", "table", "view", "materialized view", "foreign table", "column") %} {# A function, procedure, trigger, sequence, type, synonym...: what was read of it. #} {% call ui.section("attributes", "Properties", name="properties", divider=true) %}
{% for key, value in data.items() if key not in ("comment", "definition") %}
{{ key | replace("_", " ") }}
{% if value is string and value.startswith(("db:", "origin:")) %}{{ link(value, db_place(value)) }}{% elif value is sameas true or value is sameas false %}{{ "yes" if value else "no" }}{% elif value is sequence and value is not string %}{{ value | join(", ") }}{% else %}{{ value }}{% endif %}
{% else %}
Nothing else was read of it.
{% endfor %}
{% endcall %} {% endif %} {% if kind not in ("server", "database", "schema") %} {% call ui.section("class", "Relations", count=item.relations | length, name="relations", divider=true) %} {% for group in item.groups %}

{{ group.kind }} {% if group.direction == "out" %}→{% else %}←{% endif %} {{ group["items"] | length }}

{% else %}

No relations read.

{% endfor %} {% endcall %} {% endif %} {% if tabular %} {% set triggers = item.children | selectattr("kind", "equalto", "trigger") | list %} {% call ui.section("function", "Behaviour", count=triggers | length, name="behaviour", divider=true) %} {% if triggers %} {% else %}

No triggers.

{% endif %} {{ unavailable("rules, row security policies and grants are not read yet.") }} {% endcall %} {% call ui.section("attributes", "Physical structure", name="physical", divider=true) %}
{% if data.partition_of %}
Partition of
{{ link(data.partition_of, db_place(data.partition_of)) }}{% if data.partition_bound %} {{ data.partition_bound }}{% endif %}
{% endif %} {% if data.inherits %}
Inherits
{{ link(data.inherits, db_place(data.inherits)) }}
{% endif %} {% if data.partitioning %}
Partitioned by
{{ data.partitioning }}
{% endif %} {% for part in data.partitions or [] %}
Partition
{{ part.name }} {{ part.bound or "" }}
{% endfor %} {% if data.foreign_target %}
Stands for
{{ link(data.foreign_target, db_place(data.foreign_target)) }} through {{ data.foreign_server }}
{% endif %}
{{ unavailable("tablespace, unlogged and temporal settings are not read yet.") }} {% endcall %} {% call ui.section("attributes", "Numbers", name="numbers", divider=true) %} {% set found = namespace(reason="") %} {% for entry in item.unavailable if "row estimates" in entry.what %}{% set found.reason = entry.reason %}{% endfor %} {% set reason = found.reason %}
Rows (estimate)
{% if data.rows is defined %}{{ "{:,}".format(data.rows) }}{% else %}Data unavailable{% if reason %}: {{ reason }}{% endif %}{% endif %}
Size
{% if data.bytes is defined %}{{ data.bytes | size }}{% else %}Data unavailable{% if reason %}: {{ reason }}{% endif %}{% endif %}
{% endcall %} {% endif %} {% if item.declared_by is defined %} {# The origins (SQL files, migrations) that declare it, for a database they describe, and where they differ. #} {% call ui.section("source", "Declared by", count=(item.declared_by or []) | length, name="declared-by", divider=true) %} {% for d in item.declared_by or [] %} {% else %}

No origin linked to this database declares it.

{% endfor %} {% endcall %} {% endif %} {% if item.found_in is defined %} {# Its mirror, on what a file declares: where it is in the live databases its file describes, and where they differ. #} {% call ui.section("source", "Found in", count=item.found_in | length, name="found-in", divider=true) %} {% for d in item.found_in %} {% else %}

No database its file describes holds it.

{% endfor %} {% endcall %} {% endif %} {% if tabular or kind in ("function", "procedure", "aggregate", "trigger") %} {% call ui.section("source", "Definition", name="definition", divider=true) %} {% if data.definition %}
{{ data.definition }}
{% elif item.declared %}{{ unavailable("the file gives no definition of it but its statements, in its file's Source.") }} {% else %}{{ unavailable("the database gives no definition of it; the files declaring it are in Declared by.") }}{% endif %} {% endcall %} {% endif %} {% if tabular %} {# Its sample, one line per column: the column, and beside it the values taken at random (each once, a sensitive column's masked: each letter and digit as *). #} {% set sample = item.sample %} {% call ui.section("attributes", "Sample", count=(sample.columns or []) | length, name="sample", divider=true) %} {% if sample.unavailable %}{{ unavailable(sample.unavailable) }} {% else %}

{{ sampled(sample) }}; each column's values shown once, at most 20, sensitive ones masked.

{% if sample.rows %} {% for name in sample.columns %}{% set found = sample.profiles[name] %} {% endfor %}
ColumnSample
{{ link(item.path ~ "." ~ name, name) }}{% if found.sensitive %} sensitive{% endif %} {% for value in found["values"] %}{{ value }}{% else %}-{% endfor %}
{% else %}

The table holds no rows.

{% endif %} {% endif %} {{ sample_again(item.path) }} {% endcall %} {% endif %} {% if kind == "column" %} {# What its sampled values show: the profile, a sensitive column's values masked. #} {% set found = item.profile %} {% call ui.section("attributes", "Data", name="data", divider=true) %} {% if found.unavailable %}{{ unavailable(found.unavailable) }} {% else %}
Sample
{{ sampled(found) }}
Sensitive
{% if found.sensitive %}yes, by its {{ found.sensitive | replace("format:", "values' format:") }}: its values are masked{% else %}no{% endif %}
Nulls
{{ "{:,}".format(found.nulls) }}{% if found.rows %} ({{ (100 * found.nulls / found.rows) | round(1) }}%){% endif %}
Distinct
{{ "{:,}".format(found.distinct) }}
Smallest
{% if found.min is not none %}{{ found.min }}{% else %}-{% endif %}
Largest
{% if found.max is not none %}{{ found.max }}{% else %}-{% endif %}
Length
{% if found.min_length is not none %}{{ found.min_length }} to {{ found.max_length }} characters{% else %}-{% endif %}
Most frequent
{% for value, count in found.frequent %}{{ value }} {{ count }}×{% if not loop.last %}, {% endif %}{% else %}-{% endfor %}
Formats
{% for name, count in found.formats.items() %}{{ name }} {{ count }}{% if not loop.last %}, {% endif %}{% else %}-{% endfor %}
Values
{% for value in found["values"] %}{{ value }}{% if not loop.last %} {% endif %}{% else %}-{% endfor %}
{% endif %} {{ sample_again(item.parent) }} {% endcall %} {% endif %} {% if tabular or kind == "column" %} {# The code using it, found by reading the code: an ORM's model declaring it, SQL in the code's strings and in query files, and code naming the model; a table's also its columns'. #} {% call ui.section("warns", "Use in the code", count=item.code_use | length, name="code-use", divider=true) %} {% if item.code_use %}{% call(row, key, value) ui.list_table("code-use", "", item.code_listing, "", interactive=False) %}{{ code_cell(row, key) }}{% endcall %} {% else %}

No code of the project is found using it.

{% endif %} {% endcall %} {% if tabular %}{% call ui.section("warns", "Lint", count=item.lint | length, name="lint", divider=true) %} {% if item.lint %}{% call(row, key, value) ui.list_table("lint-object", "", item.lint_listing, "", interactive=False) %}{{ lint_cell(row, key, value, actions=false) }}{% endcall %}

{{ ui.icon("database") }}Lint lists every finding, to accept one saying why.

{% else %}

The lint finds nothing in it.

{% endif %} {% endcall %}{% endif %} {% call ui.section("warns", "History", name="history", divider=true) %}{{ unavailable("the history from migrations and commits comes in a later step.") }}{% endcall %} {% endif %}