/** * Returns all instances managed by $clusterId * * @param \DreamFactory\Enterprise\Database\Models\Cluster|int $clusterId * @param array $columns The columns to retrieve * * @return \Illuminate\Support\Collection */ protected static function findClusterInstances($clusterId, $columns = ['*']) { return Instance::where('cluster_id', static::findCluster($clusterId)->id)->orderBy('instance_id_text')->get($columns); }
/** * Generates a unique db-name/user/pass for MySQL for an instance * * @param Instance $instance * * @return array * @throws SchemaExistsException */ protected function generateSchemaCredentials(Instance $instance) { $_tries = 0; $_dbUser = null; $_dbName = $this->generateDatabaseName($instance); $_seed = $_dbName . env('APP_KEY') . $instance->instance_name_text; // Make sure our user name is unique... while (true) { $_baseHash = sha1(microtime(true) . $_seed); $_dbUser = substr('u' . $_baseHash, 0, 16); if (0 == Instance::where('db_user_text', '=', $_dbUser)->count()) { $_sql = 'SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = :schema_name'; // Make sure the database name is unique as well. /** @noinspection PhpUndefinedMethodInspection */ $_names = DB::select($_sql, [':schema_name' => $_dbName]); if (!empty($_names)) { throw new SchemaExistsException('The schema "' . $_dbName . '" already exists.'); } break; } if (++$_tries > 10) { throw new \LogicException('Unable to locate a non-unique database user name after ' . $_tries . ' attempts.'); } // Quick snoozy and we try again usleep(500000); } $_creds = ['database' => $_dbName, 'username' => $_dbUser, 'password' => sha1(microtime(true) . $_seed . $_dbUser . microtime(true))]; return $_creds; }