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
83 changes: 53 additions & 30 deletions src/Console/Command/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,46 +50,52 @@ public function reset(): void
}

/**
* Create a migration in both directions
* Run migration action (up, rollback, reset)
*
* @param string $type
* @param string $type
* @return void
* @throws Exception
*/
private function factory(string $type): void
{
$migrations = [];

// We include all migrations files and collect it for make great manage
foreach ($this->getMigrationFiles() as $file) {
$migrations[$file] = explode('.', basename($file))[0];
}
$migrations = $this->collectMigrationFiles();

// We create the migration database status
$this->createMigrationTable();

$action = 'make' . strtoupper($type);
$connection = $this->arg->getParameter("--connection", config("database.default"));


$this->$action($migrations);
try {
Database::connection($connection);
} catch (Exception $exception) {
throw new MigrationException($exception->getMessage(), (int)$exception->getCode());
}

try {
Database::startTransaction();
// We create the migration database status
$this->createMigrationTable($connection);

$action = 'make' . ucfirst($type);
if (!method_exists($this, $action)) {
throw new MigrationException("Migration action '$action' not found.");
}
$this->$action($migrations);
Database::commitTransaction();
} catch (Exception $exception) {
Database::rollbackTransaction();
throw new MigrationException($exception->getMessage(), (int)$exception->getCode());
}
}

/**
* Create the migration status table
* Create the migration status table if it does not exist
*
* @return void
* @throws ConnectionException
*/
private function createMigrationTable(): void
{
$connection = $this->arg->getParameter("--connection", config("database.default"));

try {
Database::connection($connection);
} catch (Exception $exception) {
echo Color::red("▶ Please check your database configuration on .env.json file\n");
throw new MigrationException($exception->getMessage(), (int)$exception->getCode());
}

$adapter = Database::getConnectionAdapter();

$table = $adapter->getTablePrefix() . config('database.migration', 'migrations');
Expand Down Expand Up @@ -121,9 +127,9 @@ private function createMigrationTable(): void
}

/**
* Up migration
* Run all up migrations
*
* @param array $migrations
* @param array $migrations
* @return void
* @throws ConnectionException
* @throws QueryBuilderException
Expand Down Expand Up @@ -153,6 +159,7 @@ protected function makeUp(array $migrations): void
(new $migration())->up();
} catch (Exception $exception) {
$this->throwMigrationException($exception, $migration);
break;
}

// Create new migration status
Expand Down Expand Up @@ -209,7 +216,7 @@ private function printExceptionMessage(string $message, string $migration): void
$message = Color::red($message);
$migration = Color::yellow($migration);

exit(sprintf("\nOn %s\n\n%s\n\n", $migration, $message));
echo sprintf("\nOn %s\n\n%s\n\n", $migration, $message);
}

/**
Expand Down Expand Up @@ -249,9 +256,9 @@ private function updateMigrationStatus(string $migration, int $batch): void
}

/**
* Rollback migration
* Rollback all migrations in batch 1
*
* @param array $migrations
* @param array $migrations
* @return void
* @throws ConnectionException
* @throws QueryBuilderException
Expand Down Expand Up @@ -288,6 +295,7 @@ protected function makeRollback(array $migrations): void
(new $migration())->rollback();
} catch (Exception $exception) {
$this->throwMigrationException($exception, $migration);
return;
}

break;
Expand All @@ -311,9 +319,9 @@ protected function makeRollback(array $migrations): void
}

/**
* Reset migration
* Reset all migrations
*
* @param array $migrations
* @param array $migrations
* @return void
* @throws ConnectionException
* @throws QueryBuilderException
Expand Down Expand Up @@ -347,6 +355,7 @@ protected function makeReset(array $migrations): void
(new $migration())->rollback();
} catch (Exception $exception) {
$this->throwMigrationException($exception, $migration);
break;
}

$this->getMigrationTable()->where('migration', $migration)->delete();
Expand All @@ -358,17 +367,31 @@ protected function makeReset(array $migrations): void
}

/**
* Get migration pattern
* Get migration file paths
*
* @return array
*/
private function getMigrationFiles(): array
{
$file_pattern = $this->setting->getMigrationDirectory() . strtolower("/*.php");

return glob($file_pattern);
}

