Skip to content

Commit b5626b9

Browse files
authored
Merge pull request #139 from utopia-php/multitype-db
2 parents 97583ae + c7e0ae3 commit b5626b9

File tree

19 files changed

+1562
-581
lines changed

19 files changed

+1562
-581
lines changed

src/Migration/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function resolveResourceCacheKey(Resource $resource): string
4646
case Resource::TYPE_TABLE:
4747
case Resource::TYPE_COLLECTION:
4848
/** @var Table $resource */
49+
$keys[] = $resource->getDatabase()->getType();
4950
$keys[] = $resource->getDatabase()->getSequence();
5051
break;
5152

src/Migration/Destinations/Appwrite.php

Lines changed: 225 additions & 175 deletions
Large diffs are not rendered by default.

src/Migration/Resource.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ abstract class Resource implements \JsonSerializable
3030

3131
public const TYPE_DATABASE = 'database';
3232

33+
public const TYPE_DATABASE_LEGACY = 'legacy';
34+
35+
public const TYPE_DATABASE_TABLESDB = 'tablesdb';
36+
37+
public const TYPE_DATABASE_DOCUMENTSDB = 'documentsdb';
38+
public const TYPE_DATABASE_VECTORSDB = 'vectorsdb';
39+
3340
public const TYPE_ROW = 'row';
3441

3542
public const TYPE_FILE = 'file';
@@ -84,6 +91,8 @@ abstract class Resource implements \JsonSerializable
8491
self::TYPE_BUCKET,
8592
self::TYPE_TABLE,
8693
self::TYPE_DATABASE,
94+
self::TYPE_DATABASE_VECTORSDB,
95+
self::TYPE_DATABASE_DOCUMENTSDB,
8796
self::TYPE_ROW,
8897
self::TYPE_FILE,
8998
self::TYPE_FUNCTION,
@@ -108,6 +117,39 @@ abstract class Resource implements \JsonSerializable
108117
self::TYPE_COLLECTION,
109118
];
110119

120+
// index terminology is same for all
121+
public const DATABASE_TYPE_RESOURCE_MAP = [
122+
self::TYPE_DATABASE => [
123+
'entity' => self::TYPE_TABLE,
124+
'field' => self::TYPE_COLUMN,
125+
'record' => self::TYPE_ROW,
126+
],
127+
self::TYPE_DATABASE_DOCUMENTSDB => [
128+
'entity' => self::TYPE_COLLECTION,
129+
// HACK: not required in documentsdb but adding it for consistency in the db reader(not gonna impact)
130+
'field' => self::TYPE_ATTRIBUTE,
131+
'record' => self::TYPE_DOCUMENT,
132+
],
133+
self::TYPE_DATABASE_VECTORSDB => [
134+
'entity' => self::TYPE_COLLECTION,
135+
'field' => self::TYPE_ATTRIBUTE,
136+
'record' => self::TYPE_DOCUMENT,
137+
]
138+
];
139+
140+
public const ENTITY_TYPE_RESOURCE_MAP = [
141+
self::TYPE_TABLE => [
142+
'field' => self::TYPE_COLUMN,
143+
'record' => self::TYPE_ROW,
144+
'index' => self::TYPE_INDEX
145+
],
146+
self::TYPE_COLLECTION => [
147+
'field' => self::TYPE_ATTRIBUTE,
148+
'record' => self::TYPE_DOCUMENT,
149+
'index' => self::TYPE_INDEX
150+
],
151+
];
152+
111153
protected string $id = '';
112154

