Skip to content

Commit

Permalink
PHPORM-216 Remove $collection setting from DocumentModel and Connecti…
Browse files Browse the repository at this point in the history
…on::collection(). Use $table and Connection::table() instead (#3104)
  • Loading branch information
GromNaN authored Aug 26, 2024
1 parent f3bb644 commit 9c1146c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 85 deletions.
18 changes: 0 additions & 18 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
use function is_array;
use function preg_match;
use function str_contains;
use function trigger_error;

use const E_USER_DEPRECATED;
use const FILTER_FLAG_IPV6;
use const FILTER_VALIDATE_IP;

Expand Down Expand Up @@ -77,22 +75,6 @@ public function __construct(array $config)
$this->useDefaultQueryGrammar();
}

/**
* Begin a fluent query against a database collection.
*
* @deprecated since mongodb/laravel-mongodb 4.8, use the function table() instead
*
* @param string $collection
*
* @return Query\Builder
*/
public function collection($collection)
{
@trigger_error('Since mongodb/laravel-mongodb 4.8, the method Connection::collection() is deprecated and will be removed in version 5.0. Use the table() method instead.', E_USER_DEPRECATED);

return $this->table($collection);
}

/**
* Begin a fluent query against a database collection.
*
Expand Down
15 changes: 0 additions & 15 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@
use function str_starts_with;
use function strcmp;
use function strlen;
use function trigger_error;
use function var_export;

use const E_USER_DEPRECATED;

trait DocumentModel
{
use HybridRelations;
Expand Down Expand Up @@ -140,18 +137,6 @@ public function freshTimestamp()
return new UTCDateTime(Date::now());
}

/** @inheritdoc */
public function getTable()
{
if (isset($this->collection)) {
trigger_error('Since mongodb/laravel-mongodb 4.8: Using "$collection" property is deprecated. Use "$table" instead.', E_USER_DEPRECATED);

return $this->collection;
}

return parent::getTable();
}

/** @inheritdoc */
public function getAttribute($key)
{
Expand Down
19 changes: 0 additions & 19 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
use function iterator_to_array;
use function sort;
use function sprintf;
use function trigger_error;
use function usort;

use const E_USER_DEPRECATED;

class Builder extends \Illuminate\Database\Schema\Builder
{
/**
Expand Down Expand Up @@ -75,22 +72,6 @@ public function hasTable($table)
return $this->hasCollection($table);
}

/**
* Modify a collection on the schema.
*
* @deprecated since mongodb/laravel-mongodb 4.8, use the function table() instead
*
* @param string $collection
*
* @return void
*/
public function collection($collection, Closure $callback)
{
@trigger_error('Since mongodb/laravel-mongodb 4.8, the method Schema\Builder::collection() is deprecated and will be removed in version 5.0. Use the function table() instead.', E_USER_DEPRECATED);

$this->table($collection, $callback);
}

/** @inheritdoc */
public function table($table, Closure $callback)
{
Expand Down
66 changes: 33 additions & 33 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testBluePrint(): void
{
$instance = $this;

Schema::collection('newcollection', function ($collection) use ($instance) {
Schema::table('newcollection', function ($collection) use ($instance) {
$instance->assertInstanceOf(Blueprint::class, $collection);
});

Expand All @@ -72,21 +72,21 @@ public function testBluePrint(): void

public function testIndex(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->index('mykey1');
});

$index = $this->getIndex('newcollection', 'mykey1');
$this->assertEquals(1, $index['key']['mykey1']);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->index(['mykey2']);
});

$index = $this->getIndex('newcollection', 'mykey2');
$this->assertEquals(1, $index['key']['mykey2']);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->string('mykey3')->index();
});

Expand All @@ -96,7 +96,7 @@ public function testIndex(): void

public function testPrimary(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->string('mykey', 100)->primary();
});

Expand All @@ -106,7 +106,7 @@ public function testPrimary(): void

public function testUnique(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->unique('uniquekey');
});

Expand All @@ -116,58 +116,58 @@ public function testUnique(): void

public function testDropIndex(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->unique('uniquekey');
$collection->dropIndex('uniquekey_1');
});

$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->unique('uniquekey');
$collection->dropIndex(['uniquekey']);
});

$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->index(['field_a', 'field_b']);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->dropIndex(['field_a', 'field_b']);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->index(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertNotNull($index);

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->dropIndex('custom_index_name');
});

Expand All @@ -177,44 +177,44 @@ public function testDropIndex(): void

public function testDropIndexIfExists(): void
{
Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists('uniquekey_1');
});

$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->unique('uniquekey');
$collection->dropIndexIfExists(['uniquekey']);
});

$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals(null, $index);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b']);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertNotNull($index);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists(['field_a', 'field_b']);
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertNotNull($index);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->dropIndexIfExists('custom_index_name');
});

Expand All @@ -226,19 +226,19 @@ public function testHasIndex(): void
{
$instance = $this;

Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey1');
$instance->assertTrue($collection->hasIndex('myhaskey1_1'));
$instance->assertFalse($collection->hasIndex('myhaskey1'));
});

Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index('myhaskey2');
$instance->assertTrue($collection->hasIndex(['myhaskey2']));
$instance->assertFalse($collection->hasIndex(['myhaskey2_1']));
});

Schema::collection('newcollection', function (Blueprint $collection) use ($instance) {
Schema::table('newcollection', function (Blueprint $collection) use ($instance) {
$collection->index(['field_a', 'field_b']);
$instance->assertTrue($collection->hasIndex(['field_a_1_field_b']));
$instance->assertFalse($collection->hasIndex(['field_a_1_field_b_1']));
Expand All @@ -247,7 +247,7 @@ public function testHasIndex(): void

public function testBackground(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->background('backgroundkey');
});

Expand All @@ -257,7 +257,7 @@ public function testBackground(): void

public function testSparse(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->sparse('sparsekey');
});

Expand All @@ -267,7 +267,7 @@ public function testSparse(): void

public function testExpire(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->expire('expirekey', 60);
});

Expand All @@ -277,11 +277,11 @@ public function testExpire(): void

public function testSoftDeletes(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->softDeletes();
});

Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->string('email')->nullable()->index();
});

Expand All @@ -291,7 +291,7 @@ public function testSoftDeletes(): void

public function testFluent(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->string('email')->index();
$collection->string('token')->index();
$collection->timestamp('created_at');
Expand All @@ -306,7 +306,7 @@ public function testFluent(): void

public function testGeospatial(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->geospatial('point');
$collection->geospatial('area', '2d');
$collection->geospatial('continent', '2dsphere');
Expand All @@ -324,7 +324,7 @@ public function testGeospatial(): void

public function testDummies(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->boolean('activated')->default(0);
$collection->integer('user_id')->unsigned();
});
Expand All @@ -333,7 +333,7 @@ public function testDummies(): void

public function testSparseUnique(): void
{
Schema::collection('newcollection', function ($collection) {
Schema::table('newcollection', function ($collection) {
$collection->sparse_and_unique('sparseuniquekey');
});

Expand Down Expand Up @@ -361,7 +361,7 @@ public function testRenameColumn(): void
$this->assertArrayNotHasKey('test', $check[2]);
$this->assertArrayNotHasKey('newtest', $check[2]);

Schema::collection('newcollection', function (Blueprint $collection) {
Schema::table('newcollection', function (Blueprint $collection) {
$collection->renameColumn('test', 'newtest');
});

Expand Down

0 comments on commit 9c1146c

Please sign in to comment.