Пример #1
0
def construct_sqlalchemy_db(uri="sqlite://") -> SQLAlchemyDatastore:
    db = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri),
                             tables=(StoredEventRecord, ))
    db.setup_connection()
    db.drop_tables()
    db.setup_tables()
    return db
Пример #2
0
    def start_app(self):
        # Make up a DB URI using a named temporary file.
        self.tempfile = NamedTemporaryFile()
        uri = "sqlite:///{}".format(self.tempfile.name)

        from eventsourcing.example.interface.flaskapp import IntegerSequencedItem

        # Close application, importing the module constructed
        # the application, which will leave event handlers subscribed.
        close_example_application()

        # Setup tables.
        datastore = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri=uri),
                                        tables=(IntegerSequencedItem, ))
        datastore.setup_connection()
        datastore.setup_tables()
        datastore.close_connection()

        # Run uwsgi.
        path_to_uwsgi = shutil.which("uwsgi")
        if not os.path.exists(path_to_uwsgi):
            raise AssertionError("Can't find uwsgi: %s" % path_to_uwsgi)
        cmd = [path_to_uwsgi]
        if path_to_virtualenv is not None:
            cmd += ["-H", path_to_virtualenv]
        cmd += ["--master"]
        cmd += ["--processes", "4"]
        cmd += ["--threads", "2"]
        cmd += ["--wsgi-file", path_to_flaskapp]
        cmd += ["--http", ":{}".format(self.port)]
        pythonpath = ":".join(
            os.getenv("PYTHONPATH", "").split(":") + [path_to_eventsourcing])
        return Popen(cmd, env={"PYTHONPATH": pythonpath, "DB_URI": uri})
Пример #3
0
    def start_app(self):
        # Make up a DB URI using a named temporary file.
        self.tempfile = NamedTemporaryFile()
        uri = 'sqlite:///{}'.format(self.tempfile.name)

        from eventsourcing.example.interface.flaskapp import IntegerSequencedItem
        # Close application, importing the module constructed
        # the application, which will leave event handlers subscribed.
        close_example_application()

        # Setup tables.
        datastore = SQLAlchemyDatastore(
            settings=SQLAlchemySettings(uri=uri),
            tables=(IntegerSequencedItem,),
        )
        datastore.setup_connection()
        datastore.setup_tables()
        datastore.close_connection()

        # Run uwsgi.
        path_to_uwsgi = join(path_to_virtualenv, 'bin', 'uwsgi')
        assert os.path.exists(path_to_uwsgi), path_to_uwsgi
        cmd = [path_to_uwsgi]
        if path_to_virtualenv is not None:
            cmd += ['-H', path_to_virtualenv]
        cmd += ['--master']
        cmd += ['--processes', '4']
        cmd += ['--threads', '2']
        cmd += ['--wsgi-file', path_to_flaskwsgi]
        cmd += ['--http', ':{}'.format(self.port)]
        pythonpath = ':'.join(os.getenv('PYTHONPATH', '').split(':') + [path_to_eventsourcing])
        return Popen(cmd, env={
            'PYTHONPATH': pythonpath,
            'DB_URI': uri
        })
Пример #4
0
    def start_app(self):
        # Make up a DB URI using a named temporary file.
        self.tempfile = NamedTemporaryFile()
        uri = 'sqlite:///{}'.format(self.tempfile.name)

        from eventsourcing.example.interface.flaskapp import IntegerSequencedItem
        # Close application, importing the module constructed
        # the application, which will leave event handlers subscribed.
        close_example_application()

        # Setup tables.
        datastore = SQLAlchemyDatastore(
            settings=SQLAlchemySettings(uri=uri),
            tables=(IntegerSequencedItem,),
        )
        datastore.setup_connection()
        datastore.setup_tables()
        datastore.drop_connection()

        # Run uwsgi.
        path_to_uwsgi = join(path_to_virtualenv, 'bin', 'uwsgi')
        assert os.path.exists(path_to_uwsgi), path_to_uwsgi
        cmd = [path_to_uwsgi]
        if path_to_virtualenv is not None:
            cmd += ['-H', path_to_virtualenv]
        cmd += ['--master']
        cmd += ['--processes', '4']
        cmd += ['--threads', '2']
        cmd += ['--wsgi-file', path_to_flaskwsgi]
        cmd += ['--http', ':{}'.format(self.port)]
        pythonpath = ':'.join(os.getenv('PYTHONPATH', '').split(':') + [path_to_eventsourcing])
        return Popen(cmd, env={
            'PYTHONPATH': pythonpath,
            'DB_URI': uri
        })
Пример #5
0
    def __init__(self, session):
        # Construct event stores and persistence policies.
        datastore = SQLAlchemyDatastore(
            settings=SQLAlchemySettings(uri='sqlite:///:memory:'),
            tables=(IntegerSequencedItemRecord, ),
        )

        datastore.setup_connection()
        datastore.setup_tables()

        super(TodoApp, self).__init__()
        pass
