Skip to content

Commit

Permalink
update migrate execution to keep track of old migration
Browse files Browse the repository at this point in the history
  • Loading branch information
abdslam01 committed Sep 9, 2021
1 parent 833985c commit 6968239
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
30 changes: 23 additions & 7 deletions console/MigrationExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Abdslam01\MiniFrameworkCore\Console;

use Abdslam01\MiniFrameworkCore\Database\MigrationTable;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -62,6 +63,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
unset($files);

$migrationTable = new MigrationTable;

if($input->getOption($this->downCommandOptionName)){ // --down
rsort($migrationFiles);
foreach($migrationFiles as $file){
Expand All @@ -70,18 +73,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
(new $pathFile())->down();
$output->writeln("table deleted: <info>migration $file successfully executed.</info>");
}
$migrationTable->dropMigrationTable();
$output->writeln("<comment>table deleted:</comment> <info>migration table successfully executed.</info>");

}elseif($input->getOption($this->upCommandOptionName)
|| !($input->getOption($this->downCommandOptionName)
&& $input->getOption($this->upCommandOptionName))
){ // --up or (no argument)
foreach($migrationFiles as $file){
sort($migrationFiles);
$pathFile = "App\\database\\migrations\\$file";
$pathFile = str_replace(".php", "", $pathFile);
(new $pathFile())->up();
$output->writeln("table created: <info>migration $file successfully executed.</info>");
}
if($migrationTable->createMigrationTable())
$output->writeln("<comment>table created:</comment> <info>migration table successfully executed.</info>");

$migrationFiles = array_diff($migrationFiles, $migrationTable->getAppliedMigrations());
if($migrationFiles == [])
$output->writeln("<info>Nothing to migrate...</info>");
else
foreach($migrationFiles as $file){
sort($migrationFiles);
$pathFile = "App\\database\\migrations\\$file";
$pathFile = str_replace(".php", "", $pathFile);
(new $pathFile())->up();
$output->writeln("table created: <info>migration $file successfully executed.</info>");

$tmpObj = new MigrationTable;
$tmpObj->migration = $file;
$tmpObj->save();
}
}

return 0;
Expand Down
56 changes: 56 additions & 0 deletions database/MigrationTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Abdslam01\MiniFrameworkCore\Database;

use Abdslam01\MiniFrameworkCore\Model;

class MigrationTable extends Model {

private Database $db;

public function __construct(){
$this->db = new Database;
$this->table = "migrations";
}
/**
* createMigrationTable
*
* @param bool $drop_if_exists
* @return bool
*/
public function createMigrationTable(bool $drop_if_exists = false){
if($drop_if_exists)
$this->db->schema->dropIfExists('migrations');

if(!$this->db->schema->hasTable("migrations")){
$this->db->schema->create('migrations', function ($table) {
$table->id();
$table->string('migration');
$table->timestamps();
});
return true;
}
return false;
}

/**
* dropMigrationTable
*
* @return void
*/
public function dropMigrationTable(){
$this->db->schema->dropIfExists('migrations');
}

/**
* getAppliedMigration
*
* @return array
*/
public function getAppliedMigrations(): array{
return array_map(
fn($p)=>$p['migration'],
MigrationTable::select("migration")->get()->toArray()
);
}
}

0 comments on commit 6968239

Please sign in to comment.