module-db icon indicating copy to clipboard operation
module-db copied to clipboard

Add 'timezone' config support for Db model

Open xieqiaoyu opened this issue 7 years ago • 5 comments

What are you trying to achieve?

Specific timezone when use Db Model connect to mysql , then the test logic won't rely on database config.

What do you get instead?

Can't achieve this for now.

Provide console output if related. Use -vvv mode for more details.

# paste output here

Provide test source code if related

// paste test

Details

  • Codeception version:
  • PHP Version:
  • Operating System:
  • Installation type: Phar || Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here

xieqiaoyu avatar Jan 17 '18 16:01 xieqiaoyu

Try to add timezone to dump.sql

For example "headers" of mysqldump contain someone like this (see last line of example)

-- MySQL dump 10.13  Distrib 8.0.3-rc, for Linux (x86_64)
...
-- ------------------------------------------------------
-- Server version	8.0.3-rc-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
 SET NAMES utf8mb4 ;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;

bscheshirwork avatar Jan 18 '18 13:01 bscheshirwork

@bscheshirwork Sounds a good solution , but I think timezone is an important property in a database connection , Db model should have the ability to control it.

xieqiaoyu avatar Jan 19 '18 04:01 xieqiaoyu

timezone is an important property in a database connection

So... Example for PDO connect, please? (without test case)

bscheshirwork avatar Jan 19 '18 08:01 bscheshirwork

For now I add the method below in my Actor class,And call this method before each test

    use \Codeception\Module\Db;

    public function customDbModelTimezone(Db $Db){
      $Dbtimezone = $Db->_getConfig('timezone');
      if (!is_null($Dbtimezone)) {
        $pdo = $Db->dbh;
        switch ($pdo->getAttribute(PDO::ATTR_DRIVER_NAME)) {
          case 'mysql':
            $pdo->prepare('set time_zone="'.$Dbtimezone.'"')->execute();
          break;
          default:
          break;
        }
      }
    }

my Db Model config like this

modules:
  config:
      Db:
         dsn: 'mysql:host=HOST;dbname=DBNAME'
         user: USERNAME
         password: PASSWD
         populate: true
         timezone: '+8:00' 
         dump: tests/_data/dump.sql

xieqiaoyu avatar Jan 19 '18 11:01 xieqiaoyu

If you think it's important you can send Pull Request

DavertMik avatar Jan 26 '18 23:01 DavertMik