Skip to content

Commit e5afeca

Browse files
author
Fredrick Peter
committed
DBImport and Migration Update
1 parent 03fa044 commit e5afeca

File tree

4 files changed

+42
-85
lines changed

4 files changed

+42
-85
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Prior to installing `php-orm-database` get the [Composer](https://getcomposer.or
104104
**Step 1** — update your `composer.json`:
105105
```composer.json
106106
"require": {
107-
"peterson/php-orm-database": "^3.1.3"
107+
"peterson/php-orm-database": "^3.1.4"
108108
}
109109
```
110110

@@ -1207,8 +1207,6 @@ $response = $import->DatabaseImport('orm.sql');
12071207
</details>
12081208

12091209
## Update Env Variable
1210-
<details><summary>Read more...</summary>
1211-
12121210
- You can use this class to import .sql into a database programatically
12131211

12141212
| Params | Description |
@@ -1225,10 +1223,12 @@ OrmDotEnv::updateENV('DB_PASSWORD', 'newPassword');
12251223
OrmDotEnv::updateENV('APP_DEBUG', false);
12261224
OrmDotEnv::updateENV('DB_CHARSET', 'utf8', false);
12271225
1226+
or
1227+
dot_env()->updateENV('DB_CHARSET', 'utf8', false);
1228+
12281229
Returns - Boolean
12291230
true|false
12301231
```
1231-
</details>
12321232

12331233
## Collation And Charset
12341234
- Collation and Charset Data `listing`

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"extra": {
3939
"branch-alias": {
40-
"dev-main": "3.1.3-dev"
40+
"dev-main": "3.1.4-dev"
4141
}
4242
},
4343
"minimum-stability": "stable",

src/DBImport.php

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,24 @@ class DBImport extends DB{
1414
use DBImportTrait;
1515

1616
private $db_connection;
17-
private $error;
18-
private $message;
1917
private $realpath;
20-
private $template;
21-
private $array = [];
18+
public $error;
19+
public $message;
2220

2321
/**
2422
* Construct Instance of Database
2523
*/
2624
public function __construct() {
2725
parent::__construct();
28-
26+
$this->error = self::ERROR_404;
2927
$this->db_connection = $this->getConnection();
3028
}
3129

3230
/**
3331
* Database Importation
3432
* @param string path_to_sql
3533
*
36-
* @return
34+
* @return object\builder\Database\DatabaseImport
3735
*/
3836
public function DatabaseImport($path_to_sql = NULL)
3937
{
@@ -42,83 +40,42 @@ public function DatabaseImport($path_to_sql = NULL)
4240
/**
4341
* If SQL file does'nt exists
4442
*/
45-
if(!file_exists($this->realpath) || is_dir($this->realpath))
46-
{
47-
return [
48-
'response' => self::ERROR_404,
49-
'message' => "Failed to open stream: `{$path_to_sql}` does'nt exist."
50-
];
51-
43+
if(!file_exists($this->realpath) || is_dir($this->realpath)){
44+
$this->message = "Failed to open stream: `{$path_to_sql}` does'nt exist.";
5245
} else{
5346

5447
// read a file into an array
5548
$readFile = file($this->realpath);
5649

5750
// is readable
58-
if(!$this->isReadable($readFile))
59-
{
60-
return [
61-
'response' => self::ERROR_404,
62-
'message' => "Failed to read file or empty data."
63-
];
51+
if(!$this->isReadable($readFile)){
52+
$this->message = "Failed to read file or empty data.";
6453
} else{
6554

66-
// Begin our final importation
67-
foreach($readFile as $key => $query)
68-
{
69-
// skip if its a comment
70-
if($this->isComment($query))
71-
continue;
72-
73-
//Add to the current segment
74-
$this->template .= $query;
75-
76-
// Check if it's a query
77-
if($this->isQuery($query))
78-
{
79-
// check if connection test is okay
80-
if($this->DBConnect()){
81-
try{
82-
//Query the database
83-
$this->query($this->template)->execute();
84-
85-
} catch(PDOException $e){
55+
// check if connection test is okay
56+
if($this->DBConnect()){
57+
try{
58+
// connection driver
59+
$Driver = $this->connection['driver'];
8660

87-
// get error msg
88-
$errorMsg = $e->getMessage();
61+
// get content
62+
$sql = file_get_contents($this->realpath);
8963

90-
if($errorMsg != '0'){
91-
if(
92-
strpos($errorMsg, "Multiple primary key defined") === false
93-
&& strpos($errorMsg, "Duplicate entry") === false){
64+
// execute query
65+
$Driver->exec($sql);
9466

95-
$this->message = "- Performing query: <strong style='color: #000'>{$errorMsg}</strong>";
96-
$this->array[] = $this->message;
97-
}
98-
$this->error = self::ERROR_400;
99-
}
100-
}
101-
}else{
102-
$this->message = $this->db_connection['message'];
103-
$this->array[] = $this->message;
104-
$this->error = $this->db_connection['status'];
105-
break;
106-
}
107-
108-
// Set the template to an empty string
109-
$this->template = '';
67+
$this->error = self::ERROR_200;
68+
$this->message = "- Database has been imported successfully.";
69+
} catch(PDOException $e){
70+
$this->message = "- Performing query: <strong style='color: #000'>{$e->getMessage()}</strong>";
71+
$this->error = self::ERROR_400;
11072
}
73+
} else{
74+
$this->message = $this->db_connection['message'];
11175
}
11276
}
11377
}
11478

115-
// successful and no errors
116-
if(count($this->array) === 0 && $this->db_connection['status'] == self::ERROR_200){
117-
$this->error = self::ERROR_200;
118-
$this->message = "- Database has been imported successfully.";
119-
$this->array[0] = $this->message;
120-
}
121-
12279
/*
12380
| ----------------------------------------------------------------------------
12481
| Database importation use. Below are the response code
@@ -128,9 +85,11 @@ public function DatabaseImport($path_to_sql = NULL)
12885
| if ->response === 200 (Success importing to database
12986
*/
13087

131-
return [
132-
'response' => $this->error,
133-
'message' => $this->array
88+
return (object) [
89+
'response' => $this->error,
90+
'message' => is_array($this->message)
91+
? implode('\n<br>', $this->message)
92+
: $this->message
13493
];
13594
}
13695

src/Migrations/Traits/TableStructureTrait.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,11 @@ private function createTriggers()
223223
--
224224
-- Trigger to set created_at timestamp on insert
225225
--
226-
CREATE TRIGGER {$this->tableName}_created_at
227-
BEFORE INSERT ON {$this->tableName}
228-
FOR EACH ROW
226+
DROP TRIGGER IF EXISTS `{$this->tableName}_created_at`;
227+
CREATE TRIGGER `{$this->tableName}_created_at` BEFORE INSERT ON `{$this->tableName}` FOR EACH ROW
229228
BEGIN
230-
IF (SELECT COUNT(*) FROM information_schema.columns
231-
WHERE table_name = '{$this->tableName}'
229+
IF (SELECT COUNT(*) FROM information_schema.columns
230+
WHERE table_name = '{$this->tableName}'
232231
AND column_name = 'created_at') > 0 THEN
233232
SET NEW.created_at = IFNULL(NEW.created_at, NOW());
234233
SET NEW.updated_at = NOW();
@@ -242,12 +241,11 @@ private function createTriggers()
242241
--
243242
-- Trigger to update updated_at timestamp on update
244243
--
245-
CREATE TRIGGER {$this->tableName}_updated_at
246-
BEFORE UPDATE ON {$this->tableName}
247-
FOR EACH ROW
244+
DROP TRIGGER IF EXISTS `{$this->tableName}_updated_at`;
245+
CREATE TRIGGER `{$this->tableName}_updated_at` BEFORE UPDATE ON `{$this->tableName}` FOR EACH ROW
248246
BEGIN
249-
IF (SELECT COUNT(*) FROM information_schema.columns
250-
WHERE table_name = '{$this->tableName}'
247+
IF (SELECT COUNT(*) FROM information_schema.columns
248+
WHERE table_name = '{$this->tableName}'
251249
AND column_name = 'updated_at') > 0 THEN
252250
SET NEW.updated_at = NOW();
253251
END IF;

0 commit comments

Comments
 (0)