예제 #1
0
        def go(BaseType):
            declarative = registry().mapped

            @declarative
            @dataclasses.dataclass
            class Single(BaseType):

                __tablename__ = "single"
                __mapper_args__ = dict(polymorphic_identity="type1")

            @declarative
            @dataclasses.dataclass
            class Joined(Single):
                __tablename__ = "joined"
                __mapper_args__ = dict(polymorphic_identity="type2")
                id = Column(Integer, ForeignKey("single.id"), primary_key=True)

            eq_(Single.__table__.name, "single")
            eq_(
                list(Single.__table__.c.keys()),
                ["type", "id", "value", "timestamp"],
            )
            eq_(Single.__table__.kwargs, {"mysql_engine": "InnoDB"})

            eq_(Joined.__table__.name, "joined")
            eq_(list(Joined.__table__.c.keys()), ["id"])
            eq_(Joined.__table__.kwargs, {"mysql_engine": "InnoDB"})
예제 #2
0
    def test_dupe_classes_name_race(self):
        """test the race condition that the class was garbage "
        "collected while being resolved from a dupe class."""
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)

        dupe_reg = base._class_registry["Foo"]
        dupe_reg.contents = [lambda: None]
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())
        f_resolver = resolver("Foo")
        assert_raises_message(
            exc.InvalidRequestError,
            r"When initializing mapper some_parent, expression "
            r"'Foo' failed to locate a name \('Foo'\).",
            f_resolver,
        )

        f_resolver = name_resolver("Foo")
        assert_raises_message(
            exc.InvalidRequestError,
            r"When initializing mapper some_parent, expression "
            r"'Foo' failed to locate a name \('Foo'\).",
            f_resolver,
        )
예제 #3
0
    def test_module_reg_cleanout_race(self):
        """test the race condition that a class was gc'ed as we tried
        to look it up by module name."""

        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        reg = base._class_registry["_sa_module_registry"]

        mod_entry = reg["foo"]["bar"]
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())
        f_resolver = resolver("foo")
        del mod_entry.contents["Foo"]
        assert_raises_message(
            NameError,
            "Module 'bar' has no mapped classes registered "
            "under the name 'Foo'",
            lambda: f_resolver().bar.Foo,
        )

        f_resolver = name_resolver("foo")
        assert_raises_message(
            NameError,
            "Module 'bar' has no mapped classes registered "
            "under the name 'Foo'",
            lambda: f_resolver().bar.Foo,
        )
예제 #4
0
    def test_resolve_dupe_by_name(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)

        gc_collect()

        name_resolver, resolver = clsregistry._resolver(f1, MockProp())
        resolver = resolver("Foo")
        assert_raises_message(
            exc.InvalidRequestError,
            'Multiple classes found for path "Foo" in the '
            "registry of this declarative base. Please use a "
            "fully module-qualified path.",
            resolver,
        )

        resolver = name_resolver("Foo")
        assert_raises_message(
            exc.InvalidRequestError,
            'Multiple classes found for path "Foo" in the '
            "registry of this declarative base. Please use a "
            "fully module-qualified path.",
            resolver,
        )
