Coder Social home page Coder Social logo

sqlbag's Introduction

sqlbag: various sql boilerplate

This is just a collection of handy code for doing database things.

What is in the box

Connections, flask setup, SQLAlchemy ORM helpers, temporary database setup and teardown (handy for integration tests).

Installation

Simply install with pip:

$ pip install sqlbag

If you want you can install the database drivers you need at the same time, by specifying one of the optional bundles.

If you're using postgres, this installs sqlbag and psycopg2:

$ pip install sqlbag[pg]

If you're installing MySQL/MariaDB then this installs pymysql as well:

$ pip install sqlbag[maria]

sqlbag's People

Contributors

acarapetis avatar djrobstep avatar groner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sqlbag's Issues

Temporary database and pytest

I'm running into a permission error on windows when using the temp file in testing. Not sure if that's me or the implementation only considers the linux environment. (https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file)

Say I have some test

@patch("builtins.input", lambda _: "yes")
def test_sync_schema():
    dbh = DataBaseHandler("test", "sqlite:///:memory:")
    # add a new column so we have a migration safety issue when reverting back
    with dbh.engine.connect() as conn:
        conn.execute('ALTER TABLE "SomeTable" ADD COLUMN "test" INTEGER')
    # make sure drop column isnt just executed
    with pytest.raises(UnsafeMigrationException):
        sync_schema(dbh, safety=True, export=False)   # < ---------------------- ERROR OCCURS HERE
    # actually create it
    sync_schema(dbh, safety=False, export=False)

and then the sync_schema function basically looks like the one in the documentation

def sync_schema(dbh: DataBaseHandler, safety: bool = True, export: bool = True):
    if "postgresql" in dbh.url:
        dialect = "postgresql"
    else:
        dialect = "sqlite"

    with temporary_database(dialect=dialect) as temp_db_url:
        # migra schema will always be kept up to date with the current object_model
        migra_dbh = DataBaseHandler("migra", temp_db_url)

        with dbh.session_scope() as s_current, migra_dbh.session_scope() as s_target:
            m = migra.Migration(s_current, s_target)
            m.set_safety(safety)
            m.add_all_changes()

            if m.statements:
                print("THE FOLLOWING CHANGES ARE PENDING:", end="\n\n")
                print(m.sql)
                answer = input("Apply these changes? [yes/NO]").lower()
                if answer == "yes":
                    # the export stuff should be irrelevant, just saving the migration statements
                    if export:
                        p = (
                            Path(__file__).parent
                            / "schemas"
                            / f"{customer}_migrate.sql"
                        )
                        if p.is_file():
                            p.unlink()
                        with open(p, "a") as file:
                            file.write(str(m.sql))
                        dbh.get_sql_schema()
                    print("Applying...")
                    m.apply()

                else:
                    print("Not applying.")
            else:
                print("Already synced.")

I also tried using double nesting of temporary db (instead of the "sqlite://:memory:" in the test, but it throws the same error:

            finally:
                if not do_not_delete:
>                   os.remove(tmp.name)
E                    PermissionError: [WinError 32] The process cannot access the file, it's used by another process: 'C:\\Users\\~user\\AppData\\Local\\Temp\\tmp7dj3qb67'

Might also be my fault and I'm overlooking something.

Failing tests

Following test seem to fail, when run with the latest versions of all dependencies:

    "test_basic"
    "test_createdrop"
    "test_errors_and_messages"
    "test_flask_integration"
    "test_orm_stuff"
    "test_pendulum_for_time_types"
    "test_transaction_separation"

The issue seems to be caused by the same problems described in the migra issue.

BUG: Mutates dictionary while iterating over it

parse_interval_values in sqlbag/pg/datetimes.py mutates the dictionary while iterating over it when the key doesn't end in 's', which throws a "RuntimeError: dictionary keys changed during iteration".
For example, calling it with parse_interval_values('1 day') throws the error.

Doesn't work on windows due to `pwd`

pwd is used to find the current username when creating temporary databases.

Unfortunately this is unix-only. Need to swap it out with something platform-independent.

Cannot install on a box without gcc - due to psycopg2

I'm building a fork of migra.
It includes schemainspect package.
That relays on sqlbag[pg].
That wants psycopg2 (not psycopg2-binary) in turn.

The box where I have to run the code has no gcc installed.

So, I see the crash in attempt to build psycopg2 (even having psycopg2-binary installed!).

This effectively prevents me from using migra.

Proposal: could you please replace psycopg2 with psycopg2-binary in your dependancies?

P.S. It's Python 3.10.12 in Ubuntu 22.04.3 LTS if it matters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.