Пример #6
0
def test():
    # Configure database.
    settings = SQLAlchemySettings(uri='sqlite:///:memory:')

    # Setup database.
    datastore = SQLAlchemyDatastore(
        settings=settings,
        tables=(IntegerSequencedItemRecord, ),
    )
    datastore.setup_connection()
    datastore.setup_tables()

    # Construct application.
    app = TodoApp(session=datastore.session)

    # Check the user initially has no lists.
    user_id = uuid4()
    todo_list_ids = app.get_todo_list_ids(user_id)
    assert todo_list_ids == []

    # Start a new list.
    todo_list_id = app.start_todo_list(user_id)

    # Check the user has one list.
    todo_list_ids = app.get_todo_list_ids(user_id)
    assert todo_list_ids == {todo_list_id}

    # Check the list has no items.
    assert app.get_todo_items(todo_list_id) == ()

    # Add an item to the list.
    app.add_todo_item(todo_list_id=todo_list_id, item='item1')

    # Check the list has one item.
    assert app.get_todo_items(todo_list_id) == ('item1', )

    # Update the item.
    app.update_todo_item(todo_list_id=todo_list_id, index=0, item='item1.1')

    # Get the list and see it has the updated item.
    assert app.get_todo_items(todo_list_id) == ('item1.1', )

    # Discard the item, and check by getting the list and checking it has no items.
    app.discard_todo_item(todo_list_id=todo_list_id, index=0)
    assert app.get_todo_items(todo_list_id) == ()

    # Discard the list, and check there are no list IDs for the user.
    app.discard_todo_list(todo_list_id=todo_list_id)
    todo_list_ids = app.get_todo_list_ids(user_id)
    assert len(todo_list_ids) == 0
    def start_app(self):
        # Make up a DB URI using a named temporary file.
        self.tempfile = NamedTemporaryFile()
        uri = "sqlite:///{}".format(self.tempfile.name)

        from eventsourcing.example.interface.flaskapp import IntegerSequencedItem

        # Close application, importing the module constructed
        # the application, which will leave event handlers subscribed.
        close_example_application()

        # Setup tables.
        datastore = SQLAlchemyDatastore(settings=SQLAlchemySettings(uri=uri),
                                        tables=(IntegerSequencedItem, ))
        datastore.setup_connection()
        datastore.setup_tables()
        datastore.close_connection()

        # Run uwsgi.
        if path_to_virtualenv:
            path_to_uwsgi = join(path_to_virtualenv, "bin", "uwsgi")
            assert os.path.exists(path_to_uwsgi), path_to_uwsgi
        else:
            # In a container, without a virtualenv?
            path_to_uwsgi = "/usr/local/bin/uwsgi"
            # Todo: Maybe use shutil.which, after dropping support for Python 2.7.

        cmd = [path_to_uwsgi]
        if path_to_virtualenv is not None:
            cmd += ["-H", path_to_virtualenv]
        cmd += ["--master"]
        cmd += ["--processes", "4"]
        cmd += ["--threads", "2"]
        cmd += ["--wsgi-file", path_to_flaskwsgi]
        cmd += ["--http", ":{}".format(self.port)]
        pythonpath = ":".join(
            os.getenv("PYTHONPATH", "").split(":") + [path_to_eventsourcing])
        return Popen(cmd, env={"PYTHONPATH": pythonpath, "DB_URI": uri})
Пример #8
0
import uuid
from eventsourcing.infrastructure.sequenceditemmapper import SequencedItemMapper
from eventsourcing.infrastructure.eventsourcedrepository import EventSourcedRepository
from eventsourcing.infrastructure.eventstore import EventStore
from eventsourcing.infrastructure.sequenceditem import StoredEvent
from eventsourcing.infrastructure.sqlalchemy.datastore import SQLAlchemyDatastore, SQLAlchemySettings
from eventsourcing.infrastructure.sqlalchemy.manager import SQLAlchemyRecordManager
from eventsourcing.infrastructure.sqlalchemy.records import StoredEventRecord

datastore = SQLAlchemyDatastore(
    tables=(StoredEventRecord, ),
    settings=SQLAlchemySettings(uri='sqlite:///mydatabase'))

datastore.setup_connection()
datastore.setup_tables()

recordmanager = SQLAlchemyRecordManager(session=datastore.session,
                                        record_class=StoredEventRecord,
                                        application_name=uuid.uuid4().hex,
                                        contiguous_record_ids=True,
                                        sequenced_item_class=StoredEvent)

sequenceitemmapper = SequencedItemMapper(sequenced_item_class=StoredEvent)

eventstore = EventStore(record_manager=recordmanager,
                        sequenced_item_mapper=sequenceitemmapper)

repository = EventSourcedRepository(event_store=eventstore)