ITR database model

From ITR Wiki

The ITR keeps all its data in PostgreSQL. Every organisation gets a database of its own, and one master database holds what the whole server shares. This keeps the data of each organisation neatly apart, while tests, reports and screen templates can still be shared by everyone.

This page describes the databases, the tables, what is stored where and how the databases are created and kept up to date. It is written for developers and administrators. The JavaScript objects that are stored in these tables are described on ITR data model. The code is leading: when this page and the code differ, the code is right.

The master database and the customer databases

The databases

Database Name What it holds
Master database <prefix>Master Everything the server shares: organisations, logins, credits, central tests, reports and screen templates, research data and the server settings.
Customer database <prefix>{<company id>} Everything of one organisation: candidates, sessions, answers, scores, generated reports, the audit trail and its own tests, reports and screen templates.

The prefix is the setting DBPREFIX in application.cfg (see Server settings and tools), for example ITR. The company id is the ID of the organisation in the table SecurityCompanies, with curly braces around it. An organisation with ID 5f0c… on a server with prefix ITR therefore has the database ITR{5f0c…}.

All databases have exactly the same tables. Which tables are actually used depends on the database, as described below. Because every database has the same structure, the same code works on all of them, and a single migration script updates them all.

The tables

The 23 tables fall into a few groups. The name tells you the group: Client… for the work of an organisation, Security… for organisations, logins and credits, and System… for settings.

Content

Table Holds Object in the web client
Tests Test definitions: screens, scales, norms and more ITSTest
Reports Report definitions ITSReport
TestScreenTemplates Screen templates ITSScreenTemplate
ClientBatteries Batteries: fixed sets of tests and reports ITSBattery

Candidates and sessions

Table Holds Object in the web client
ClientPersons Candidates and other persons ITSCandidate
ClientSessions Individual, group and public sessions ITSCandidateSession
ClientSessionTests One test in a session, with the answers (Results) and the scores (Scores) ITSCandidateSessionTest
ClientGeneratedReports Reports that were saved after editing ITSCandidateSessionGeneratedReport
ClientGroups Groups of persons
ClientGroupMember The persons in a group
ClientOrganisations Organisations that candidates belong to
ClientEducations The list of education levels
ClientNationalities The list of nationalities
ClientAuditLog The audit trail: who changed what and when

Organisations, logins and credits

Table Holds Object in the web client
SecurityCompanies Organisations on the server, with their credit balance ITSCompany
SecurityUsers Logins and their rights ITSUser
SecurityWebSessionTokens The tokens of logged in users
SecurityTemplates Templates with rights for new users
SecurityCreditGrants Credits given to an organisation ITSCreditGrant
SecurityCreditUsage Credits used, one line per finished test
SecurityDataGathering Anonymised results for research

Settings

Table Holds
SystemParam Settings as name and value pairs, including DBVersion, the version of the database
SystemTranslations Part of the schema, but not used: translations are kept in files, see ITR file formats

What is stored where

Every class in the API states whether, and for whom, its table in the master database may be read next to the table in the customer database:

Setting Tables Meaning
for all users Tests, TestScreenTemplates The central tests and screen templates are available to everyone, including candidates while they take a test.
for all regular office users Reports, ClientBatteries, ClientEducations, ClientNationalities, SecurityTemplates Office users see the central reports, batteries and lists next to their own.
for master users SecurityCompanies, SecurityUsers, SecurityWebSessionTokens, SecurityCreditGrants, SecurityCreditUsage, SecurityDataGathering, ClientAuditLog, ClientOrganisations, SystemParam Only master users (server administrators) work with these tables in the master database.
for nobody ClientPersons, ClientSessions, ClientSessionTests, ClientGroups, ClientGroupMember, ClientGeneratedReports and the views These tables are only read from the database of the organisation.

In practice this means:

  • Logins, tokens, organisations and the credit balance are always in the master database. Every user, including a candidate, has a line in SecurityUsers there, with the ID of the organisation in CompanyID. When a candidate gets a login, the line in SecurityUsers has the same ID as the person in ClientPersons.
  • Candidates, sessions, answers, scores, generated reports and the audit trail are always in the database of the organisation.
  • Tests, reports, screen templates and batteries can be in both. The ones in the master database are the central ones, installed for example from GitHub (see ITR file formats). The ones in the customer database belong to that organisation.

How the API picks the database

The API takes the organisation from the login token. A request then works on the database of that organisation, unless the web client asks for the master database with a header:

  • IncludeMaster: Y adds the lines from the master database to a list, as far as the table allows it (see the table above);
  • IncludeClient: N leaves out the lines of the own database;
  • MASTER: Y works on the master database only, which is reserved for master users.

In a combined list every line has an extra column dbsource: 0 for a line from the own database and 1 for a line from the master database. This is how the editors know whether an item is central or your own.

