Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy with dynamic connection #271

Merged
merged 34 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2054625
Block Preview styling
boehsermoe Nov 9, 2018
fcd0e42
Tooltip preview fix #246
boehsermoe Nov 13, 2018
715cab9
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Nov 22, 2018
572faf5
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Nov 26, 2018
082c9f6
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe Jan 28, 2019
77570a4
Alternative proxy db connection
boehsermoe Jan 28, 2019
f775595
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe Mar 4, 2019
d66099e
Date plugin with custom format
boehsermoe Mar 4, 2019
5aeeea8
Changelog for #270
boehsermoe Mar 5, 2019
13c52e2
Refactored namespace using
boehsermoe Mar 5, 2019
6a73c46
Merge branch 'master' into proxy-connection
boehsermoe Mar 5, 2019
27ddf48
PHP Doc
boehsermoe Mar 5, 2019
a51f27f
Changelog #271
boehsermoe Mar 5, 2019
5d45200
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Mar 5, 2019
515c98c
Merge branch 'master' into proxy-connection
boehsermoe Mar 5, 2019
10855ac
Update Module.php
boehsermoe Mar 5, 2019
d1f211e
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Apr 10, 2019
c9008aa
Merge branch 'master' into proxy-connection
boehsermoe Apr 10, 2019
330a929
Refactoring proxy connection
boehsermoe Apr 10, 2019
879525f
Merge branch 'proxy-connection' of github.com:boehsermoe/luya-module-…
boehsermoe Apr 10, 2019
0093b25
Refactoring proxy connection
boehsermoe Apr 11, 2019
44f07a0
Refactoring proxy connection
boehsermoe Apr 11, 2019
edc243c
Refactoring proxy connection
boehsermoe Apr 11, 2019
7272ce6
Unit test fixed
boehsermoe Apr 11, 2019
fefa5e3
Unit test fixed
boehsermoe Apr 11, 2019
537400b
Merge branch 'master' into proxy-connection
boehsermoe May 11, 2019
dddb818
Syntax fix
boehsermoe May 15, 2019
1ac5b08
PhpDocs
boehsermoe May 15, 2019
a34ee6a
Merge branch 'master' of https://github.com/luyadev/luya-module-admin…
boehsermoe May 15, 2019
92891a8
PhpDocs
boehsermoe May 15, 2019
74c9275
Refactoring
boehsermoe May 15, 2019
0592ccf
Refactoring
boehsermoe May 20, 2019
e1dc620
Refactoring
boehsermoe May 20, 2019
b30b31e
Refactoring
boehsermoe May 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ This release contains new migrations and requires to run the `migrate` command a
+ [#259](https://github.com/luyadev/luya-module-admin/pull/259) SelectRelationActiveQuery supports related i18n label fields
+ [#253](https://github.com/luyadev/luya-module-admin/pull/253) Added command action to reset password for users via cli.
+ [#270](https://github.com/luyadev/luya-module-admin/pull/270) Custom date format for date plugin in listing.
+ [#271](https://github.com/luyadev/luya-module-admin/pull/271) Proxy with different database connection.

## 1.2.3 (21. November 2018)

Expand Down
10 changes: 8 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ final class Module extends \luya\admin\base\Module implements CoreModuleInterfac
* @since 1.2.0
*/
public $userIdleTimeout = 1800;


/**
* @var string The component/connection name from application.
* @since 2.0.0
*/
public $proxyDbConnection = 'db';
boehsermoe marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var integer The number of rows which should be transferd for each request.
*/
Expand Down Expand Up @@ -237,7 +243,7 @@ public static function onLoad()
'admin' => 'admin.php',
]);
}

/**
* @inheritdoc
*/
Expand Down
30 changes: 24 additions & 6 deletions src/apis/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace luya\admin\apis;

use luya\admin\Module;
use Yii;
use luya\rest\Controller;
use luya\admin\models\ProxyMachine;
use yii\db\Connection;
use yii\di\Instance;
use yii\web\ForbiddenHttpException;
use yii\db\Query;
use luya\admin\models\ProxyBuild;
Expand All @@ -24,18 +27,33 @@
* 4. Generate Build.
* 5. Send build identifier to the client.
*
* @property Module $module
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.0
*/
class ProxyController extends Controller
{
/**
* @var Connection
* @since 2.0.0
*/
protected $db;
nadar marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var array A list of tables which will be ignored and can not be synced with the proxy command.
*/
protected $ignoreTables = [
'migration', 'admin_proxy_build', 'admin_proxy_machine',
];

public function init()
{
$this->db = Instance::ensure($this->module->proxyDbConnection, Connection::class);

parent::init();
}

/**
* Gathers basic informations about the build.
*
Expand All @@ -47,11 +65,11 @@ class ProxyController extends Controller
public function actionIndex($identifier, $token)
{
$machine = ProxyMachine::findOne(['identifier' => $identifier, 'is_deleted' => false]);

if (!$machine) {
throw new ForbiddenHttpException("Unable to acccess the proxy api.");
}

if (sha1($machine->access_token) !== $token) {
throw new ForbiddenHttpException("Unable to acccess the proxy api due to invalid token.");
}
Expand All @@ -64,13 +82,13 @@ public function actionIndex($identifier, $token)
'storageFilesCount' => StorageFile::find()->count(),
];

foreach (Yii::$app->db->schema->tableNames as $table) {
foreach ($this->db->schema->tableNames as $table) {
if (in_array($table, $this->ignoreTables)) {
continue;
}

$schema = Yii::$app->db->getTableSchema($table);
$rows = (new Query())->from($table)->count();
$schema = $this->db->getTableSchema($table);
$rows = (new Query())->from($table)->count('*', $this->db);
$config['tables'][$table] = [
'pks' => $schema->primaryKey,
'name' => $table,
Expand Down Expand Up @@ -165,7 +183,7 @@ public function actionDataProvider($machine, $buildToken, $table, $offset)
$query->orderBy($orders);
}

return $query->all();
return $query->all($this->db);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/commands/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace luya\admin\commands;

use Yii;
use Curl\Curl;
use luya\admin\Module;
use yii\db\Connection;
use yii\di\Instance;
use yii\helpers\Json;
use luya\admin\models\Config;
use luya\admin\proxy\ClientBuild;
Expand Down Expand Up @@ -62,6 +66,8 @@
* ./vendor/bin/luya admin/proxy/clear
* ```
*
* @property Module $module
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.0
*/
Expand Down Expand Up @@ -118,13 +124,18 @@ class ProxyController extends Command
* @var integer Number of requests collected until they are written to the database.
*/
public $syncRequestsCount = 10;

/**
* @var string Database connection (component name) where the data will be stored.
*/
public $db = 'db';

/**
* @inheritdoc
*/
public function options($actionID)
{
return array_merge(parent::options($actionID), ['strict', 'table', 'url', 'idf', 'token', 'syncRequestsCount']);
return array_merge(parent::options($actionID), ['strict', 'table', 'url', 'idf', 'token', 'syncRequestsCount', 'db']);
}

/**
Expand All @@ -142,6 +153,8 @@ public function optionAliases()
*/
public function actionSync()
{
$this->db = Instance::ensure($this->db, Connection::class);

if ($this->url === null) {
$url = Config::get(self::CONFIG_VAR_URL);

Expand Down Expand Up @@ -187,7 +200,7 @@ public function actionSync()

$this->verbosePrint($curl->response);
$response = Json::decode($curl->response);
$build = new ClientBuild($this, [
$build = new ClientBuild($this, $this->db, [
'optionStrict' => $this->strict,
'optionTable' => $this->table,
'syncRequestsCount' => (int)$this->syncRequestsCount,
Expand Down
15 changes: 11 additions & 4 deletions src/proxy/ClientBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace luya\admin\proxy;

use Yii;
use luya\console\Command;
use yii\base\InvalidConfigException;
use luya\helpers\StringHelper;
use yii\base\BaseObject;
use yii\db\Connection;

/**
* Admin Proxy Build.
Expand All @@ -16,6 +16,12 @@
*/
class ClientBuild extends BaseObject
{
/**
* @var \yii\db\Connection
* @since 2.0.0
*/
public $db;
nadar marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var \luya\console\Command $command object
*/
Expand Down Expand Up @@ -55,9 +61,10 @@ public function getOptionTable()
return $this->_optionTable;
}

public function __construct(Command $command, array $config = [])
public function __construct(Command $command, Connection $db, array $config = [])
{
$this->command = $command;
$this->db = $db;
parent::__construct($config);
}

Expand All @@ -83,10 +90,10 @@ public function setBuildConfig(array $config)
}
}

$schema = Yii::$app->db->getTableSchema($tableName);
$schema = $this->db->getTableSchema($tableName);

if ($schema !== null) {
$this->_tables[$tableName] = new ClientTable($this, $tableConfig);
$this->_tables[$tableName] = new ClientTable($this, $tableConfig, ['db' => $this->db]);
}
}
}
Expand Down
63 changes: 47 additions & 16 deletions src/proxy/ClientTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Curl\Curl;
use Yii;
use yii\base\BaseObject;
use yii\db\Connection;
use yii\db\Exception;
use yii\helpers\Console;
use yii\helpers\Json;
Expand All @@ -13,6 +14,7 @@
* Admin Proxy comands Sync Database.
*
* @property \yii\db\TableSchema $schema Schema object
* @property Connection $db Database connection. By default `Yii::$app->db` will be used.
*
* @author Basil Suter <basil@nadar.io>
* @author Bennet Klarhölter <boehsermoe@me.com>
Expand All @@ -24,12 +26,12 @@ class ClientTable extends BaseObject
const LARGE_TABLE_PROMPT = 10000;

private $_data;

/**
* @var \luya\admin\proxy\ClientBuild
*/
public $build;

/**
* @param ClientBuild $build
* @param array $data
Expand All @@ -41,13 +43,42 @@ public function __construct(ClientBuild $build, array $data, array $config = [])
$this->_data = $data;
parent::__construct($config);
}


/**
* @var Connection
* @since 2.0.0
*/
private $_db;

/**
* @return Connection
* @since 2.0.0
*/
public function getDb()
{
if (!$this->_db) {
$this->setDb(Yii::$app->db);
}

return $this->_db;
}

/**
* @param Connection $db
*
* @since 2.0.0
*/
public function setDb(Connection $db)
{
$this->_db = $db;
}

private $_schema;

public function getSchema()
{
if ($this->_schema === null) {
$this->_schema = Yii::$app->db->getTableSchema($this->getName());
$this->_schema = $this->getDb()->getTableSchema($this->getName());
}

return $this->_schema;
Expand Down Expand Up @@ -132,7 +163,7 @@ public function syncData()
$sqlMode = $this->prepare();

try {
Yii::$app->db->createCommand()->truncateTable($this->getName())->execute();
$this->getDb()->createCommand()->truncateTable($this->getName())->execute();

$this->syncDataInternal();
} finally {
Expand All @@ -154,12 +185,12 @@ protected function prepare()
{
$sqlMode = null;

if (Yii::$app->db->schema instanceof \yii\db\mysql\Schema) {
Yii::$app->db->createCommand('SET FOREIGN_KEY_CHECKS = 0;')->execute();
Yii::$app->db->createCommand('SET UNIQUE_CHECKS = 0;')->execute();
if ($this->getDb()->schema instanceof \yii\db\mysql\Schema) {
$this->getDb()->createCommand('SET FOREIGN_KEY_CHECKS = 0;')->execute();
$this->getDb()->createCommand('SET UNIQUE_CHECKS = 0;')->execute();

$sqlMode = Yii::$app->db->createCommand('SELECT @@SQL_MODE;')->queryScalar();
Yii::$app->db->createCommand('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";')->execute();
$sqlMode = $this->getDb()->createCommand('SELECT @@SQL_MODE;')->queryScalar();
$this->getDb()->createCommand('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";')->execute();
}

return $sqlMode;
Expand All @@ -178,18 +209,18 @@ protected function prepare()
*/
protected function cleanup($sqlMode)
{
if (Yii::$app->db->schema instanceof \yii\db\mysql\Schema) {
if ($this->getDb()->schema instanceof \yii\db\mysql\Schema) {
try {
Yii::$app->db->createCommand('SELECT CONNECTION_ID()')->execute();
$this->getDb()->createCommand('SELECT CONNECTION_ID()')->execute();
} catch (Exception $ex) {
throw new \luya\Exception('Connection lost. Server has gone away?');
}

Yii::$app->db->createCommand('SET FOREIGN_KEY_CHECKS = 1;')->execute();
Yii::$app->db->createCommand('SET UNIQUE_CHECKS = 1;')->execute();
$this->getDb()->createCommand('SET FOREIGN_KEY_CHECKS = 1;')->execute();
$this->getDb()->createCommand('SET UNIQUE_CHECKS = 1;')->execute();

if ($sqlMode !== null) {
Yii::$app->db->createCommand('SET SQL_MODE=:sqlMode;', [':sqlMode' => $sqlMode])->execute();
$this->getDb()->createCommand('SET SQL_MODE=:sqlMode;', [':sqlMode' => $sqlMode])->execute();
}
}
}
Expand Down Expand Up @@ -273,7 +304,7 @@ private function request($offset)
*/
private function insertData($data)
{
$inserted = Yii::$app->db->createCommand()->batchInsert(
$inserted = $this->getDb()->createCommand()->batchInsert(
$this->getName(),
$this->cleanUpBatchInsertFields($this->getFields()),
$this->cleanUpMatchRow($data)
Expand Down
5 changes: 5 additions & 0 deletions src/proxy/ClientTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function start()
}
}
}

if ($this->build->db->schema instanceof \yii\db\mysql\Schema) {
$this->build->command->outputInfo('Using local database ' . $this->build->db->createCommand('SELECT DATABASE()')->queryScalar());
}

foreach ($this->build->getTables() as $name => $table) {
$table->syncData();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/admin/proxy/ClientBuildTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ClientBuildTest extends AdminTestCase
public function testIsSkippableTable()
{
$ctrl = new ProxyController('proxyctrl', $this->app);
$build = new ClientBuild($ctrl, [
$build = new ClientBuild($ctrl, $this->app->db, [
'buildConfig' => ['tables' => []],
]);

Expand All @@ -37,7 +37,7 @@ public function testIsSkippableTable()
public function testIsNotSkippableTable()
{
$ctrl = new ProxyController('proxyctrl', $this->app);
$build = new ClientBuild($ctrl, [
$build = new ClientBuild($ctrl, $this->app->db, [
'buildConfig' => ['tables' => []],
]);

Expand Down
Loading