A friendly wrapper for modern SQLAlchemy and Alembic

Overview

SQLA-Wrapper

A friendly wrapper for modern SQLAlchemy (v1.4 or later) and Alembic.

Documentation: https://jpsca.github.io/sqla-wrapper/

Includes:

  • A SQLAlchemy wrapper, that does all the SQLAlchemy setup and gives you:

    • A preconfigured scoped session.
    • A model baseclass including some helper methods.
    • A helper for performant testing with a real database
  • An Alembic wrapper that loads the config from your application instead of an ini file.

  • A sa helper module, that imports all the functions and classes from sqlalchemyand sqlalchemy.orm, so you don't need to repeat those imports everywhere.

Comments
  • Allow to change engine and session configuration after init

    Allow to change engine and session configuration after init

    It may be useful for unit tests. Short example for py.test:

    @pytest.fixture(scope='function', autouse=True)
    def clean_up_db():
        # called before all test functions
        db.reconfigure(uri='sqlite://', echo=True)
        db.drop_all()
        db.create_all()
    
    def test_something():
        db.add(Item())
        db.commit()
        ...
    

    Also related to #24 cause we can not read Flask-SQLAlchemy's app.config

    opened by vsurjaninov 4
  • Inheritance does not work properly

    Inheritance does not work properly

    Because db.Model explicitly defines __tablename__, STI doesn't work since the declarative_base expects __tablename__ to be undefined to work properly.

    http://docs.sqlalchemy.org/en/rel_1_0/orm/inheritance.html?highlight=inheritance#single-table-inheritance

    flask-sqlalchemy actually had the same problem at one point and they fixed this by checking to see whether the table name should actually be set in different instances: https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/init.py#L553-L591

    opened by jcn 3
  • "verbose" argument to alembic.history cli command

    without this parameter command actually doesn't work:

     python manage.py db history
    Traceback (most recent call last):
      File "manage.py", line 41, in <module>
        cli()
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
        return self.main(*args, **kwargs)
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 1055, in main
        rv = self.invoke(ctx)
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/click/core.py", line 760, in invoke
        return __callback(*args, **kwargs)
      File "/home/krnr/.pyenv/versions/ard38/lib/python3.8/site-packages/sqla_wrapper/cli/click_cli.py", line 71, in history
        alembic.history(verbose, start=start, end=end)
    TypeError: history() takes 1 positional argument but 2 positional arguments (and 2 keyword-only arguments) were given
    
    opened by krnr 2
  • Two Pypi (pip) packages

    Two Pypi (pip) packages

    Hi!

    There are two Pypi packages for this project:

    The latter is probably the correct one (or the latest one), but having two pip packages can confuse users. Please either delete the SQLAlchemy-Wrapper package or add a notice in the README.md file to install sqla-wrapper.

    Usually readme files start with installation instructions: pip install sqla-wrapper. This makes it clear which package to install.

    opened by ramuta 2
  • Flask debug toolbar doesnt show queries(shows unavailable)

    Flask debug toolbar doesnt show queries(shows unavailable)

    As i am trying to make the "transition" from Flask-SQLAlchemy to your fork i found this problem where the debug toolbar doesnt show information about the queries, it just tells me that i have to install Flask-SQLAlachemy.

    I saw a commit about supporting the toolbar in your fork but im not expert to debug what is wrong cause maybe im missing something.

    # __init__.py
    from flask import Flask
    from sqlalchemy_wrapper import SQLAlchemy
    from flask.ext.bcrypt import Bcrypt
    
    app = Flask(__name__, instance_relative_config=True, static_url_path='')
    bcrypt = Bcrypt(app)
    
    app.config.from_pyfile('development.py')
    
    db = SQLAlchemy(app.config['SQLALCHEMY_DATABASE_URI'], app=app)
    
    
    # run.py
    from flask_debugtoolbar import DebugToolbarExtension
    toolbar = DebugToolbarExtension()
    toolbar.init_app(app)
    
    opened by ev-agelos 2
  • UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 63: ordinal not in range(128)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 63: ordinal not in range(128)

    Orignal Query -

    data = db.session.query(PublishEvent, ImageLicense).join(PublishEvent.images).join(ImageLicense).filter(ImageLicense.licensed_at != None')
    

    Traceback -

    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/displayhook.pyc in __call__(self, result)
        245             self.start_displayhook()
        246             self.write_output_prompt()
    --> 247             format_dict, md_dict = self.compute_format_data(result)
        248             self.write_format_data(format_dict, md_dict)
        249             self.update_user_ns(result)
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
        155
        156         """
    --> 157         return self.shell.display_formatter.format(result)
        158
        159     def write_format_data(self, format_dict, md_dict=None):
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
        150             md = None
        151             try:
    --> 152                 data = formatter(obj)
        153             except:
        154                 # FIXME: log the exception
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
        479                 type_pprinters=self.type_printers,
        480                 deferred_pprinters=self.deferred_printers)
    --> 481             printer.pretty(obj)
        482             printer.flush()
        483             return stream.getvalue()
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
        345                 if cls in self.type_pprinters:
        346                     # printer registered in self.type_pprinters
    --> 347                     return self.type_pprinters[cls](obj, self, cycle)
        348                 else:
        349                     # deferred printer
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in inner(obj, p, cycle)
        529                 p.text(',')
        530                 p.breakable()
    --> 531             p.pretty(x)
        532         if len(obj) == 1 and type(obj) is tuple:
        533             # Special case for 1-item tuples.
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
        345                 if cls in self.type_pprinters:
        346                     # printer registered in self.type_pprinters
    --> 347                     return self.type_pprinters[cls](obj, self, cycle)
        348                 else:
        349                     # deferred printer
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in inner(obj, p, cycle)
        529                 p.text(',')
        530                 p.breakable()
    --> 531             p.pretty(x)
        532         if len(obj) == 1 and type(obj) is tuple:
        533             # Special case for 1-item tuples.
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
        360                             if callable(meth):
        361                                 return meth(obj, self, cycle)
    --> 362             return _default_pprint(obj, self, cycle)
        363         finally:
        364             self.end_group()
    
    /Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
        480     if getattr(klass, '__repr__', None) not in _baseclass_reprs:
        481         # A user-provided repr.
    --> 482         p.text(repr(obj))
        483         return
        484     p.begin_group(1, '<')
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 63: ordinal not in range(128)
    
    opened by pranto157 2
  • default extend_existing to True

    default extend_existing to True

    In model.py after cls.__tablename__ = _get_table_name(cls.__name__), would you consider adding cls.__table_args__ = {'extend_existing':True}?

    It allows to change the model interactively, e.g. in IPython.

    opened by rpuntaie 1
  • installation instructions

    installation instructions

    From this issue: https://github.com/jpscaletti/sqla-wrapper/issues/31

    There are two Pypi packages for this project:

    The latter is probably the correct one (or the latest one), but having two pip packages can confuse users.

    I added installation instructions now to make it clear which package needs to be installed.

    opened by ramuta 1
  • User.query AttributeError

    User.query AttributeError

    https://github.com/jpscaletti/sqla-wrapper/blob/master/docs/quickstart.rst#simple-relationships Quickstart provides example:

    users = User.query.all()
    

    but actually I am getting error: AttributeError: type object 'User' has no attribute 'query'

    Type:Enhancement Status:🤔 
    opened by kiddten 1
  • create_engine() falls with mysql and something pool classes

    create_engine() falls with mysql and something pool classes

    Hello!

    Something pool classes such as NullPool, StaticPool not supports 'pool_size' argument. Following code produces an error:

    SQLAlchemy('mysql://...', poolclass=NullPool)

    TypeError: Invalid argument(s) 'pool_size' sent to create_engine(), using configuration MySQLDialect_mysqldb/NullPool/Engine. Please check that the keyword arguments are appropriate for this combination of components.

    My patch checks arguments of pool class __init__ function for fix it. That's little dirty, but removing problem part from apply_driver_hacks may produce unexpected consequences.

    opened by vsurjaninov 1
  • add

    add "poolclass" parameter

    Hello! I want to allow passing "poolclass" parameter to create_engine() . In my project I have to patching or subclassing original SQLAlchemy class for change it.

    Also flask-sqlachemy has the same issue: https://github.com/mitsuhiko/flask-sqlalchemy/issues/266

    opened by vsurjaninov 1
Releases(v5.6.1)
  • v5.6.1(May 26, 2022)

    What's Changed

    • "verbose" argument to alembic.history cli command by @krnr in https://github.com/jpsca/sqla-wrapper/pull/40

    New Contributors

    • @krnr made their first contribution in https://github.com/jpsca/sqla-wrapper/pull/40

    Full Changelog: https://github.com/jpsca/sqla-wrapper/compare/v5.4...v5.6.1

    Source code(tar.gz)
    Source code(zip)
  • v5.4(Jan 31, 2022)

  • v5.3(Jan 31, 2022)

  • v5.2(Jan 2, 2022)

  • v5.1(Oct 19, 2021)

  • v5.0.0(Oct 19, 2021)

    A new beginning...

    This version is a complete rewrite of the library and the documentation. From now on, only the new SQLAlchemy API (v1.4+) will be supported.

    SQLA-wrapper now includes:

    1. A wrapper for SQLAlchemy that does all the setup and gives you:
    • A session class and a scoped session, both extended with some useful active-record-like methods and pagination.
    • A declarative base class.
    • A helper for performant testing with a real database.
    1. A new Alembic wrapper that loads the config from your application instead of from separated alembic.ini and env.py files.
    • CLI integrations with click and pyCEO.
    Source code(tar.gz)
    Source code(zip)
Owner
Juan-Pablo Scaletti
JP spends most of his days trying to convince computers to do things.
Juan-Pablo Scaletti
Penetration testing

Penetration testing

3 Jan 11, 2022
Android automation project with pytest+appium

Android automation project with pytest+appium

1 Oct 28, 2021
A collection of benchmarking tools.

Benchmark Utilities About A collection of benchmarking tools. PYPI Package Table of Contents Using the library Installing and using the library Manual

Kostas Georgiou 2 Jan 28, 2022
Python Webscraping using Selenium

Web Scraping with Python and Selenium The code shows how to do web scraping using Python and Selenium. We use as data the https://sbot.org.br/localize

Luís Miguel Massih Pereira 1 Dec 01, 2021
This is a Python script for Github Bot which uses Selenium to Automate things.

github-follow-unfollow-bot This is a Python script for Github Bot which uses Selenium to Automate things. Pre-requisites :- Python A Github Account Re

Chaudhary Hamdan 10 Jul 01, 2022
Connexion-faker - Auto-generate mocks from your Connexion API using OpenAPI

Connexion Faker Get Started Install With poetry: poetry add connexion-faker # a

Erle Carrara 6 Dec 19, 2022
pytest_pyramid provides basic fixtures for testing pyramid applications with pytest test suite

pytest_pyramid pytest_pyramid provides basic fixtures for testing pyramid applications with pytest test suite. By default, pytest_pyramid will create

Grzegorz Śliwiński 12 Dec 04, 2022
API mocking with Python.

apyr apyr (all lowercase) is a simple & easy to use mock API server. It's great for front-end development when your API is not ready, or when you are

Umut Seven 55 Nov 25, 2022
Fully functioning price detector built with selenium and python

Fully functioning price detector built with selenium and python

mark sikaundi 4 Mar 30, 2022
Useful additions to Django's default TestCase

django-test-plus Useful additions to Django's default TestCase from REVSYS Rationale Let's face it, writing tests isn't always fun. Part of the reason

REVSYS 546 Dec 22, 2022
Fi - A simple Python 3.9+ command-line application for managing Fidelity portfolios

fi fi is a simple Python 3.9+ command-line application for managing Fidelity por

Darik Harter 2 Feb 26, 2022
A twitter bot that simply replies with a beautiful screenshot of the tweet, powered by poet.so

Poet this! Replies with a beautiful screenshot of the tweet, powered by poet.so Installation git clone https://github.com/dhravya/poet-this.git cd po

Dhravya Shah 30 Dec 04, 2022
A single module to link Python ecosystem to the Web

A single module to link Python ecosystem to the Web. Have a quick look at the Gallery first to get convinced ! FAQ For any questions, please use Stack

66 Dec 21, 2022
Tools for test driven data-wrangling and data validation.

datatest: Test driven data-wrangling and data validation Datatest helps to speed up and formalize data-wrangling and data validation tasks. It impleme

269 Dec 16, 2022
Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.

WebTest This wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server. This provides

Pylons Project 325 Dec 30, 2022
自动化爬取并自动测试所有swagger-ui.html显示的接口

swagger-hack 在测试中偶尔会碰到swagger泄露 常见的泄露如图: 有的泄露接口特别多,每一个都手动去试根本试不过来 于是用python写了个脚本自动爬取所有接口,配置好传参发包访问 原理是首先抓取http://url/swagger-resources 获取到有哪些标准及对应的文档地

jayus 534 Dec 29, 2022
a wrapper around pytest for executing tests to look for test flakiness and runtime regression

bubblewrap a wrapper around pytest for assessing flakiness and runtime regressions a cs implementations practice project How to Run: First, install de

Anna Nagy 1 Aug 05, 2021
Pymox - open source mock object framework for Python

Pymox is an open source mock object framework for Python. First Steps Installation Tutorial Documentation http://pymox.readthedocs.io/en/latest/index.

Ivan Rocha 7 Feb 02, 2022
Python package to easily work with selenium and manage tabs effectively.

Simple Selenium The aim of this package is to quickly get started with working with selenium for simple browser automation tasks. Installation Install

Vishal Kumar Mishra 1 Oct 27, 2021
Test python asyncio-based code with ease.

aiounittest Info The aiounittest is a helper library to ease of your pain (and boilerplate), when writing a test of the asynchronous code (asyncio). Y

Krzysztof Warunek 55 Oct 30, 2022