Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions tiled/catalog/migrations/versions/e05e918092c3_add_closure_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ def upgrade():
)
logger.info("Inserted root node with id=0.")

# 4. Insert self-referential records into nodes_closure for each node, including the "root" node
connection.execute(
sa.text(
"""
INSERT INTO nodes_closure(ancestor, descendant, depth)
SELECT id, id, 0 FROM nodes;
"""
)
)
logger.info("Inserted self-referential records into 'nodes_closure' for each node.")

# 5. Populate the 'parent' column of the 'nodes' table based on the 'ancestors' column
json_len_func = (
"jsonb_array_length"
Expand Down Expand Up @@ -196,6 +207,7 @@ def upgrade():
WHERE {json_len_func}(child.ancestors) >= {depth + 1}
AND {condition_statement}
AND parent.parent = child.parent
AND child.id != parent.id;
"""
)
)
Expand All @@ -217,24 +229,6 @@ def upgrade():
)
logger.info("Completed updating 'parent' column recursively.")

# 7½. Insert self-referential records into nodes_closure for each node, including the "root" node
# (Any conflicts here are clearly a mistake, so we just clobber them)
conflict_strategy = (
"ON CONFLICT (ancestor, descendant) DO UPDATE SET depth = EXCLUDED.depth"
if connection.engine.dialect.name == "postgresql"
else ""
)
connection.execute(
sa.text(
f"""
INSERT INTO nodes_closure(ancestor, descendant, depth)
SELECT id, id, 0 FROM nodes
{conflict_strategy};
"""
)
)
logger.info("Inserted self-referential records into 'nodes_closure' for each node.")

# 8. Update index in the 'nodes' table: drop old, add new
op.drop_index("top_level_metadata", table_name="nodes")
op.create_index(
Expand Down
Loading