Skip to content

Commit 18ec4d0

Browse files
authored
Adds Purge & Polygon to ParseSchema (#365)
* Adds 'purge' to ParseSchema * Updated README * Adds Polygon to Schema * lint
1 parent 669f6f2 commit 18ec4d0

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ from your PHP app or script. Designed to work with the self-hosted Parse Server
3333
- [Server Info](#server-info)
3434
- [Version](#version)
3535
- [Features](#features)
36+
- [Schema](#schema)
37+
- [Purge](#purge)
3638
- [Contributing / Testing](#contributing--testing)
3739

3840
## Installation
@@ -477,6 +479,61 @@ $globalConfigFeatures = ParseServerInfo::getGlobalConfigFeatures();
477479

478480
```
479481

482+
### Schema
483+
Direct manipulation of the classes that are on your server is possible through `ParseSchema`.
484+
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.
485+
```php
486+
// create an instance to manage your class
487+
$mySchema = new ParseSchema("MyClass");
488+
489+
// gets the current schema data as an associative array, for inspection
490+
$data = $mySchema->get();
491+
492+
// add any # of fields, without having to create any objects
493+
$mySchema->addString('string_field');
494+
$mySchema->addNumber('num_field');
495+
$mySchema->addBoolean('bool_field');
496+
$mySchema->addDate('date_field');
497+
$mySchema->addFile('file_field');
498+
$mySchema->addGeoPoint('geopoint_field');
499+
$mySchema->addPolygon('polygon_field');
500+
$mySchema->addArray('array_field');
501+
$mySchema->addObject('obj_field');
502+
$mySchema->addPointer('pointer_field');
503+
504+
// you can even setup pointer/relation fields this way
505+
$mySchema->addPointer('pointer_field', 'TargetClass');
506+
$mySchema->addRelation('relation_field', 'TargetClass');
507+
508+
// new types can be added as they are available
509+
$mySchema->addField('new_field', 'ANewDataType');
510+
511+
// save/update this schema to persist your field changes
512+
$mySchema->save();
513+
// or
514+
$mySchema->update();
515+
516+
```
517+
Assuming you want to remove a field you can simply call `deleteField` and `save/update` to clear it out.
518+
```php
519+
$mySchema->deleteField('string_field');
520+
$mySchema->save():
521+
// or for an existing schema...
522+
$mySchema->update():
523+
```
524+
A schema can be removed via `delete`, but it must be empty first.
525+
```php
526+
$mySchema->delete();
527+
```
528+
529+
#### Purge
530+
All objects can be purged from a schema (class) via `purge`. But be careful! This can be considered an irreversible action.
531+
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).
532+
```php
533+
// delete all objects in the schema
534+
$mySchema->purge();
535+
```
536+
480537
## Contributing / Testing
481538

482539
See [CONTRIBUTING](CONTRIBUTING.md) for information on testing and contributing to

src/Parse/ParseSchema.php

+53
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class ParseSchema
6161
*/
6262
public static $GEO_POINT = 'GeoPoint';
6363

64+
/**
65+
* Polygon data type
66+
*
67+
* @var string
68+
*/
69+
public static $POLYGON = 'Polygon';
70+
6471
/**
6572
* Array data type
6673
*
@@ -258,6 +265,29 @@ public function update()
258265
return $result;
259266
}
260267

268+
/**
269+
* Removes all objects from a Schema (class) in Parse.
270+
* EXERCISE CAUTION, running this will delete all objects for this schema and cannot be reversed
271+
*
272+
* @throws Exception
273+
*/
274+
public function purge()
275+
{
276+
self::assertClassName();
277+
278+
$result = ParseClient::_request(
279+
'DELETE',
280+
'purge/'.$this->className,
281+
null,
282+
null,
283+
$this->useMasterKey
284+
);
285+
286+
if (!empty($result)) {
287+
throw new Exception('Error on purging all objects from schema "'.$this->className.'"');
288+
}
289+
}
290+
261291
/**
262292
* Removing a Schema from Parse.
263293
* You can only remove a schema from your app if it is empty (has 0 objects).
@@ -450,6 +480,28 @@ public function addGeoPoint($fieldName = null)
450480
return $this;
451481
}
452482

483+
/**
484+
* Adding Polygon Field.
485+
*
486+
* @param string $fieldName Name of the field will created on Parse
487+
*
488+
* @throws \Exception
489+
*
490+
* @return ParseSchema fields return self to create field on Parse
491+
*/
492+
public function addPolygon($fieldName = null)
493+
{
494+
if (!$fieldName) {
495+
throw new Exception('field name may not be null.', 105);
496+
}
497+
498+
$this->fields[$fieldName] = [
499+
'type' => self::$POLYGON,
500+
];
501+
502+
return $this;
503+
}
504+
453505
/**
454506
* Adding Array Field.
455507
*
@@ -589,6 +641,7 @@ public function assertTypes($type = null)
589641
$type !== self::$DATE &&
590642
$type !== self::$FILE &&
591643
$type !== self::$GEO_POINT &&
644+
$type !== self::$POLYGON &&
592645
$type !== self::$ARRAY &&
593646
$type !== self::$OBJECT &&
594647
$type !== self::$POINTER &&

tests/Parse/ParseSchemaTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Parse\HttpClients\ParseCurlHttpClient;
1414
use Parse\HttpClients\ParseStreamHttpClient;
1515
use Parse\ParseClient;
16+
use Parse\ParseException;
17+
use Parse\ParseObject;
18+
use Parse\ParseQuery;
1619
use Parse\ParseSchema;
1720
use Parse\ParseUser;
1821

@@ -72,6 +75,7 @@ public function testGetFieldsSchema()
7275
$this->assertEquals(ParseSchema::$DATE, $result['fields']['dateField']['type']);
7376
$this->assertEquals(ParseSchema::$FILE, $result['fields']['fileField']['type']);
7477
$this->assertEquals(ParseSchema::$GEO_POINT, $result['fields']['geoPointField']['type']);
78+
$this->assertEquals(ParseSchema::$POLYGON, $result['fields']['polygonField']['type']);
7579
$this->assertEquals(ParseSchema::$ARRAY, $result['fields']['arrayField']['type']);
7680
$this->assertEquals(ParseSchema::$OBJECT, $result['fields']['objectField']['type']);
7781
$this->assertEquals(ParseSchema::$POINTER, $result['fields']['pointerField']['type']);
@@ -90,6 +94,7 @@ private static function createFieldsOfSchema()
9094
->addDate('dateField')
9195
->addFile('fileField')
9296
->addGeoPoint('geoPointField')
97+
->addPolygon('polygonField')
9398
->addArray('arrayField')
9499
->addObject('objectField')
95100
->addPointer('pointerField', '_User')
@@ -194,6 +199,54 @@ public function testUpdateWrongFieldType()
194199
$schema->update();
195200
}
196201

202+
/**
203+
* @group schema-purge
204+
*/
205+
public function testPurgeSchema()
206+
{
207+
// get a handle to this schema
208+
$schema = new ParseSchema('SchemaTest');
209+
210+
// create an object in this schema
211+
$obj = new ParseObject('SchemaTest');
212+
$obj->set('field', 'the_one_and_only');
213+
$obj->save();
214+
215+
// attempt to delete this schema (expecting to fail)
216+
try {
217+
$schema->delete();
218+
$this->assertTrue(false, 'Did not fail on delete as expected');
219+
} catch (ParseException $pe) {
220+
$this->assertEquals(
221+
'Class SchemaTest is not empty, contains 1 objects, cannot drop schema.',
222+
$pe->getMessage()
223+
);
224+
}
225+
226+
// purge this schema
227+
$schema->purge();
228+
229+
// verify no more objects are present
230+
$query = new ParseQuery('SchemaTest');
231+
$this->assertEquals(0, $query->count());
232+
233+
// delete again
234+
$schema->delete();
235+
}
236+
237+
/**
238+
* @group schema-purge
239+
*/
240+
public function testPurgingNonexistentSchema()
241+
{
242+
$this->setExpectedException(
243+
'Parse\ParseException',
244+
'Bad Request'
245+
);
246+
$schema = new ParseSchema('NotARealSchema');
247+
$schema->purge();
248+
}
249+
197250
public function testDeleteSchema()
198251
{
199252
$createSchema = new ParseSchema('SchemaDeleteTest');
@@ -267,6 +320,13 @@ public function testGeoPointFieldNameException()
267320
$schema->addGeoPoint();
268321
}
269322

323+
public function testPolygonFieldNameException()
324+
{
325+
$schema = self::$schema;
326+
$this->setExpectedException('\Exception', 'field name may not be null.');
327+
$schema->addPolygon();
328+
}
329+
270330
public function testArrayFieldNameException()
271331
{
272332
$schema = self::$schema;

0 commit comments

Comments
 (0)