about summary refs log tree commit diff
path: root/server
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-01-13 22:37:31 +0100
committerMelonai <einebeere@gmail.com>2021-01-13 22:37:31 +0100
commit5b6ed8cfb245f7f9991f5fc192b8c9215f0ecdc1 (patch)
treec4b9ef58c708900d2a88fa5e8790629d415e2b75 /server
parent6e8a1117ddef390453d6b695c301d7f4d219f947 (diff)
downloadshorest-5b6ed8cfb245f7f9991f5fc192b8c9215f0ecdc1.tar.zst
shorest-5b6ed8cfb245f7f9991f5fc192b8c9215f0ecdc1.zip
Created diesel migrations
Diffstat (limited to 'server')
-rw-r--r--server/Cargo.lock32
-rw-r--r--server/Cargo.toml1
-rw-r--r--server/diesel.toml5
-rw-r--r--server/migrations/00000000000000_diesel_initial_setup/down.sql6
-rw-r--r--server/migrations/00000000000000_diesel_initial_setup/up.sql36
-rw-r--r--server/migrations/2021-01-12-171443_create_links/down.sql1
-rw-r--r--server/migrations/2021-01-12-171443_create_links/up.sql4
-rw-r--r--server/src/main.rs15
-rw-r--r--server/src/schema.rs6
9 files changed, 98 insertions, 8 deletions
diff --git a/server/Cargo.lock b/server/Cargo.lock
index bb85565..f77d973 100644
--- a/server/Cargo.lock
+++ b/server/Cargo.lock
@@ -552,6 +552,16 @@ dependencies = [
 ]
 
 [[package]]
+name = "diesel_migrations"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf3cde8413353dc7f5d72fa8ce0b99a560a359d2c5ef1e5817ca731cd9008f4c"
+dependencies = [
+ "migrations_internals",
+ "migrations_macros",
+]
+
+[[package]]
 name = "dotenv"
 version = "0.15.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1023,6 +1033,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
 
 [[package]]
