Repository: dmwm/wmcore
Issue: Wrong logic in setting database parameters in ExecuteDAO

**Impact of the bug**
Standalone script ExecuteDAO.py

**Describe the bug**
While working with the standalone script ExecuteDAO, I found out that there is a bug in the logic of how the database parameters were read from all the possible sources. The script tries them in the following order:
* command line parameters 
* agent configuration 
* environment

The full set of parameters needed to create a database connection is:
 * url
 * dialect
 * socket

From which only the first two are mandatory, and the socket is optional. If one of them is missing and not found in either of the sources described above the script fails with an error [1]  


**How to reproduce it**
Just execute e.g.:
```
python3 ../ExecuteDAO.py -c /data/srv/wmagent/current/config/wmagent/config.py -p WMCore.WMBS -m Monitoring.JobTypeCountByState -- created
```

**Expected behavior**
Provided that at least the database URL is present to try to figure out all the rest of the parameters from there and not fail. 

**Additional context and error message**
[1]
```
python3 ../ExecuteDAO.py -c /data/srv/wmagent/current/config/wmagent/config.py -p WMCore.WMBS -m Monitoring.JobTypeCountByState -- created
2022-10-13 14:02:21,699:INFO:ExecuteDAO: configFile: /data/srv/wmagent/current/config/wmagent/config.py
2022-10-13 14:02:21,700:INFO:ExecuteDAO: Loading configFile: /data/srv/wmagent/current/config/wmagent/config.py
Traceback (most recent call last):
  File "../ExecuteDAO.py", line 291, in <module>
    main()
  File "../ExecuteDAO.py", line 285, in main
    daoObject = ExecuteDAO(logger=logger, configFile=configFile,
  File "../ExecuteDAO.py", line 167, in __init__
    raise RuntimeError("You must set proper DATABASE parameters: connectUrl, dialect, socket!")
RuntimeError: You must set proper DATABASE parameters: connectUrl, dialect, socket!

```

Changed files:
src/python/WMCore/Database/ExecuteDAO.py

diff --git a/src/python/WMCore/Database/ExecuteDAO.py b/src/python/WMCore/Database/ExecuteDAO.py
index 397ef4f22..ea21405f6 100644
--- a/src/python/WMCore/Database/ExecuteDAO.py
+++ b/src/python/WMCore/Database/ExecuteDAO.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 Script to create a dedicated DAOFactory and execute a single DAO.
@@ -111,7 +111,7 @@ class ExecuteDAO():
     A generic class to create the DAO Factory and execute the DAO module.
     """
     def __init__(self, logger=None, configFile=None,
-                 connectUrl=None, dialect=None, socket=None,
+                 connectUrl=None, socket=None,
                  package=None, daoModule=None):
         """
         __init__
@@ -120,7 +120,6 @@ class ExecuteDAO():
         :param package: The Package from which the DAO factory to be initialised.
         :param configFile: Path to WMAgent configuration file.
         :param connectUrl: Database connection URL (overwrites the connectUrl param from configFile if both present)
-        :param dialect: Database connection URL (overwrites the dialect param from configFile if both present)
         :param socket: Database connection URL (overwrites the socket param from configFile if both present)
         :param module: The DAO module to be executed.
         """
@@ -147,9 +146,6 @@ class ExecuteDAO():
         if connectUrl is not None:
             config.CoreDatabase.connectUrl = connectUrl
 
-        if dialect is not None:
-            config.CoreDatabase.dialect = dialect
-
         if socket is not None:
             config.CoreDatabase.socket = socket
 
@@ -160,16 +156,22 @@ class ExecuteDAO():
             config.CoreDatabase.dialect = getBackendFromDbURL(os.getenv("DATABASE"))
             config.CoreDatabase.socket = os.getenv("DBSOCK", None)
 
+        # always try to determine the dialect from the URL
+        if getattr(config.CoreDatabase, "connectUrl", None):
+            config.CoreDatabase.dialect = getBackendFromDbURL(config.CoreDatabase.connectUrl)
+
+        # finally if no socket is provided, set it to None and let WMInit to create it.
+        config.CoreDatabase.socket = getattr(config.CoreDatabase, "socket", None)
+
         # check if all database connection parameters are provided:
         if not all([getattr(config.CoreDatabase, "connectUrl", None),
-                    getattr(config.CoreDatabase, "dialect", None),
-                    getattr(config.CoreDatabase, "socket", None)]):
+                    getattr(config.CoreDatabase, "dialect", None)]):
             raise RuntimeError("You must set proper DATABASE parameters: connectUrl, dialect, socket!")
 
         # Connecting to database:
         self.init.setDatabaseConnection(config.CoreDatabase.connectUrl,
                                         config.CoreDatabase.dialect,
-                                        config.CoreDatabase.socket)
+                                        socketLoc=config.CoreDatabase.socket)
 
         self.dbi = myThread.dbi
         self.package = package