Skip to content

Commit 652a715

Browse files
committed
Split Rename, Add and remove collumns from MongoDB::setFields
1 parent 47ed60e commit 652a715

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

publishable/assets/js/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10651,6 +10651,8 @@ __webpack_require__.r(__webpack_exports__);
1065110651
_this2.$Progress.finish();
1065210652
}
1065310653
})["catch"](function (err) {
10654+
console.log(err);
10655+
1065410656
_this2.$Progress.fail();
1065510657

1065610658
_this2.displayError(err.response);

resources/assets/js/views/database/Builder.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
211211
})
212212
.catch(err => {
213+
console.log(err);
213214
this.$Progress.fail()
214215
this.displayError(err.response)
215216
});

src/Database/Drivers/MongoDB.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,10 @@ public function updateCollection($collection)
112112

113113
}
114114

115-
public function setFields($collectionName, $fields)
115+
public function renameColumns($collectionName, $fields)
116116
{
117117
$collection = $this->selectCollection($collectionName);
118-
/*
119-
* Rename Columns
120-
*/
121-
$renames = [];
118+
$renames = [];
122119
foreach ($fields as $field) {
123120
if ($field->oldName != "") {
124121
if ($field->oldName != $field->name) {
@@ -131,19 +128,23 @@ public function setFields($collectionName, $fields)
131128
if (count($renames) > 0) {
132129
$update['$rename'] = $renames;
133130
$collection->updateMany(array(), $update, array('upsert' => true));
131+
$dbmCollection = DBM_Collection::where('name', $collectionName)->first();
134132
foreach ($renames as $oldName => $newName) {
135-
$collection_field = CollectionField::where('old_name', $oldName)->first();
133+
$collection_field = CollectionField::where([
134+
'dbm_collection_id' => $dbmCollection->_id,
135+
'old_name' => $oldName,
136+
])->first();
136137
$collection_field->old_name = $newName;
137138
$collection_field->update();
138139
}
139140
}
141+
}
140142

141-
$newFields = $this->getColumnsName($collectionName);
142-
$columns = $this->getCollectionColumns($collectionName);
143-
144-
/*
145-
* Add Columns
146-
*/
143+
public function addColumns($collectionName)
144+
{
145+
$collection = $this->selectCollection($collectionName);
146+
$newFields = $this->getColumnsName($collectionName);
147+
$update = [];
147148

148149
if ($collection->count() > 0) {
149150
foreach ($newFields as $newField) {
@@ -176,14 +177,16 @@ public function setFields($collectionName, $fields)
176177
}
177178

178179
}
180+
}
179181

180-
/*
181-
* Remove Columns
182-
*/
183-
184-
$update = [];
182+
public function removeColumns($collectionName)
183+
{
184+
$collection = $this->selectCollection($collectionName);
185+
$newFields = $this->getColumnsName($collectionName);
186+
$columns = $this->getCollectionColumns($collectionName);
187+
$update = [];
188+
$unsets = [];
185189

186-
$unsets = [];
187190
foreach ($columns as $column) {
188191
if (!in_array($column, $newFields)) {
189192
$unsets[$column] = "";
@@ -194,7 +197,23 @@ public function setFields($collectionName, $fields)
194197
$update['$unset'] = $unsets;
195198
$collection->updateMany(array(), $update, array('upsert' => true));
196199
}
200+
}
201+
202+
public function setFields($collectionName, $fields)
203+
{
204+
/*
205+
* Rename Columns
206+
*/
207+
$this->renameColumns($collectionName, $fields);
197208

209+
/*
210+
* Add Columns
211+
*/
212+
$this->addColumns($collectionName);
213+
/*
214+
* Remove Columns
215+
*/
216+
$this->removeColumns($collectionName);
198217
return true;
199218
}
200219

src/Database/Drivers/MongoDB/Index.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,41 @@ public static function getIndexes(Collection $collection)
3131

3232
public static function getType(IndexInfo $index)
3333
{
34-
if ($index->isText()) {
35-
return "TEXT";
36-
}
34+
$type = static::getCommonType($index);
3735

3836
if ($index->isUnique() && !$index->isSparse() && !static::checkDescending($index)) {
39-
return "UNIQUE";
37+
$type = "UNIQUE";
4038
}
4139

4240
if ($index->isUnique() && !$index->isSparse() && static::checkDescending($index)) {
43-
return "UNIQUE_DESC";
41+
$type = "UNIQUE_DESC";
4442
}
4543

4644
if ($index->isSparse() && !static::checkDescending($index)) {
47-
return "SPARSE";
45+
$type = "SPARSE";
4846
}
4947

5048
if ($index->isSparse() && $index->isUnique() && !static::checkDescending($index)) {
51-
return "SPARSE_UNIQUE";
49+
$type = "SPARSE_UNIQUE";
5250
}
5351

5452
if ($index->isSparse() && $index->isUnique() && static::checkDescending($index)) {
55-
return "SPARSE_UNIQUE_DESC";
53+
$type = "SPARSE_UNIQUE_DESC";
5654
}
5755

5856
if ($index->isSparse() && static::checkDescending($index)) {
59-
return "SPARSE_DESC";
57+
$type = "SPARSE_DESC";
58+
}
59+
60+
$type = static::getDefaultType($index);
61+
62+
return $type;
63+
}
64+
65+
protected static function getCommonType(IndexInfo $index)
66+
{
67+
if ($index->isText()) {
68+
return "TEXT";
6069
}
6170

6271
if ($index->is2dSphere()) {
@@ -70,8 +79,6 @@ public static function getType(IndexInfo $index)
7079
if ($index->isGeoHaystack()) {
7180
return "GEOHAYSTACK";
7281
}
73-
74-
return static::getDefaultType($index);
7582
}
7683

7784
protected static function getDefaultType(IndexInfo $index)

src/Http/Controllers/DatabaseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function updateMongoDbTable($table)
9393

9494
if (in_array($column['oldName'], $fieldNames)) {
9595

96-
$collection_field = $collection_field = CollectionField::where([
96+
$collection_field = CollectionField::where([
9797
'dbm_collection_id' => $collection->_id,
9898
'old_name' => $column['oldName'],
9999
])->first();

0 commit comments

Comments
 (0)