Skip to content

Commit 183de33

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 9e0aef3 commit 183de33

File tree

255 files changed

+833
-917
lines changed

Some content is hidden

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

255 files changed

+833
-917
lines changed

Cargo.lock

+91
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE users;
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+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE packages;
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+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE versions;
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+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions DROP CONSTRAINT unique_num;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ADD CONSTRAINT unique_num UNIQUE (package_id, num);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE version_dependencies;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE version_dependencies (
2+
version_id INTEGER NOT NULL,
3+
depends_on_id INTEGER NOT NULL
4+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages DROP COLUMN updated_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT now();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages DROP COLUMN created_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT now();

migrations/20140925132250_dumped_migration_8/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
UPDATE packages SET updated_at = now() WHERE updated_at IS NULL;
2+
UPDATE packages SET created_at = now() WHERE created_at IS NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions DROP COLUMN updated_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT now();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions DROP COLUMN created_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT now();

migrations/20140925132253_dumped_migration_11/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
UPDATE versions SET updated_at = now() WHERE updated_at IS NULL;
2+
UPDATE versions SET created_at = now() WHERE created_at IS NULL;

migrations/20140925132254_dumped_migration_12/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE versions ALTER COLUMN updated_at DROP DEFAULT;
2+
ALTER TABLE versions ALTER COLUMN created_at DROP DEFAULT;
3+
ALTER TABLE packages ALTER COLUMN updated_at DROP DEFAULT;
4+
ALTER TABLE packages ALTER COLUMN created_at DROP DEFAULT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE metadata;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE metadata (
2+
total_downloads BIGINT NOT NULL
3+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE FROM metadata;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
INSERT INTO metadata (total_downloads) VALUES (0);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages DROP COLUMN downloads;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages ADD COLUMN downloads INTEGER NOT NULL DEFAULT 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions DROP COLUMN downloads;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ADD COLUMN downloads INTEGER NOT NULL DEFAULT 0;

migrations/20140925161625_dumped_migration_17/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE versions ALTER COLUMN downloads DROP DEFAULT;
2+
ALTER TABLE packages ALTER COLUMN downloads DROP DEFAULT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages DROP COLUMN max_version;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE packages ADD COLUMN max_version VARCHAR;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ALTER COLUMN downloads DROP NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE versions ALTER COLUMN downloads SET NOT NULL;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE crates RENAME TO packages;
2+
ALTER TABLE versions RENAME COLUMN crate_id TO package_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE packages RENAME TO crates;
2+
ALTER TABLE versions RENAME COLUMN package_id TO crate_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_crate_updated_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_crate_updated_at ON crates (updated_at);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_crate_created_at;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_crate_created_at ON crates (created_at);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_crate_downloads;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_crate_downloads ON crates (downloads);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_version_crate_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_version_crate_id ON versions (crate_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_version_num;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_version_num ON versions (num);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_version_dependencies_version_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_version_dependencies_version_id ON version_dependencies (version_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_version_dependencies_depends_on_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_version_dependencies_depends_on_id ON version_dependencies (depends_on_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE crate_downloads;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE crate_downloads (
2+
id SERIAL PRIMARY KEY,
3+
crate_id INTEGER NOT NULL,
4+
downloads INTEGER NOT NULL,
5+
date TIMESTAMP NOT NULL
6+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_crate_downloads_crate_id;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_crate_downloads_crate_id ON crate_downloads (crate_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX index_crate_downloads_date;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX index_crate_downloads_date ON crate_downloads (date(date));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE version_downloads;

0 commit comments

Comments
 (0)