예제 #5
0
    def test_fragment_ambiguous(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        f3 = MockClass(base, "bat.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)
        clsregistry.add_class("Foo", f3, base._class_registry)
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())

        gc_collect()

        assert_raises_message(
            exc.InvalidRequestError,
            'Multiple classes found for path "alt.Foo" in the registry '
            "of this declarative base. Please use a fully "
            "module-qualified path.",
            resolver("alt.Foo"),
        )

        assert_raises_message(
            exc.InvalidRequestError,
            'Multiple classes found for path "alt.Foo" in the registry '
            "of this declarative base. Please use a fully "
            "module-qualified path.",
            name_resolver("alt.Foo"),
        )
예제 #6
0
 def test_module_reg_cleanout_cls_to_base(self):
     base = registry()
     f4 = MockClass(base, "single.Blat")
     clsregistry.add_class("Blat", f4, base._class_registry)
     reg = base._class_registry["_sa_module_registry"]
     assert reg["single"]
     del f4
     gc_collect()
     assert "single" not in reg
예제 #7
0
    def test_module_reg_cleanout_sub_to_base(self):
        base = registry()
        f3 = MockClass(base, "bat.bar.Hoho")
        clsregistry.add_class("Hoho", f3, base._class_registry)
        reg = base._class_registry["_sa_module_registry"]

        assert reg["bat"]["bar"]
        del f3
        gc_collect()
        assert "bat" not in reg
예제 #8
0
    def test_bound_cls_registry_decorated(self):
        reg = registry(_bind=testing.db)

        @reg.mapped
        class User(object):
            __tablename__ = "user"
            id = Column(Integer, primary_key=True)

        s = Session()

        is_(s.get_bind(User), testing.db)
예제 #9
0
    def test_bound_cls_registry_base(self):
        reg = registry(_bind=testing.db)

        Base = reg.generate_base()

        class User(Base):
            __tablename__ = "user"
            id = Column(Integer, primary_key=True)

        s = Session()

        is_(s.get_bind(User), testing.db)
예제 #10
0
    def test_bound_cls_registry_decorated(self):
        reg = registry(_bind=testing.db)

        @reg.mapped
        class User(object):
            __tablename__ = "user"
            id = Column(Integer, primary_key=True)

        s = Session()

        with testing.expect_deprecated_20(
            "This Session located a target engine via bound metadata"
        ):
            is_(s.get_bind(User), testing.db)
예제 #11
0
    def test_resolve(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())

        gc_collect()

        is_(resolver("foo.bar.Foo")(), f1)
        is_(resolver("foo.alt.Foo")(), f2)

        is_(name_resolver("foo.bar.Foo")(), f1)
        is_(name_resolver("foo.alt.Foo")(), f2)
예제 #12
0
def __create_in_memory_db_table(name, *columns, **kwargs):
    """
    Create an in-memory temporary table using the sqlite memory driver.
    Make sqlalchemy  aware of that table by registering it via a
    declarative base.
    """
    import datetime
    from sqlalchemy import Column, DateTime, CheckConstraint
    from sqlalchemy.pool import StaticPool
    from sqlalchemy.schema import Table
    from sqlalchemy.orm import registry
    from rucio.db.sqla.models import ModelBase
    from rucio.db.sqla.session import get_maker, create_engine

    engine = create_engine('sqlite://',
                           connect_args={'check_same_thread': False},
                           poolclass=StaticPool)

    # Create a class which inherits from ModelBase. This will allow us to use the rucio-specific methods like .save()
    DeclarativeObj = type('DeclarativeObj{}'.format(name), (ModelBase, ), {})
    # Create a new declarative base and map the previously created object into the base
    mapper_registry = registry()
    InMemoryBase = mapper_registry.generate_base(
        name='InMemoryBase{}'.format(name))
    table_args = tuple(columns) + tuple(kwargs.get('table_args', ())) + (
        Column("created_at", DateTime, default=datetime.datetime.utcnow),
        Column("updated_at",
               DateTime,
               default=datetime.datetime.utcnow,
               onupdate=datetime.datetime.utcnow),
        CheckConstraint('CREATED_AT IS NOT NULL',
                        name=name.upper() + '_CREATED_NN'),
        CheckConstraint('UPDATED_AT IS NOT NULL',
                        name=name.upper() + '_UPDATED_NN'),
    )
    table = Table(name, InMemoryBase.metadata, *table_args)
    mapper_registry.map_imperatively(DeclarativeObj, table)
    # Performa actual creation of the in-memory table
    InMemoryBase.metadata.create_all(engine)

    # Register the new table with the associated engine into the sqlalchemy sessionmaker
    # In theory, this code must be protected by rucio.db.scla.session._LOCK, but this code will be executed
    # during test case initialization, so there is no risk here to have concurrent calls from within the
    # same process
    senssionmaker = get_maker()
    senssionmaker.kw.setdefault('binds', {}).update({DeclarativeObj: engine})
    return DeclarativeObj
예제 #13
0
    def test_module_reg_cleanout_two_sub(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        reg = base._class_registry["_sa_module_registry"]

        f2 = MockClass(base, "foo.alt.Bar")
        clsregistry.add_class("Bar", f2, base._class_registry)
        assert reg["foo"]["bar"]
        del f1
        gc_collect()
        assert "bar" not in reg["foo"]
        assert "alt" in reg["foo"]

        del f2
        gc_collect()
        assert "foo" not in reg.contents
예제 #14
0
    def test_dupe_classes_back_to_one(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)

        del f2
        gc_collect()

        # registry restores itself to just the one class
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())
        f_resolver = resolver("Foo")
        is_(f_resolver(), f1)

        f_resolver = name_resolver("Foo")
        is_(f_resolver(), f1)
예제 #15
0
    def test_same_module_same_name(self):

        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.bar.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        gc_collect()

        with expect_warnings(
                "This declarative base already contains a class with the "
                "same class name and module name as foo.bar.Foo, and "
                "will be replaced in the string-lookup table."):
            clsregistry.add_class(
                "Foo",
                f2,
                base._class_registry,
            )
예제 #16
0
    def test_dupe_classes_cleanout(self):
        # force this to maintain isolation between tests
        clsregistry._registries.clear()

        base = registry()

        for i in range(3):
            f1 = MockClass(base, "foo.bar.Foo")
            f2 = MockClass(base, "foo.alt.Foo")
            clsregistry.add_class("Foo", f1, base._class_registry)
            clsregistry.add_class("Foo", f2, base._class_registry)

            eq_(len(clsregistry._registries), 11)

            del f1
            del f2
            gc_collect()

            eq_(len(clsregistry._registries), 1)
예제 #17
0
    def setUp(self):
        super(IntegrationTest, self).setUp()
        meta = sqla.MetaData()
        self.test_table = sqla.Table(
            _TABLE_NAME, meta,
            sqla.Column('id', sqla.Integer, primary_key=True, nullable=False),
            sqla.Column('counter', sqla.Integer, nullable=False),
            sqla.UniqueConstraint('counter', name='uniq_counter'))
        self.test_table.create(self.engine)
        self.addCleanup(self.test_table.drop, self.engine)

        reg = registry()

        class Foo(object):
            def __init__(self, counter):
                self.counter = counter

        reg.map_imperatively(Foo, self.test_table)
        self.Foo = Foo
예제 #18
0
    def test_module_reg_no_class(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        reg = base._class_registry["_sa_module_registry"]
        mod_entry = reg["foo"]["bar"]  # noqa
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())
        f_resolver = resolver("foo")
        assert_raises_message(
            NameError,
            "Module 'bar' has no mapped classes registered "
            "under the name 'Bat'",
            lambda: f_resolver().bar.Bat,
        )

        f_resolver = name_resolver("foo")
        assert_raises_message(
            NameError,
            "Module 'bar' has no mapped classes registered "
            "under the name 'Bat'",
            lambda: f_resolver().bar.Bat,
        )
예제 #19
0
        def go(CommonMixin):
            declarative = registry().mapped

            @declarative
            @dataclasses.dataclass
            class BaseType(CommonMixin):

                discriminator = Column("type", String(50))
                __mapper_args__ = dict(polymorphic_on=discriminator)
                id = Column(Integer, primary_key=True)
                value = Column(Integer())

            @declarative
            @dataclasses.dataclass
            class Single(BaseType):

                __tablename__ = None
                __mapper_args__ = dict(polymorphic_identity="type1")

            @declarative
            @dataclasses.dataclass
            class Joined(BaseType):

                __mapper_args__ = dict(polymorphic_identity="type2")
                id = Column(Integer,
                            ForeignKey("basetype.id"),
                            primary_key=True)

            eq_(BaseType.__table__.name, "basetype")
            eq_(
                list(BaseType.__table__.c.keys()),
                ["timestamp", "type", "id", "value"],
            )
            eq_(BaseType.__table__.kwargs, {"mysql_engine": "InnoDB"})
            assert Single.__table__ is BaseType.__table__
            eq_(Joined.__table__.name, "joined")
            eq_(list(Joined.__table__.c.keys()), ["id"])
            eq_(Joined.__table__.kwargs, {"mysql_engine": "InnoDB"})
예제 #20
0
    def multi_mixin_fixture(self):
        orders, items = self.tables.orders, self.tables.items
        order_items = self.tables.order_items

        class HasFoob(object):
            description = Column(String)

        class HasBat(HasFoob):
            some_nothing = Column(Integer)

        class Order(HasFoob, self.Comparable):
            pass

        class Item(HasBat, self.Comparable):
            pass

        base = registry()
        base.map_imperatively(
            Order,
            orders,
            properties={"items": relationship("Item", secondary=order_items)},
        )
        base.map_imperatively(Item, items)
        return HasFoob, Order, Item
예제 #21
0
    def test_no_fns_in_name_resolve(self):
        base = registry()
        f1 = MockClass(base, "foo.bar.Foo")
        f2 = MockClass(base, "foo.alt.Foo")
        clsregistry.add_class("Foo", f1, base._class_registry)
        clsregistry.add_class("Foo", f2, base._class_registry)
        name_resolver, resolver = clsregistry._resolver(f1, MockProp())

        gc_collect()

        import sqlalchemy

        is_(
            resolver("__import__('sqlalchemy.util').util.EMPTY_SET")(),
            sqlalchemy.util.EMPTY_SET,
        )

        assert_raises_message(
            exc.InvalidRequestError,
            r"When initializing mapper some_parent, expression "
            r"\"__import__\('sqlalchemy.util'\).util.EMPTY_SET\" "
            "failed to locate a name",
            name_resolver("__import__('sqlalchemy.util').util.EMPTY_SET"),
        )
예제 #22
0
파일: orm.py 프로젝트: destch/ItemService
def setup_orm():
    mapper = registry()
    metadata = MetaData()

    items = Table("items", metadata, Column("id", primary_key=True),
                  Column("name"), Column("deleted", Intege),
                  Column("description"),
                  Column("brand_id", Integer, ForeignKey("brands.id")))

    brands = Table("brands", metadata, Column("id", primary_key=True),
                   Column("name"))

    thumbnails = Table("thumbnails", metadata, Column("id", primary_key=True),
                       Column("filename"),
                       Column("item_id", Integer, ForeignKey("items.id")))

    def start_mappers():
        mapper.map_imperatively(item.Item,
                                items,
                                properties={
                                    "thumbnails":
                                    relationship(thumbnail.Thumbnail,
                                                 backref="item",
                                                 order_by=thumbnails.c.id)
                                })
        mapper.map_imperatively(
            brand.Brand,
            brands,
            properties={
                "items":
                relationship(item.Item, backref="brand", order_by=items.c.id)
            },
        )
        mapper.map_imperatively(thumbnail.Thumbnail, thumbnails)

    start_mappers()
예제 #23
0
class Base(metaclass=DeclarativeMeta):
    __abstract__ = True
    registry = registry()
    metadata = registry.metadata
예제 #24
0
    def get_user_engine(self, db_name: str):
        if db_name in self.engines:
            return self.engines[db_name]
        else:
            self.engines[db_name] = create_async_engine(
                url=get_sqlalchemy_user_url(db_name=db_name),
                future=True,
                echo=self.echo)
            return self.engines[db_name]

    async def dispatch_func(self, request: Request,
                            call_next: RequestResponseEndpoint) -> Response:
        if yfa.locals.current_user:
            engine = self.get_user_engine(yfa.locals.current_user.db_name)
        else:
            engine = self.engines["main"]

        session_maker = sessionmaker(engine, class_=AsyncSession)
        async with session_maker() as session:
            async with session.begin():
                yfa.locals.db = session
                response = await call_next(request)
                await session.commit()
                del yfa.locals.db

        return response


user_registry = registry()
예제 #25
0
    Integer,
    String,
    Table,
    Text,
    func,
)
from sqlalchemy.engine import Engine, create_engine
from sqlalchemy.orm import Session, registry, relationship
from url_normalize import url_normalize

