Ferdigheter · Data og personvern
Query data safely
Look at the rows in a project's database with read-only SELECT queries, with names, fødselsnumre and contact details replaced by codes.
query_database lets you look at real data to debug, count or explain something, without seeing who it is about. It runs as the database's read-only role, and before the rows reach you Skybu replaces personal data with codes according to tags that people in the organisation have set on each column. Columns nobody has tagged are hidden.
In short
list_database_fieldsto see the tables, columns and the tag on each.- Write one SELECT that asks for what you need, preferably counts and sums rather than raw rows.
query_database. Readcolumnsto see how each column was shown, andnoteif something was hidden.- If a column you need is hidden, tell the user which one and ask them to tag it in the dashboard.
query_database needs the data:read permission. If the tool is missing from your tool list, the connection was not given it; the user can reconnect the agent and tick it.
Step 1: see the fields
{ "name": "list_database_fields", "arguments": { "databaseId": "<database id>" } }
You get each table (as schema.table) with its columns, their types and tag, plus how many are untagged. Only tables the read-only role can read are listed.
| Tag | Holds | Shown by query_database as |
|---|---|---|
name | People's names | PERSON_1, PERSON_2, … |
national_id | Fødselsnumre | FNR_1, … |
contact | Emails, phone numbers | EPOST_1, TLF_1, or KONTAKT_1 for anything else |
free_text | Comments, notes, messages | The text, with emails, fødselsnumre and phone numbers replaced |
none | Not personal data | The value, with emails and fødselsnumre replaced |
| no tag | Unknown | SKJULT (hidden) |
Only people tag fields, on the database's page in the dashboard; an agent cannot. Do not suggest you will tag them.
Step 2: run the query
{
"name": "query_database",
"arguments": {
"databaseId": "<database id>",
"sql": "select status, count(*) as orders, sum(total) as amount from public.orders group by status order by orders desc"
}
}
The rules:
- One statement, starting with
SELECT,WITH,VALUESorTABLE, at most 5000 characters. A trailing;is fine; a second statement fails. - It runs in a read-only transaction as the read-only role, so nothing can be changed.
- At most 5 seconds. A slow query fails with
query_failed; narrow it withWHEREor aLIMIT. - At most 200 rows.
truncated: truemeans there were more.
The answer:
{
"columns": [{ "name": "navn", "source": "public.kunde.navn", "tag": "name", "shown": "coded" }, …],
"rows": [{ "navn": "PERSON_1", "ordrer": 12 }, …],
"truncated": false,
"replaced": { "PERSON": 20 },
"note": "Columns without a personal-data tag are hidden as SKJULT. …"
}
shown is coded, scanned (shown, with personal data in the text replaced), hidden, or computed.
Writing useful queries
- Codes are per answer. The same value gets the same code within one answer, so you can see that two rows belong to the same person. In the next query
PERSON_1may be someone else, and you cannot put a code in aWHERE. - Computed columns show numbers and hide text.
count(*),sum(…),avg(…)andextract(…)come through.upper(name),concat(…),date_trunc(…)or a cast todateare hidden, because Skybu cannot tell where the value came from. To group by month, selectextract(year from created_at) as year, extract(month from created_at) as month. - Select the column itself to keep its tag: plain column references are traced back to their table, also through subqueries and
WITH. - Aggregate first. Most questions ("how many orders failed yesterday", "which status is most common") need counts, not rows about people.
WHERE,JOINandGROUP BYcan use any column; only what is returned is masked. Do not use that to work out who a row is about, for example by filtering on a name or a number until one row is left.
When a column is hidden
Tell the user which columns came back as SKJULT and what you wanted them for, and suggest a tag if it is obvious (orders.status is probably none). They decide: a person tags it on the database's page at https://platform.skybu.no, and every change is logged. Until then, work with what is shown; do not try to get the value another way, such as reading credentials and connecting directly.
Things to know
- Masking is an extra layer, not a guarantee. A name written in a free-text column gets through. If you see personal data, do not repeat it; tell the user that the column may need another tag.
- Every query is written to the organisation's audit log, with its SQL, the number of rows and what was replaced.
- The database must be
ready; during a restore you getdatabase_not_ready. - For your own reads use this tool. The app's code uses
DATABASE_URL_READONLYorDATABASE_URL(skill://databases/use-the-database), and those see everything unmasked. skybu db fields <db>andskybu db query <db> "select …"do the same from the CLI.