Skip to content

Commit 9f4945e

Browse files
authored
[DCP] Remove DB and Table Auto Creation from DCP Service + Modify the schema to match Ingestion Pipeline (#26)
* Removes the logic to create the DB and update the schema from the DCP service. The Ingestion Pipeline will be responsible for this moving forward. * Also modify the Edge schema to match the new spanner schema from ingestion pipeline. Verified this works with retrieval * Graph Service changes to reflect schema updates. * Lint
1 parent d90d19d commit 9f4945e

File tree

2 files changed

+2
-45
lines changed

2 files changed

+2
-45
lines changed

packages/datacommons-api/datacommons_api/api_cli.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from datacommons_api.app import app
1919
from datacommons_api.core.config import get_config, initialize_config
2020
from datacommons_api.core.logging import get_logger, setup_logging
21-
from datacommons_db.session import get_session, initialize_db
21+
from datacommons_db.session import get_session
2222
from datacommons_api.services.graph_service import GraphService
2323

2424
setup_logging()
@@ -56,16 +56,6 @@ def start(
5656
gcp_spanner_database_name=gcp_spanner_database_name,
5757
)
5858

59-
# Initialize the database
60-
logger.info("Initializing database...")
61-
logger.info("GCP Project ID: %s", config.GCP_PROJECT_ID)
62-
logger.info("GCP Spanner Instance ID: %s", config.GCP_SPANNER_INSTANCE_ID)
63-
logger.info("GCP Spanner Database Name: %s", config.GCP_SPANNER_DATABASE_NAME)
64-
initialize_db(
65-
config.GCP_PROJECT_ID,
66-
config.GCP_SPANNER_INSTANCE_ID,
67-
config.GCP_SPANNER_DATABASE_NAME,
68-
)
6959
logger.info("Starting API server...")
7060
uvicorn.run(
7161
"datacommons_api.app:app",

packages/datacommons-db/datacommons_db/session.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
import logging
1616

17-
from sqlalchemy import Engine, create_engine, inspect
17+
from sqlalchemy import Engine, create_engine
1818
from sqlalchemy.orm import Session, sessionmaker
1919

2020
from datacommons_db.models.base import Base
2121

2222
logger = logging.getLogger(__name__)
2323

24-
REQUIRED_TABLES = ["Edge", "Node", "Observation"]
25-
2624

2725
# DDL for Creating Property Graph
2826
DDL_PROPERTY_GRAPH = """
@@ -93,34 +91,3 @@ def get_session(project_id: str, instance_id: str, database_name: str) -> Sessio
9391
engine = get_engine(project_id, instance_id, database_name)
9492
session = sessionmaker(bind=engine)
9593
return session()
96-
97-
98-
def initialize_db(project_id: str, instance_id: str, database_name: str):
99-
"""Initialize the Spanner database.
100-
101-
Args:
102-
project_id: GCP project ID
103-
instance_id: Cloud Spanner instance ID
104-
database_name: Cloud Spanner database name
105-
"""
106-
engine = get_engine(project_id, instance_id, database_name)
107-
108-
# Check if database is empty by inspecting existing tables
109-
inspector = inspect(engine)
110-
existing_tables = inspector.get_table_names()
111-
112-
# Check if all required tables exist
113-
missing_tables = [
114-
table for table in REQUIRED_TABLES if table not in existing_tables
115-
]
116-
if missing_tables:
117-
logger.warning(
118-
"Missing required tables in database %s: %s", database_name, missing_tables
119-
)
120-
121-
# Only create tables if database is completely empty
122-
if not existing_tables or missing_tables:
123-
# Import all models so they are properly initialized with the call to Base.metadata.create_all
124-
logger.info("Creating tables %s in database %s", REQUIRED_TABLES, database_name)
125-
Base.metadata.create_all(engine)
126-
create_property_graph(engine)

0 commit comments

Comments
 (0)