Skip to content

Commit 0b56c0d

Browse files
author
Fredrick Peter
committed
update
1 parent 88fa395 commit 0b56c0d

File tree

8 files changed

+36
-53
lines changed

8 files changed

+36
-53
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,27 +1006,25 @@ Migration runned successfully on `2023_04_19_1681860618_user_wallet`
10061006
<details><summary>Read more...</summary>
10071007

10081008
- Be careful as this will execute and drop all files table `located in the migration`
1009-
1009+
- [optional param] `bool` to force delete of tables
10101010
```
10111011
Migration::drop();
10121012
10131013
or
1014-
migration()->drop();
1014+
migration()->drop(true);
10151015
```
10161016
</details>
10171017

10181018
### Drop Table
10191019
<details><summary>Read more...</summary>
10201020

10211021
- Takes one param as `string` $table_name
1022-
10231022
```
10241023
use Tamedevelopers\Database\Migrations\Schema;
10251024
10261025
Schema::dropTable('table_name');
10271026
10281027
or
1029-
10301028
schema()->dropTable('table_name');
10311029
```
10321030
</details>
@@ -1042,7 +1040,6 @@ use Tamedevelopers\Database\Migrations\Schema;
10421040
Schema::dropColumn('table_name', 'column_name');
10431041
10441042
or
1045-
10461043
schema()->dropColumn('table_name', 'column_name');
10471044
```
10481045
</details>

src/Connectors/Traits/ConnectorTrait.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ protected static function createConnector($driver = 'mysql')
2727
default => throw new Exception("Unsupported driver [{$driver}]."),
2828
};
2929
}
30-
3130

3231
/**
3332
* Check if Model Class is in use
@@ -46,19 +45,16 @@ protected static function isModelExtended()
4645
}
4746

4847
/**
49-
* Convert Model Class to table camel case
48+
* Convert Model Class to table tabelPluralization name
5049
*
5150
* @return string|null
5251
*/
53-
protected static function tabelCamelCase()
52+
protected static function tabelPluralization()
5453
{
5554
// check if child class is instance of Tamedevelopers\Database\Model
5655
if(self::isModelExtended()){
57-
// Convert camel case to snake case
58-
$snakeCase = Str::snakeCase(get_called_class());
59-
6056
return Str::pluralize(
61-
strtolower($snakeCase)
57+
Str::snake(get_called_class())
6258
);
6359
}
6460
}

src/DatabaseManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function connection($name = null, $default = [])
4848
* @param bool $useDefault
4949
* @return void
5050
*/
51-
private static function prepareValues($name = null, $default = [], $useDefault)
51+
private static function prepareValues($name = null, $default = [], $useDefault = false)
5252
{
5353
// when only one data is passed
5454
// now we just check if data is an array

src/Migrations/Traits/MigrationTrait.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tamedevelopers\Database\Migrations\Traits;
66

77
use Tamedevelopers\Support\Env;
8+
use Tamedevelopers\Support\Str;
89

910
/**
1011
*
@@ -27,7 +28,7 @@ trait MigrationTrait{
2728
private static function runMigration($table_name, $type = null)
2829
{
2930
// table name
30-
$case_table = self::toSnakeCase($table_name);
31+
$case_table = Str::snake($table_name);
3132

3233
// Date convert
3334
$fileName = sprintf( "%s_%s_%s", date('Y_m_d'), substr((string) time(), 4), "{$case_table}.php" );
@@ -154,26 +155,6 @@ private static function initStatic()
154155
self::$migrations = self::$database . "migrations/";
155156
self::$seeders = self::$database . "seeders/";
156157
}
157-
158-
/**
159-
* drop database column
160-
* @param string $input
161-
*
162-
* @return string
163-
* - String toSnakeCase
164-
*/
165-
private static function toSnakeCase($input)
166-
{
167-
$output = preg_replace_callback(
168-
'/[A-Z]/',
169-
function ($match) {
170-
return '_' . strtolower($match[0]);
171-
},
172-
$input
173-
);
174-
175-
return ltrim($output, '_');
176-
}
177158

178159
/**
179160
* Getting all files in directory

src/Migrations/Traits/SchemaConfigurationTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,19 @@ protected function getUnsigned($type)
376376
*
377377
* @return string
378378
*/
379-
protected function generix_name($name = null)
379+
protected function genericIdentifier($name = null)
380380
{
381381
$column = $this->columns[count($this->columns) - 1];
382382
$unique = (new Exception)->getTrace()[1]['function'] ?? '__';
383383

384384
// for foreign keys
385385
if($column['type'] == 'foreign'){
386386
$name = "{$this->tableName}_{$column['name']}_{$column['type']}";
387-
}else{
387+
} else{
388388
// create unique name
389-
if(is_null($name)){
389+
if(empty($name)){
390390
$name = "{$this->tableName}_{$column['name']}_{$unique}";
391-
}else{
391+
} else{
392392
$name = "{$this->tableName}_{$name}";
393393
}
394394
}

src/Migrations/Traits/SchemaTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ public function timestamps()
453453
{
454454
return $this->addColumn('created_at', 'timestamps', [
455455
'nullable' => true,
456-
'index' => $this->generix_name('created_at_index')
456+
'index' => $this->genericIdentifier('created_at_index')
457457
])
458458
->addColumn('updated_at', 'timestamps', [
459459
'nullable' => true,
460-
'index' => $this->generix_name('updated_at_index')
460+
'index' => $this->genericIdentifier('updated_at_index')
461461
]);
462462
}
463463

src/Migrations/Traits/SchemeCollectionTrait.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function primary($name, ?bool $autoIncrement = true, ?bool $unsigned = tr
8181
*/
8282
public function index($name = null)
8383
{
84-
$this->columns[count($this->columns) - 1]['index'] = $this->generix_name($name);
84+
$this->columns[count($this->columns) - 1]['index'] = $this->genericIdentifier($name);
8585

8686
return $this;
8787
}
@@ -94,7 +94,7 @@ public function index($name = null)
9494
*/
9595
public function unique($name = null)
9696
{
97-
$this->columns[count($this->columns) - 1]['unique'] = $this->generix_name($name);
97+
$this->columns[count($this->columns) - 1]['unique'] = $this->genericIdentifier($name);
9898

9999
return $this;
100100
}
@@ -132,40 +132,49 @@ public function foreignId($column)
132132
* @param string $column
133133
* - [optional] Default is `id`
134134
*
135+
* @param string|null $indexName
136+
*
135137
* @return $this
136138
*/
137-
public function constrained($table = null, $column = 'id')
139+
public function constrained($table = null, $column = 'id', $indexName = null)
138140
{
139141
// we try to use defined table name, if no name is given to the method
140-
$tableName = explode('_', $this->tableName);
142+
if(empty($table)){
143+
$table = explode('_', $this->tableName)[0] ?? '';
144+
}
141145

142-
return $this->references($column)->on($table ?? $tableName[0] ?? '');
146+
return $this->references($column, $indexName)->on($table);
143147
}
144148

145149
/**
146150
* Creating Constraints Property
147-
* @param string $referencedColumn
151+
*
152+
* @param string $columns
148153
* <code> - Parent Table References Column name </code>
149154
*
155+
* @param string|null $indexName
156+
*
150157
* @return $this
151158
*/
152-
public function references($referencedColumn)
159+
public function references($columns, $indexName = null)
153160
{
154-
$this->columns[count($this->columns) - 1]['references'] = $referencedColumn;
155-
$this->columns[count($this->columns) - 1]['generix'] = $this->generix_name($referencedColumn);
161+
$this->columns[count($this->columns) - 1]['references'] = $columns;
162+
$this->columns[count($this->columns) - 1]['generix'] = $this->genericIdentifier($indexName ?? $columns);
163+
156164
return $this;
157165
}
158166

159167
/**
160168
* Creating Constraints Property
161-
* @param string $referencedTable
169+
*
170+
* @param string $table
162171
* - Table name you're referencing to
163172
*
164173
* @return $this
165174
*/
166-
public function on($referencedTable)
175+
public function on($table)
167176
{
168-
$this->columns[count($this->columns) - 1]['on'] = $referencedTable;
177+
$this->columns[count($this->columns) - 1]['on'] = $table;
169178
return $this;
170179
}
171180

src/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static function initTableWithConnector()
7878
// and convert model class name to table name
7979
// using pluralization method
8080
if(empty($table)){
81-
$table = self::tabelCamelCase();
81+
$table = self::tabelPluralization();
8282
}
8383

8484
return (new Connector)->table($table);

0 commit comments

Comments
 (0)