Skip to content

Temporary database and pytest #11

Open
@klnrdknt

Description

@klnrdknt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions