Skip to content

Commit 04fba5c

Browse files
Merge pull request #69 from bgdevlab/feature/postgresSpecificPDO
Add Postgres specific PDO Adapter.
2 parents 3d729d0 + f519d7e commit 04fba5c

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,33 @@ return $container;
135135

136136
```
137137

138+
### Postgres PDO `SqlPgsql`
139+
Adds support for qualifying the migrations table with a schema.
140+
141+
```php
142+
<?php
143+
144+
# phpmig
145+
146+
use \Phpmig\Adapter,
147+
\Pimple;
148+
149+
$container = new Pimple();
150+
151+
$container['db'] = $container->share(function() {
152+
$dbh = new PDO(sprintf('pgsql:dbname=%s;host=%s;password=%s', 'dbname', 'localhost', 'password'), 'dbuser', '');
153+
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
154+
return $dbh;
155+
});
156+
157+
$container['phpmig.adapter'] = $container->share(function() use ($container) {
158+
return new Adapter\PDO\SqlPgsql($container['db'], 'migrations', 'migrationschema');
159+
});
160+
161+
return $container;
162+
```
163+
164+
138165

139166
Or you can use Doctrine's DBAL:
140167

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Phpmig\Adapter\PDO;
4+
5+
use Phpmig\Migration\Migration,
6+
Phpmig\Adapter\AdapterInterface,
7+
PDO;
8+
9+
/**
10+
* Simple PDO adapter to work with Postgres SQL database in particular.
11+
* @author Theodson https://github.com/theodson
12+
*/
13+
class SqlPgsql extends Sql
14+
{
15+
private $quote = "\"";
16+
private $schemaName = "public";
17+
18+
/**
19+
* Constructor
20+
*
21+
* @param \PDO $connection
22+
* @param string $tableName
23+
*/
24+
public function __construct(\PDO $connection, $tableName, $schemaName = 'public')
25+
{
26+
parent::__construct($connection, $tableName);
27+
$driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME);
28+
$this->quote = in_array($driver, array('mysql', 'pgsql')) ? '"' : '`';
29+
$this->schemaName = $schemaName;
30+
}
31+
32+
private function quotedTableName()
33+
{
34+
$sql = "{$this->quote}{$this->schemaName}{$this->quote}.{$this->quote}{$this->tableName}{$this->quote}";
35+
return $sql;
36+
}
37+
38+
/**
39+
* Fetch all
40+
*
41+
* @return array
42+
*/
43+
public function fetchAll()
44+
{
45+
$sql = "SELECT {$this->quote}version{$this->quote} FROM {$this->quotedTableName()} ORDER BY {$this->quote}version{$this->quote} ASC";
46+
return $this->connection->query($sql, PDO::FETCH_COLUMN, 0)->fetchAll();
47+
}
48+
49+
/**
50+
* Up
51+
*
52+
* @param Migration $migration
53+
* @return self
54+
*/
55+
public function up(Migration $migration)
56+
{
57+
$sql = "INSERT into {$this->quotedTableName()} (version) VALUES (:version);";
58+
$this->connection->prepare($sql)
59+
->execute(array(':version' => $migration->getVersion()));
60+
return $this;
61+
}
62+
63+
/**
64+
* Down
65+
*
66+
* @param Migration $migration
67+
* @return self
68+
*/
69+
public function down(Migration $migration)
70+
{
71+
$sql = "DELETE from {$this->quotedTableName()} where version = :version";
72+
$this->connection->prepare($sql)
73+
->execute(array(':version' => $migration->getVersion()));
74+
return $this;
75+
}
76+
77+
78+
/**
79+
* Is the schema ready?
80+
*
81+
* @return bool
82+
*/
83+
public function hasSchema()
84+
{
85+
$tables = $this->connection->query("SELECT table_name FROM information_schema.tables WHERE table_schema = '{$this->schemaName}';");
86+
while ($table = $tables->fetchColumn()) {
87+
if ($table == $this->tableName) {
88+
return true;
89+
}
90+
}
91+
return false;
92+
}
93+
94+
95+
/**
96+
* Create Schema
97+
*
98+
* @return DBAL
99+
*/
100+
public function createSchema()
101+
{
102+
$sql = sprintf("SELECT schema_name FROM {$this->quote}information_schema{$this->quote}.{$this->quote}schemata{$this->quote} WHERE schema_name = '%s';",
103+
$this->schemaName);
104+
$pgSchemas = $this->connection->exec($sql);
105+
106+
if (empty($pgSchemas)) {
107+
$sql = sprintf("CREATE SCHEMA %s;", $this->schemaName);
108+
if (FALSE === $this->connection->exec($sql)) {
109+
$e = $this->connection->errorInfo();
110+
}
111+
}
112+
113+
$sql = "CREATE table {$this->quotedTableName()} (version %s NOT NULL, {$this->quote}migrate_date{$this->quote} timestamp(6) WITH TIME ZONE DEFAULT now())";
114+
$driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME);
115+
$sql = sprintf($sql, in_array($driver, array('mysql', 'pgsql')) ? 'VARCHAR(255)' : '');
116+
117+
if (FALSE === $this->connection->exec($sql)) {
118+
$e = $this->connection->errorInfo();
119+
}
120+
return $this;
121+
}
122+
123+
}
124+

0 commit comments

Comments
 (0)