/**
* Collect migration files as [file => className]
*
* @return array
*/
private function collectMigrationFiles(): array
{
$files = $this->getMigrationFiles();
$migrations = [];
foreach ($files as $file) {
$migrations[$file] = explode('.', basename($file))[0];
}
return $migrations;
}

/**
* Get migration table
*
Expand Down
26 changes: 8 additions & 18 deletions src/Database/Barry/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
use ReflectionClass;

/**
* @method select(array|string[] $select)
* @method whereIn(string $primary_key, array $id)
* @method get()
* @method where(string $column, mixed $value)
* @method orderBy(string $latest, string $string)
* @method static select(array|string[] $select): Builder
* @method static whereIn(string $primary_key, array $id): Builder
* @method static all(): Collection
* @method static where(string $column, mixed $value): Builder
* @method static orderBy(string $latest, string $string): Builder
*/
abstract class Model implements ArrayAccess, JsonSerializable
{
Expand Down Expand Up @@ -188,7 +188,6 @@ public function getConnection(): ?string
* Initialize the connection
*
* @return Builder
* @throws
*/
public static function query(): Builder
{
Expand Down Expand Up @@ -374,7 +373,6 @@ public static function retrieve(
* Delete a record
*
* @return int
* @throws
*/
public function delete(): int
{
Expand Down Expand Up @@ -482,7 +480,6 @@ public static function create(array $data): Model
* persist aliases on insert action
*
* @return int
* @throws
*/
public function persist(): int
{
Expand Down Expand Up @@ -601,7 +598,6 @@ private function transtypeKeyValue(mixed $primary_key_value): string|int|float
*
* @param array $attributes
* @return int|bool
* @throws
*/
public function update(array $attributes): int|bool
{
Expand Down Expand Up @@ -666,7 +662,6 @@ public static function paginate(int $page_number, int $current = 0, ?int $chunk
* Allows to associate listener
*
* @param callable $cb
* @throws
*/
public static function deleted(callable $cb): void
{
Expand All @@ -679,7 +674,6 @@ public static function deleted(callable $cb): void
* Allows to associate listener
*
* @param callable $cb
* @throws
*/
public static function deleting(callable $cb): void
{
Expand All @@ -692,7 +686,6 @@ public static function deleting(callable $cb): void
* Allows to associate a listener
*
* @param callable $cb
* @throws
*/
public static function creating(callable $cb): void
{
Expand All @@ -705,7 +698,6 @@ public static function creating(callable $cb): void
* Allows to associate a listener
*
* @param callable $cb
* @throws
*/
public static function created(callable $cb): void
{
Expand All @@ -718,7 +710,6 @@ public static function created(callable $cb): void
* Allows to associate a listener
*
* @param callable $cb
* @throws
*/
public static function updating(callable $cb): void
{
Expand All @@ -731,7 +722,6 @@ public static function updating(callable $cb): void
* Allows to associate a listener
*
* @param callable $cb
* @throws
*/
public static function updated(callable $cb): void
{
Expand Down Expand Up @@ -760,7 +750,7 @@ public static function deleteBy(string $column, mixed $value): int
*
* @param string $name
* @param array $arguments
* @return mixed
* @return Builder|Collection|Model|mixed
*/
public static function __callStatic(string $name, array $arguments)
{
Expand Down Expand Up @@ -1037,9 +1027,9 @@ private function executeDataCasting(string $name): mixed
* Parse value to json
*
* @param string $value
* @return void
* @return mixed
*/
private function parseToJson($value)
private function parseToJson($value): mixed
{
return json_decode(
$value,
Expand Down
16 changes: 16 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ public static function inTransaction(): bool
* Validate a transaction
*/
public static function commit(): void
{
static::commitTransaction();
}

/**
* Validate a transaction
*/
public static function commitTransaction(): void
{
if (static::inTransaction()) {
static::$adapter->getConnection()->commit();
Expand All @@ -435,6 +443,14 @@ public static function commit(): void
* Cancel a transaction
*/
public static function rollback(): void
{
static::rollbackTransaction();
}

/**
* Cancel a transaction
*/
public static function rollbackTransaction(): void
{
if (static::inTransaction()) {
static::$adapter->getConnection()->rollBack();
Expand Down
Loading
Loading