Skip to content

Commit

Permalink
Merge pull request #1660 from codeigniter4/migrations
Browse files Browse the repository at this point in the history
Migrations Tests and database tweaks
  • Loading branch information
lonnieezell authored Jan 14, 2019
2 parents fa5549f + ce69558 commit c3cf7b1
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 112 deletions.
29 changes: 29 additions & 0 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,35 @@ function config(string $name, bool $getShared = true)

//--------------------------------------------------------------------

if (! function_exists('db_connnect'))
{
/**
* Grabs a database connection and returns it to the user.
*
* This is a convenience wrapper for \Config\Database::connect()
* and supports the same parameters. Namely:
*
* When passing in $db, you may pass any of the following to connect:
* - group name
* - existing connection instance
* - array of database configuration values
*
* If $getShared === false then a new connection instance will be provided,
* otherwise it will all calls will return the same instance.
*
* @param \CodeIgniter\Database\ConnectionInterface|array|string $db
* @param boolean $getShared
*
* @return \CodeIgniter\Database\BaseConnection
*/
function db_connect($db = null, bool $getShared = true)
{
return \Config\Database::connect($db, $getShared);
}
}

//--------------------------------------------------------------------

if (! function_exists('view'))
{
/**
Expand Down
90 changes: 11 additions & 79 deletions system/Database/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class Config extends BaseConfig
*/
public static function connect($group = null, bool $getShared = true)
{
// If a DB connection is passed in, just pass it back
if ($group instanceof BaseConnection)
{
return $group;
}

if (is_array($group))
{
$config = $group;
Expand Down Expand Up @@ -135,44 +141,7 @@ public static function getConnections()
*/
public static function forge($group = null)
{
// Allow custom connections to be sent in
if (is_array($group))
{
$config = $group;
$group = 'custom-' . md5(json_encode($config));
}
else
{
$config = config('Database');
}

static::ensureFactory();

if (empty($group))
{
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
}

if (is_string($group) && ! isset($config->$group) && ! is_array($config))
{
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
}

if (! isset(static::$instances[$group]))
{
if (is_array($config))
{
$db = static::connect($config);
}
else
{
$db = static::connect($group);
}
}
else
{
$db = static::$instances[$group];
}
$db = static::connect($group);

return static::$factory->loadForge($db);
}
Expand All @@ -182,50 +151,13 @@ public static function forge($group = null)
/**
* Returns a new instance of the Database Utilities class.
*
* @param string|null $group
* @param string|array|null $group
*
* @return BaseUtils
*/
public static function utils(string $group = null)
public static function utils($group = null)
{
// Allow custom connections to be sent in
if (is_array($group))
{
$config = $group;
$group = 'custom-' . md5(json_encode($config));
}
else
{
$config = config('Database');
}

static::ensureFactory();

if (empty($group))
{
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
}

if (is_string($group) && ! isset($config->$group) && ! is_array($config))
{
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
}

if (! isset(static::$instances[$group]))
{
if (is_array($config))
{
$db = static::connect($config);
}
else
{
$db = static::connect($group);
}
}
else
{
$db = static::$instances[$group];
}
$db = static::connect($group);

return static::$factory->loadUtils($db);
}
Expand All @@ -241,7 +173,7 @@ public static function utils(string $group = null)
*/
public static function seeder(string $group = null)
{
$config = new \Config\Database();
$config = config('Database');

return new Seeder($config, static::connect($group));
}
Expand Down
Loading

0 comments on commit c3cf7b1

Please sign in to comment.