Skip to content

Commit 067eb7c

Browse files
committed
Set up the groundwork for a port to Diesel
This PR changes the `users#show` endpoint to use Diesel instead of rust-postgres, (and adds a test for that endpoint). I chose that endpoint as it seemed likely to touch as few things as possible, but enough to require setting up Diesel connection pooling, and testing. I have also ported over the migrations to use Diesel's migration infrastructure. The migration files (except the final migration) were generated programatically. Any non-reversible migrations were not dumped as these contained procedural data updates which aren't relevant to setting up a new database. `cargo run --bin migrate` will move all the entries in the old migrations table into the one used by Diesel. That function is brittle since it relies on Diesel internals. However, it really only matters for existing contributors (and one deploy to Heroku), so when that function breaks we can just delete it. I've added an additional migration to make the schema compatible with `infer_schema!`, which doesn't support tables that have no primary key. I'm not using `infer_schema!` just yet, as it doesn't work with non-core types and there are some tsvector columns. I re-ordered the columns on the `User` struct, as diesel emits an explicit select clause and then fetches columns by index rather than by name. This means that the order has to match how it was defined to Diesel (which in the case of `infer_schema!` will be the database definition order). If we don't want this restriction, we can replace the `infer_schema!` call with manual `table!` calls (which can be automatically generated by `diesel print-schema`), and have the columns ordered there to match the struct. Differences to note ------------------- The rust-postgres connection is set to require TLS when the env var `HEROKU` is set. In Diesel, this would be to add `?sslmode=require` onto the database URL. I'm unsure if heroku-postgres URLs already have this or not, so I have not added any explicit code for that. It's a question that should be answered before this is deployed. Additionally, I chose not to automatically wrap each request in a transaction. It's unneccessary most of the time, and seems like it was only done to make testing easier. Since Diesel has explicit support for "run tests in a transaction that never commits", we don't need that here.
1 parent 639ba16 commit 067eb7c

File tree

256 files changed

+863
-953
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+863
-953
lines changed

Cargo.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ rustc-serialize = "0.3"
3636
license-exprs = "^1.3"
3737
dotenv = "0.8.0"
3838
toml = "0.2"
39+
diesel = { version = "0.11.0", features = ["postgres"] }
40+
diesel_codegen = { version = "0.11.0", features = ["postgres"] }
41+
r2d2-diesel = "0.11.0"
3942

4043
conduit = "0.8"
4144
conduit-conditional-get = "0.8"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE users;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
email VARCHAR NOT NULL UNIQUE,
4+
gh_access_token VARCHAR NOT NULL,
5+
api_token VARCHAR NOT NULL
6+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE packages;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE packages (
2+
id SERIAL PRIMARY KEY,
3+
name VARCHAR NOT NULL UNIQUE,
4+
user_id INTEGER NOT NULL
5+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE versions;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE versions (
2+
id SERIAL PRIMARY KEY,
3+
package_id INTEGER NOT NULL,
4+
num VARCHAR NOT NULL
5+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions DROP CONSTRAINT unique_num;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ADD CONSTRAINT unique_num UNIQUE (package_id, num);

0 commit comments

Comments
 (0)