Skip to content

Commit

Permalink
fix(cli): handles fields and collections naming conflicts in snapshot…
Browse files Browse the repository at this point in the history
… files (#119) (#120)

* feat(e2e): add test

* feat(e2e): add test for colletions  conflicts

* fix(e2e): test for colletions

* fix(cli): deal fields and collection naming conflicts

* chore: update package-lock.json
  • Loading branch information
EdouardDem authored Nov 21, 2024
1 parent 5d4c454 commit 955233a
Show file tree
Hide file tree
Showing 29 changed files with 487 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 51 additions & 6 deletions packages/cli/src/lib/services/snapshot/snapshot-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,76 @@ export class SnapshotClient {
{ path: INFO_JSON, content: info },
];

// Split collections
/*
* Split collections
* Folder and collections may have the same name (with different casing).
* Also, some file systems are case-insensitive.
*/
const existingCollections = new Set<string>();
for (const collection of collections) {
const suffix = this.getSuffix(collection.collection, existingCollections);
files.push({
path: `${COLLECTIONS_DIR}/${collection.collection}.json`,
path: `${COLLECTIONS_DIR}/${collection.collection}${suffix}.json`,
content: collection,
});
}
// Split fields

/*
* Split fields
* Groups and fields may have the same name (with different casing).
* Also, some file systems are case-insensitive.
* Therefore, inside a collection we have to deal with names conflicts.
*/
const existingFiles = new Set<string>();
for (const field of fields) {
const suffix = this.getSuffix(
`${field.collection}/${field.field}`,
existingFiles,
);
files.push({
path: `${FIELDS_DIR}/${field.collection}/${field.field}.json`,
path: `${FIELDS_DIR}/${field.collection}/${field.field}${suffix}.json`,
content: field,
});
}
// Split relations

/*
* Split relations
* There should not be any conflicts here, but we still split them for consistency.
*/
const existingRelations = new Set<string>();
for (const relation of relations) {
const suffix = this.getSuffix(
`${relation.collection}/${relation.field}`,
existingRelations,
);
files.push({
path: `${RELATIONS_DIR}/${relation.collection}/${relation.field}.json`,
path: `${RELATIONS_DIR}/${relation.collection}/${relation.field}${suffix}.json`,
content: relation,
});
}

return files;
}

/**
* Get the suffix that should be added to the field name in order to avoid conflicts.
*/
protected getSuffix(baseName: string, existing: Set<string>): string {
const base = baseName.toLowerCase(); // Some file systems are case-insensitive
let suffix = '';

if (existing.has(base)) {
let i = 2;
while (existing.has(`${base}_${i}`)) {
i++;
}
suffix = `_${i}`;
}

existing.add(`${base}${suffix}`);
return suffix;
}

