Skip to content

Add Indexes via Schema #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Please note that this documentation contains the latest changes that may as of y
- [Version](#version)
- [Features](#features)
- [Schema](#schema)
- [Index](#index)
- [Purge](#purge)
- [Logs](#logs)
- [Contributing / Testing](#contributing--testing)
Expand Down Expand Up @@ -679,6 +680,23 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a few lines showing how to check existing indexes?

$schema->addString('field');
$index = [ 'field' => 1 ];
$schema->addIndex('index_name', $index);
$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
All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action.
Expand Down
62 changes: 57 additions & 5 deletions src/Parse/ParseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class ParseSchema
*/
private $fields = [];

/**
* Indexes to create.
*
* @var array
*/
private $indexes = [];

/**
* Force to use master key in Schema Methods.
*
Expand Down Expand Up @@ -215,6 +222,10 @@ public function save()
$schema['fields'] = $this->fields;
}

if (!empty($this->indexes)) {
$schema['indexes'] = $this->indexes;
}

$result = ParseClient::_request(
'POST',
'schemas/'.$this->className,
Expand Down Expand Up @@ -247,14 +258,18 @@ public function update()
}

// Schema
$Schema['className'] = $this->className;
$Schema['fields'] = $this->fields;
$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
);

Expand Down Expand Up @@ -323,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
Expand All @@ -348,10 +363,35 @@ public function addField($fieldName = null, $fieldType = 'String')
return $this;
}

/**
* Adding an Index to Create / Update a Schema.
*
* @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, $index)
{
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.
*
* @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
*
Expand Down Expand Up @@ -614,6 +654,18 @@ public function deleteField($fieldName = null)
];
}

/**
* Deleting an Index to Update on a Schema.
*
* @param string $indexName Name of the index that will be deleted
*/
public function deleteIndex($indexName = null)
{
$this->indexes[$indexName] = [
'__op' => 'Delete',
];
}

/**
* Assert if ClassName has filled.
*
Expand Down
83 changes: 80 additions & 3 deletions tests/Parse/ParseSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,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.');
Expand Down Expand Up @@ -411,7 +429,7 @@ public function testBadSchemaGet()
*/
public function testBadSchemaSave()
{
$this->setExpectedException('\Parse\ParseException');
$this->setExpectedException('\Exception');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing. Is there a particular reason you changed the expected exception here?

Copy link
Member Author

@dplewis dplewis Dec 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/dplewis/parse-php-sdk/blob/schema-index/src/Parse/ParseSchema.php#L277

It throws exception instead of parse exception. The test were failing locally for some reason.

On a side note testBadSchemaDelete doesn't pass locally for me. If I remove the spaces from $badClassName = "<Bad~ Class~ Name> it passes. Weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh that's something I missed. That change is good then. And quick question, are you running these tests on a windows machine by chance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS 10.12.6 Sierra

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good on mine and we're on the same setup. There's certainly a reason for it but I wonder what 🤔 . Anyways this is all good then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for waiting. Now onto 1.4.0 👍


$user = new ParseUser();
$user->setUsername('schema-user');
Expand All @@ -427,7 +445,7 @@ public function testBadSchemaSave()
*/
public function testBadSchemaUpdate()
{
$this->setExpectedException('\Parse\ParseException');
$this->setExpectedException('\Exception');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question for this here too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same above


$user = new ParseUser();
$user->setUsername('schema-user');
Expand All @@ -449,8 +467,67 @@ public function testBadSchemaDelete()
$user->setUsername('schema-user');
$user->setPassword('basicpassword');
$user->signUp();

$schema = new ParseSchema(self::$badClassName);
$schema->delete();
}

public function testCreateIndexSchema()
{
$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 testUpdateIndexSchema()
{
$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()
{
$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('text_index', $result['indexes']), 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);
}
}