Skip to content

Commit d1f1eac

Browse files
authored
Merge pull request #4412 from opsmill/dga-20240921-rm-index-value
Remove the database index on AttributeValue value to allow attribute value to be larger than 8167 bytes
2 parents 1be1759 + c45bfff commit d1f1eac

File tree

10 files changed

+115
-4
lines changed

10 files changed

+115
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
GRAPH_VERSION = 13
1+
GRAPH_VERSION = 14

backend/infrahub/core/graph/index.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
IndexItem(name="node_kind", label="Node", properties=["kind"], type=IndexType.RANGE),
99
IndexItem(name="attr_name", label="Attribute", properties=["name"], type=IndexType.RANGE),
1010
IndexItem(name="attr_uuid", label="Attribute", properties=["uuid"], type=IndexType.RANGE),
11-
IndexItem(name="attr_value", label="AttributeValue", properties=["value"], type=IndexType.RANGE),
1211
IndexItem(name="attr_ipnet_bin", label="AttributeIPNetwork", properties=["binary_address"], type=IndexType.RANGE),
1312
IndexItem(name="attr_iphost_bin", label="AttributeIPHost", properties=["binary_address"], type=IndexType.RANGE),
1413
IndexItem(name="rel_uuid", label="Relationship", properties=["uuid"], type=IndexType.RANGE),

backend/infrahub/core/migrations/graph/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .m011_remove_profile_relationship_schema import Migration011
1616
from .m012_convert_account_generic import Migration012
1717
from .m013_convert_git_password_credential import Migration013
18+
from .m014_remove_index_attr_value import Migration014
1819

1920
if TYPE_CHECKING:
2021
from infrahub.core.root import Root
@@ -35,6 +36,7 @@
3536
Migration011,
3637
Migration012,
3738
Migration013,
39+
Migration014,
3840
]
3941

4042

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Sequence
4+
5+
from infrahub.core.migrations.shared import MigrationResult
6+
from infrahub.core.query import Query # noqa: TCH001
7+
from infrahub.database import DatabaseType
8+
from infrahub.database.constants import IndexType
9+
from infrahub.database.index import IndexItem
10+
11+
from ..shared import GraphMigration
12+
13+
if TYPE_CHECKING:
14+
from infrahub.database import InfrahubDatabase
15+
16+
17+
INDEX_TO_DELETE = IndexItem(name="attr_value", label="AttributeValue", properties=["value"], type=IndexType.RANGE)
18+
19+
20+
class Migration014(GraphMigration):
21+
name: str = "014_remove_index_attr_value"
22+
queries: Sequence[type[Query]] = []
23+
minimum_version: int = 13
24+
25+
async def execute(self, db: InfrahubDatabase) -> MigrationResult:
26+
result = MigrationResult()
27+
28+
# Only execute this migration for Neo4j
29+
if db.db_type != DatabaseType.NEO4J:
30+
return result
31+
32+
async with db.start_transaction() as ts:
33+
try:
34+
ts.manager.index.init(nodes=[INDEX_TO_DELETE], rels=[])
35+
await ts.manager.index.drop()
36+
except Exception as exc: # pylint: disable=broad-exception-caught
37+
result.errors.append(str(exc))
38+
return result
39+
40+
return result
41+
42+
async def validate_migration(self, db: InfrahubDatabase) -> MigrationResult:
43+
result = MigrationResult()
44+
return result