/**
* Get the diff from Directus instance
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"name": "Administrator",
"icon": "verified",
"description": "$t:admin_description",
"ip_access": null,
"enforce_tfa": false,
"admin_access": true,
"app_access": true,
"roles": [
{
"role": "_sync_default_admin_role",
"sort": null
}
],
"_syncId": "_sync_default_admin_policy"
},
{
"name": "$t:public_label",
"icon": "public",
"description": "$t:public_description",
"ip_access": null,
"enforce_tfa": false,
"admin_access": false,
"app_access": false,
"roles": [
{
"role": null,
"sort": 1
}
],
"_syncId": "_sync_default_public_policy"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"name": "Administrator",
"icon": "verified",
"description": "$t:admin_description",
"parent": null,
"_syncId": "_sync_default_admin_role"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"collection": "Profile",
"meta": {
"accountability": "all",
"archive_app_filter": true,
"archive_field": null,
"archive_value": null,
"collapse": "open",
"collection": "Profile",
"color": null,
"display_template": null,
"group": null,
"hidden": false,
"icon": "folder",
"item_duplication_fields": null,
"note": null,
"preview_url": null,
"singleton": false,
"sort": 1,
"sort_field": null,
"translations": null,
"unarchive_value": null,
"versioning": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"collection": "directus_sync_id_map",
"meta": null,
"schema": {
"name": "directus_sync_id_map"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"collection": "profile",
"meta": {
"accountability": "all",
"archive_app_filter": true,
"archive_field": null,
"archive_value": null,
"collapse": "open",
"collection": "profile",
"color": null,
"display_template": null,
"group": null,
"hidden": false,
"icon": null,
"item_duplication_fields": null,
"note": null,
"preview_url": null,
"singleton": false,
"sort": null,
"sort_field": null,
"translations": null,
"unarchive_value": null,
"versioning": false
},
"schema": {
"name": "profile"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collection": "directus_sync_id_map",
"field": "created_at",
"type": "dateTime",
"meta": null,
"schema": {
"name": "created_at",
"table": "directus_sync_id_map",
"data_type": "datetime",
"default_value": "CURRENT_TIMESTAMP",
"max_length": null,
"numeric_precision": null,
"numeric_scale": null,
"is_nullable": true,
"is_unique": false,
"is_indexed": true,
"is_primary_key": false,
"is_generated": false,
"generation_expression": null,
"has_auto_increment": false,
"foreign_key_table": null,
"foreign_key_column": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collection": "directus_sync_id_map",
"field": "id",
"type": "integer",
"meta": null,
"schema": {
"name": "id",
"table": "directus_sync_id_map",
"data_type": "integer",
"default_value": null,
"max_length": null,
"numeric_precision": null,
"numeric_scale": null,
"is_nullable": false,
"is_unique": false,
"is_indexed": false,
"is_primary_key": true,
"is_generated": false,
"generation_expression": null,
"has_auto_increment": true,
"foreign_key_table": null,
"foreign_key_column": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collection": "directus_sync_id_map",
"field": "local_id",
"type": "string",
"meta": null,
"schema": {
"name": "local_id",
"table": "directus_sync_id_map",
"data_type": "varchar",
"default_value": null,
"max_length": 255,
"numeric_precision": null,
"numeric_scale": null,
"is_nullable": false,
"is_unique": false,
"is_indexed": false,
"is_primary_key": false,
"is_generated": false,
"generation_expression": null,
"has_auto_increment": false,
"foreign_key_table": null,
"foreign_key_column": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collection": "directus_sync_id_map",
"field": "sync_id",
"type": "string",
"meta": null,
"schema": {
"name": "sync_id",
"table": "directus_sync_id_map",
"data_type": "varchar",
"default_value": null,
"max_length": 255,
"numeric_precision": null,
"numeric_scale": null,
"is_nullable": false,
"is_unique": false,
"is_indexed": false,
"is_primary_key": false,
"is_generated": false,
"generation_expression": null,
"has_auto_increment": false,
"foreign_key_table": null,
"foreign_key_column": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collection": "directus_sync_id_map",
"field": "table",
"type": "string",
"meta": null,
"schema": {
"name": "table",
"table": "directus_sync_id_map",
"data_type": "varchar",
"default_value": null,
"max_length": 255,
"numeric_precision": null,
"numeric_scale": null,
"is_nullable": false,
"is_unique": false,
"is_indexed": false,
"is_primary_key": false,
"is_generated": false,
"generation_expression": null,
"has_auto_increment": false,
"foreign_key_table": null,
"foreign_key_column": null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"collection": "profile",
"field": "Content",
"type": "alias",
"meta": {
"collection": "profile",
"conditions": null,
"display": null,
"display_options": null,
"field": "Content",
"group": null,
"hidden": false,
"interface": "group-raw",
"note": null,
"options": null,
"readonly": false,
"required": false,
"sort": 3,
"special": ["alias", "no-data", "group"],
"translations": null,
"validation": null,
"validation_message": null,
"width": "full"
}
}
Loading

0 comments on commit 955233a

Please sign in to comment.