<commit_before>Diesel - A safe, extensible ORM and Query Builder for Rust
==========================================================

[![Build Status](https://travis-ci.org/sgrif/diesel.svg)](https://travis-ci.org/sgrif/diesel)
[Documentation](http://sgrif.github.io/diesel/diesel/index.html)

Diesel gets rid of the boilerplate for database interaction and eliminates
runtime errors, without sacrificing performance. It takes full advantage of
Rust's type system to create a low overhead query builder that "feels like
Rust".

We are not feature complete, nor do I think we've covered all use cases. If
you've found something difficult to accomplish, please open an issue.

Getting Started
---------------

Before you can do anything, you'll first need to set up your table. You'll want
to specify the columns and tables that exist using the [`table!` macro][table]
Once you've done that, you can already start using the query builder, and
pulling out primitives.

Much of the behavior in diesel comes from traits, and it is recommended that you
import `diesel::*`. We avoid exporting generic type names, or any bare functions
at that level.

```rust
#[macro_use]
extern crate diesel;

use diesel::*;

table! {
    users {
        id -> Serial,
        name -> VarChar,
        favorite_color -> Nullable<VarChar>,
    }
}

fn users_with_name(connection: &Connection, target_name: &str)
    -> QueryResult<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .map(|x| x.collect())
}
```

Note that we're importing `users::dsl::*` here. This allows us to deal with
only the users table, and not have to qualify everything. If we did not have
this import, we'd need to put `users::` before each column, and reference the
table as `users::table`.

You can also use
[diesel_codegen](https://github.com/sgrif/diesel/tree/master/diesel_codegen) to
call [`table!`][table] for you automatically, based on your existing database
schema. See the [diesel_codegen
README](https://github.com/sgrif/diesel/tree/master/diesel_codegen) for details
on how to get started. Here's the same code with Diesel Codegen.

```rust
#[macro_use] extern crate diesel;

use diesel::*;

infer_schema!(dotenv!("DATABASE_URL"));

fn users_with_name(connection: &Connection, target_name: &str)
    -> QueryResult<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .map(|x| x.collect())
}
```

If you want to be able to query for a struct, you'll need to implement the
[`Queriable` trait][queriable] Luckily,
[diesel_codegen](https://github.com/sgrif/diesel/tree/master/diesel_codegen) can do
this for us automatically.

```rust
#[derive(Queriable, Debug)]
pub struct User {
    id: i32,
    name: String,
    favorite_color: Option<String>,
}

fn main() {
    let connection = Connection::establish(env!("DATABASE_URL"))
        .unwrap();
    let users: Vec<User> = users::table.load(&connection)
        .unwrap().collect();

    println!("Here are all the users in our database: {:?}", users);
}
```

Insert
------<commit_msg>Add a quick migrations guide to the README<commit_after>Diesel - A safe, extensible ORM and Query Builder for Rust
==========================================================

[![Build Status](https://travis-ci.org/sgrif/diesel.svg)](https://travis-ci.org/sgrif/diesel)
[Documentation](http://sgrif.github.io/diesel/diesel/index.html)

Diesel gets rid of the boilerplate for database interaction and eliminates
runtime errors, without sacrificing performance. It takes full advantage of
Rust's type system to create a low overhead query builder that "feels like
Rust".

We are not feature complete, nor do I think we've covered all use cases. If
you've found something difficult to accomplish, please open an issue.

Getting Started
---------------

Before you can do anything, you'll first need to set up your table. You'll want
to specify the columns and tables that exist using the [`table!` macro][table]
Once you've done that, you can already start using the query builder, and
pulling out primitives.

Much of the behavior in diesel comes from traits, and it is recommended that you
import `diesel::*`. We avoid exporting generic type names, or any bare functions
at that level.

```rust
#[macro_use]
extern crate diesel;

use diesel::*;

table! {
    users {
        id -> Serial,
        name -> VarChar,
        favorite_color -> Nullable<VarChar>,
    }
}

fn users_with_name(connection: &Connection, target_name: &str)
    -> QueryResult<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .map(|x| x.collect())
}
```

Note that we're importing `users::dsl::*` here. This allows us to deal with
only the users table, and not have to qualify everything. If we did not have
this import, we'd need to put `users::` before each column, and reference the
table as `users::table`.

You can also use
[diesel_codegen](https://github.com/sgrif/diesel/tree/master/diesel_codegen) to
call [`table!`][table] for you automatically, based on your existing database
schema. See the [diesel_codegen
README](https://github.com/sgrif/diesel/tree/master/diesel_codegen) for details
on how to get started. Here's the same code with Diesel Codegen.

```rust
#[macro_use] extern crate diesel;

use diesel::*;

infer_schema!(dotenv!("DATABASE_URL"));

fn users_with_name(connection: &Connection, target_name: &str)
    -> QueryResult<Vec<(i32, String, Option<String>)>>
{
    use self::users::dsl::*;
    users.filter(name.eq(target_name)).load(connection)
        .map(|x| x.collect())
}
```

If you want to be able to query for a struct, you'll need to implement the
[`Queriable` trait][queriable] Luckily,
[diesel_codegen](https://github.com/sgrif/diesel/tree/master/diesel_codegen) can do
this for us automatically.

```rust
#[derive(Queriable, Debug)]
pub struct User {
    id: i32,
    name: String,
    favorite_color: Option<String>,
}

fn main() {
    let connection = Connection::establish(env!("DATABASE_URL"))
        .unwrap();
    let users: Vec<User> = users::table.load(&connection)
        .unwrap().collect();

    println!("Here are all the users in our database: {:?}", users);
}
```

Database Migrations
-------------------

Diesel CLI is a tool that aids in managing your database schema. Migrations are
bi-directional changes to your database that get applied sequentially. You can
use it on your project like so:

```shell
cargo install diesel_cli
mkdir migrations
diesel migration generate create_users_table
```

You'll see that two files were generated for you,
`migrations/{current_timestamp}_create_users_table/up.sql` and
`migrations/{current_timestamp}_create_users_table/down.sql`. You should edit
these files to show how to update your schema, and how to undo that change.

```sql
-- up.sql
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL,
    favorite_color VARCHAR
);
```

```sql
-- down.sql
DROP TABLE USERS;
```

You can then run your new migration by running `diesel migration run`. Make sure
that you set the `DATABASE_URL` environment variable first, or pass it directly
by doing `diesel migration run --database-url="postgres://localhost/your_database"`
Alternatively, you can call
[`diesel::migrations::run_pending_migrations`][pending-migrations] from
`build.rs`.

[pending-migrations]: http://sgrif.github.io/diesel/diesel/migrations/fn.run_pending_migrations.html

If you ever need to revert or make changes to your migrations, the commands
`diesel migration revert` and `diesel migration redo`. Type `diesel migration
--help` for more information.

Insert
------
{'fraction_non_alphanumeric': 0.13436492585428755, 'fraction_numerical': 0.0015473887814313346, 'mean_word_length': 4.352657004830918, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2155684', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2315, 'n_words': 838}