Commit graph

1037 commits

Author SHA1 Message Date
Peter Maatman
4122a3dbd9 mysql: Advance buffer to comply with protocol
Fixes #254
2020-04-27 16:47:38 -07:00
Hasan Ali
3e2ed00b60 Drop all statements before closing sqlite connection 2020-04-27 10:20:57 -07:00
Dan B
e7c1486005 Add support for crate specific .env files
This change will attempt to load an .env file from CARGO_MANIFEST_DIR, if it exists.

For backwards compatibility, if the .env file does not exist, we will fall back to default dotenv behaviour.

Resolves #267
2020-04-24 15:40:20 -07:00
Josh Holmer
581bcf0ceb Extract slow query threshold to constant 2020-04-23 14:09:59 -07:00
Josh Holmer
627d8292f8 Log queries from sqlx::query
This will make logged queries show
as from `sqlx::query` instead of `sqlx::db_name::executor`.

Followup to #268
2020-04-23 14:09:59 -07:00
Jesper Axelsson
38892c1012 Use dialoger for prompt 2020-04-19 16:29:34 -07:00
Jesper Axelsson
f491198891 List migrations 2020-04-19 16:29:34 -07:00
Jesper Axelsson
70654ff2db Use text instead of varchar 2020-04-19 16:29:34 -07:00
Jesper Axelsson
c58b15ce83 Add mysql as default feature 2020-04-19 16:29:34 -07:00
Jesper Axelsson
ccf630fa62 Use sqlite connection to create database 2020-04-19 16:29:34 -07:00
Josh Holmer
f73149a90f Format SQL queries when printing them to the logs
Before, the query would be formatted equivalent to the input string:

```
[2020-04-18T23:47:32Z DEBUG sqlx_core::postgres::executor] SELECT id, queue, ..., elapsed: 2.320µs

        SELECT id, queue, payload, status, priority, created_at, updated_at
    FROM jobs
    WHERE status = $1
    ORDER BY priority ASC, created_at ASC

```

After, the query is formatted cleanly and consistently:

```
[2020-04-19T00:30:18Z DEBUG sqlx_core::postgres::executor] SELECT id, queue, ..., elapsed: 2.280µs

    SELECT
      id,
      queue,
      payload,
      status,
      priority,
      created_at,
      updated_at
    FROM
      jobs
    WHERE
      status = $1
    ORDER BY
      priority ASC,
      created_at ASC

```

This uses the `sqlformat` crate, which was ported from the
Javascript `sql-formatter-plus` library specifically for this purpose.
2020-04-19 16:11:50 -07:00
Ryan Leckey
1cdfb8507c fix: remove the clone on Encode for JsonValue 2020-04-19 16:05:37 -07:00
Peter Maatman
1b04829c46 json: Refactor Encode/Decode for serde values to be generic for DB
Since the implementation of Encode and Decode for both mysql and
postgres on serde's Value and RawValue were practically the same they
were moved to the generic json module.
2020-04-19 15:58:47 -07:00
Peter Maatman
30d13174ea mysql: Add JSON support 2020-04-19 15:58:47 -07:00
Felix Wiedemann
57f52ffbc4 postgres: Use $USER as default for the username 2020-04-18 14:31:31 -07:00
Felix Wiedemann
f3fe41ebdb postgres: Use username as default for database name 2020-04-18 14:31:31 -07:00
Felix Wiedemann
a2673f7880 postgres: Add support for postgres:///?host=... connection strings 2020-04-18 14:31:31 -07:00
Felix Wiedemann
fc78f15ebf postgres: Add support for non-default socket paths 2020-04-18 14:31:31 -07:00
Felix Wiedemann
5628658d3f postgres: Add unix domain socket support 2020-04-18 14:31:31 -07:00
Felix Wiedemann
49f15713d6 Move empty host handling to the DB specific code 2020-04-18 14:31:31 -07:00
Jesper Axelsson
f1c1d9ae07 Formatting 2020-04-17 23:03:46 -07:00
Jesper Axelsson
19f086cc23 Add alias 'mig' for migration 2020-04-17 23:03:46 -07:00
Jesper Axelsson
cbac531a63 Changed from db_creator to migrator 2020-04-17 23:03:46 -07:00
Jesper Axelsson
cf3d8c295e Make sure user really want to drop their database 2020-04-17 23:03:46 -07:00
Jesper Axelsson
18800b6e2a Only set features from features... 2020-04-17 23:03:46 -07:00
Jesper Axelsson
c1f17c75d2 Add suport for sqlite 2020-04-17 23:03:46 -07:00
Jesper Axelsson
961ade18d0 Moved all migration code for postgres behind traits 2020-04-17 23:03:46 -07:00
Jesper Axelsson
b20c6a2e09 Move migration code to their own files 2020-04-17 23:03:46 -07:00
Jesper Axelsson
93135c51f4 Start supporting more databases 2020-04-17 23:03:46 -07:00
Jesper Axelsson
42213b9937 Add feature check for database 2020-04-17 23:03:46 -07:00
Jesper Axelsson
5295389fdf Setup features 2020-04-17 23:03:46 -07:00
Jesper Axelsson
36301f6698 Started to abstract the postgres database 2020-04-17 23:03:46 -07:00
Ryan Leckey
145218f116 Merge branch 'shssoichiro-snake-case-support' 2020-04-17 22:52:11 -07:00
Ryan Leckey
230eeef940 Merge branch 'snake-case-support' of git://github.com/shssoichiro/sqlx into shssoichiro-snake-case-support 2020-04-17 22:51:52 -07:00
Josh Holmer
16229fd0ad Add basic query logging
Prints each query performed at DEBUG level,
along with a timing of how long the query took
to execute.

Slow queries will be printed at WARN level.
Currently the slow query threshold is
hardcoded to 1 second.
2020-04-17 22:49:33 -07:00
George Kaplan
7df6d4dbcf undo URL percent-encoding for SQLite connection strings 2020-04-17 18:23:33 -07:00
Samani G. Gikandi
45744e8033 Updates CI workflow for realworld
* Moves realworld into a separate file since it's long
* Adds checkout, db provisioning, and build steps for postgres and
  sqlite
2020-04-17 12:42:22 -07:00
Samani G. Gikandi
841d9a7f45 Changes PKs in realworld postgres to SERIAL
`GENERATED` was added in Postgres 12 and we test as far back as 9
2020-04-17 12:42:22 -07:00
Samani G. Gikandi
9fb523d4e8 Runs rustfmt across realworld 2020-04-17 12:42:22 -07:00
Samani G. Gikandi
8138a26b61 Finishes realworld example implementation
* Adds tables for  storing articles, tags, favorites, and comments.
* Implements all remaining web APIs (articles, tags, profiles, etc)

* Refactors `Provide` traits into
* `ProvideAuthn` is used to store/retrieve user info
* `ProvideData` is used to retrieve application data
* ` Provide` traits are now implemented on Connections instead of Pools
* Introduces `Db` trait that encapsulates DB connections

* Cleans up endpoint functions
2020-04-17 12:42:22 -07:00
Samani G. Gikandi
7038dd8ab2 Refactors realworld example with multi DB support
**General**

* Moves `examples/postgres/realworld` to `examples/realworld`
* The app is now architected to support multiple DBs
* Adds feature flags for `sqlite` and `postgres` to allow user to choose
  which backend to use

    *NOTE* Currently it is not possible to compile with `postgres` and `sqlite`
      enabled as we are using the `query!` and `query_as!` macros and they
      seem to get unhappy.

* Adds CLI flags for picking the DB backend to use at runtime
* Adds schema file and implementation for SQLite for `/api/user` routes
* Adds stub routes and trait for articles and Articles entity

**Changes**

* We now use i32 instead of i64 as the user_id to get around some quirks
  w/ the SQLite driver.
* Reimplements existing route handlers w/ an error handling shim so we can use
  Try inside the biz logic
* *FIX* Adds a `user` key to the register user body to conform w/ realworld's
  API specs

APIs were functionally tested using realworld's API test script
  (https://github.com/gothinkster/realworld/tree/master/api#authentication)
2020-04-17 12:42:22 -07:00
Josh Holmer
8b78680d9c Add snake_case rename support for Type macro
I encountered a use case while converting a project
from Diesel to sqlx, where I had a custom Postgres enum
which included a snake case field name:

```rust
pub enum JobStatus {
    NotRun,
    Finished,
    Failed,
}
```

Which translates to:

```sql
CREATE TYPE job_status AS ENUM ('not_run', 'finished', 'failed');
```

This is likely to be a semi-common use case,
so this commit adds snake case support for enums
via the `#[sqlx(rename_all = "snake_case")]` attribute.
2020-04-17 03:59:19 -04:00
meh
2fb38dd0c1 Derive all traits and impl Deref and AsRef for types::Json 2020-04-16 11:00:31 -07:00
meh
bfc52ca2f4 Add query_unchecked and query_file_unchecked macros 2020-04-16 09:54:09 -07:00
Jesper Axelsson
c3cc146e7c Update README.md 2020-04-15 08:26:50 -07:00
Jesper Axelsson
c6e9b2739b Formatting 2020-04-13 14:36:42 -07:00
Jesper Axelsson
719b04b61e Add drop and create database 2020-04-13 14:36:42 -07:00
Jesper Axelsson
3ec97c51b1 Setup database commands 2020-04-13 14:36:42 -07:00
Jesper Axelsson
35bcfd60c3 Create database based on connection string 2020-04-13 14:36:42 -07:00
Jesper Axelsson
9a765e4e86 Use anihow 2020-04-13 14:36:42 -07:00