Skip to content

Commit

Permalink
Prevent multiple connections on the same database
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Aug 19, 2016
1 parent 9dde766 commit 508966f
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 114 deletions.
21 changes: 21 additions & 0 deletions src/Etl.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,25 @@ public static function config($config, $default = null)

static::$config = $config;
}

/**
* Get a database connection.
*
* @param string $connection
* @return \Marquine\Etl\Database\Connection
*/
public static function database($connection = 'default')
{
if ($connection == 'default') {
$connection = static::config('database.default');
}

if (! isset(static::$connections[$connection])) {
static::$connections[$connection] = ConnectionFactory::make(
static::config("database.connections.$connection")
);
}

return static::$connections[$connection];
}
}
6 changes: 1 addition & 5 deletions src/Extractors/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Marquine\Etl\Extractors;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;

class Query implements ExtractorInterface
{
use Database;
/**
* The connection name.
*
Expand All @@ -30,9 +28,7 @@ class Query implements ExtractorInterface
*/
public function extract($query)
{
$this->connect($this->connection);

$query = $this->db->prepare($query);
$query = Etl::database($this->connection)->prepare($query);

$query->execute($this->bindings);

Expand Down
9 changes: 3 additions & 6 deletions src/Extractors/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Marquine\Etl\Extractors;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;

class Table implements ExtractorInterface
{
use Database;

/**
* The connection name.
*
Expand Down Expand Up @@ -38,12 +35,12 @@ class Table implements ExtractorInterface
*/
public function extract($table)
{
$this->connect($this->connection);

if (is_string($this->columns)) {
$this->columns = [$this->columns];
}

return $this->db->select($table, $this->columns, $this->where);
return Etl::database($this->connection)->select(
$table, $this->columns, $this->where
);
}
}
44 changes: 30 additions & 14 deletions src/Loaders/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Marquine\Etl\Loaders;

use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;
use Marquine\Etl\Traits\Indexable;

class Table implements LoaderInterface
{
use Indexable, Database;
use Indexable;

/**
* The connection name.
Expand Down Expand Up @@ -103,8 +102,6 @@ class Table implements LoaderInterface
*/
public function load($table, $items)
{
$this->connect($this->connection);

$this->table = $table;

$this->time = date('Y-m-d G:i:s');
Expand All @@ -116,7 +113,7 @@ public function load($table, $items)
$old = [];

if (! $this->skipDataCheck) {
$select = $this->db->select($this->table);
$select = Etl::database($this->connection)->select($this->table);

$old = $this->index($select, $this->keys);

Expand Down Expand Up @@ -152,7 +149,9 @@ protected function insert($items)
return;
}

$statement = $this->db->prepareInsert($this->table, $this->columns);
$statement = Etl::database($this->connection)->prepareInsert(
$this->table, $this->columns
);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
Expand All @@ -165,7 +164,9 @@ protected function insert($items)
}
};

$this->db->transaction($items, $callback, $this->transaction);
Etl::database($this->connection)->transaction(
$items, $callback, $this->transaction
);
}

/**
Expand All @@ -181,7 +182,9 @@ protected function update($items, $old)
return;
}

$statement = $this->db->prepareUpdate($this->table, $this->columns, $this->keys);
$statement = Etl::database($this->connection)->prepareUpdate(
$this->table, $this->columns, $this->keys
);

$callback = function($items) use ($statement, $old) {
foreach ($items as $key => $item) {
Expand All @@ -200,7 +203,9 @@ protected function update($items, $old)
}
};

$this->db->transaction($items, $callback, $this->transaction);
Etl::database($this->connection)->transaction(
$items, $callback, $this->transaction
);
}

/**
Expand All @@ -215,15 +220,19 @@ protected function delete($items)
return;
}

$statement = $this->db->prepareDelete($this->table, $this->keys);
$statement = Etl::database($this->connection)->prepareDelete(
$this->table, $this->keys
);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
$statement->execute(array_intersect_key($item, $this->keys));
}
};

$this->db->transaction($items, $callback, $this->transaction);
Etl::database($this->connection)->transaction(
$items, $callback, $this->transaction
);
}

/**
Expand All @@ -238,17 +247,24 @@ protected function softDelete($items)
return;
}

$statement = $this->db->prepareUpdate($this->table, $this->columns, $this->keys);
$statement = Etl::database($this->connection)->prepareUpdate(
$this->table, $this->columns, $this->keys
);

$callback = function ($items) use ($statement) {
foreach ($items as $item) {
$params = array_merge(['deleted_at' => $this->time], array_intersect_key($item, $this->keys));
$params = array_merge(
['deleted_at' => $this->time],
array_intersect_key($item, $this->keys)
);

$statement->execute($params);
}
};

$this->db->transaction($items, $callback, $this->transaction);
Etl::database($this->connection)->transaction(
$items, $callback, $this->transaction
);
}

/**
Expand Down
34 changes: 0 additions & 34 deletions src/Traits/Database.php

This file was deleted.

25 changes: 10 additions & 15 deletions tests/Extractors/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

use Tests\TestCase;
use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;
use Marquine\Etl\Extractors\Query;

class QueryTest extends TestCase
{
use Database;

protected $items = [
['id' => '1', 'name' => 'John Doe', 'email' => 'johndoe@email.com'],
['id' => '2', 'name' => 'Jane Doe', 'email' => 'janedoe@email.com'],
Expand All @@ -20,15 +17,14 @@ protected function setUp()
{
parent::setUp();

$this->connect('primary');
$this->db->exec('delete from users; delete from users_ts');
$this->createTables();
}

/** @test */
function extract_data_from_a_database_using_a_custom_query()
{
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
Etl::database()->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$query = 'SELECT * FROM users';

Expand All @@ -42,8 +38,8 @@ function extract_data_from_a_database_using_a_custom_query()
/** @test */
function extract_data_from_a_database_using_a_custom_query_and_bindings()
{
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
Etl::database()->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$query = 'SELECT * FROM users WHERE id = ?';

Expand All @@ -61,8 +57,8 @@ function extract_data_from_a_database_using_a_custom_query_and_bindings()
/** @test */
function extract_data_from_a_database_using_a_custom_query_and_named_bindings()
{
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
Etl::database()->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$query = 'SELECT * FROM users WHERE id = :id AND name = :name';

Expand All @@ -80,10 +76,9 @@ function extract_data_from_a_database_using_a_custom_query_and_named_bindings()
/** @test */
function extract_data_from_a_database_using_a_custom_query_and_connection()
{
$this->connect('secondary');
$this->db->exec('delete from users; delete from users_ts');
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
$this->createTables('secondary');
Etl::database('secondary')->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database('secondary')->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$query = 'SELECT * FROM users';

Expand Down
27 changes: 11 additions & 16 deletions tests/Extractors/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

use Tests\TestCase;
use Marquine\Etl\Etl;
use Marquine\Etl\Traits\Database;
use Marquine\Etl\Extractors\Table;

class TableTest extends TestCase
{
use Database;

protected function setUp()
{
parent::setUp();

$this->connect('primary');
$this->db->exec('delete from users; delete from users_ts');
$this->createTables();
}

protected $items = [
Expand All @@ -27,11 +23,11 @@ protected function setUp()
/** @test */
function extract_data_from_a_database_table()
{
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$extractor = new Table;

Etl::database()->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$results = $extractor->extract('users');

$this->assertEquals($this->items, $results);
Expand All @@ -40,8 +36,8 @@ function extract_data_from_a_database_table()
/** @test */
function extract_specific_columns_from_a_database_table()
{
$this->db->exec("insert into users_ts (id, name, email) values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users_ts (id, name, email) values (2, 'Jane Doe', 'janedoe@email.com')");
Etl::database()->exec("insert into users_ts (id, name, email) values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users_ts (id, name, email) values (2, 'Jane Doe', 'janedoe@email.com')");

$extractor = new Table;

Expand All @@ -55,8 +51,8 @@ function extract_specific_columns_from_a_database_table()
/** @test */
function extract_data_from_a_database_table_with_a_where_clause()
{
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
Etl::database()->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database()->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$extractor = new Table;

Expand All @@ -72,10 +68,9 @@ function extract_data_from_a_database_table_with_a_where_clause()
/** @test */
function extract_data_from_a_database_table_using_a_custom_connection()
{
$this->connect('secondary');
$this->db->exec('delete from users; delete from users_ts');
$this->db->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
$this->db->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");
$this->createTables('secondary');
Etl::database('secondary')->exec("insert into users values (1, 'John Doe', 'johndoe@email.com')");
Etl::database('secondary')->exec("insert into users values (2, 'Jane Doe', 'janedoe@email.com')");

$extractor = new Table;

Expand Down

0 comments on commit 508966f

Please sign in to comment.