I have the following two tables that represent a nested folder hierarchy
Table "folder" contains basic folder information
+----+--------------+
| id | name |
+----+--------------+
| 1 | Top |
| 2 | Science |
| 3 | Astronomy |
| 5 | Astrophysics |
| 6 | Cosmology |
+----+--------------+
Table "folder_tree" contains the hierarchy structure, using the folder ids and column path being LTREE type.
+----+--------+---------+
| id | folder | path |
+----+--------+---------+
| 32 | 1 | 1 |
| 33 | 2 | 1.2 |
| 36 | 3 | 1.2.3 |
| 37 | 4 | 1.2.3.4 |
| 38 | 5 | 1.2.3.5 |
+----+--------+---------+
I’m looking to validate an exact folder structure exists, when given an array of the structure for example how to validate [‘Top’, ‘Science’, ‘Astronomy’,’ Astrophysics’] in that order.
I believe a recursive query could work, where it looks at the root folder ‘Top’ first then works its way down to Astrophysics, confirming each folder exists along the way.
Can this be achieved with a recursive query? Or similar?
I understand that the path of the folder_tree could contain the names fully like Top.Science.etc but in this case, the folder names contain spaces and symbols which aren't allowed in LTREE.