From 7b633b26db9715296fdf66a01c1c3eede6d5647f Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 1 Nov 2017 23:37:00 -0700 Subject: [PATCH 1/4] Adds 'purge' to ParseSchema --- src/Parse/ParseSchema.php | 23 +++++++++++++++ tests/Parse/ParseSchemaTest.php | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index cfc1e549..1dca699f 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -258,6 +258,29 @@ public function update() return $result; } + /** + * Removes all objects from a Schema (class) in Parse. + * EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed + * + * @throws Exception + */ + public function purge() + { + self::assertClassName(); + + $result = ParseClient::_request( + 'DELETE', + 'purge/'.$this->className, + null, + null, + $this->useMasterKey + ); + + if(!empty($result)) { + throw new Exception('Error on purging all objects from schema "'.$this->className.'"'); + } + } + /** * Removing a Schema from Parse. * You can only remove a schema from your app if it is empty (has 0 objects). diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index 960915cf..79e78c86 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -13,6 +13,9 @@ use Parse\HttpClients\ParseCurlHttpClient; use Parse\HttpClients\ParseStreamHttpClient; use Parse\ParseClient; +use Parse\ParseException; +use Parse\ParseObject; +use Parse\ParseQuery; use Parse\ParseSchema; use Parse\ParseUser; @@ -194,6 +197,54 @@ public function testUpdateWrongFieldType() $schema->update(); } + /** + * @group schema-purge + */ + public function testPurgeSchema() + { + // get a handle to this schema + $schema = new ParseSchema('SchemaTest'); + + // create an object in this schema + $obj = new ParseObject('SchemaTest'); + $obj->set('field', 'the_one_and_only'); + $obj->save(); + + // attempt to delete this schema (expecting to fail) + try { + $schema->delete(); + $this->assertTrue(false, 'Did not fail on delete as expected'); + } catch(ParseException $pe) { + $this->assertEquals( + 'Class SchemaTest is not empty, contains 1 objects, cannot drop schema.', + $pe->getMessage() + ); + } + + // purge this schema + $schema->purge(); + + // verify no more objects are present + $query = new ParseQuery('SchemaTest'); + $this->assertEquals(0, $query->count()); + + // delete again + $schema->delete(); + } + + /** + * @group schema-purge + */ + public function testPurgingNonexistentSchema() + { + $this->setExpectedException( + 'Parse\ParseException', + 'Bad Request' + ); + $schema = new ParseSchema('NotARealSchema'); + $schema->purge(); + } + public function testDeleteSchema() { $createSchema = new ParseSchema('SchemaDeleteTest'); From 8d9caafc85207a0402feae0eaea50528460809e3 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Thu, 2 Nov 2017 00:00:38 -0700 Subject: [PATCH 2/4] Updated README --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index aa120964..bc931d14 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ from your PHP app or script. Designed to work with the self-hosted Parse Server - [Server Info](#server-info) - [Version](#version) - [Features](#features) + - [Schema](#schema) + - [Purge](#purge) - [Contributing / Testing](#contributing--testing) ## Installation @@ -477,6 +479,61 @@ $globalConfigFeatures = ParseServerInfo::getGlobalConfigFeatures(); ``` +### Schema +Direct manipulation of the classes that are on your server is possible through `ParseSchema`. +Although fields and classes can be automatically generated (the latter assuming client class creation is enabled) `ParseSchema` gives you explicit control over these classes and their fields. +```php +// create an instance to manage your class +$mySchema = new ParseSchema("MyClass"); + +// gets the current schema data as an associative array, for inspection +$data = $mySchema->get(); + +// add any # of fields, without having to create any objects +$mySchema->addString('string_field'); +$mySchema->addNumber('num_field'); +$mySchema->addBoolean('bool_field'); +$mySchema->addDate('date_field'); +$mySchema->addFile('file_field'); +$mySchema->addGeoPoint('geopoint_field'); +$mySchema->addPolygon('polygon_field'); +$mySchema->addArray('array_field'); +$mySchema->addObject('obj_field'); +$mySchema->addPointer('pointer_field'); + +// you can even setup pointer/relation fields this way +$mySchema->addPointer('pointer_field', 'TargetClass'); +$mySchema->addRelation('relation_field', 'TargetClass'); + +// new types can be added as they are available +$mySchema->addField('new_field', 'ANewDataType'); + +// save/update this schema to persist your field changes +$mySchema->save(); +// or +$mySchema->update(); + +``` +Assuming you want to remove a field you can simply call `deleteField` and `save/update` to clear it out. +```php +$mySchema->deleteField('string_field'); +$mySchema->save(): +// or for an existing schema... +$mySchema->update(): +``` +A schema can be removed via `delete`, but it must be empty first. +```php +$mySchema->delete(); +``` + +#### Purge +All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action. +Only do this if you _really_ need to delete all objects from a class, such as when you need to delete the class (as in the code example above). +```php +// delete all objects in the schema +$mySchema->purge(); +``` + ## Contributing / Testing See [CONTRIBUTING](CONTRIBUTING.md) for information on testing and contributing to From e9b8a28d30faf18a6aec89ed34f2be24724931bc Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Thu, 2 Nov 2017 00:04:17 -0700 Subject: [PATCH 3/4] Adds Polygon to Schema --- src/Parse/ParseSchema.php | 30 ++++++++++++++++++++++++++++++ tests/Parse/ParseSchemaTest.php | 9 +++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index 1dca699f..3b0846b7 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -61,6 +61,13 @@ class ParseSchema */ public static $GEO_POINT = 'GeoPoint'; + /** + * Polygon data type + * + * @var string + */ + public static $POLYGON = 'Polygon'; + /** * Array data type * @@ -473,6 +480,28 @@ public function addGeoPoint($fieldName = null) return $this; } + /** + * Adding Polygon Field. + * + * @param string $fieldName Name of the field will created on Parse + * + * @throws \Exception + * + * @return ParseSchema fields return self to create field on Parse + */ + public function addPolygon($fieldName = null) + { + if (!$fieldName) { + throw new Exception('field name may not be null.', 105); + } + + $this->fields[$fieldName] = [ + 'type' => self::$POLYGON, + ]; + + return $this; + } + /** * Adding Array Field. * @@ -612,6 +641,7 @@ public function assertTypes($type = null) $type !== self::$DATE && $type !== self::$FILE && $type !== self::$GEO_POINT && + $type !== self::$POLYGON && $type !== self::$ARRAY && $type !== self::$OBJECT && $type !== self::$POINTER && diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index 79e78c86..1343a7d2 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -75,6 +75,7 @@ public function testGetFieldsSchema() $this->assertEquals(ParseSchema::$DATE, $result['fields']['dateField']['type']); $this->assertEquals(ParseSchema::$FILE, $result['fields']['fileField']['type']); $this->assertEquals(ParseSchema::$GEO_POINT, $result['fields']['geoPointField']['type']); + $this->assertEquals(ParseSchema::$POLYGON, $result['fields']['polygonField']['type']); $this->assertEquals(ParseSchema::$ARRAY, $result['fields']['arrayField']['type']); $this->assertEquals(ParseSchema::$OBJECT, $result['fields']['objectField']['type']); $this->assertEquals(ParseSchema::$POINTER, $result['fields']['pointerField']['type']); @@ -93,6 +94,7 @@ private static function createFieldsOfSchema() ->addDate('dateField') ->addFile('fileField') ->addGeoPoint('geoPointField') + ->addPolygon('polygonField') ->addArray('arrayField') ->addObject('objectField') ->addPointer('pointerField', '_User') @@ -318,6 +320,13 @@ public function testGeoPointFieldNameException() $schema->addGeoPoint(); } + public function testPolygonFieldNameException() + { + $schema = self::$schema; + $this->setExpectedException('\Exception', 'field name may not be null.'); + $schema->addPolygon(); + } + public function testArrayFieldNameException() { $schema = self::$schema; From 17571aca83892e88a1aa7710c08d1e64fd623c3b Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Thu, 2 Nov 2017 00:11:58 -0700 Subject: [PATCH 4/4] lint --- src/Parse/ParseSchema.php | 2 +- tests/Parse/ParseSchemaTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Parse/ParseSchema.php b/src/Parse/ParseSchema.php index 3b0846b7..2d22d259 100644 --- a/src/Parse/ParseSchema.php +++ b/src/Parse/ParseSchema.php @@ -283,7 +283,7 @@ public function purge() $this->useMasterKey ); - if(!empty($result)) { + if (!empty($result)) { throw new Exception('Error on purging all objects from schema "'.$this->className.'"'); } } diff --git a/tests/Parse/ParseSchemaTest.php b/tests/Parse/ParseSchemaTest.php index 1343a7d2..c953508a 100644 --- a/tests/Parse/ParseSchemaTest.php +++ b/tests/Parse/ParseSchemaTest.php @@ -216,7 +216,7 @@ public function testPurgeSchema() try { $schema->delete(); $this->assertTrue(false, 'Did not fail on delete as expected'); - } catch(ParseException $pe) { + } catch (ParseException $pe) { $this->assertEquals( 'Class SchemaTest is not empty, contains 1 objects, cannot drop schema.', $pe->getMessage()