From b175c8262e225511e2f11b52846dd44fe5b0453a Mon Sep 17 00:00:00 2001 From: Marc Mautz Date: Wed, 22 Oct 2025 13:23:04 +0200 Subject: [PATCH] added "FileMigration" for postgre --- db/postgre/FileMigration.php | 72 +++++++++++++++++++++++++ db/postgre/templates/file-migration.php | 17 ++++++ 2 files changed, 89 insertions(+) create mode 100644 db/postgre/FileMigration.php create mode 100644 db/postgre/templates/file-migration.php diff --git a/db/postgre/FileMigration.php b/db/postgre/FileMigration.php new file mode 100644 index 0000000..08821d3 --- /dev/null +++ b/db/postgre/FileMigration.php @@ -0,0 +1,72 @@ + + */ +class FileMigration extends Migration +{ + public $file = null; + + public function init() + { + parent::init(); + + if ($this->file === null) { + $reflection = new \ReflectionClass($this); + $this->file = str_replace('.php', '.sql', $reflection->getFileName()); + } else { + $reflection = new \ReflectionClass($this); + $this->file = dirname($reflection->getFileName()).DIRECTORY_SEPARATOR.$this->file; + } + + if (!is_file($this->file)) { + throw new Exception("File {$this->file} not found"); + } + } + + /** + * {@inheritdoc} + */ + public function up() + { + // Open connection if not opened already as we execute raw SQL on PDO we have no "autoconnect magick" here. + if (!$this->db->isActive) { + $this->db->open(); + } + + $sql = file_get_contents($this->file); + + try { + $result = $this->db->pdo->exec($sql); + echo "Success exec: " . $this->file . "\n"; + } catch (Exception $exception) { + echo "\n-----\n" . $exception->getMessage() . "\n-----\n"; + return false; + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function down() + { + echo $this::class . " cannot be reverted.\n"; + return false; + } +} diff --git a/db/postgre/templates/file-migration.php b/db/postgre/templates/file-migration.php new file mode 100644 index 0000000..6dd8e0f --- /dev/null +++ b/db/postgre/templates/file-migration.php @@ -0,0 +1,17 @@ + + +use dmstr\db\postgre\FileMigration; + +class extends FileMigration +{ + # create a sql file `.sql` or adjust and uncomment the following line, do not change this class name + //public $file = 'custom-filename.sql'; +}