From 56a3fc06dc4ff5cf100e7acbe5323cb7daeceb4c Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sun, 8 Oct 2017 15:37:37 -0500 Subject: [PATCH 1/4] Add Indexes via Schema --- src/Parse/ParseSchema.php | 49 ++++++++++++++++ tests/Parse/ParseSchemaTest.php | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index dfdad7cc..e67484c3 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -103,6 +103,13 @@ class ParseSchema */ private $fields = []; + /** + * Indexes to create. + * + * @var array + */ + private $indexes = []; + /** * Force to use master key in Schema Methods. * @@ -208,6 +215,10 @@ public function save() $schema['fields'] = $this->fields; } + if (!empty($this->indexes)) { + $schema['indexes'] = $this->indexes; + } + $result = ParseClient::_request( 'POST', 'schemas/'.$this->className, @@ -242,6 +253,7 @@ public function update() // Schema $Schema['className'] = $this->className; $Schema['fields'] = $this->fields; + $Schema['indexes'] = $this->indexes; $result = ParseClient::_request( 'PUT', @@ -318,6 +330,30 @@ public function addField($fieldName = null, $fieldType = 'String') return $this; } + /** + * Adding an Index to Create / Update a Schema. + * + * @param string $IndexName Name of the index will created on Parse + * + * @throws \Exception + * + * @return ParseSchema indexes return self to create index on Parse + */ + public function addIndex($indexName = null, $index = null) + { + if (!$indexName) { + throw new Exception('index name may not be null.', 105); + } + + if (!$index) { + throw new Exception('index may not be null.', 105); + } + + $this->indexes[$indexName] = $index; + + return $this; + } + /** * Adding String Field. * @@ -566,6 +602,19 @@ public function deleteField($fieldName = null) ]; } + /** + * Deleting a Index to Update on a Schema. + * + * @param string $indexName Name of the index will be deleted + */ + public function deleteIndex($indexName = null) + { + $this->fields = []; + $this->indexes[$indexName] = [ + '__op' => 'Delete', + ]; + } + /** * Assert if ClassName has filled. * diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index b30eee5a..e9df1f84 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -393,4 +393,105 @@ public function testBadSchemaDelete() $schema = new ParseSchema(self::$badClassName); $schema->delete(); } + + public function testCreateIndexSchemaStream() + { + ParseClient::setHttpClient(new ParseStreamHttpClient()); + + $schema = self::$schema; + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->save(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + + public function testCreateIndexSchemaCurl() + { + if (function_exists('curl_init')) { + ParseClient::setHttpClient(new ParseCurlHttpClient()); + + $schema = self::$schema; + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->save(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + } + + public function testUpdateIndexSchemaStream() + { + ParseClient::setHttpClient(new ParseStreamHttpClient()); + + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + + public function testUpdateIndexSchemaCurl() + { + if (function_exists('curl_init')) { + ParseClient::setHttpClient(new ParseCurlHttpClient()); + + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + } + } + + public function testDeleteIndexSchema() + { + ParseClient::setHttpClient(new ParseStreamHttpClient()); + + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $index = [ 'name' => 1 ]; + $schema->addIndex('test_index', $index); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertNotNull($result['indexes']['test_index']); + + $schema->deleteIndex('test_index'); + $schema->update(); + $result = $getSchema->get(); + $this->assertEquals(array_key_exists('indexes', $result), false); + } + + public function testIndexNameException() + { + $schema = self::$schema; + $this->setExpectedException('\Exception', 'index name may not be null.'); + $schema->addIndex(null, null); + } + + public function testIndexException() + { + $schema = self::$schema; + $this->setExpectedException('\Exception', 'index may not be null.'); + $schema->addIndex('name', null); + } } From ec14e63f2337886014bb09b55083242c85088eec Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 11 Oct 2017 16:27:47 -0500 Subject: [PATCH 2/4] support for multiple update --- src/Parse/ParseSchema.php | 12 +++++++----- tests/Parse/ParseSchemaTest.php | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index e67484c3..62905405 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -251,15 +251,18 @@ public function update() } // Schema - $Schema['className'] = $this->className; - $Schema['fields'] = $this->fields; - $Schema['indexes'] = $this->indexes; + $schema['className'] = $this->className; + $schema['fields'] = $this->fields; + $schema['indexes'] = $this->indexes; + + $this->fields = []; + $this->indexes = []; $result = ParseClient::_request( 'PUT', 'schemas/'.$this->className, $sessionToken, - json_encode($Schema), + json_encode($schema), $this->useMasterKey ); @@ -609,7 +612,6 @@ public function deleteField($fieldName = null) */ public function deleteIndex($indexName = null) { - $this->fields = []; $this->indexes[$indexName] = [ '__op' => 'Delete', ]; diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index e9df1f84..ce77bd86 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -160,6 +160,23 @@ public function testUpdateSchemaStream() $this->assertNotNull($result['fields']['status']); } + public function testUpdateMultipleSchemaStream() + { + ParseClient::setHttpClient(new ParseStreamHttpClient()); + + $schema = self::$schema; + $schema->save(); + + $schema->addString('name'); + $schema->update(); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + + $this->assertEquals(count($result['fields']), 5); + } + public function testUpdateSchemaCurl() { if (function_exists('curl_init')) { From a921116e222869ae7d28f697015b8ed797787d54 Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Sat, 25 Nov 2017 17:56:45 -0600 Subject: [PATCH 3/4] Documentation and Test Fix --- README.md | 14 ++++++++++++++ src/Parse/ParseSchema.php | 3 ++- tests/Parse/ParseSchemaTest.php | 7 +++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5e986c44..aa8dc5c2 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ from your PHP app or script. Designed to work with the self-hosted Parse Server - [Version](#version) - [Features](#features) - [Schema](#schema) + - [Index](#index) - [Purge](#purge) - [Logs](#logs) - [Contributing / Testing](#contributing--testing) @@ -677,6 +678,19 @@ A schema can be removed via `delete`, but it must be empty first. ```php $mySchema->delete(); ``` +#### Index +Indexes support efficient execution of queries from the database. MasterKey is required. +```php +// To add an index, the field must exist before you create an index +$schema->addString('field'); +$index = [ 'field' => 1 ]; +$schema->addIndex('index_name', $index); +$schema->save(); + +// Delete an index +$schema->deleteIndex('index_name'); +$schema->save(); + ``` #### Purge All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action. diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index 2e702cc1..ecd96577 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -366,7 +366,8 @@ public function addField($fieldName = null, $fieldType = 'String') /** * Adding an Index to Create / Update a Schema. * - * @param string $IndexName Name of the index will created on Parse + * @param string $indexName Name of the index will created on Parse + * @param string $index Key / Value to be saved * * @throws \Exception * diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index 187a710b..91630685 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -428,7 +428,7 @@ public function testBadSchemaGet() */ public function testBadSchemaSave() { - $this->setExpectedException('\Parse\ParseException'); + $this->setExpectedException('\Exception'); $user = new ParseUser(); $user->setUsername('schema-user'); @@ -444,7 +444,7 @@ public function testBadSchemaSave() */ public function testBadSchemaUpdate() { - $this->setExpectedException('\Parse\ParseException'); + $this->setExpectedException('\Exception'); $user = new ParseUser(); $user->setUsername('schema-user'); @@ -466,7 +466,6 @@ public function testBadSchemaDelete() $user->setUsername('schema-user'); $user->setPassword('basicpassword'); $user->signUp(); - $schema = new ParseSchema(self::$badClassName); $schema->delete(); } @@ -555,7 +554,7 @@ public function testDeleteIndexSchema() $schema->deleteIndex('test_index'); $schema->update(); $result = $getSchema->get(); - $this->assertEquals(array_key_exists('indexes', $result), false); + $this->assertEquals(array_key_exists('text_index', $result['indexes']), false); } public function testIndexNameException() From c3531e73ca879d9bfbaa89e0a94aada7ad97f6ae Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Wed, 29 Nov 2017 18:54:00 -0600 Subject: [PATCH 4/4] Typos and test updates --- README.md | 4 ++ src/Parse/ParseSchema.php | 12 ++--- tests/Parse/ParseSchemaTest.php | 80 +++++++++------------------------ 3 files changed, 30 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index a616d086..725518d3 100644 --- a/README.md +++ b/README.md @@ -692,6 +692,10 @@ $schema->save(); // Delete an index $schema->deleteIndex('index_name'); $schema->save(); + +// If indexes exist, you can retrieve them +$result = $schema->get(); +$indexes = $result['indexes']; ``` #### Purge diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index ecd96577..5d250c0c 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -338,7 +338,7 @@ public function delete() /** * Adding a Field to Create / Update a Schema. * - * @param string $fieldName Name of the field will created on Parse + * @param string $fieldName Name of the field that will be created on Parse * @param string $fieldType Can be a (String|Number|Boolean|Date|File|GeoPoint|Array|Object|Pointer|Relation) * * @throws \Exception @@ -366,14 +366,14 @@ public function addField($fieldName = null, $fieldType = 'String') /** * Adding an Index to Create / Update a Schema. * - * @param string $indexName Name of the index will created on Parse + * @param string $indexName Name of the index that will be created on Parse * @param string $index Key / Value to be saved * * @throws \Exception * * @return ParseSchema indexes return self to create index on Parse */ - public function addIndex($indexName = null, $index = null) + public function addIndex($indexName, $index) { if (!$indexName) { throw new Exception('index name may not be null.', 105); @@ -391,7 +391,7 @@ public function addIndex($indexName = null, $index = null) /** * Adding String Field. * - * @param string $fieldName Name of the field will created on Parse + * @param string $fieldName Name of the field that will be created on Parse * * @throws \Exception * @@ -655,9 +655,9 @@ public function deleteField($fieldName = null) } /** - * Deleting a Index to Update on a Schema. + * Deleting an Index to Update on a Schema. * - * @param string $indexName Name of the index will be deleted + * @param string $indexName Name of the index that will be deleted */ public function deleteIndex($indexName = null) { diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index 91630685..e17287b9 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -164,23 +164,6 @@ public function testUpdateSchemaStream() $this->assertNotNull($result['fields']['status']); } - public function testUpdateMultipleSchemaStream() - { - ParseClient::setHttpClient(new ParseStreamHttpClient()); - - $schema = self::$schema; - $schema->save(); - - $schema->addString('name'); - $schema->update(); - $schema->update(); - - $getSchema = new ParseSchema('SchemaTest'); - $result = $getSchema->get(); - - $this->assertEquals(count($result['fields']), 5); - } - public function testUpdateSchemaCurl() { if (function_exists('curl_init')) { @@ -207,6 +190,24 @@ public function testUpdateSchemaCurl() } } + public function testUpdateMultipleNoDuplicateFields() + { + $schema = self::$schema; + $schema->save(); + $schema->addString('name'); + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertEquals(count($result['fields']), 5); + + $schema->update(); + + $getSchema = new ParseSchema('SchemaTest'); + $result = $getSchema->get(); + $this->assertEquals(count($result['fields']), 5); + } + public function testUpdateWrongFieldType() { $this->setExpectedException('Exception', 'WrongType is not a valid type.'); @@ -470,10 +471,8 @@ public function testBadSchemaDelete() $schema->delete(); } - public function testCreateIndexSchemaStream() + public function testCreateIndexSchema() { - ParseClient::setHttpClient(new ParseStreamHttpClient()); - $schema = self::$schema; $schema->addString('name'); $index = [ 'name' => 1 ]; @@ -485,27 +484,8 @@ public function testCreateIndexSchemaStream() $this->assertNotNull($result['indexes']['test_index']); } - public function testCreateIndexSchemaCurl() + public function testUpdateIndexSchema() { - if (function_exists('curl_init')) { - ParseClient::setHttpClient(new ParseCurlHttpClient()); - - $schema = self::$schema; - $schema->addString('name'); - $index = [ 'name' => 1 ]; - $schema->addIndex('test_index', $index); - $schema->save(); - - $getSchema = new ParseSchema('SchemaTest'); - $result = $getSchema->get(); - $this->assertNotNull($result['indexes']['test_index']); - } - } - - public function testUpdateIndexSchemaStream() - { - ParseClient::setHttpClient(new ParseStreamHttpClient()); - $schema = self::$schema; $schema->save(); $schema->addString('name'); @@ -518,28 +498,8 @@ public function testUpdateIndexSchemaStream() $this->assertNotNull($result['indexes']['test_index']); } - public function testUpdateIndexSchemaCurl() - { - if (function_exists('curl_init')) { - ParseClient::setHttpClient(new ParseCurlHttpClient()); - - $schema = self::$schema; - $schema->save(); - $schema->addString('name'); - $index = [ 'name' => 1 ]; - $schema->addIndex('test_index', $index); - $schema->update(); - - $getSchema = new ParseSchema('SchemaTest'); - $result = $getSchema->get(); - $this->assertNotNull($result['indexes']['test_index']); - } - } - public function testDeleteIndexSchema() { - ParseClient::setHttpClient(new ParseStreamHttpClient()); - $schema = self::$schema; $schema->save(); $schema->addString('name');