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
Improved database managing for tests #1521
Comments
My comment:
@samdark comment (#1393 (comment))
|
Other developers opinions are very welcome :) |
Could be useful https://github.com/bmabey/database_cleaner |
About transaction: @qiangxue i think that possible solution can be like this (just a demo, ofcourse this code should not be in the test-case it will be in # DbTestCase < TestCase
class DbTestCase extends yii\codeception\TestCase
{
protected $dbConnections = ['db'];
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
$this->checkConnections(); //check if they all are available
$this->initTransactions(); //start transaction per each connection
}
public static function tearDownAfterClass()
{
$this->rollbackTransactions();
parent::tearDownAfterClass();
}
protected function initTransactions()
{
foreach($this->dbConnections as $connection)
$this->transactions[] = $connection->beginTransaction();
}
protected function rollbackTransactions()
{
foreach($this->transactions as $transaction)
$transaction->rollback();
}
} Also to avoid commit transactions by developer code we may need to modify |
Other solutions like |
@qiangxue what do you think if we change https://github.com/yiisoft/yii2/blob/master/framework/yii/db/Connection.php#L399 and add check if there is already an active transaction, a lot of dbms dont have nested transaction support and handle this only with save-points that is a hack. By adding $this->open();
if (!$this->getTransaction()) {
$this->_transaction = new Transaction(['db' => $this]);
$this->_transaction->begin();
}
return $this->_transaction; we will achieve:
But there are some notes in implementing
I think this check is a welcome improvement. |
I also think think that introducing |
Closed in order of #1956. Also reasons why it was closed:
If someone will think this issue is needed and some of the reasons are incorrect, please feel free to write here and ask for reopen this one. |
Will duplicate here this comment (#1393 (comment))
Transaction
strategy for database cleanup (start transaction for connection and then rollback)Reset
strategy for database cleanup (tables truncate)Drop/Create
strategy for database cleanup (drop tables then create them <->)I think that we definitely need to improve this to make tdd very native and easy in Yii2.
The text was updated successfully, but these errors were encountered: