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

Document using in-memory SQLite databases #419

Closed
pop opened this Issue Aug 27, 2016 · 5 comments

Comments

Projects
None yet
4 participants
@pop

pop commented Aug 27, 2016

I'm trying to use diesel with SQLite. For testing purposes it'd be great to setup and teardown ":memory:" databases but there's not really any documentation on how to do this.

I'd love to write the docs if someone could explain how I'm supposed to A) setup a connection with a ":memory:" database and B) Run migrations on that database. Preferably without also having manually setup a file-based SQLite database.

I really like diesel and would love to contribute.

Edit:

I'm aware that I can just run SqliteConnection::establish(":memory:").unwrap() to get an in-memory SQLite connection. What I'm really curious about is how to compile a program using diesel without a previously existing database and not getting error output like

   Compiling crisco v0.1.0 (file:///home/e/svc/source/crisco)
error[E0432]: unresolved import `super::schema::urls`
 --> src/database/models.rs:6:5
  |
6 | use super::schema::urls;
  |     ^^^^^^^^^^^^^^^^^^^ no `urls` in `database::schema`

error[E0432]: unresolved import `self::schema::urls`
  --> src/database/mod.rs:25:5
   |
25 | use self::schema::urls;
   |     ^^^^^^^^^^^^^^^^^^ no `urls` in `database::schema`

Either way I'm up for documenting this, it took a bit of digging to get as far as I have.

@sgrif

This comment has been minimized.

Member

sgrif commented Aug 27, 2016

Using an in memory database is certainly supported, but you not be able to use infer_schema! with an in memory database (as there's no persistent schema to infer from). You will need to either use the table! macro directly, or compile against an on disk database for schema inference purposes.

@sgrif

This comment has been minimized.

Member

sgrif commented Aug 27, 2016

You can run migrations against the database after establishing the connection using the run_pending_migrations function. That function requires the migrations to be on disk in the appropriate location. If you want to avoid that constraint, use the embed_migrations! macro. I've just noticed that the documentation is a bit lacking for embed_migrations! (which I will fix later today) but you use it like so:

embed_migrations!();

fn establish_connection() -> Result<SqliteConnection, MigrationError> {
    let connection = try!(SqliteConnection::establish(":memory:"));
    try!(embedded_migrations::run(&connection).unwrap();
    Ok(connection)
}

@sgrif sgrif closed this Aug 27, 2016

@Thomasdezeeuw

This comment has been minimized.

Thomasdezeeuw commented Apr 28, 2017

@sgrif your example helps a lot to understanding the embed_migrations macro, maybe it's worth adding it to the docs?

@Eijebong

This comment has been minimized.

@Thomasdezeeuw

This comment has been minimized.

Thomasdezeeuw commented Apr 28, 2017

I didn't see that, I was using local (outdated) documentation. Great that it's in!

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