Skip to content

Commit 22523e4

Browse files
update
1 parent d24a69e commit 22523e4

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

src/Migrations/Traits/SchemaConfigurationTrait.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,24 @@ protected function getUnsigned($type)
372372

373373
/**
374374
* Create generix identifier name
375-
* @param array $column
376-
* @param string|null $name
377375
*
376+
* @param string|null $name
378377
* @return string
379378
*/
380-
protected function genericIdentifier($column, $name = null)
379+
protected function genericIdentifier($name = null)
381380
{
382-
dd(
383-
$column,
384-
);
385-
386-
$column = $this->columns[count($this->columns)];
381+
// Always reference the last added column in the collection
382+
$lastIndex = array_key_last($this->columns);
383+
$current = $this->columns[$lastIndex] ?? [];
387384
$unique = (new Exception)->getTrace()[1]['function'] ?? '__';
388385

389386
// for foreign keys
390-
if($column['type'] == 'foreign'){
391-
$name = "{$this->tableName}_{$column['name']}_{$column['type']}";
387+
if(($current['type'] ?? null) === 'foreign'){
388+
$name = "{$this->tableName}_" . ($current['name'] ?? 'column') . "_" . ($current['type'] ?? 'foreign');
392389
} else{
393390
// create unique name
394391
if(empty($name)){
395-
$name = "{$this->tableName}_{$column['name']}_{$unique}";
392+
$name = "{$this->tableName}_" . ($current['name'] ?? 'column') . "_{$unique}";
396393
} else{
397394
$name = "{$this->tableName}_{$name}";
398395
}

src/Migrations/Traits/SchemeCollectionTrait.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ public function id($name = 'id')
2828
*/
2929
public function primary()
3030
{
31-
$name = $this->columns[0]['name'] ?? 'id';
32-
$type = $this->columns[0]['type'] ?? 'integer';
31+
$lastIndex = array_key_last($this->columns);
32+
$name = $this->columns[$lastIndex]['name'] ?? 'id';
33+
$type = $this->columns[$lastIndex]['type'] ?? 'integer';
3334

34-
// unset first element in columns array
35+
// unset the element in columns
3536
// since we're trying to create an auto incrementing primary key
36-
// for the first column in the schema collection.
37-
unset($this->columns[0]);
37+
// for the column in the schema collection.
38+
unset($this->columns[$lastIndex]);
3839

3940
return $this->addColumn($name, $type, [
4041
'primary' => "PRIMARY",
@@ -101,10 +102,9 @@ public function constrained($table = null, $column = 'id', $indexName = null)
101102
*/
102103
public function references($columns, $indexName = null)
103104
{
104-
$this->columns[count($this->columns)]['references'] = $columns;
105-
$this->columns[count($this->columns)]['generix'] = $this->genericIdentifier(
106-
$indexName ?? $columns
107-
);
105+
$lastIndex = array_key_last($this->columns);
106+
$this->columns[$lastIndex]['references'] = $columns;
107+
$this->columns[$lastIndex]['generix'] = $this->genericIdentifier($indexName);
108108

109109
return $this;
110110
}
@@ -119,7 +119,8 @@ public function references($columns, $indexName = null)
119119
*/
120120
public function on($table)
121121
{
122-
$this->columns[count($this->columns)]['on'] = $table;
122+
$lastIndex = array_key_last($this->columns);
123+
$this->columns[$lastIndex]['on'] = $table;
123124
return $this;
124125
}
125126

@@ -131,7 +132,8 @@ public function on($table)
131132
*/
132133
public function onDelete($action)
133134
{
134-
$this->columns[count($this->columns)]['onDelete'] = $action;
135+
$lastIndex = array_key_last($this->columns);
136+
$this->columns[$lastIndex]['onDelete'] = $action;
135137
return $this;
136138
}
137139

@@ -143,7 +145,8 @@ public function onDelete($action)
143145
*/
144146
public function onUpdate($action)
145147
{
146-
$this->columns[count($this->columns)]['onUpdate'] = $action;
148+
$lastIndex = array_key_last($this->columns);
149+
$this->columns[$lastIndex]['onUpdate'] = $action;
147150
return $this;
148151
}
149152

@@ -154,7 +157,8 @@ public function onUpdate($action)
154157
*/
155158
public function unsigned()
156159
{
157-
$this->columns[count($this->columns)]['unsigned'] = true;
160+
$lastIndex = array_key_last($this->columns);
161+
$this->columns[$lastIndex]['unsigned'] = true;
158162
return $this;
159163
}
160164

@@ -166,7 +170,8 @@ public function unsigned()
166170
*/
167171
public function default($value)
168172
{
169-
$this->columns[count($this->columns)]['default'] = $value;
173+
$lastIndex = array_key_last($this->columns);
174+
$this->columns[$lastIndex]['default'] = $value;
170175
return $this;
171176
}
172177

@@ -178,7 +183,8 @@ public function default($value)
178183
*/
179184
public function nullable()
180185
{
181-
$this->columns[count($this->columns)]['nullable'] = true;
186+
$lastIndex = array_key_last($this->columns);
187+
$this->columns[$lastIndex]['nullable'] = true;
182188

183189
return $this;
184190
}
@@ -191,7 +197,8 @@ public function nullable()
191197
*/
192198
public function index($name = null)
193199
{
194-
$this->columns[count($this->columns)]['index'] = $this->genericIdentifier($name);
200+
$lastIndex = array_key_last($this->columns);
201+
$this->columns[$lastIndex]['index'] = $this->genericIdentifier($name);
195202

196203
return $this;
197204
}
@@ -204,7 +211,8 @@ public function index($name = null)
204211
*/
205212
public function unique($name = null)
206213
{
207-
$this->columns[count($this->columns)]['unique'] = $this->genericIdentifier($name);
214+
$lastIndex = array_key_last($this->columns);
215+
$this->columns[$lastIndex]['unique'] = $this->genericIdentifier($name);
208216

209217
return $this;
210218
}

src/Migrations/Traits/TableStructureTrait.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,6 @@ private function createQueryCollections()
139139
}
140140
}
141141

142-
dd(
143-
'createQueryCollections',
144-
$column
145-
);
146-
147142
// table query structure
148143
// exclude references
149144
if($column['type'] != 'foreign'){

0 commit comments

Comments
 (0)