[refactor] postgres-init,solution: DB 구조 재편 — 스키마 해체 · 공용 콘텐츠 한 벌 · 마이그레이션 체계
도메인별 스키마(company·place·fact·local·site·job)를 걷어내고 public 한 벌로 폈다.
스키마 한정자가 붙은 순간부터 ORM·raw SQL·테스트 픽스처가 각자 그 이름을 들고 다녀야 했다.
- 공용 콘텐츠를 한 테이블로 되돌린다. spots·region_stories 를 따로 파 놓고 보니
같은 성격이 세 곳으로 갈라져 있었다 — `area_contents` 가 처음부터 content_type 으로
종류를 가르는 설계였고 그걸 쓰면 됐다. 관계(거리·숨김)만 `place_area_refs` 로 남긴다.
- migrations/ + scripts/migrate.py: `init.sql` 은 **DB 를 처음 만들 때만** 돈다. 파일에
컬럼을 더해도 이미 데이터가 든 DB 에는 반영되지 않는다 — 실제로 TourAPI 가 주변 정보를
받아 와도 저장할 곳이 없어 축제·맛집이 0건이었고, 화면에는 "그냥 안 나오는 것" 으로만 보였다.
DECISIONS.md 가 예고한 그대로다("운영 DB 가 생기는 순간 다시 필요해진다").
Alembic 을 쓰지 않는 이유는 스키마 정의가 이미 두 곳(ORM·init.sql)이라 세 번째를
더하면 어긋날 자리가 하나 더 생기기 때문이다.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7bbeb068a8
commit
64ce467f21
189 files changed
+13374
-5023
No files matched your search
@@ -5,7 +5,7 @@ from sqlalchemy import and_, delete, func, or_, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import place_links, places, units
|
||||
from common.database.model.models import place_channels, places, place_units
|
||||
from common.enums import ErrorType
|
||||
from common.logger import LOG
|
||||
from common.utils.gtime import GTime
|
||||
@@ -38,11 +38,11 @@ class IPlaceCRUD(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def get_unit(self, cdb: AsyncSession, place_id, unit_id) -> Tuple[ErrorType, units]:
|
||||
async def get_unit(self, cdb: AsyncSession, place_id, unit_id) -> Tuple[ErrorType, place_units]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def add_unit(self, cdb: AsyncSession, unit: units) -> ErrorType:
|
||||
async def add_unit(self, cdb: AsyncSession, unit: place_units) -> ErrorType:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
@@ -50,7 +50,7 @@ class IPlaceCRUD(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def add_link(self, cdb: AsyncSession, link: place_links) -> ErrorType:
|
||||
async def add_link(self, cdb: AsyncSession, link: place_channels) -> ErrorType:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
@@ -156,9 +156,9 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
async def list_units(self, cdb: AsyncSession, place_id) -> Tuple[ErrorType, list]:
|
||||
try:
|
||||
query = (
|
||||
select(units)
|
||||
.where(units.place_id == place_id, units.deleted == False) # noqa: E712
|
||||
.order_by(units.sort_order.asc(), units.created_at.asc())
|
||||
select(place_units)
|
||||
.where(place_units.place_id == place_id, place_units.deleted == False) # noqa: E712
|
||||
.order_by(place_units.sort_order.asc(), place_units.created_at.asc())
|
||||
)
|
||||
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
|
||||
return (err_type, list(rows) if err_type == ErrorType.SUCCESS else [])
|
||||
@@ -166,11 +166,11 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def get_unit(self, cdb: AsyncSession, place_id, unit_id) -> Tuple[ErrorType, units]:
|
||||
async def get_unit(self, cdb: AsyncSession, place_id, unit_id) -> Tuple[ErrorType, place_units]:
|
||||
try:
|
||||
query = (
|
||||
select(units)
|
||||
.where(units.unit_id == unit_id, units.place_id == place_id, units.deleted == False) # noqa: E712
|
||||
select(place_units)
|
||||
.where(place_units.unit_id == unit_id, place_units.place_id == place_id, place_units.deleted == False) # noqa: E712
|
||||
.limit(1)
|
||||
)
|
||||
err_type, row_list = await DB_SESSION_MNG.execute(cdb, query)
|
||||
@@ -183,7 +183,7 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, None
|
||||
|
||||
async def add_unit(self, cdb: AsyncSession, unit: units) -> ErrorType:
|
||||
async def add_unit(self, cdb: AsyncSession, unit: place_units) -> ErrorType:
|
||||
try:
|
||||
return await DB_SESSION_MNG.insert(cdb, unit)
|
||||
except Exception as ex:
|
||||
@@ -193,17 +193,17 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
async def list_links(self, cdb: AsyncSession, place_id, confirmed_only: bool = False) -> Tuple[ErrorType, list]:
|
||||
"""채널 URL 목록. confirmed_only=True 면 ★ 크롤링 대상(확정된 URL)만."""
|
||||
try:
|
||||
conditions = [place_links.place_id == place_id, place_links.deleted == False] # noqa: E712
|
||||
conditions = [place_channels.place_id == place_id, place_channels.deleted == False] # noqa: E712
|
||||
if confirmed_only:
|
||||
conditions.append(place_links.confirmed_at.is_not(None))
|
||||
query = select(place_links).where(and_(*conditions)).order_by(place_links.discovered_at.asc())
|
||||
conditions.append(place_channels.confirmed_at.is_not(None))
|
||||
query = select(place_channels).where(and_(*conditions)).order_by(place_channels.discovered_at.asc())
|
||||
err_type, rows = await DB_SESSION_MNG.execute(cdb, query)
|
||||
return (err_type, list(rows) if err_type == ErrorType.SUCCESS else [])
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
return ErrorType.DB_RUN_FAILED, []
|
||||
|
||||
async def add_link(self, cdb: AsyncSession, link: place_links) -> ErrorType:
|
||||
async def add_link(self, cdb: AsyncSession, link: place_channels) -> ErrorType:
|
||||
try:
|
||||
return await DB_SESSION_MNG.insert(cdb, link)
|
||||
except Exception as ex:
|
||||
@@ -218,12 +218,12 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
경로를 늘리지 않으려고 일부러 좁게 열어 둔다(collect_service.discover_naver_place)."""
|
||||
try:
|
||||
query = (
|
||||
update(place_links)
|
||||
update(place_channels)
|
||||
.where(
|
||||
place_links.place_id == place_id,
|
||||
place_links.url == url,
|
||||
place_links.confirmed_at.is_(None),
|
||||
place_links.deleted == False, # noqa: E712
|
||||
place_channels.place_id == place_id,
|
||||
place_channels.url == url,
|
||||
place_channels.confirmed_at.is_(None),
|
||||
place_channels.deleted == False, # noqa: E712
|
||||
)
|
||||
.values(confirmed_at=ts, confirmed_by=user_id, updated_at=ts)
|
||||
)
|
||||
@@ -246,11 +246,11 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
생성 시점에만 근거로 넘긴다(services/copy_service.py)."""
|
||||
try:
|
||||
query = (
|
||||
update(place_links)
|
||||
update(place_channels)
|
||||
.where(
|
||||
place_links.place_id == place_id,
|
||||
place_links.url == url,
|
||||
place_links.deleted == False, # noqa: E712
|
||||
place_channels.place_id == place_id,
|
||||
place_channels.url == url,
|
||||
place_channels.deleted == False, # noqa: E712
|
||||
)
|
||||
.values(raw=raw, updated_at=GTime.UTC())
|
||||
)
|
||||
@@ -263,12 +263,12 @@ class PlaceCRUD(IPlaceCRUD):
|
||||
"""미확정 링크만 확정한다(이미 확정된 건 rowcount 0 — 동시 처리 가드)."""
|
||||
try:
|
||||
query = (
|
||||
update(place_links)
|
||||
update(place_channels)
|
||||
.where(
|
||||
place_links.link_id == link_id,
|
||||
place_links.place_id == place_id,
|
||||
place_links.confirmed_at.is_(None),
|
||||
place_links.deleted == False, # noqa: E712
|
||||
place_channels.link_id == link_id,
|
||||
place_channels.place_id == place_id,
|
||||
place_channels.confirmed_at.is_(None),
|
||||
place_channels.deleted == False, # noqa: E712
|
||||
)
|
||||
.values(confirmed_at=ts, confirmed_by=user_id, updated_at=ts)
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user