SkybuDocs

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.

skill://data/query-data-safelyAgenter leser den med resources/read eller skills_read. Den er skrevet på engelsk, for agentene.

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

  1. list_database_fields to see the tables, columns and the tag on each.
  2. Write one SELECT that asks for what you need, preferably counts and sums rather than raw rows.
  3. query_database. Read columns to see how each column was shown, and note if something was hidden.
  4. 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.

TagHoldsShown by query_database as
namePeople's namesPERSON_1, PERSON_2, …
national_idFødselsnumreFNR_1, …
contactEmails, phone numbersEPOST_1, TLF_1, or KONTAKT_1 for anything else
free_textComments, notes, messagesThe text, with emails, fødselsnumre and phone numbers replaced
noneNot personal dataThe value, with emails and fødselsnumre replaced
no tagUnknownSKJULT (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, VALUES or TABLE, 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 with WHERE or a LIMIT.
  • At most 200 rows. truncated: true means 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_1 may be someone else, and you cannot put a code in a WHERE.
  • Computed columns show numbers and hide text. count(*), sum(…), avg(…) and extract(…) come through. upper(name), concat(…), date_trunc(…) or a cast to date are hidden, because Skybu cannot tell where the value came from. To group by month, select extract(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, JOIN and GROUP BY can 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 get database_not_ready.
  • For your own reads use this tool. The app's code uses DATABASE_URL_READONLY or DATABASE_URL (skill://databases/use-the-database), and those see everything unmasked.
  • skybu db fields <db> and skybu db query <db> "select …" do the same from the CLI.