Public sessions

A public session with a short login code (session types 200 and 1200) is also saved in the master database. When someone logs in with only the short code, the ITR does not know the organisation yet, so it looks up the code in the master database and continues in the right customer database.

JSON in text columns

Many objects of the web client have lists and nested objects, such as the screens of a test or the answers of a session test. The tables keep these as JSON text in a single column. The API passes these columns on unchanged ("pass through fields"), so the web client reads and writes them as it pleases. The ITR data model describes what is inside.

Table JSON columns
Tests screens, scales, norms, documents, scoreRules, graphs, media, files, RequiredParsPerson, RequiredParsSession, RequiredParsGroup, RequiredParsOrganisation, PluginData
ClientSessionTests Results, Scores, PluginData
Reports ReportGraphs, PluginData
TestScreenTemplates TemplateVariables, PluginData
ClientBatteries BatteryTests, BatteryReports, PluginData
SecurityCompanies AdditionalPersonFields, AdditionalGroupFields, AdditionalSessionFields, PluginData
Most other tables PluginData

PluginData is the general place for extra information. A new feature can store its data there without a change to the database.

Relations between tables

The tables refer to each other by ID. A few of these relations are declared as foreign keys in the database:

From To
ClientSessionTests.SessionID ClientSessions
ClientGroupMember.ID ClientGroups
ClientGroupMember.PersonID ClientPersons
SecurityUsers.CompanyID SecurityCompanies
SecurityCreditGrants.CompanyID SecurityCompanies
SecurityCreditUsage.CompanyID SecurityCompanies

All other relations are kept by the API and the web client, for example ClientSessions.PersonID to a person and ClientSessionTests.TestID to a test. This is deliberate: a test can live in the master database while the session that uses it lives in the customer database, and a foreign key cannot cross from one database to another.

Views

Four views make common lists fast to read:

View Shows
ViewClientSessionsWithPerson Sessions together with the person that takes them
ViewClientSessionTestsWithPerson Session tests together with the person and the session
viewclientgroupsessions Group sessions (session type 100), with the number of candidates that are ready to start, in progress and done
viewclientgroupsessioncandidates The candidates of each group session, with the status of their own session

When a test is finished

As soon as a session test reaches status 30 or higher (finished), the API does a few things on its own:

  1. It makes an anonymised copy of the session test: the answers of components marked Anonimise are replaced by *ANONIMISED*. The session test itself keeps the answers.
  2. A line is added to the audit trail (ClientAuditLog) of the organisation.
  3. The anonymised copy, together with selected details of the person (such as age and sex), the group, the session and the test, is saved in SecurityDataGathering, in the customer database and in the master database. A later change of the same test in the same session updates that line.
  4. If the test has not been billed yet, a line is added to SecurityCreditUsage in the customer database and an anonymised line in the master database. The credits are deducted from the balance of the organisation in SecurityCompanies, and the session test is marked as billed, so it is never billed twice.

See also Credits.

Settings in the database

Settings are first taken from application.cfg and from environment variables. Many settings can also be stored in the table SystemParam. For an organisation the ITR looks, in this order:

  1. in the settings of the consultant (PluginData of the user), when given;
  2. in SystemParam of the customer database;
  3. in SystemParam of the master database, when the setting may come from there;
  4. in application.cfg or the environment.

The first value that is not empty is used.

Creation and updates

The databases look after themselves. The first time a process of the API uses a database, it reads DBVersion from SystemParam:

  • If the database does not exist yet, the API creates it with the script CREATEDB.SQL and then runs all update scripts from the start. A new organisation therefore gets its database the first time it is used.
  • The update scripts are called MIGRATEDB1.SQL, MIGRATEDB2.SQL and so on, in the folder database of the API (or the folder in the setting SQLPATH). Starting from the current version, the API runs every next script until there is no next file. Every script ends by raising DBVersion.
  • Each script runs as one transaction. Statements are separated by an empty line, and lines that start with -- or // are skipped.
  • The scripts can use placeholders that the API fills in, such as %DBNAME%, %TABLESPACE% and %NEWCOMPANYID%.

There are 34 update scripts, so an up to date database has DBVersion 35.

Because every database is updated by the same scripts, all databases on a server always have the same structure. The scripts only add: new tables, new columns, new views and new settings. Nothing is removed, so older data and older content always keep working.

Adding something to the database

To add a column or table:

  1. Add a new script MIGRATEDB<n>.SQL with the next number, with the change and, at the end, the update of DBVersion to n + 1. Separate the statements with an empty line.
  2. Add the column to the class in ITSRestAPIORM.py.
  3. If the column holds JSON, add it to pass_through_fields in ITSRestAPIORMExtensions.py.
  4. Add the property to persistentProperties of the object in the web client, see ITR data model.

Every database picks up the change on its own the next time it is used.

Where to go next