Example: Applying a migration to a specific Galaxies Site

There will be some database changes that only need to be applied to a specific Galaxies site, for example where a development stores data in a custom table and is only required to be present on a single site.

Where the ID of the Galaxies site is known and consistent across all environments (likely only to be the case if using a migration to create the site) then you can connect directly to the required site within your migration, steps are as follows:

  1. Read galaxies database credentials from config/system.xml - db_galaxies_username and db_galaxies_password
  2. Create a connection using DriverManager::getConnection(), passing in the site’s database name as the dbname connection parameter
  3. Use the DBAL\Connection object to execute queries directly on the Galaxies site’s database

You may wish to call the migration’s addSql() method and pass it a comment (starting with ‘--') to prevent Doctrine reporting that no changes were made.

If the site ID is not known, you will need to identify the site by name. The recommended way to do this is to fetch all values of siteName and dbName from the JaduSiteRegistry and iterate over them until a row with the correct siteName is found. You can then connect to that database and apply the change using the three steps above.

Examples

Example method to read galaxies database credentials:

    /**
     * Tries to get galaxies credentials either from variables or system files.
     *
     * @return array|bool
     */
    private function getGalaxyCredentials()
    {
        if (!is_null($this->GUusername)) {
            return [
                'username' => $this->GUusername,
                'password' => $this->GUpassword,
            ];
        }

        $homeDir = $this->version->getConfiguration()->getJaduPath();

        if (!file_exists($homeDir . '/config/system.xml')) {
            return false;
        }

        $systemConfig = simplexml_load_file($systemXML);
        if (!$systemConfig) {
            return false;
        }
        $this->GUusername = (string) $systemConfig->db_galaxies_username;
        $this->GUpassword = (string) $systemConfig->db_galaxies_password;
        if (is_null($this->GUusername)) {
            return false;
        }

        return $this->getGalaxyCredentials();
    }

Example method to list all records in the JaduSiteRegistry:

    /**
     * Gets all galaxies from the main site registry table.
     */
    private function getGalaxyDBs()
    {
        if (!is_null($this->galaxyNames)) {
            return $this->galaxyNames;
        }
        $fetchMicroDbsSql = "SELECT DISTINCT dbname, siteName FROM JaduSiteRegistry WHERE isMainSite='0' ";
        $this->galaxyNames = $this->connection->fetchAll($fetchMicroDbsSql);

        return $this->getGalaxyDBs();
    }

Example method to create galaxies db connection:

    /**
     * Tries to get a Connection object based upon credentials for each galaxy.
     *
     * @param $database
     *
     * @return bool|Connection
     *
     * @throws \Doctrine\DBAL\DBALException
     */
    private function connectDb($database)
    {
        $config = new Configuration();
        $galaxyCredentials = $this->getGalaxyCredentials();
        if (!is_array($galaxyCredentials)) {
            return false;
        }
        $connectionParams = [
            'dbname' => $database,
            'user' => $galaxyCredentials['username'],
            'password' => $galaxyCredentials['password'],
            'host' => $this->connection->getHost(),
            'port' => $this->connection->getPort(),
            'driver' => $this->connection->getDriver()->getName(),
        ];

        return DriverManager::getConnection($connectionParams, $config);
    }

results matching ""

    No results matching ""