Skip to content

Commit 7297b4f

Browse files
committed
Fixed normal migrator
1 parent 1e992a8 commit 7297b4f

File tree

9 files changed

+89
-18
lines changed

9 files changed

+89
-18
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/uloleorm",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"type": "library",
55
"authors": [
66
{

de/interaapps/ulole/orm/Database.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public function autoMigrate(): Database {
8888
}
8989

9090
foreach (UloleORM::getModelInformationList() as $modelInformation) {
91+
if ($modelInformation->isAutoMigrateDisabled())
92+
continue;
93+
9194
$fields = $modelInformation->getFields();
9295

9396
$columns = array_map(function ($field) use ($modelInformation) {

de/interaapps/ulole/orm/ModelInformation.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ModelInformation {
1717
*/
1818
private array $fields;
1919
private ?string $name;
20+
private bool $disableAutoMigrate = false;
2021

2122
/**
2223
* @throws ReflectionException
@@ -31,6 +32,7 @@ public function __construct(
3132
if (count($tableAttributes) > 0) {
3233
$tableAttribute = $tableAttributes[0]->newInstance();
3334
$this->name = $tableAttribute->value;
35+
$this->disableAutoMigrate = $tableAttribute->disableAutoMigrate;
3436
} else {
3537
$this->name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $reflection->getShortName()));
3638
if (!str_ends_with($this->name, "s"))
@@ -77,11 +79,11 @@ public function setName(string $name): ModelInformation {
7779
}
7880

7981
public function getFieldName(string $field): string {
80-
return $this->getColumnInformation($field)->getFieldName();
82+
return $this->getColumnInformation($field)?->getFieldName() ?? $field;
8183
}
8284

83-
public function getColumnInformation(string $name): ColumnInformation {
84-
return $this->fields[$name];
85+
public function getColumnInformation(string $name): ColumnInformation|null {
86+
return $this->fields[$name] ?? null;
8587
}
8688

8789
public function getFieldValue($obj, string $field): string {
@@ -93,4 +95,8 @@ public function getFieldValue($obj, string $field): string {
9395
public function getIdentifierValue($obj): string {
9496
return $obj->{$this->getIdentifier()};
9597
}
98+
99+
public function isAutoMigrateDisabled(): bool {
100+
return $this->disableAutoMigrate;
101+
}
96102
}

de/interaapps/ulole/orm/attributes/Table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
class Table {
99
public function __construct(
1010
/** Name */
11-
public string $value
11+
public string $value,
12+
public bool $disableAutoMigrate = false
1213
) {
1314
}
1415
}

de/interaapps/ulole/orm/migration/Blueprint.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public function getQueries($addKeys = false): array {
116116
$columns = [];
117117
$primaryKeys = [];
118118
foreach ($this->columns as $column) {
119-
$columns[$column->name] = $column->generateStructure();
120-
if ($column->primary)
121-
array_push($primaryKeys, "`" . ($column->renameTo === null ? $column->name : $column->renameTo) . "`");
119+
$columns[$column->getName()] = $column->generateStructure();
120+
if ($column->isPrimary())
121+
$primaryKeys[] = "`" . ($column->getRenameTo() === null ? $column->getName() : $column->getRenameTo()) . "`";
122122
}
123123
if ($addKeys)
124124
$columns[":primary_key"] = "PRIMARY KEY (" . implode(",", $primaryKeys) . ")";

de/interaapps/ulole/orm/migration/Column.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,52 @@ public function generateStructure(): string {
105105
$structure .= "AUTO_INCREMENT";
106106
return $structure;
107107
}
108+
109+
public function getName(): ?string {
110+
return $this->name;
111+
}
112+
113+
public function isPrimary(): bool {
114+
return $this->primary;
115+
}
116+
117+
public function isAi(): bool {
118+
return $this->ai;
119+
}
120+
121+
public function isDefaultDefined(): bool {
122+
return $this->defaultDefined;
123+
}
124+
125+
public function isDrop(): bool {
126+
return $this->drop;
127+
}
128+
129+
public function isEscapeDefault(): bool {
130+
return $this->escapeDefault;
131+
}
132+
133+
public function isNullable(): bool {
134+
return $this->nullable;
135+
}
136+
137+
public function isUnique(): bool {
138+
return $this->unique;
139+
}
140+
141+
public function getDefault(): mixed {
142+
return $this->default;
143+
}
144+
145+
public function getRenameTo(): ?string {
146+
return $this->renameTo;
147+
}
148+
149+
public function getSize(): mixed {
150+
return $this->size;
151+
}
152+
153+
public function getType(): string {
154+
return $this->type;
155+
}
108156
}

de/interaapps/ulole/orm/migration/Migrator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public function __construct(string $database) {
1616
$this->migrations = [];
1717
$db = UloleORM::getDatabase($this->database);
1818
(new CreateMigrationsMigration())->up($db);
19-
UloleORM::register("uloleorm_migrations", MigrationModel::class);
19+
UloleORM::register(MigrationModel::class);
2020

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

5151
$latestVersion = 0;
52-
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->get();
52+
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->first();
5353
if ($latestVersionObject !== null) {
5454
$latestVersion = $latestVersionObject->version;
5555
}
5656

5757
$newerVersion = $latestVersion + 1;
5858
foreach ($this->migrations as $clazz) {
59-
if (MigrationModel::table($this->database)->where("migrated_model", $clazz)->get() === null) {
59+
if (MigrationModel::table($this->database)->where("migrated_model", $clazz)->first() === null) {
6060
$returnValue = (new $clazz())->up($db);
6161
if ($returnValue === null)
6262
$returnValue = true;
6363
if ($returnValue) {
6464
$migration = new MigrationModel;
65-
$migration->migrated_model = $clazz;
65+
$migration->migratedModel = $clazz;
6666
$migration->version = $newerVersion;
6767
if ($migration->save())
68-
if ($this->logging) echo "Migrated " . $migration->migrated_model . "\n";
68+
if ($this->logging) echo "Migrated " . $migration->migratedModel . "\n";
6969
else
70-
if ($this->logging) echo "[x] Couldn't migrated " . $migration->migrated_model . "\n";
70+
if ($this->logging) echo "[x] Couldn't migrated " . $migration->migratedModel . "\n";
7171
}
7272
}
7373
}
@@ -78,7 +78,7 @@ public function down($down = 1): Migrator {
7878
$db = UloleORM::getDatabase($this->database);
7979

8080
$version = 0;
81-
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->get();
81+
$latestVersionObject = MigrationModel::table($this->database)->orderBy("version", true)->first();
8282
if ($latestVersionObject !== null) {
8383
$version = $latestVersionObject->version;
8484
}

de/interaapps/ulole/orm/migration/table/MigrationModel.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

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

5+
use de\interaapps\ulole\orm\attributes\Column;
6+
use de\interaapps\ulole\orm\attributes\Table;
57
use de\interaapps\ulole\orm\ORMModel;
68

9+
#[Table("uloleorm_migrations", disableAutoMigrate: true)]
710
class MigrationModel {
811
use ORMModel;
912

10-
public $id, $migrated_model, $version, $created;
13+
#[Column]
14+
public int $id;
15+
16+
#[Column]
17+
public ?string $migratedModel;
18+
19+
#[Column]
20+
public string $version;
21+
22+
#[Column]
23+
public string $created;
1124
}

uppm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uloleorm",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"repositories": [],
55
"run": {},
66
"build": {},

0 commit comments

Comments
 (0)