Skip to content

Commit

Permalink
Fixed normal migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Jul 24, 2022
1 parent 1e992a8 commit 7297b4f
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "interaapps/uloleorm",
"version": "3.1.0",
"version": "3.1.1",
"type": "library",
"authors": [
{
Expand Down
3 changes: 3 additions & 0 deletions de/interaapps/ulole/orm/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public function autoMigrate(): Database {
}

foreach (UloleORM::getModelInformationList() as $modelInformation) {
if ($modelInformation->isAutoMigrateDisabled())
continue;

$fields = $modelInformation->getFields();

$columns = array_map(function ($field) use ($modelInformation) {
Expand Down
12 changes: 9 additions & 3 deletions de/interaapps/ulole/orm/ModelInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ModelInformation {
*/
private array $fields;
private ?string $name;
private bool $disableAutoMigrate = false;

/**
* @throws ReflectionException
Expand All @@ -31,6 +32,7 @@ public function __construct(
if (count($tableAttributes) > 0) {
$tableAttribute = $tableAttributes[0]->newInstance();
$this->name = $tableAttribute->value;
$this->disableAutoMigrate = $tableAttribute->disableAutoMigrate;
} else {
$this->name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $reflection->getShortName()));
if (!str_ends_with($this->name, "s"))
Expand Down Expand Up @@ -77,11 +79,11 @@ public function setName(string $name): ModelInformation {
}

public function getFieldName(string $field): string {
return $this->getColumnInformation($field)->getFieldName();
return $this->getColumnInformation($field)?->getFieldName() ?? $field;
}

public function getColumnInformation(string $name): ColumnInformation {
return $this->fields[$name];
public function getColumnInformation(string $name): ColumnInformation|null {
return $this->fields[$name] ?? null;
}

public function getFieldValue($obj, string $field): string {
Expand All @@ -93,4 +95,8 @@ public function getFieldValue($obj, string $field): string {
public function getIdentifierValue($obj): string {
return $obj->{$this->getIdentifier()};
}

public function isAutoMigrateDisabled(): bool {
return $this->disableAutoMigrate;
}
}
3 changes: 2 additions & 1 deletion de/interaapps/ulole/orm/attributes/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
class Table {
public function __construct(
/** Name */
public string $value
public string $value,
public bool $disableAutoMigrate = false
) {
}
}
6 changes: 3 additions & 3 deletions de/interaapps/ulole/orm/migration/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public function getQueries($addKeys = false): array {
$columns = [];
$primaryKeys = [];
foreach ($this->columns as $column) {
$columns[$column->name] = $column->generateStructure();
if ($column->primary)
array_push($primaryKeys, "`" . ($column->renameTo === null ? $column->name : $column->renameTo) . "`");
$columns[$column->getName()] = $column->generateStructure();
if ($column->isPrimary())
$primaryKeys[] = "`" . ($column->getRenameTo() === null ? $column->getName() : $column->getRenameTo()) . "`";
}
if ($addKeys)
$columns[":primary_key"] = "PRIMARY KEY (" . implode(",", $primaryKeys) . ")";
Expand Down
48 changes: 48 additions & 0 deletions de/interaapps/ulole/orm/migration/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,52 @@ public function generateStructure(): string {
$structure .= "AUTO_INCREMENT";
return $structure;
}

public function getName(): ?string {
return $this->name;
}

public function isPrimary(): bool {
return $this->primary;
}

public function isAi(): bool {
return $this->ai;
}

public function isDefaultDefined(): bool {
return $this->defaultDefined;
}

public function isDrop(): bool {
return $this->drop;
}

public function isEscapeDefault(): bool {
return $this->escapeDefault;
}

public function isNullable(): bool {
return $this->nullable;
}

public function isUnique(): bool {
return $this->unique;
}

public function getDefault(): mixed {
return $this->default;
}

public function getRenameTo(): ?string {
return $this->renameTo;
}

public function getSize(): mixed {
return $this->size;
}

public function getType(): string {
return $this->type;
}
}
16 changes: 8 additions & 8 deletions de/interaapps/ulole/orm/migration/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public function __construct(string $database) {
$this->migrations = [];
$db = UloleORM::getDatabase($this->database);
(new CreateMigrationsMigration())->up($db);
UloleORM::register("uloleorm_migrations", MigrationModel::class);
UloleORM::register(MigrationModel::class);

if (MigrationModel::table($this->database)->count() == 0) {
if ($this->logging) echo "Creating first migration\n";
$migration = new MigrationModel;
$migration->migrated_model = 'de\interaapps\ulole\orm\migration\table\CreateMigrationsMigration';
$migration->migratedModel = 'de\interaapps\ulole\orm\migration\table\CreateMigrationsMigration';
$migration->version = 0;
$migration->save();
}
Expand Down Expand Up @@ -49,25 +49,25 @@ public function up(): Migrator {
$db = UloleORM::getDatabase($this->database);

$latestVersion = 0;
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->get();
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->first();
if ($latestVersionObject !== null) {
$latestVersion = $latestVersionObject->version;
}

$newerVersion = $latestVersion + 1;
foreach ($this->migrations as $clazz) {
if (MigrationModel::table($this->database)->where("migrated_model", $clazz)->get() === null) {
if (MigrationModel::table($this->database)->where("migrated_model", $clazz)->first() === null) {
$returnValue = (new $clazz())->up($db);
if ($returnValue === null)
$returnValue = true;
if ($returnValue) {
$migration = new MigrationModel;
$migration->migrated_model = $clazz;
$migration->migratedModel = $clazz;
$migration->version = $newerVersion;
if ($migration->save())
if ($this->logging) echo "Migrated " . $migration->migrated_model . "\n";
if ($this->logging) echo "Migrated " . $migration->migratedModel . "\n";
else
if ($this->logging) echo "[x] Couldn't migrated " . $migration->migrated_model . "\n";
if ($this->logging) echo "[x] Couldn't migrated " . $migration->migratedModel . "\n";
}
}
}
Expand All @@ -78,7 +78,7 @@ public function down($down = 1): Migrator {
$db = UloleORM::getDatabase($this->database);

$version = 0;
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->get();
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->first();
if ($latestVersionObject !== null) {
$version = $latestVersionObject->version;
}
Expand Down
15 changes: 14 additions & 1 deletion de/interaapps/ulole/orm/migration/table/MigrationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

namespace de\interaapps\ulole\orm\migration\table;

use de\interaapps\ulole\orm\attributes\Column;
use de\interaapps\ulole\orm\attributes\Table;
use de\interaapps\ulole\orm\ORMModel;

#[Table("uloleorm_migrations", disableAutoMigrate: true)]
class MigrationModel {
use ORMModel;

public $id, $migrated_model, $version, $created;
#[Column]
public int $id;

#[Column]
public ?string $migratedModel;

#[Column]
public string $version;

#[Column]
public string $created;
}
2 changes: 1 addition & 1 deletion uppm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uloleorm",
"version": "3.1.0",
"version": "3.1.1",
"repositories": [],
"run": {},
"build": {},
Expand Down

0 comments on commit 7297b4f

Please sign in to comment.