aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFran├žois Kooman <fkooman@tuxed.net>2019-04-17 22:57:16 +0200
committerFran├žois Kooman <fkooman@tuxed.net>2019-04-17 22:57:16 +0200
commit2f9e46d3a3f2c34a5859b996fec752384c6e3eaa (patch)
tree9a806f2006f006a7cf3117b857c92bc5f162521c
parent6e1c8f369f260bf0b556ed63d58aad970c102920 (diff)
downloadphp-sqlite-migrate-2f9e46d3a3f2c34a5859b996fec752384c6e3eaa.zip
php-sqlite-migrate-2f9e46d3a3f2c34a5859b996fec752384c6e3eaa.tar.gz
php-sqlite-migrate-2f9e46d3a3f2c34a5859b996fec752384c6e3eaa.tar.xz
small refactor
-rw-r--r--src/Migration.php47
1 files changed, 31 insertions, 16 deletions
diff --git a/src/Migration.php b/src/Migration.php
index d41102a..f866124 100644
--- a/src/Migration.php
+++ b/src/Migration.php
@@ -53,8 +53,7 @@ class Migration
public function __construct(PDO $dbh, $schemaDir, $schemaVersion)
{
if ('sqlite' !== $dbh->getAttribute(PDO::ATTR_DRIVER_NAME)) {
- // we only support SQLite for now
- throw new RuntimeException('only SQLite is supported');
+ throw new RuntimeException('driver "sqlite" expected');
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbh = $dbh;
@@ -165,6 +164,34 @@ class Migration
}
/**
+ * @return void
+ */
+ private function disableForeignKeys()
+ {
+ $this->dbh->exec('PRAGMA foreign_keys = OFF');
+ }
+
+ /**
+ * @return void
+ */
+ private function enableForeignKeys()
+ {
+ $this->dbh->exec('PRAGMA foreign_keys = ON');
+ }
+
+ /**
+ * @return bool
+ */
+ private function hasForeignKeys()
+ {
+ $sth = $this->dbh->query('PRAGMA foreign_keys');
+ $hasForeignKeys = '1' === $sth->fetchColumn(0);
+ $sth->closeCursor();
+
+ return $hasForeignKeys;
+ }
+
+ /**
* @return bool
*/
private function lock()
@@ -173,7 +200,7 @@ class Migration
$this->dbh->exec('CREATE TABLE _migration_in_progress (dummy INTEGER)');
if ($hasForeignKeys = $this->hasForeignKeys()) {
- $this->dbh->exec('PRAGMA foreign_keys = OFF');
+ $this->disableForeignKeys();
}
return $hasForeignKeys;
@@ -188,7 +215,7 @@ class Migration
{
// enable "foreign_keys" if they were on...
if ($hasForeignKeys) {
- $this->dbh->exec('PRAGMA foreign_keys = ON');
+ $this->enableForeignKeys();
}
// release "lock"
$this->dbh->exec('DROP TABLE _migration_in_progress');
@@ -222,18 +249,6 @@ class Migration
}
/**
- * @return bool
- */
- private function hasForeignKeys()
- {
- $sth = $this->dbh->query('PRAGMA foreign_keys');
- $hasForeignKeys = '1' === $sth->fetchColumn(0);
- $sth->closeCursor();
-
- return $hasForeignKeys;
- }
-
- /**
* @param string $filePath
*
* @return array<string>