mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 19:31:35 +00:00
Finish writing migrations & generate diesel schema
This commit is contained in:
parent
8c532c97e6
commit
d475723087
12 changed files with 353 additions and 0 deletions
|
@ -3,6 +3,7 @@ mod embedded {
|
|||
|
||||
embed_migrations!("./src/repo/postgres/migrations");
|
||||
}
|
||||
mod schema;
|
||||
|
||||
use diesel_async::{
|
||||
pooled_connection::{
|
||||
|
|
11
src/repo/postgres/migrations/V0000__enable_pgcrypto.rs
Normal file
11
src/repo/postgres/migrations/V0000__enable_pgcrypto.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.inject_custom("CREATE EXTENSION pgcrypto;");
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
|
@ -25,6 +25,8 @@ pub(crate) fn migration() -> String {
|
|||
.nullable(false)
|
||||
.default(AutogenFunction::CurrentTimestamp),
|
||||
);
|
||||
|
||||
t.add_index("ordered_hash_index", types::index(["created_at", "hash"]));
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
|
|
30
src/repo/postgres/migrations/V0002__create_variants.rs
Normal file
30
src/repo/postgres/migrations/V0002__create_variants.rs
Normal file
|
@ -0,0 +1,30 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("variants", |t| {
|
||||
t.inject_custom(r#""id" UUID PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL UNIQUE"#);
|
||||
t.add_column("hash", types::binary().nullable(false));
|
||||
t.add_column("variant", types::text().nullable(false));
|
||||
t.add_column("identifier", types::text().nullable(false));
|
||||
t.add_column(
|
||||
"accessed",
|
||||
types::datetime()
|
||||
.nullable(false)
|
||||
.default(AutogenFunction::CurrentTimestamp),
|
||||
);
|
||||
|
||||
t.add_foreign_key(&["hash"], "hashes", &["hash"]);
|
||||
t.add_index(
|
||||
"hash_variant_index",
|
||||
types::index(["hash", "variant"]).unique(true),
|
||||
);
|
||||
});
|
||||
|
||||
let s = m.make::<Pg>().to_string();
|
||||
println!("{s}");
|
||||
s
|
||||
}
|
25
src/repo/postgres/migrations/V0003__create_aliases.rs
Normal file
25
src/repo/postgres/migrations/V0003__create_aliases.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("aliases", |t| {
|
||||
t.add_column(
|
||||
"alias",
|
||||
types::text()
|
||||
.size(60)
|
||||
.primary(true)
|
||||
.unique(true)
|
||||
.nullable(false),
|
||||
);
|
||||
t.add_column("hash", types::binary().nullable(false));
|
||||
t.add_column("token", types::text().size(60).nullable(false));
|
||||
|
||||
t.add_foreign_key(&["hash"], "hashes", &["hash"]);
|
||||
t.add_index("aliases_hash_index", types::index(["hash"]));
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
21
src/repo/postgres/migrations/V0004__create_settings.rs
Normal file
21
src/repo/postgres/migrations/V0004__create_settings.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("settings", |t| {
|
||||
t.add_column(
|
||||
"key",
|
||||
types::text()
|
||||
.size(80)
|
||||
.primary(true)
|
||||
.unique(true)
|
||||
.nullable(false),
|
||||
);
|
||||
t.add_column("value", types::text().size(80).nullable(false));
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
17
src/repo/postgres/migrations/V0005__create_details.rs
Normal file
17
src/repo/postgres/migrations/V0005__create_details.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("details", |t| {
|
||||
t.add_column(
|
||||
"identifier",
|
||||
types::text().primary(true).unique(true).nullable(false),
|
||||
);
|
||||
t.add_column("details", types::custom("jsonb").nullable(false));
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
53
src/repo/postgres/migrations/V0006__create_queue.rs
Normal file
53
src/repo/postgres/migrations/V0006__create_queue.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.inject_custom("CREATE TYPE job_status AS ENUM ('new', 'running');");
|
||||
|
||||
m.create_table("queue", |t| {
|
||||
t.inject_custom(r#""id" UUID PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL UNIQUE"#);
|
||||
t.add_column("queue", types::text().size(50).nullable(false));
|
||||
t.add_column("job", types::custom("jsonb").nullable(false));
|
||||
t.add_column("status", types::custom("job_status").nullable(false));
|
||||
t.add_column(
|
||||
"queue_time",
|
||||
types::datetime()
|
||||
.nullable(false)
|
||||
.default(AutogenFunction::CurrentTimestamp),
|
||||
);
|
||||
t.add_column("heartbeat", types::datetime());
|
||||
|
||||
t.add_index("queue_status_index", types::index(["queue", "status"]));
|
||||
t.add_index("heartbeat_index", types::index(["heartbeat"]));
|
||||
});
|
||||
|
||||
m.inject_custom(
|
||||
r#"
|
||||
CREATE OR REPLACE FUNCTION queue_status_notify()
|
||||
RETURNS trigger AS
|
||||
$$
|
||||
BEGIN
|
||||
PERFORM pg_notify('queue_status_channel', NEW.id::text);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
"#
|
||||
.trim(),
|
||||
);
|
||||
|
||||
m.inject_custom(
|
||||
r#"
|
||||
CREATE TRIGGER queue_status
|
||||
AFTER INSERT OR UPDATE OF status
|
||||
ON queue
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE queue_status_notify();
|
||||
"#
|
||||
.trim(),
|
||||
);
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("store_migrations", |t| {
|
||||
t.add_column(
|
||||
"identifier",
|
||||
types::text().primary(true).nullable(false).unique(true),
|
||||
);
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
25
src/repo/postgres/migrations/V0008__create_proxies.rs
Normal file
25
src/repo/postgres/migrations/V0008__create_proxies.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("proxies", |t| {
|
||||
t.add_column(
|
||||
"url",
|
||||
types::text().primary(true).unique(true).nullable(false),
|
||||
);
|
||||
t.add_column("alias", types::text().nullable(false));
|
||||
t.add_column(
|
||||
"accessed",
|
||||
types::datetime()
|
||||
.nullable(false)
|
||||
.default(AutogenFunction::CurrentTimestamp),
|
||||
);
|
||||
|
||||
t.add_foreign_key(&["alias"], "aliases", &["alias"]);
|
||||
});
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
38
src/repo/postgres/migrations/V0009__create_uploads.rs
Normal file
38
src/repo/postgres/migrations/V0009__create_uploads.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use barrel::backend::Pg;
|
||||
use barrel::functions::AutogenFunction;
|
||||
use barrel::{types, Migration};
|
||||
|
||||
pub(crate) fn migration() -> String {
|
||||
let mut m = Migration::new();
|
||||
|
||||
m.create_table("uploads", |t| {
|
||||
t.inject_custom(r#""id" UUID PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL UNIQUE"#);
|
||||
t.add_column("result", types::custom("jsonb"));
|
||||
});
|
||||
|
||||
m.inject_custom(
|
||||
r#"
|
||||
CREATE OR REPLACE FUNCTION upload_completion_notify()
|
||||
RETURNS trigger AS
|
||||
$$
|
||||
BEGIN
|
||||
PERFORM pg_notify('upload_completion_channel', NEW.id::text);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
"#
|
||||
.trim(),
|
||||
);
|
||||
|
||||
m.inject_custom(
|
||||
r#"
|
||||
CREATE TRIGGER upload_result
|
||||
AFTER INSERT OR UPDATE OF result
|
||||
ON uploads
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE upload_completion_notify();
|
||||
"#,
|
||||
);
|
||||
|
||||
m.make::<Pg>().to_string()
|
||||
}
|
114
src/repo/postgres/schema.rs
Normal file
114
src/repo/postgres/schema.rs
Normal file
|
@ -0,0 +1,114 @@
|
|||
// @generated automatically by Diesel CLI.
|
||||
|
||||
pub mod sql_types {
|
||||
#[derive(diesel::sql_types::SqlType)]
|
||||
#[diesel(postgres_type(name = "job_status"))]
|
||||
pub struct JobStatus;
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
aliases (alias) {
|
||||
alias -> Text,
|
||||
hash -> Bytea,
|
||||
token -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
details (identifier) {
|
||||
identifier -> Text,
|
||||
#[sql_name = "details"]
|
||||
details_json -> Jsonb,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
hashes (hash) {
|
||||
hash -> Bytea,
|
||||
identifier -> Text,
|
||||
motion_identifier -> Nullable<Text>,
|
||||
created_at -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
proxies (url) {
|
||||
url -> Text,
|
||||
alias -> Text,
|
||||
accessed -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
use diesel::sql_types::*;
|
||||
use super::sql_types::JobStatus;
|
||||
|
||||
queue (id) {
|
||||
id -> Uuid,
|
||||
#[sql_name = "queue"]
|
||||
queue_name -> Text,
|
||||
job -> Jsonb,
|
||||
status -> JobStatus,
|
||||
queue_time -> Timestamp,
|
||||
heartbeat -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
refinery_schema_history (version) {
|
||||
version -> Int4,
|
||||
#[max_length = 255]
|
||||
name -> Nullable<Varchar>,
|
||||
#[max_length = 255]
|
||||
applied_on -> Nullable<Varchar>,
|
||||
#[max_length = 255]
|
||||
checksum -> Nullable<Varchar>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
settings (key) {
|
||||
key -> Text,
|
||||
value -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
store_migrations (identifier) {
|
||||
identifier -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
uploads (id) {
|
||||
id -> Uuid,
|
||||
result -> Jsonb,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
variants (id) {
|
||||
id -> Uuid,
|
||||
hash -> Bytea,
|
||||
variant -> Text,
|
||||
identifier -> Text,
|
||||
accessed -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::joinable!(aliases -> hashes (hash));
|
||||
diesel::joinable!(proxies -> aliases (alias));
|
||||
diesel::joinable!(variants -> hashes (hash));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(
|
||||
aliases,
|
||||
details,
|
||||
hashes,
|
||||
proxies,
|
||||
queue,
|
||||
refinery_schema_history,
|
||||
settings,
|
||||
store_migrations,
|
||||
uploads,
|
||||
variants,
|
||||
);
|
Loading…
Reference in a new issue