Developing Databases
This is a quick guide about developing classes interacting with MySQL databases. DIRAC supports also Elasticsearch/OpenSearch NoSQL database, but it is not part of this document.
Before starting developing databases, you have to make sure that MySQL is installed, as well as python-mysql, as explained in Editing DIRAC code, and make sure that MySQL service is on.
Develop the database
To develop a new database structure it requires to design a database schema and develop the python database class that will interact with the database itself.
The DIRAC.Core.Base.DB
module provides a base class for defining and interacting with MySQL DBs.
The example that follows make use of it.
There is also the possibility to define the DB using sqlalchemy python package, (and some DIRAC DBs do indeed that)
but this is not covered in this document.
A simple example of the python class of a database follows:
1""" A test DB in DIRAC, using MySQL as backend
2"""
3from DIRAC.Core.Base.DB import DB
4
5
6class AtomDB(DB):
7 def __init__(self):
8 DB.__init__(self, "AtomDB", "Test/AtomDB")
9 retVal = self.__initializeDB()
10 if not retVal["OK"]:
11 raise Exception(f"Can't create tables: {retVal['Message']}")
12
13 def __initializeDB(self):
14 """
15 Create the tables
16 """
17 retVal = self._query("show tables")
18 if not retVal["OK"]:
19 return retVal
20
21 tablesInDB = [t[0] for t in retVal["Value"]]
22 tablesD = {}
23
24 if "atom_mytable" not in tablesInDB:
25 tablesD["atom_mytable"] = {
26 "Fields": {"Id": "INTEGER NOT NULL AUTO_INCREMENT", "Stuff": "VARCHAR(64) NOT NULL"},
27 "PrimaryKey": ["Id"],
28 }
29
30 return self._createTables(tablesD)
31
32 def addStuff(self, something):
33 return self.insertFields("atom_mytable", ["stuff"], [something])
Let’s break down the example. The first two lines are simple includes required. Then the class definition. The name of the class should be the same name as the file where it is.
So AtomDB should be in AtomDB.py. The class should inherit from the DB class. The DB class includes all the methods necessary to access, query, modify… the database.
The first line in the __init__
method should be the initialization of the parent (DB) class. That initialization requires 2 or 3 parameters:
Logging name of the database. This name will be used in all the logging messages generated by this class.
Full name of the database. With System/Name. So it can know where in the CS look for the initialization parameters. In this case it would be /Systems/Test/<instance name>/Databases/AtomDB.
Boolean for the debug flag
After the initialization of the DB parent class we call our own __initializeDB
method.
This method (following __init__
in the example) first retrieves all the tables already in the database.
Then for each table that has not yet been created then it creates a definition of the table and creates all the missing tables.
Each table definition includes all the fields with their value type, primary keys, extra indexes…
By default all tables will be created using the InnoDB engine.
The addStuff method simply inserts into the created table the argument value.
Configure the database access
The last step is to configure the database credentials for DIRAC to be able to connect. In our previous example the CS path was /Systems/Test/<instance name>/Databases/AtomDB. That section should contain:
Systems
{
Test
{
<instance name>
{
Databases
{
AtomDB
{
Host = localhost
User = yourusername
Password = yourpasswd
DBName = yourdbname
}
}
}
}
In a production environment, the “Password” should be defined in a non-accessible file, while the rest of the configuration can go in the central Configuration Service.
If you encounter any problem with sockets, you should replace “localhost” (DIRAC/Systems/Test/<instance name>/AtomDB/Host) by 127.0.0.1.
Keep in mind that <instance name> is the name of the instance defined under /DIRAC/Setups/<your setup>/Test and <your setup> is defined under /DIRAC/Setup.
Once that is defined you’re ready to go.
Trying the database from the command line
You can try to access the database by doing:
from DIRAC.TestSystem.DB.AtomDB import AtomDB
try:
atomdb = AtomDB()
except Exception:
print("Oops. Something went wrong...")
raise
result = atomdb.addStuff('something')
if not result['OK']:
print("Error while inserting into db:", result['Message']) # Here, in DIRAC, you better use the gLogger
else:
print(result['Value']) # Here, in DIRAC, you better use the gLogger