Skip to content

Commit

Permalink
refactor(Migration): clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldogedev committed Oct 17, 2023
1 parent 32eb3c3 commit 8fd0a8e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/cooldogedev/BedrockEconomy/BedrockEconomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ protected function onEnable(): void
function (): void {
[$oldVersion, $oldProvider] = $this->migrationInfo;

foreach (MigrationRegistry::get($oldVersion) as $migration) {
foreach (MigrationRegistry::get($oldVersion) as $migrationClass) {
$migration = new $migrationClass($this);

if ($migration->run($oldProvider)) {
$this->getLogger()->debug($migration->getName() . " migration ran successfully");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@

namespace cooldogedev\BedrockEconomy\database\migration;

interface IMigration
use cooldogedev\BedrockEconomy\BedrockEconomy;
use PrefixedLogger;

abstract class BaseMigration
{
public function getName(): string;
public function getMin(): string;
public function getMax(): string;
protected readonly PrefixedLogger $logger;

final public function __construct(protected readonly BedrockEconomy $plugin)
{
$this->logger = new PrefixedLogger($this->plugin->getLogger(), "Migration:" . $this->getName());
}

abstract static public function getName(): string;
abstract static public function getMin(): string;
abstract static public function getMax(): string;

public function run(string $mode): bool;
abstract public function run(string $mode): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,27 @@

final class MigrationRegistry
{
/**
* @var array<int, IMigration>
*/
protected static array $migrations = [];

public static function init(): void
{
MigrationRegistry::register(new EconomyAPI());
MigrationRegistry::register(new v2_1_2());
MigrationRegistry::register(EconomyAPI::class);
MigrationRegistry::register(v2_1_2::class);
}

/**
* @return array<class-string<BaseMigration>>
*/
public static function get(string $version): array
{
$migrations = [];

/**
* @var class-string<BaseMigration> $migration
*/
foreach (MigrationRegistry::$migrations as $migration) {
$min = $migration->getMin();
$max = $migration->getMax();
$min = $migration::getMin();
$max = $migration::getMax();

if ($min !== MigrationVersion::VERSION_ANY && !version_compare($version, $min, ">=")) {
continue;
Expand All @@ -69,7 +72,7 @@ public static function get(string $version): array
return $migrations;
}

protected static function register(IMigration $migration): void
protected static function register(string $migration): void
{
MigrationRegistry::$migrations[] = $migration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,33 @@
namespace cooldogedev\BedrockEconomy\database\migration\economyapi;

use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\database\constant\MigrationVersion;
use cooldogedev\BedrockEconomy\database\exception\RecordAlreadyExistsException;
use cooldogedev\BedrockEconomy\database\migration\IMigration;
use cooldogedev\BedrockEconomy\database\migration\BaseMigration;
use cooldogedev\libSQL\exception\SQLException;
use Generator;
use SOFe\AwaitGenerator\Await;

final class Migration implements IMigration
final class Migration extends BaseMigration
{
public function getName(): string
public static function getName(): string
{
return "EconomyAPI";
}

public function getMin(): string
public static function getMin(): string
{
return MigrationVersion::VERSION_ANY;
}

public function getMax(): string
public static function getMax(): string
{
return MigrationVersion::VERSION_ANY;
}

public function run(string $mode): bool
{
$economyAPI = BedrockEconomy::getInstance()->getServer()->getPluginManager()->getPlugin("EconomyAPI");
$economyAPI = $this->plugin->getServer()->getPluginManager()->getPlugin("EconomyAPI");

if ($economyAPI === null) {
return false;
Expand All @@ -73,10 +72,11 @@ function () use ($economyAPI): Generator {

try {
yield from BedrockEconomyAPI::ASYNC()->insert($username, $username, $amount, $decimals);
$this->logger->info("Migrated data for player " . $username);
} catch (RecordAlreadyExistsException) {
BedrockEconomy::getInstance()->getLogger()->warning("Attempted to migrate data for player " . $username . " but they already exist. (" . $this->getName() . ")");
$this->logger->warning("Attempted to migrate data for player " . $username . " but they already exist");
} catch (SQLException) {
BedrockEconomy::getInstance()->getLogger()->warning("Failed to migrate data for player " . $username . ". (" . $this->getName() . ")");
$this->logger->warning("Failed to migrate data for player " . $username);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@
namespace cooldogedev\BedrockEconomy\database\migration\v2_1_2;

use cooldogedev\BedrockEconomy\api\BedrockEconomyAPI;
use cooldogedev\BedrockEconomy\BedrockEconomy;
use cooldogedev\BedrockEconomy\database\exception\RecordAlreadyExistsException;
use cooldogedev\BedrockEconomy\database\migration\IMigration;
use cooldogedev\BedrockEconomy\database\migration\BaseMigration;
use cooldogedev\libSQL\exception\SQLException;
use Generator;
use SOFe\AwaitGenerator\Await;

final class Migration implements IMigration
final class Migration extends BaseMigration
{
public function getName(): string
public static function getName(): string
{
return "<=2.1.2";
}

public function getMin(): string
public static function getMin(): string
{
return "0.0.1";
}

public function getMax(): string
public static function getMax(): string
{
return "2.1.2";
}
Expand All @@ -64,16 +63,17 @@ function () use ($records): Generator {
foreach ($records as $record) {
try {
yield from BedrockEconomyAPI::ASYNC()->insert($record["username"], $record["username"], (int)$record["balance"], 0);
$this->logger->info("Migrated data for player " . $record["username"]);
} catch (RecordAlreadyExistsException) {
BedrockEconomy::getInstance()->getLogger()->warning("Attempted to migrate data for player " . $record["username"] . " but they already exist. (" . $this->getName() . ")");
$this->logger->warning("Attempted to migrate data for player " . $record["username"] . " but they already exist");
} catch (SQLException) {
BedrockEconomy::getInstance()->getLogger()->warning("Failed to migrate data for player " . $record["username"] . ". (" . $this->getName() . ")");
$this->logger->warning("Failed to migrate data for player " . $record["username"]);
}
}
}
),
onFail: function (): void {
BedrockEconomy::getInstance()->getLogger()->warning("Failed to migrate data from old database. (" . $this->getName() . ")");
$this->logger->warning("Failed to migrate data from old database");
}
);

Expand Down

0 comments on commit 8fd0a8e

Please sign in to comment.