Skip to content

Commit 7dbf3e3

Browse files
update
1 parent 1dfa88e commit 7dbf3e3

File tree

6 files changed

+74
-44
lines changed

6 files changed

+74
-44
lines changed

src/Console/Commands/MigrationCommand.php

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ public function refresh()
6464
Artisan::call('migrate:fresh --force --drop-types --drop-views');
6565
}
6666

67+
/**
68+
* Rollback all database migrations
69+
*/
70+
public function reset()
71+
{
72+
// Require --force in production environments
73+
$this->forceChecker();
74+
75+
$force = (bool) $this->option('force');
76+
77+
$migration = new Migration();
78+
$response = $migration->drop($force);
79+
80+
if ($response['status'] != Constant::STATUS_200) {
81+
$this->error($response['message']);
82+
return;
83+
}
84+
85+
$this->info($response['message']);
86+
}
87+
6788
/**
6889
* Show the status of each migration
6990
*/
@@ -92,11 +113,12 @@ public function status()
92113
return 0;
93114
}
94115

95-
Logger::info("Migration status:");
116+
// Build a single table output
117+
$rows = [];
96118
foreach ($files as $file) {
97119
$fullPath = $migrationsDir . $file;
98120

99-
// Try to detect created table from file content first
121+
// Detect created table from file content or filename
100122
$table = null;
101123
$content = File::get($fullPath) ?: '';
102124
if (preg_match("/Schema::create\\(['\"]([a-zA-Z0-9_]+)['\"]/", $content, $m)) {
@@ -111,35 +133,40 @@ public function status()
111133
}
112134

113135
$exists = (bool) $conn->tableExists($table);
114-
if ($exists) {
115-
$this->success(sprintf("%-30s %s", $table, '[OK]'));
116-
} else {
117-
$this->warning(sprintf("%-30s %s", $table, '[MISSING]'));
118-
}
136+
$rows[] = [$file, $table, $exists ? 'OK' : 'MISSING'];
119137
}
120138

121-
return 0;
122-
}
123-
124-
/**
125-
* Rollback all database migrations
126-
*/
127-
public function reset()
128-
{
129-
// Require --force in production environments
130-
$this->forceChecker();
131-
132-
$force = (bool) $this->option('force');
139+
if (empty($rows)) {
140+
$this->info("No detectable migration tables.");
141+
return 0;
142+
}
133143

134-
$migration = new Migration();
135-
$response = $migration->drop($force);
144+
// Compute column widths
145+
$headers = ['Migration', 'Table', 'Status'];
146+
$w0 = strlen($headers[0]);
147+
$w1 = strlen($headers[1]);
148+
$w2 = strlen($headers[2]);
149+
foreach ($rows as [$f, $t, $s]) {
150+
$w0 = max($w0, strlen((string)$f));
151+
$w1 = max($w1, strlen((string)$t));
152+
$w2 = max($w2, strlen((string)$s));
153+
}
136154

137-
if ($response['status'] != Constant::STATUS_200) {
138-
$this->error($response['message']);
139-
return 0;
155+
// Helpers to draw lines
156+
$sep = '+' . str_repeat('-', $w0 + 2) . '+' . str_repeat('-', $w1 + 2) . '+' . str_repeat('-', $w2 + 2) . '+';
157+
$rowFn = static function ($a, $b, $c) use ($w0, $w1, $w2) {
158+
return sprintf('| %-' . $w0 . 's | %-' . $w1 . 's | %-' . $w2 . 's |', $a, $b, $c);
159+
};
160+
161+
// Render table
162+
Logger::writeln($sep);
163+
Logger::writeln($rowFn($headers[0], $headers[1], $headers[2]));
164+
Logger::writeln($sep);
165+
foreach ($rows as [$f, $t, $s]) {
166+
Logger::writeln($rowFn($f, $t, $s));
140167
}
168+
Logger::writeln($sep);
141169

142-
$this->info($response['message']);
143170
return 0;
144171
}
145172

src/Dummy/dummyJobsMigration.dum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ return new class extends Migration
1818
$table->id();
1919
$table->string('queue')->index();
2020
$table->longText('payload');
21-
$table->tinyInteger('attempts')->unsigned();
21+
$table->unsignedTinyInteger('attempts');
2222
$table->unsignedInteger('reserved_at')->nullable();
2323
$table->unsignedInteger('available_at');
2424
$table->unsignedInteger('created_at');

src/Dummy/dummySessionsMigration.dum

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ return new class extends Migration
1616
{
1717
Schema::create('{{TABLE}}', function (Blueprint $table) {
1818
$table->id();
19-
$table->unsignedBigInteger('user_id')->index()->nullable();
2019
$table->string('ip_address', 45)->nullable();
21-
$table->json('user_agent')->nullable();
22-
$table->json('payload');
20+
$table->text('user_agent')->nullable();
21+
$table->longText('payload');
22+
$table->bigInteger('uid')->nullable()->index();
23+
$table->string('browser', 50)->nullable()->index();
24+
$table->string('platform', 50)->nullable()->index();
25+
$table->text('platform_details')->nullable();
2326
$table->integer('last_activity')->index();
2427
});
2528
}

src/Migrations/Migration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function __construct($connection = null)
2828
/**
2929
* Create migration name
3030
* @param string $table_name
31-
* @param string|null $type
32-
* - optional $jobs\To create dummy Jobs table Data
31+
* @param string|null $type
32+
* - [optional] <jobs, sessions> create schema with dummy Data
3333
*
3434
* @return \Tamedevelopers\Support\Collections\Collection
3535
*/
@@ -132,7 +132,7 @@ public function drop($force = false)
132132

133133
// error occured stop code execution
134134
if($handle['status'] != Constant::STATUS_200){
135-
$errorstatus = Constant::STATUS_404;
135+
$errorstatus = $handle['status'];
136136
break;
137137
}
138138
}

src/Migrations/Schema.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ public static function updateColumnDefaultValue($table, $column, mixed $value =
115115
{
116116
self::initSchemaDatabase();
117117

118-
// handle error
119-
$handle = self::checkDBConnect();
120-
if(is_array($handle)){
121-
return $handle;
122-
}
118+
// handle db conn error
119+
$conn = self::checkDBConnect();
120+
if($conn['status'] != Constant::STATUS_200){
121+
return $conn;
122+
}
123123

124124
// Handle query
125125
try{
@@ -182,12 +182,12 @@ public static function dropTable($tableName, $force = false)
182182
{
183183
self::initSchemaDatabase();
184184

185-
// handle error
186-
$handle = self::checkDBConnect();
187-
if(is_array($handle)){
188-
self::$lastResult = $handle;
189-
return $handle;
190-
}
185+
// handle db conn error
186+
$conn = self::checkDBConnect();
187+
if($conn['status'] != Constant::STATUS_200){
188+
self::$lastResult = $conn;
189+
return $conn;
190+
}
191191

192192
// Handle query
193193
try{

tests/migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
dump(
15-
$migration::create('users')
15+
$migration::create('usersr')
1616
);
1717

1818
?>

0 commit comments

Comments
 (0)