-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Diesel is used for schema migrations (written in raw SQL; targetting PostgreSQL at the moment) and conversions between SQL and Rust datatypes.
- Loading branch information
Showing
10 changed files
with
242 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# For documentation on how to configure this file, | ||
# see diesel.rs/guides/configuring-diesel-cli | ||
|
||
[print_schema] | ||
file = "src/storage/sql/schema.rs" | ||
|
||
[migrations_directory] | ||
dir = "src/storage/sql/migrations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Do not fail compilation until we've started using that code | ||
#[allow(dead_code)] | ||
pub mod sql; |
3 changes: 3 additions & 0 deletions
3
src/storage/sql/migrations/2020-07-25-065258_create_snippets/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE tags; | ||
DROP TABLE changesets; | ||
DROP TABLE snippets; |
59 changes: 59 additions & 0 deletions
59
src/storage/sql/migrations/2020-07-25-065258_create_snippets/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
CREATE TABLE snippets ( | ||
-- an internal autoincrementing identifier used in foreign keys. | ||
-- Normally not visible publicly, except for the case when it is | ||
-- used for looking legacy snippets up by id | ||
id SERIAL PRIMARY KEY, | ||
-- a short unique snippet identifier visible to users | ||
slug VARCHAR(32) NOT NULL, | ||
|
||
title TEXT, | ||
syntax TEXT, | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, | ||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, | ||
|
||
-- slugs must be unique (this will also automatically create a unique index) | ||
CONSTRAINT uq_slug UNIQUE (slug) | ||
); | ||
|
||
|
||
-- will be used for pagination; slug guarantees uniqueness of the sorting key | ||
CREATE INDEX snippets_created_at_slug ON snippets (created_at, slug); | ||
CREATE INDEX snippets_updated_at_slug ON snippets (updated_at, slug); | ||
|
||
|
||
CREATE TABLE changesets ( | ||
id SERIAL PRIMARY KEY, | ||
snippet_id INTEGER NOT NULL, | ||
|
||
-- numeric index used to determine the ordering of changesets for a given snippet | ||
version INTEGER DEFAULT 0 NOT NULL, | ||
content TEXT NOT NULL, | ||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, | ||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, | ||
|
||
-- there can be multiple changesets per snippet | ||
CONSTRAINT fk_snippet FOREIGN KEY (snippet_id) REFERENCES snippets(id), | ||
-- but each one is supposed to have a unique version number | ||
CONSTRAINT uq_version UNIQUE (snippet_id, version), | ||
-- sanity check: do not allow empty changesets | ||
CONSTRAINT check_not_empty CHECK (LENGTH(content) > 0), | ||
-- sanity check: version numbers are non-negative integers | ||
CONSTRAINT check_non_negative_version CHECK (version >= 0) | ||
); | ||
|
||
|
||
-- tags could have been associated with snippets as M:M via an auxiliary table, | ||
-- but Diesel only supports child-parent associations, so let's do that instead | ||
CREATE TABLE tags ( | ||
id SERIAL PRIMARY KEY, | ||
snippet_id INTEGER NOT NULL, | ||
|
||
value TEXT NOT NULL, | ||
|
||
-- there can be multiple tags per snippet | ||
CONSTRAINT fk_snippet FOREIGN KEY (snippet_id) REFERENCES snippets(id), | ||
-- do not allow to abuse the tags for storing too much data | ||
CONSTRAINT check_length CHECK (LENGTH(value) < 128), | ||
-- do not allow repeated tags per snippet | ||
CONSTRAINT uq_snippet_tag UNIQUE (snippet_id, value) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @generated automatically by Diesel CLI. | ||
|
||
diesel::table! { | ||
changesets (id) { | ||
id -> Int4, | ||
snippet_id -> Int4, | ||
version -> Int4, | ||
content -> Text, | ||
created_at -> Timestamptz, | ||
updated_at -> Timestamptz, | ||
} | ||
} | ||
|
||
diesel::table! { | ||
snippets (id) { | ||
id -> Int4, | ||
slug -> Varchar, | ||
title -> Nullable<Text>, | ||
syntax -> Nullable<Text>, | ||
created_at -> Timestamptz, | ||
updated_at -> Timestamptz, | ||
} | ||
} | ||
|
||
diesel::table! { | ||
tags (id) { | ||
id -> Int4, | ||
snippet_id -> Int4, | ||
value -> Text, | ||
} | ||
} | ||
|
||
diesel::joinable!(changesets -> snippets (snippet_id)); | ||
diesel::joinable!(tags -> snippets (snippet_id)); | ||
|
||
diesel::allow_tables_to_appear_in_same_query!(changesets, snippets, tags,); |