113155
protected string $originalId = '';
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?php
2+
3+
namespace Utopia\Migration\Resources\Database;
4+
5+
use Utopia\Migration\Resource;
6+
use Utopia\Migration\Transfer;
7+
8+
class Attribute extends Resource
9+
{
10+
public const TYPE_STRING = 'string';
11+
public const TYPE_INTEGER = 'integer';
12+
public const TYPE_FLOAT = 'double';
13+
public const TYPE_BOOLEAN = 'boolean';
14+
public const TYPE_DATETIME = 'datetime';
15+
public const TYPE_EMAIL = 'email';
16+
public const TYPE_ENUM = 'enum';
17+
public const TYPE_IP = 'ip';
18+
public const TYPE_URL = 'url';
19+
public const TYPE_RELATIONSHIP = 'relationship';
20+
21+
public const TYPE_POINT = 'point';
22+
public const TYPE_LINE = 'linestring';
23+
public const TYPE_POLYGON = 'polygon';
24+
25+
public const TYPE_OBJECT = 'object';
26+
public const TYPE_VECTOR = 'vector';
27+
28+
/**
29+
* @param string $key
30+
* @param Table $table
31+
* @param string $fieldType The actual field type (e.g., 'string', 'integer', 'email')
32+
* @param int $size
33+
* @param bool $required
34+
* @param mixed|null $default
35+
* @param bool $array
36+
* @param bool $signed
37+
* @param string $format
38+
* @param array<string, mixed> $formatOptions
39+
* @param array<string> $filters
40+
* @param array<string, mixed> $options
41+
* @param string $createdAt
42+
* @param string $updatedAt
43+
*/
44+
public function __construct(
45+
protected readonly string $key,
46+
protected readonly Table $table,
47+
protected readonly string $fieldType = '',
48+
protected readonly int $size = 0,
49+
protected readonly bool $required = false,
50+
protected readonly mixed $default = null,
51+
protected readonly bool $array = false,
52+
protected readonly bool $signed = false,
53+
protected readonly string $format = '',
54+
protected readonly array $formatOptions = [],
55+
protected readonly array $filters = [],
56+
protected array $options = [],
57+
protected string $createdAt = '',
58+
protected string $updatedAt = '',
59+
) {
60+
}
61+
62+
/**
63+
* @return array<string, mixed>
64+
*/
65+
public function jsonSerialize(): array
66+
{
67+
return [
68+
'key' => $this->key,
69+
'table' => $this->table,
70+
'type' => $this->getType(),
71+
'size' => $this->size,
72+
'required' => $this->required,
73+
'default' => $this->default,
74+
'array' => $this->array,
75+
'signed' => $this->signed,
76+
'format' => $this->format,
77+
'formatOptions' => $this->formatOptions,
78+
'filters' => $this->filters,
79+
'options' => $this->options,
80+
'createdAt' => $this->createdAt,
81+
'updatedAt' => $this->updatedAt,
82+
];
83+
}
84+
85+
public static function getName(): string
86+
{
87+
return Resource::TYPE_ATTRIBUTE;
88+
}
89+
90+
public function getType(): string
91+
{
92+
return $this->fieldType;
93+
}
94+
95+
/**
96+
* Convert a Column resource to an Attribute resource.
97+
*
98+
* @param Column $column
99+
* @return self
100+
*/
101+
public static function fromColumn(Column $column): self
102+
{
103+
return new self(
104+
$column->getKey(),
105+
$column->getTable(),
106+
$column->getType(),
107+
$column->getSize(),
108+
$column->isRequired(),
109+
$column->getDefault(),
110+
$column->isArray(),
111+
$column->isSigned(),
112+
$column->getFormat(),
113+
$column->getFormatOptions(),
114+
$column->getFilters(),
115+
$column->getOptions(),
116+
$column->getCreatedAt(),
117+
$column->getUpdatedAt()
118+
);
119+
}
120+
121+
public function getGroup(): string
122+
{
123+
return Transfer::GROUP_DATABASES;
124+
}
125+
126+
public function getKey(): string
127+
{
128+
return $this->key;
129+
}
130+
131+
public function getTable(): Table
132+
{
133+
return $this->table;
134+
}
135+
136+
public function getSize(): int
137+
{
138+
return $this->size;
139+
}
140+
141+
public function isRequired(): bool
142+
{
143+
return $this->required;
144+
}
145+
146+
public function getDefault(): mixed
147+
{
148+
return $this->default;
149+
}
150+
151+
public function isArray(): bool
152+
{
153+
return $this->array;
154+
}
155+
156+
public function isSigned(): bool
157+
{
158+
return $this->signed;
159+
}
160+
161+
public function getFormat(): string
162+
{
163+
return $this->format;
164+
}
165+
166+
/**
167+
* @return array<string, mixed>
168+
*/
169+
public function getFormatOptions(): array
170+
{
171+
return $this->formatOptions;
172+
}
173+
174+
/**
175+
* @return array<string>
176+
*/
177+
public function getFilters(): array
178+
{
179+
return $this->filters;
180+
}
181+
182+
/**
183+
* @return array<string, mixed>
184+
*/
185+
public function &getOptions(): array
186+
{
187+
return $this->options;
188+
}
189+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Utopia\Migration\Resources\Database;
4+
5+
use Utopia\Migration\Resource;
6+
7+
class Collection extends Table
8+
{
9+
public static function getName(): string
10+
{
11+
return Resource::TYPE_COLLECTION;
12+
}
13+
14+
/**
15+
* @param array{
16+
* database: array{
17+
* id: string,
18+
* name: string,
19+
* type: string
20+
* },
21+
* name: string,
22+
* id: string,
23+
* documentSecurity?: bool,
24+
* rowSecurity?: bool,
25+
* permissions: ?array<string>,
26+
* createdAt: string,
27+
* updatedAt: string,
28+
* enabled: bool
29+
* } $array
30+
*/
31+
public static function fromArray(array $array): self
32+
{
33+
$database = match ($array['database']['type']) {
34+
Resource::TYPE_DATABASE_DOCUMENTSDB => DocumentsDB::fromArray($array['database']),
35+
Resource::TYPE_DATABASE_VECTORSDB => VectorsDB::fromArray($array['database']),
36+
default => Database::fromArray($array['database'])
37+
};
38+
39+
return new self(
40+
$database,
41+
name: $array['name'],
42+
id: $array['id'],
43+
rowSecurity: $array['rowSecurity'] ?? $array['documentSecurity'],
44+
permissions: $array['permissions'] ?? [],
45+
createdAt: $array['createdAt'] ?? '',
46+
updatedAt: $array['updatedAt'] ?? '',
47+
enabled: $array['enabled'] ?? true,
48+
);
49+
}
50+
}

src/Migration/Resources/Database/Column.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ abstract class Column extends Resource
2727
public const TYPE_LINE = 'linestring';
2828
public const TYPE_POLYGON = 'polygon';
2929

30+
public const TYPE_OBJECT = 'object';
31+
public const TYPE_VECTOR = 'vector';
32+
3033
/**
3134
* @param string $key
3235
* @param Table $table
@@ -157,4 +160,16 @@ public function &getOptions(): array
157160
{
158161
return $this->options;
159162
}
163+
164+
/**
165+
* Convert this Column resource to an Attribute resource.
166+
* This provides a deterministic way to derive attributes from columns,
167+
* eliminating the need to maintain duplicate per-type Attribute implementations.
168+
*
169+
* @return Attribute
170+
*/
171+
public function getAttribute(): Attribute
172+
{
173+
return Attribute::fromColumn($this);
174+
}
160175
}

0 commit comments

Comments
 (0)