from .config import instance as config
from .hasher import HashType, hash_io
from .log import instance as log

# The SQLAlchemy ORM registry that we use to decorate dataclasses with
orm_registry = registry()


@orm_registry.mapped
@dataclass
class Content:
    """Describes some extracted content."""

    __table__ = Table(
        "content",
        orm_registry.metadata,
        Column("id",
               BigInteger().with_variant(Integer, "sqlite"),
               primary_key=True),
        Column("created_at", DateTime, server_default=func.now()),
        Column("source", String(256)),
예제 #26
0
from __future__ import annotations

from typing import Final

from botus_receptus.sqlalchemy import async_sessionmaker
from sqlalchemy.orm import registry

_mapper_registry: Final = registry()

mapped: Final = _mapper_registry.mapped
Session: Final = async_sessionmaker(expire_on_commit=False)
예제 #27
0
파일: mapping.py 프로젝트: wiw/pdf_parser
from questionnaire.application.dataclasses import (
    Report,
    Questionnaire,
    Task,
    Email,
    QuestionnaireType,
)
from questionnaire.adapters.app_storage.tables import (
    task,
    report,
    questionnaire,
    email,
    questionnaire_types,
)

mapper = registry()

mapper.map_imperatively(QuestionnaireType,
                        questionnaire_types,
                        properties={'name': questionnaire_types.c.type_name})

mapper.map_imperatively(Report,
                        report,
                        properties={
                            'report_name': report.c.report_readable_name,
                            'file_path': report.c.report_file_path,
                            'email': relationship(Email, lazy='joined'),
                        })

mapper.map_imperatively(
    Questionnaire,
예제 #28
0
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.orm import registry

reg: registry = registry()


@reg.as_declarative_base()
class Base(object):
    updated_at = Column(Integer)


class Foo(Base):
    __tablename__ = "foo"
    id: int = Column(Integer(), primary_key=True)
    name: str = Column(String)


f1 = Foo()

val: int = f1.id

p: str = f1.name

Foo.id.property

f2 = Foo(name="some name", updated_at=5)
예제 #29
0
from typing import Any

from sqlalchemy.orm import DeclarativeMeta, registry
from sqlalchemy.sql.schema import MetaData
from sqlalchemy.util import classproperty

from app.core.db.repr import RepresentableBase

mapper_registry = registry()


class Base(RepresentableBase, metaclass=DeclarativeMeta):
    id: Any
    __abstract__ = True
    __name__: str

    registry: registry = mapper_registry
    metadata: MetaData = mapper_registry.metadata

    @classproperty
    def __tablename__(self) -> str:
        return self.__name__.lower() + "s"
예제 #30
0
# __init__ file of models module
from sqlalchemy.orm import registry

mapped_registry = registry()
BaseModel = mapped_registry.generate_base()


# Class provides representation for model
class ReprModel():
    __table__ = None

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        c_list = self.__table__.columns.keys()
        data_list = {k: v for k, v in vars(self).items() if k in c_list}
        return f'{self.__tablename__} {data_list}'