backend/infrahub/core/node/resource_manager/number_pool.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ async def get_resource(
4242

4343
async def get_next(self, db: InfrahubDatabase, branch: Branch) -> int:
4444
query = await NumberPoolGetUsed.init(db=db, branch=branch, pool=self, branch_agnostic=True)
45-
4645
await query.execute(db=db)
4746
taken = [result.get_as_optional_type("av.value", return_type=int) for result in query.results]
4847
next_number = find_next_free(

backend/infrahub/core/query/resource_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: dict[str, Any]) -> No
211211
query = """
212212
MATCH (pool:%(number_pool)s { uuid: $pool_id })-[r:IS_RESERVED]->(av:AttributeValue )
213213
WHERE
214-
av.value >= $start_range and av.value <= $end_range
214+
toInteger(av.value) >= $start_range and toInteger(av.value) <= $end_range
215215
AND
216216
%(branch_filter)s
217217
""" % {"branch_filter": branch_filter, "number_pool": InfrahubKind.NUMBERPOOL}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from infrahub.core.migrations.graph.m014_remove_index_attr_value import (
2+
Migration014,
3+
)
4+
from infrahub.database import DatabaseType, InfrahubDatabase
5+
from infrahub.database.constants import IndexType
6+
from infrahub.database.index import IndexItem
7+
8+
9+
async def test_migration_014(
10+
db: InfrahubDatabase,
11+
reset_registry,
12+
default_branch,
13+
delete_all_nodes_in_db,
14+
):
15+
indexes = [
16+
IndexItem(name="node_uuid", label="Node", properties=["uuid"], type=IndexType.RANGE),
17+
IndexItem(name="attr_value", label="AttributeValue", properties=["value"], type=IndexType.RANGE),
18+
]
19+
20+
db.manager.index.init(nodes=indexes, rels=[])
21+
await db.manager.index.add()
22+
nbr_indexes_before = len(await db.manager.index.list())
23+
24+
async with db.start_session() as dbs:
25+
migration = Migration014()
26+
execution_result = await migration.execute(db=dbs)
27+
assert not execution_result.errors
28+
29+
validation_result = await migration.validate_migration(db=dbs)
30+
assert not validation_result.errors
31+
32+
nbr_indexes_after = len(await db.manager.index.list())
33+
if db.db_type == DatabaseType.NEO4J:
34+
assert nbr_indexes_before - nbr_indexes_after == 1
35+
else:
36+
assert nbr_indexes_before - nbr_indexes_after == 0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from infrahub.core.branch import Branch
2+
from infrahub.core.initialization import initialize_registry
3+
from infrahub.core.node import Node
4+
from infrahub.core.schema import SchemaRoot
5+
from infrahub.database import InfrahubDatabase
6+
from tests.helpers.schema import TICKET, load_schema
7+
8+
9+
async def test_allocate_from_number_pool(db: InfrahubDatabase, default_branch: Branch, register_core_models_schema):
10+
await load_schema(db=db, schema=SchemaRoot(nodes=[TICKET]))
11+
await initialize_registry(db=db)
12+
13+
np1 = await Node.init(db=db, schema="CoreNumberPool")
14+
await np1.new(db=db, name="pool1", node="TestingTicket", node_attribute="ticket_id", start_range=1, end_range=10)
15+
await np1.save(db=db)
16+
17+
ticket1 = await Node.init(db=db, schema=TICKET.kind)
18+
await ticket1.new(db=db, title="ticket1", ticket_id={"from_pool": {"id": np1.id}})
19+
await ticket1.save(db=db)
20+
21+
ticket2 = await Node.init(db=db, schema=TICKET.kind)
22+
await ticket2.new(db=db, title="ticket2", ticket_id={"from_pool": {"id": np1.id}})
23+
await ticket2.save(db=db)
24+
25+
assert ticket1.ticket_id.value == 1
26+
assert ticket2.ticket_id.value == 2

backend/tests/unit/graphql/queries/test_resource_pool.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ async def test_number_pool_utilization(db: InfrahubDatabase, default_branch: Bra
626626
},
627627
)
628628

629+
assert not first.errors
630+
assert not second.errors
631+
assert not third.errors
632+
629633
assert first.data
630634
assert second.data
631635
assert third.data

changelog/4399.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Removed database index in Attribute Value to attribute larger than 8167 bytes

0 commit comments

Comments
 (0)