Skip to content

Commit b64429f

Browse files
author
Fredrick Peter
committed
Migration Upgrades
1 parent 7cec598 commit b64429f

File tree

8 files changed

+227
-76
lines changed

8 files changed

+227
-76
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
110110
**Step 1** — update your `composer.json`:
111111
```composer.json
112112
"require": {
113-
"peterson/php-orm-database": "^4.1.3"
113+
"peterson/php-orm-database": "^4.1.4"
114114
}
115115
```
116116

@@ -1113,6 +1113,8 @@ Migration::create('users_wallet');
11131113
Migration::create('tb_jobs', 'jobs');
11141114
Migration::create('tb_sessions', 'sessions');
11151115
1116+
or
1117+
migration()->create('users');
11161118
11171119
Table `2023_04_19_1681860618_user` has been created successfully
11181120
Table `2023_04_19_1681860618_user_wallet` has been created successfully
@@ -1129,6 +1131,8 @@ Table `2023_04_19_1681860618_tb_sessions` has been created successfully
11291131
```
11301132
Migration::run('up');
11311133
1134+
or
1135+
migration()->run('up');
11321136
11331137
Migration runned successfully on `2023_04_19_1681860618_user`
11341138
Migration runned successfully on `2023_04_19_1681860618_user_wallet`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"extra": {
4040
"branch-alias": {
41-
"dev-main": "4.1.3-dev"
41+
"dev-main": "4.1.4-dev"
4242
}
4343
},
4444
"minimum-stability": "stable",

src/DB.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@ public function __construct(?array $options = []) {
3535
$this->configurePagination($options);
3636
}
3737
}
38-
3938
}
4039
}

src/Dummy/dummyMigration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function up()
2727
*/
2828
public function drop()
2929
{
30-
Schema::dropTable('dummy_table');
30+
return Schema::dropTable('dummy_table');
3131
}
3232

3333
/**
@@ -38,7 +38,7 @@ public function drop()
3838
*/
3939
public function column(?string $column)
4040
{
41-
Schema::dropColumn('dummy_table', $column);
41+
return Schema::dropColumn('dummy_table', $column);
4242
}
4343

4444
};

src/Migrations/Blueprint.php

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,55 +30,93 @@ class Blueprint extends Constants{
3030
*/
3131
public function __construct(?string $tableName = null)
3232
{
33-
$this->db = new DB();
34-
$this->tableName = $tableName;
35-
$this->charSet = $_ENV['DB_CHARSET'] ?? '';
36-
$this->collation = $_ENV['DB_COLLATION'] ?? '';
33+
$this->db = new DB();
34+
$this->tableName = $tableName;
35+
$this->charSet = $_ENV['DB_CHARSET'] ?? '';
36+
$this->collation = $_ENV['DB_COLLATION'] ?? '';
37+
}
38+
39+
/**
40+
* Creating Session Query
41+
* - To hold each Migration Request
42+
* @param mixed $query
43+
*
44+
* @return void
45+
*/
46+
private function tempMigrationQuery(mixed $query = null)
47+
{
48+
// Start the session has not already been started
49+
if (session_status() == PHP_SESSION_NONE) {
50+
session_start();
51+
}
52+
53+
$_SESSION[$this->session] = json_encode($query);
3754
}
3855

3956
/**
4057
* Creating Table Structure
4158
* Indexs|Primary|Constraints
4259
*
43-
* @return string\MySQLTemplate
60+
* @return array\MySQLTemplate
4461
*/
4562
private function MySQLTemplate()
4663
{
4764
$checkPrimary = array_column($this->columns, 'primary');
4865
if(count($checkPrimary) > 1){
49-
throw new \Exception('Primary Key can not be more than one in a table');
66+
return [
67+
'response' => self::ERROR_404,
68+
'message' => sprintf("Primary Key can not be more than one in `%s` @table", $this->tableName),
69+
];
5070
}
51-
52-
return $this->toMySQLQuery();
71+
72+
return [
73+
'response' => self::ERROR_200,
74+
'message' => $this->toMySQLQuery()
75+
];
5376
}
5477

5578
/**
5679
* Creating Database Table
5780
*
5881
* @return array\handle
5982
*/
60-
private function handle()
83+
public function handle()
6184
{
85+
// create traceable table
86+
$traceTable = $this->traceable($this->tableName);
87+
88+
// handle error
89+
$handle = self::checkDBConnect($traceTable);
90+
if(is_array($handle)){
91+
return $handle;
92+
}
93+
94+
// primary key error
95+
$mysqlHandle = $this->MySQLTemplate();
96+
if($mysqlHandle['response'] != self::ERROR_200){
97+
return $mysqlHandle;
98+
}
99+
62100
// Handle query
63101
try{
64102
// check if table already exist
65103
if($this->db->tableExist($this->tableName)){
66-
$message = "Migration runned
67-
<span style='background: #ee0707; {$this->style}'>
68-
Failed
69-
</span> Table already exist on `{$this->traceable($this->tableName)}` <br>\n";
104+
$message = "Migration
105+
<span style='background: #ee0707; {$this->style}'>
106+
Failed
107+
</span> Table exist on `{$traceTable}` <br>\n";
70108
}else{
71109
$this->status_runned = true;
72110
$message = "Migration runned
73111
<span style='background: #027b02; {$this->style}'>
74112
Successfully
75-
</span> on
76-
`{$this->traceable($this->tableName)}` <br>\n";
113+
</span> on
114+
`{$traceTable}` <br>\n";
77115
}
78116

79117
// execute query
80118
if($this->status_runned){
81-
$this->db->query( $this->MySQLTemplate() )->execute();
119+
$this->db->query( $mysqlHandle['message'] )->execute();
82120
}
83121

84122
return [
@@ -87,26 +125,39 @@ private function handle()
87125
];
88126
} catch (PDOException $e){
89127
return ['response' => self::ERROR_404, 'message' => $e->getMessage()];
90-
exit();
91128
}
92129
}
93130

94-
public function __destruct()
131+
/**
132+
* Save query data into sessions
133+
*
134+
* @return void
135+
*/
136+
public function __destruct()
95137
{
96-
// Blueprint handle
97-
$handle = $this->handle();
98-
99-
if($handle['response'] !== self::ERROR_200){
100-
echo preg_replace(
101-
'/^[ \t]+|[ \t]+$/m', '',
102-
sprintf("<<\\Error code>> %s
103-
<br><br>
104-
<<\\PDO::ERROR>> %s <br>\n
105-
", $handle['response'], $handle['message'])
106-
);
107-
return;
138+
$this->tempMigrationQuery($this->handle());
139+
}
140+
141+
/**
142+
* Check database connection error
143+
* @param string $tableName
144+
*
145+
* @return mixed
146+
*/
147+
private function checkDBConnect(?string $tableName = null)
148+
{
149+
// if database connection is okay
150+
$dbConnection = $this->db->getConnection();
151+
if($dbConnection['status'] !== self::ERROR_200){
152+
return [
153+
'status' => self::ERROR_404,
154+
'message' => "Connection Error
155+
<span style='background: #ee0707; {$this->style}'>
156+
Database Connection Error
157+
</span> on `{$tableName}`
158+
`{$dbConnection['message']}` <br>\n",
159+
];
108160
}
109-
echo "{$handle['message']}";
110161
}
111162

112163
}

src/Migrations/Migration.php

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace builder\Database\Migrations;
66

7+
use builder\Database\Constants;
78
use builder\Database\Schema\OrmDotEnv;
89
use builder\Database\Migrations\Traits\ManagerTrait;
910
use builder\Database\Migrations\Traits\FilePathTrait;
1011

11-
class Migration{
12+
class Migration extends Constants{
1213

1314
use FilePathTrait,
1415
ManagerTrait;
@@ -17,6 +18,19 @@ class Migration{
1718
static private $migrations;
1819
static private $seeders;
1920

21+
22+
/**
23+
* Returns Session String
24+
*
25+
* @return string
26+
*/
27+
public static function getSession()
28+
{
29+
$instance = new self();
30+
31+
return $instance->session;
32+
}
33+
2034
/**
2135
* Creating Managers
2236
* @param string $tableName
@@ -97,7 +111,7 @@ static private function initBaseDirectory()
97111
static public function run(?string $type = null, ?string $column = null)
98112
{
99113
// read file inside folders
100-
$readDir = self::initBaseDirectory();
114+
$files = self::initBaseDirectory();
101115

102116
// use default
103117
if(empty($type)){
@@ -106,17 +120,41 @@ static public function run(?string $type = null, ?string $column = null)
106120

107121
// Check if method exist
108122
if(!in_array(strtolower($type), ['up', 'drop', 'column']) || !method_exists(__CLASS__, strtolower($type))){
109-
throw new \Exception(
110-
sprintf("The method or type `%s` you're trying to call doesn't exist", $type)
111-
);
123+
return [
124+
'response' => self::ERROR_404,
125+
'message' => sprintf("The method or type `%s` you're trying to call doesn't exist", $type)
126+
];
112127
}
113128

114129
// run migration methods of included file
115-
foreach($readDir as $dir){
116-
$migration = include_once "{$dir}";
130+
$errorMessage = [];
131+
$errorstatus = self::ERROR_200;
132+
foreach($files as $file){
133+
$migration = include_once "{$file}";
117134

135+
// error
118136
$migration->{$type}($column);
137+
138+
// handle migration query data
139+
$handle = json_decode($_SESSION[self::getSession()] ?? [], true);
140+
141+
// store all messages
142+
$errorMessage[] = $handle['message'];
143+
144+
// error occured stop code execution
145+
if($handle['response'] != self::ERROR_200){
146+
$errorstatus = self::ERROR_404;
147+
break;
148+
}
119149
}
150+
151+
// unset session
152+
unset($_SESSION[self::getSession()]);
153+
154+
return [
155+
'response' => $errorstatus,
156+
'message' => implode("\n", $errorMessage)
157+
];
120158
}
121159

122160
/**
@@ -189,20 +227,18 @@ static public function create(?string $table_name, ?string $type = null)
189227
*/
190228
public function up(){}
191229

192-
193230
/**
194231
* Drop database table
195232
*
196-
* @return void
233+
* @return mixed
197234
*/
198235
public function drop(){}
199-
200236

201237
/**
202238
* drop database column
203239
* @param string $column
204240
*
205-
* @return void
241+
* @return mixed
206242
*/
207243
public function column(?string $column){}
208244

@@ -241,7 +277,7 @@ static private function directoryfiles(?string $directory)
241277

242278
// change value to absolute path to file
243279
array_walk($readDir, function(&$value, $index) use($directory) {
244-
$value = "{$directory}/{$value}";
280+
$value = rtrim($directory, '/') . "/{$value}";
245281
});
246282

247283
return $readDir;

0 commit comments

Comments
 (0)