Пример #1
0
 protected function setupPgsqlDatabase(DbTool $db)
 {
     try {
         $db->connectToDb();
         $this->log(mt('setup', 'Successfully connected to existing database "%s"...'), $this->data['resourceConfig']['dbname']);
     } catch (PDOException $_) {
         $db->connectToHost();
         $this->log(mt('setup', 'Creating new database "%s"...'), $this->data['resourceConfig']['dbname']);
         $db->exec(sprintf("CREATE DATABASE %s WITH ENCODING 'UTF-8'", $db->quoteIdentifier($this->data['resourceConfig']['dbname'])));
         $db->reconnect($this->data['resourceConfig']['dbname']);
     }
     if (array_search(reset($this->data['tables']), $db->listTables(), true) !== false) {
         $this->log(mt('setup', 'Database schema already exists...'));
     } else {
         $this->log(mt('setup', 'Creating database schema...'));
         $db->import($this->data['schemaPath'] . '/pgsql.schema.sql');
     }
     if ($db->hasLogin($this->data['resourceConfig']['username'])) {
         $this->log(mt('setup', 'Login "%s" already exists...'), $this->data['resourceConfig']['username']);
     } else {
         $this->log(mt('setup', 'Creating login "%s"...'), $this->data['resourceConfig']['username']);
         $db->addLogin($this->data['resourceConfig']['username'], $this->data['resourceConfig']['password']);
     }
     $username = $this->data['resourceConfig']['username'];
     if ($db->checkPrivileges($this->data['privileges'], $this->data['tables'], $username)) {
         $this->log(mt('setup', 'Required privileges were already granted to login "%s".'), $this->data['resourceConfig']['username']);
     } else {
         $this->log(mt('setup', 'Granting required privileges to login "%s"...'), $this->data['resourceConfig']['username']);
         $db->grantPrivileges($this->data['privileges'], $this->data['tables'], $this->data['resourceConfig']['username']);
     }
 }