/
db-init.sh
executable file
·48 lines (35 loc) · 1.78 KB
/
db-init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
### https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-20-04
echo "This script is to be executed manually per section as needed (Linux only)."
exit 1
sudo apt-get -y install postgresql postgresql-contrib libpq-dev
sudo -i -u postgres
createuser keypost --superuser
createdb keypost
###############################################################
### BELOW COMMANDS ARE TO BE EXECUTED MANUALLY (Linux only) ###
###############################################################
sudo adduser keypost --no-create-home --disabled-login
sudo -su keypost
psql -d keypost # ALTER ROLE keypost WITH PASSWORD 'change-me-to-something-secret';
### https://diesel.rs/guides/getting-started
cargo install diesel_cli --no-default-features --features postgres --verbose
export KEYPOST_DATABASE_PSSWD=change-me-to-something-secret
export DATABASE_URL=postgres://keypost:${KEYPOST_DATABASE_PSSWD}@localhost/keypost
diesel migration run
### Only run below if the migrations directory is empty:
diesel setup
### Now you'll have to find the most current database schema to recreate the database:
diesel migration generate keypost_schema
### Paste the most recent schema into up.sql in the "migrations/<timestamp>_keypost_schema" directory then run:
diesel migration run
### Become the keypost user:
sudo -su keypost
### Login to the database and verify the tables are created:
psql -d keypost
### Start the app and verify its functionality:
cargo run
### NOTES
# - "The diesel_migrations crate provides the embed_migrations! macro, allowing you to embed migration scripts in
# the final binary. Once your code uses it, you can simply include embedded_migrations::run(&db_conn) at the
# start of your main function to run migrations every time the application starts."