From c49f601268b187a42056f6edcbdd7ef8a8956f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 19 Aug 2024 18:50:11 +0200 Subject: [PATCH] PHPORM-216 Remove $collection setting from DocumentModel and Connection::collection(). Use $table and Connection::table() instead --- src/Connection.php | 18 ---------- src/Eloquent/DocumentModel.php | 15 -------- src/Schema/Builder.php | 19 ---------- tests/QueryBuilderTest.php | 8 ++--- tests/SchemaTest.php | 66 +++++++++++++++++----------------- 5 files changed, 37 insertions(+), 89 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 9b4cc26ed..7f34e3f81 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -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; @@ -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. * diff --git a/src/Eloquent/DocumentModel.php b/src/Eloquent/DocumentModel.php index af3aec3c2..fbbc69e49 100644 --- a/src/Eloquent/DocumentModel.php +++ b/src/Eloquent/DocumentModel.php @@ -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; @@ -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) { diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index e31a1efe1..630ff4c75 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -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 { /** @@ -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) { diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 6b08a15b7..907f4eb38 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -1055,14 +1055,14 @@ public function testIncrementEach() #[TestWith(['_id', 'id'])] public function testIdAlias($insertId, $queryId): void { - DB::collection('items')->insert([$insertId => 'abc', 'name' => 'Karting']); - $item = DB::collection('items')->where($queryId, '=', 'abc')->first(); + DB::table('items')->insert([$insertId => 'abc', 'name' => 'Karting']); + $item = DB::table('items')->where($queryId, '=', 'abc')->first(); $this->assertNotNull($item); $this->assertSame('abc', $item['id']); $this->assertSame('Karting', $item['name']); - DB::collection('items')->where($insertId, '=', 'abc')->update(['name' => 'Bike']); - $item = DB::collection('items')->where($queryId, '=', 'abc')->first(); + DB::table('items')->where($insertId, '=', 'abc')->update(['name' => 'Bike']); + $item = DB::table('items')->where($queryId, '=', 'abc')->first(); $this->assertSame('Bike', $item['name']); } } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index baf78d1a5..914b79389 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -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); }); @@ -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(); }); @@ -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(); }); @@ -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'); }); @@ -116,7 +116,7 @@ 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'); }); @@ -124,7 +124,7 @@ public function testDropIndex(): void $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']); }); @@ -132,42 +132,42 @@ public function testDropIndex(): void $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'); }); @@ -177,7 +177,7 @@ 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'); }); @@ -185,7 +185,7 @@ public function testDropIndexIfExists(): void $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']); }); @@ -193,28 +193,28 @@ public function testDropIndexIfExists(): void $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'); }); @@ -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'])); @@ -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'); }); @@ -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'); }); @@ -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); }); @@ -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(); }); @@ -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'); @@ -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'); @@ -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(); }); @@ -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'); }); @@ -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'); });