Skip to content

Commit

Permalink
remove sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Jan 22, 2025
1 parent b823d86 commit 64d82eb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 90 deletions.
39 changes: 0 additions & 39 deletions breathecode/activity/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import json
import os

from google.cloud import bigquery, ndb
from sqlalchemy import JSON, TIMESTAMP, Column, Integer, String

from breathecode.utils.sqlalchemy import BigQueryBase


def is_test_env():
Expand All @@ -28,41 +24,6 @@ def _get_kind(cls):
return "student_activity"


class ActivityMeta(BigQueryBase):
__tablename__ = "4geeks.activity_nested"

email = Column(String(36), primary_key=True)
related = Column(Integer, nullable=False)
related_pk = Column(String(25), nullable=False)
# related
resource = Column(String(30), nullable=True)
resource_id = Column(String(30), nullable=True)
meta = Column(String, default="{}")
meta = Column(JSON, default="{}")
timestamp = Column(TIMESTAMP, nullable=False)


# this model is a example, it's useless because google can't support JSON on they own SQLAlchemy dialect
class Activity(BigQueryBase):
__tablename__ = "4geeks.activity"

id = Column(String(36), primary_key=True)
user_id = Column(Integer, nullable=False)
kind = Column(String(25), nullable=False)
related = Column(String(30), nullable=True)
related_id = Column(String(30), nullable=True)
meta = Column(String, default="{}")
timestamp = Column(TIMESTAMP, nullable=False)

@property
def json(self):
return json.loads(self.meta)

@json.setter
def json(self, value):
self.meta = json.dumps(value)


# it's required to transform all BigQuery models to SQLite, but it also is useless because it doesn't support JSON
# test_support(__name__)

Expand Down
14 changes: 7 additions & 7 deletions breathecode/activity/tests/mixins/media_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
"""

import os

from rest_framework.test import APITestCase

from breathecode.services.google_cloud import BigQuery
from breathecode.tests.mixins import (
GenerateModelsMixin,
BreathecodeMixin,
CacheMixin,
TokenMixin,
DatetimeMixin,
GenerateModelsMixin,
GenerateQueriesMixin,
HeadersMixin,
DatetimeMixin,
Sha256Mixin,
BreathecodeMixin,
TokenMixin,
)
from breathecode.utils.sqlalchemy import BigQueryBase
from sqlalchemy import create_engine
from breathecode.services.google_cloud import BigQuery


class MediaTestCase(
Expand Down
39 changes: 0 additions & 39 deletions breathecode/services/google_cloud/big_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
from google.cloud import bigquery
from google.cloud.bigquery.schema import SchemaField
from google.cloud.bigquery.table import RowIterator, Table
from sqlalchemy import create_engine
from sqlalchemy.engine.mock import MockConnection
from sqlalchemy.orm import sessionmaker

from breathecode.services.google_cloud import credentials
from breathecode.utils.sqlalchemy import BigQueryBase

client = None
engine = None
Expand Down Expand Up @@ -329,17 +325,12 @@ def setup():

global engine

BigQueryBase.metadata.bind = engine
BigQueryBase.metadata.create_all()

@staticmethod
def teardown():
"""Destroy the database schema."""

global engine

BigQueryBase.metadata.drop_all(engine)

@classmethod
def _setup_engine(cls):
global engine
Expand All @@ -348,8 +339,6 @@ def _setup_engine(cls):
credentials.resolve_credentials()

if not engine and is_test_env():
engine = create_engine("sqlite:///:memory:", echo=False)

client_options = ClientOptions(api_endpoint="http://0.0.0.0:9050")
client = bigquery.Client(
"test",
Expand All @@ -358,37 +347,9 @@ def _setup_engine(cls):
)

if not engine:
project = os.getenv("GOOGLE_PROJECT_ID", "")
engine = create_engine(f"bigquery://{project}")

credentials.resolve_credentials()
client = bigquery.Client(location="us-central1")

@classmethod
def session(cls) -> sessionmaker:
"""Get a BigQuery session instance."""

global engine

if not engine:
cls._setup_engine()

credentials.resolve_credentials()
session = sessionmaker(bind=engine)
return session()

@classmethod
def connection(cls) -> MockConnection:
"""Get a BigQuery connection instance."""

global engine

if not engine:
cls._setup_engine()

credentials.resolve_credentials()
return engine.connect()

@classmethod
def client(cls) -> tuple[bigquery.Client, str, str]:
"""Get a BigQuery client instance and project id."""
Expand Down
5 changes: 0 additions & 5 deletions breathecode/utils/sqlalchemy/big_query.py

This file was deleted.

0 comments on commit 64d82eb

Please sign in to comment.