Skip to content

Commit c3cf7b1

Browse files
authored
Merge pull request #1660 from codeigniter4/migrations
Migrations Tests and database tweaks
2 parents fa5549f + ce69558 commit c3cf7b1

8 files changed

Lines changed: 570 additions & 112 deletions

File tree

system/Common.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ function config(string $name, bool $getShared = true)
102102

103103
//--------------------------------------------------------------------
104104

105+
if (! function_exists('db_connnect'))
106+
{
107+
/**
108+
* Grabs a database connection and returns it to the user.
109+
*
110+
* This is a convenience wrapper for \Config\Database::connect()
111+
* and supports the same parameters. Namely:
112+
*
113+
* When passing in $db, you may pass any of the following to connect:
114+
* - group name
115+
* - existing connection instance
116+
* - array of database configuration values
117+
*
118+
* If $getShared === false then a new connection instance will be provided,
119+
* otherwise it will all calls will return the same instance.
120+
*
121+
* @param \CodeIgniter\Database\ConnectionInterface|array|string $db
122+
* @param boolean $getShared
123+
*
124+
* @return \CodeIgniter\Database\BaseConnection
125+
*/
126+
function db_connect($db = null, bool $getShared = true)
127+
{
128+
return \Config\Database::connect($db, $getShared);
129+
}
130+
}
131+
132+
//--------------------------------------------------------------------
133+
105134
if (! function_exists('view'))
106135
{
107136
/**

system/Database/Config.php

Lines changed: 11 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class Config extends BaseConfig
7474
*/
7575
public static function connect($group = null, bool $getShared = true)
7676
{
77+
// If a DB connection is passed in, just pass it back
78+
if ($group instanceof BaseConnection)
79+
{
80+
return $group;
81+
}
82+
7783
if (is_array($group))
7884
{
7985
$config = $group;
@@ -135,44 +141,7 @@ public static function getConnections()
135141
*/
136142
public static function forge($group = null)
137143
{
138-
// Allow custom connections to be sent in
139-
if (is_array($group))
140-
{
141-
$config = $group;
142-
$group = 'custom-' . md5(json_encode($config));
143-
}
144-
else
145-
{
146-
$config = config('Database');
147-
}
148-
149-
static::ensureFactory();
150-
151-
if (empty($group))
152-
{
153-
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
154-
}
155-
156-
if (is_string($group) && ! isset($config->$group) && ! is_array($config))
157-
{
158-
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
159-
}
160-
161-
if (! isset(static::$instances[$group]))
162-
{
163-
if (is_array($config))
164-
{
165-
$db = static::connect($config);
166-
}
167-
else
168-
{
169-
$db = static::connect($group);
170-
}
171-
}
172-
else
173-
{
174-
$db = static::$instances[$group];
175-
}
144+
$db = static::connect($group);
176145

177146
return static::$factory->loadForge($db);
178147
}
@@ -182,50 +151,13 @@ public static function forge($group = null)
182151
/**
183152
* Returns a new instance of the Database Utilities class.
184153
*
185-
* @param string|null $group
154+
* @param string|array|null $group
186155
*
187156
* @return BaseUtils
188157
*/
189-
public static function utils(string $group = null)
158+
public static function utils($group = null)
190159
{
191-
// Allow custom connections to be sent in
192-
if (is_array($group))
193-
{
194-
$config = $group;
195-
$group = 'custom-' . md5(json_encode($config));
196-
}
197-
else
198-
{
199-
$config = config('Database');
200-
}
201-
202-
static::ensureFactory();
203-
204-
if (empty($group))
205-
{
206-
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
207-
}
208-
209-
if (is_string($group) && ! isset($config->$group) && ! is_array($config))
210-
{
211-
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
212-
}
213-
214-
if (! isset(static::$instances[$group]))
215-
{
216-
if (is_array($config))
217-
{
218-
$db = static::connect($config);
219-
}
220-
else
221-
{
222-
$db = static::connect($group);
223-
}
224-
}
225-
else
226-
{
227-
$db = static::$instances[$group];
228-
}
160+
$db = static::connect($group);
229161

230162
return static::$factory->loadUtils($db);
231163
}
@@ -241,7 +173,7 @@ public static function utils(string $group = null)
241173
*/
242174
public static function seeder(string $group = null)
243175
{
244-
$config = new \Config\Database();
176+
$config = config('Database');
245177

246178
return new Seeder($config, static::connect($group));
247179
}

0 commit comments

Comments
 (0)