Skip to content

Commit

Permalink
Core: Fix circular import when initializing schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rdimaio committed Nov 7, 2024
1 parent e9cb135 commit 99f0ade
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 146 deletions.
76 changes: 38 additions & 38 deletions lib/rucio/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from rucio.common.cache import CacheKey, MemcacheRegion
from rucio.common.exception import ConfigNotFound
from rucio.db.sqla import models
from rucio.db.sqla.models import Config
from rucio.db.sqla.session import read_session, transactional_session

T = TypeVar('T')
Expand Down Expand Up @@ -56,7 +56,7 @@ def sections(
all_sections = read_from_cache(SECTIONS_CACHE_KEY, expiration_time)
if isinstance(all_sections, NoValue):
stmt = select(
models.Config.section
Config.section
).distinct(
)
all_sections = list(session.execute(stmt).scalars().all())
Expand Down Expand Up @@ -99,9 +99,9 @@ def has_section(
has_section = read_from_cache(has_section_key, expiration_time)
if isinstance(has_section, NoValue):
stmt = select(
models.Config
Config
).where(
models.Config.section == section
Config.section == section
)
has_section = session.execute(stmt).first() is not None
write_to_cache(has_section_key, has_section)
Expand Down Expand Up @@ -131,9 +131,9 @@ def options(
options = read_from_cache(options_key, expiration_time)
if isinstance(options, NoValue):
stmt = select(
models.Config.opt
Config.opt
).where(
models.Config.section == section
Config.section == section
).distinct()
options = list(session.execute(stmt).scalars().all())
write_to_cache(options_key, options)
Expand Down Expand Up @@ -165,10 +165,10 @@ def has_option(
has_option = read_from_cache(has_option_key, expiration_time)
if isinstance(has_option, NoValue):
stmt = select(
models.Config
Config
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
)
has_option = session.execute(stmt).first() is not None
write_to_cache(has_option_key, has_option)
Expand Down Expand Up @@ -207,10 +207,10 @@ def get(
value = read_from_cache(value_key, expiration_time)
if isinstance(value, NoValue):
stmt = select(
models.Config.value
Config.value
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
)
tmp = session.execute(stmt).first()
if tmp is not None:
Expand Down Expand Up @@ -251,10 +251,10 @@ def items(
items = read_from_cache(items_key, expiration_time)
if isinstance(items, NoValue):
stmt = select(
models.Config.opt,
models.Config.value
Config.opt,
Config.value
).where(
models.Config.section == section
Config.section == section
)
items = session.execute(stmt).all()
write_to_cache(items_key, items)
Expand All @@ -281,7 +281,7 @@ def set(
if not has_option(section=section, option=option, use_cache=False, session=session):
section_existed = has_section(section=section)

new_option = models.Config(section=section, opt=option, value=value)
new_option = Config(section=section, opt=option, value=value)
new_option.save(session=session)

delete_from_cache(key=CacheKey.value(section, option))
Expand All @@ -292,24 +292,24 @@ def set(
delete_from_cache(key=CacheKey.has_section(section))
else:
stmt = select(
models.Config.value
Config.value
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
)
old_value = session.execute(stmt).scalar_one_or_none()
if old_value != str(value):
old_option = models.ConfigHistory(section=section,
old_option = ConfigHistory(section=section,
opt=option,
value=old_value)
old_option.save(session=session)
stmt = update(
models.Config
Config
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
).values({
models.Config.value: str(value)
Config.value: str(value)
})
session.execute(stmt)
delete_from_cache(key=CacheKey.value(section, option))
Expand All @@ -330,22 +330,22 @@ def remove_section(section: str, *, session: "Session") -> bool:
return False
else:
stmt = select(
models.Config.value
Config.value
).where(
models.Config.section == section
Config.section == section
)
for old in session.execute(stmt).all():
old_option = models.ConfigHistory(section=old[0],
old_option = ConfigHistory(section=old[0],
opt=old[1],
value=old[2])
old_option.save(session=session)
delete_from_cache(key=CacheKey.has_option(old[0], old[1]))
delete_from_cache(key=CacheKey.value(old[0], old[1]))

stmt = delete(
models.Config
Config
).where(
models.Config.section == section
Config.section == section
)
session.execute(stmt)
delete_from_cache(key=SECTIONS_CACHE_KEY)
Expand All @@ -368,31 +368,31 @@ def remove_option(section: str, option: str, *, session: "Session") -> bool:
return False
else:
stmt = select(
models.Config.value
Config.value
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
)
result = session.execute(stmt).scalar_one_or_none()
old_option = models.ConfigHistory(section=section,
old_option = ConfigHistory(section=section,
opt=option,
value=result)
old_option.save(session=session)

stmt = delete(
models.Config
Config
).where(
and_(models.Config.section == section,
models.Config.opt == option)
and_(Config.section == section,
Config.opt == option)
)
session.execute(stmt)

stmt = select(
func.count()
).select_from(
models.Config
Config
).where(
models.Config.section == section
Config.section == section
)
if not session.execute(stmt).scalar_one_or_none():
# we deleted the last config entry in the section. Invalidate the section cache
Expand Down
Loading

0 comments on commit 99f0ade

Please sign in to comment.