Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save in-memory database to file? #210

Open
kavir1698 opened this issue Mar 10, 2020 · 4 comments
Open

How to save in-memory database to file? #210

kavir1698 opened this issue Mar 10, 2020 · 4 comments

Comments

@kavir1698
Copy link

I do not find the backup function in SQLite.jl. Thank you.

@felipenoris
Copy link
Contributor

This is interesting feature.
https://www.sqlite.org/backup.html

@quinnj
Copy link
Member

quinnj commented Mar 20, 2020

Yeah, it seems like all the functionality is there via the C library; if someone wants to take a stab at this that'd be great; happy to provide direction. I don't know that I'll have time for a while to get to this.

@felipenoris
Copy link
Contributor

@quinnj , yes I'm glad to help on this one, but it might take some time to get myself available for this.

@daniel-thom
Copy link

The implementation could look something like this. If you all are OK with it, I could write tests and documentation and open a PR. I might need some pointers on full error handling. Let me know and I'll be happy to work on it. I need this functionality. I'd be OK with changes to the interface.

function backup(
    dst::DB,
    src::DB;
    dst_name::AbstractString = "main",
    src_name::AbstractString = "main",
    pages::Int = -1,
    sleep::Float64 = 0.25,
)
    if src === dst
        error("src and dst cannot be the same connection")
    end

    num_pages = pages == 0 ? -1 : pages
    sleep_ms = sleep * 1000
    ptr = C.sqlite3_backup_init(dst.handle, dst_name, src.handle, src_name)
    r = C.SQLITE_OK
    try
        while r == C.SQLITE_OK || r == C.SQLITE_BUSY || r == C.SQLITE_LOCKED
            r = C.sqlite3_backup_step(ptr, num_pages)
            @debug "backup iteration: remaining = $(C.sqlite3_backup_remaining(ptr))"
            if r == C.SQLITE_BUSY || r == C.SQLITE_LOCKED
                C.sqlite3_sleep(sleep_ms)
            end
        end
    finally
        C.sqlite3_backup_finish(ptr)
        if r != C.SQLITE_DONE
            e = sqliteexception(src.handle)
            C.sqlite3_reset(src.handle)
            throw(e)
        end
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants