Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions db/postgre/FileMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @link http://www.diemeisterei.de/
* @copyright Copyright (c) 2025 diemeisterei GmbH, Stuttgart
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace dmstr\db\postgre;

use Exception;
use yii\db\Migration;

/**
* Class FileMigration
* @package common\components
* Elias Luhr <e.luhr@herzogkommunikation.de>
*/
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;
}
}
17 changes: 17 additions & 0 deletions db/postgre/templates/file-migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This view is used by console/controllers/MigrateController.php
* The following variables are available in this view:
*/
/* @var $className string the new migration class name */

echo "<?php\n";
?>

use dmstr\db\postgre\FileMigration;

class <?= $className ?> extends FileMigration
{
# create a sql file `<?= $className ?>.sql` or adjust and uncomment the following line, do not change this class name
//public $file = 'custom-filename.sql';
}