Browse Source

embed migrations

master v0.0.1
hole-thu 3 years ago
parent
commit
aabe18f5a4
  1. 2
      Cargo.toml
  2. 22
      README.md
  3. 19
      src/main.rs
  4. 6
      src/schema.rs

2
Cargo.toml

@ -9,12 +9,12 @@ license = "AGPL-3.0"
[dependencies] [dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] } rocket = { version = "0.5.0-rc.1", features = ["json"] }
diesel = { version = "1.4.8", features = ["postgres", "chrono"] } diesel = { version = "1.4.8", features = ["postgres", "chrono"] }
diesel_migrations = "1.4.0"
redis = { version="0.21.5", features = ["aio", "tokio-comp"] } redis = { version="0.21.5", features = ["aio", "tokio-comp"] }
chrono = { version="0.*", features =["serde"] } chrono = { version="0.*", features =["serde"] }
rand = "0.*" rand = "0.*"
dotenv = "0.*" dotenv = "0.*"
sha2 = "0.*" sha2 = "0.*"
diesel_logger = "0.1.1"
log = "0.4.16" log = "0.4.16"
env_logger = "0.9.0" env_logger = "0.9.0"

22
README.md

@ -5,20 +5,22 @@
### prepare database ### prepare database
```sql
CREATE USER hole CREATEDB;
ALTER USER hole WITH PASSWORD "hole_pass";
``` ```
sudo -u postgres psql
```
$ diesel setup
``` ```
```sql ```postgresql
\c hole_v2 postgres=# CREATE USER hole WITH PASSWORD 'hole_pass';
CREATE EXTENSION pg_trgm; CREATE ROLE
postgres=# CREATE DATABASE hole_v2 OWNER hole;
CREATE DATABASE
postgres=# \c hole_v2
You are now connected to database "hole_v2" as user "postgres".
hole_v2=# CREATE EXTENSION pg_trgm;
CREATE EXTENSION
hole_v2=# \q
``` ```
``` ```
$ diesel run ./hole-thu --init-database
``` ```

19
src/main.rs

@ -4,6 +4,9 @@ extern crate rocket;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
#[macro_use]
extern crate diesel_migrations;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
@ -17,13 +20,21 @@ mod rds_models;
mod cache; mod cache;
mod schema; mod schema;
use db_conn::Db; use db_conn::{Conn, Db};
use diesel::Connection;
use random_hasher::RandomHasher; use random_hasher::RandomHasher;
use rds_conn::init_rds_client; use rds_conn::init_rds_client;
use std::env;
embed_migrations!("migrations/postgres");
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), rocket::Error> { async fn main() -> Result<(), rocket::Error> {
load_env(); load_env();
if env::args().any(|arg| arg.eq("--init-database")) {
init_database();
return Ok(());
}
env_logger::init(); env_logger::init();
rocket::build() rocket::build()
.mount( .mount(
@ -58,3 +69,9 @@ fn load_env() {
e => e.map(|_| ()).unwrap(), e => e.map(|_| ()).unwrap(),
} }
} }
fn init_database() {
let database_url = env::var("DATABASE_URL").unwrap();
let conn = Conn::establish(&database_url).unwrap();
embedded_migrations::run(&conn).unwrap();
}

6
src/schema.rs

@ -42,4 +42,8 @@ table! {
joinable!(comments -> posts (post_id)); joinable!(comments -> posts (post_id));
allow_tables_to_appear_in_same_query!(comments, posts, users,); allow_tables_to_appear_in_same_query!(
comments,
posts,
users,
);

Loading…
Cancel
Save