+name = "migrations_internals"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b4fc84e4af020b837029e017966f86a1c2d5e83e64b589963d5047525995860"
+dependencies = [
+ "diesel",
+]
+
+[[package]]
+name = "migrations_macros"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9753f12909fd8d923f75ae5c3258cae1ed3c8ec052e1b38c93c21a6d157f789c"
+dependencies = [
+ "migrations_internals",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
 name = "mime"
 version = "0.3.16"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1477,6 +1508,7 @@ dependencies = [
  "base64 0.12.3",
  "crc32fast",
  "diesel",
+ "diesel_migrations",
  "dotenv",
  "env_logger",
  "openssl",
diff --git a/server/Cargo.toml b/server/Cargo.toml
index 08712d8..ebd13cd 100644
--- a/server/Cargo.toml
+++ b/server/Cargo.toml
@@ -18,6 +18,7 @@ crc32fast = "1.2.0"
 base64 = "0.12.0"
 url = "2.1.1"
 diesel = { version = "1.4.4", features = ["postgres", "r2d2"] }
+diesel_migrations = "1.4.0"
 actix-files = "0.2.1"
 openssl = "*"
 
diff --git a/server/diesel.toml b/server/diesel.toml
new file mode 100644
index 0000000..92267c8
--- /dev/null
+++ b/server/diesel.toml
@@ -0,0 +1,5 @@
+# For documentation on how to configure this file,
+# see diesel.rs/guides/configuring-diesel-cli
+
+[print_schema]
+file = "src/schema.rs"
diff --git a/server/migrations/00000000000000_diesel_initial_setup/down.sql b/server/migrations/00000000000000_diesel_initial_setup/down.sql
new file mode 100644
index 0000000..a9f5260
--- /dev/null
+++ b/server/migrations/00000000000000_diesel_initial_setup/down.sql
@@ -0,0 +1,6 @@
+-- This file was automatically created by Diesel to setup helper functions
+-- and other internal bookkeeping. This file is safe to edit, any future
+-- changes will be added to existing projects as new migrations.
+
+DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
+DROP FUNCTION IF EXISTS diesel_set_updated_at();
diff --git a/server/migrations/00000000000000_diesel_initial_setup/up.sql b/server/migrations/00000000000000_diesel_initial_setup/up.sql
new file mode 100644
index 0000000..d68895b
--- /dev/null
+++ b/server/migrations/00000000000000_diesel_initial_setup/up.sql
@@ -0,0 +1,36 @@
+-- This file was automatically created by Diesel to setup helper functions
+-- and other internal bookkeeping. This file is safe to edit, any future
+-- changes will be added to existing projects as new migrations.
+
+
+
+
+-- Sets up a trigger for the given table to automatically set a column called
+-- `updated_at` whenever the row is modified (unless `updated_at` was included
+-- in the modified columns)
+--
+-- # Example
+--
+-- ```sql
+-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
+--
+-- SELECT diesel_manage_updated_at('users');
+-- ```
+CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
+BEGIN
+    EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
+                    FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
+BEGIN
+    IF (
+        NEW IS DISTINCT FROM OLD AND
+        NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
+    ) THEN
+        NEW.updated_at := current_timestamp;
+    END IF;
+    RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
diff --git a/server/migrations/2021-01-12-171443_create_links/down.sql b/server/migrations/2021-01-12-171443_create_links/down.sql
new file mode 100644
index 0000000..72bbc99
--- /dev/null
+++ b/server/migrations/2021-01-12-171443_create_links/down.sql
@@ -0,0 +1 @@
+DROP TABLE links;
diff --git a/server/migrations/2021-01-12-171443_create_links/up.sql b/server/migrations/2021-01-12-171443_create_links/up.sql
new file mode 100644
index 0000000..17e3f92
--- /dev/null
+++ b/server/migrations/2021-01-12-171443_create_links/up.sql
@@ -0,0 +1,4 @@
+CREATE TABLE links (
+    hash CHAR(3) PRIMARY KEY,
+    url VARCHAR(2048) NOT NULL
+);
diff --git a/server/src/main.rs b/server/src/main.rs
index 92df222..515fc8f 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,5 +1,6 @@
 extern crate openssl;
 #[macro_use] extern crate diesel;
+#[macro_use] extern crate diesel_migrations;
 #[macro_use] extern crate serde_derive;
 
 mod schema;
@@ -92,16 +93,20 @@ async fn redirect(info: Path<String>, state: Data<PoolState>) -> HttpResponse {
     }
 }
 
+embed_migrations!("migrations");
+
+fn run_migrations(pool: &PoolState) {
+    let migration_connection = &pool.get().expect("Could not connect to database to run migrations");
+    embedded_migrations::run_with_output(migration_connection, &mut std::io::stdout());
+}
+
 #[actix_rt::main]
 async fn main() -> std::io::Result<()> {
     std::env::set_var("RUST_LOG", "actix_web=info");
     env_logger::init();
 
     let pool = establish_connection();
-    let port = std::env::var("PORT")
-        .unwrap()
-        .parse()
-        .expect("PORT must be a number.");
+    run_migrations(&pool);
 
     HttpServer::new(move || {
         App::new()
@@ -120,7 +125,7 @@ async fn main() -> std::io::Result<()> {
                 Files::new("/client/", "./client/")
             )
     })
-        .bind(("0.0.0.0", port))?
+        .bind(("0.0.0.0", 80))?
         .run()
         .await
 }
diff --git a/server/src/schema.rs b/server/src/schema.rs
index 5d823c0..3fa7ce9 100644
--- a/server/src/schema.rs
+++ b/server/src/schema.rs
@@ -1,6 +1,6 @@
 table! {
     links (hash) {
-        hash -> Text,
-        url -> Text,
+        hash -> Bpchar,
+        url -> Varchar,
     }
-}
\ No newline at end of file
+}