| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Docker
 | 
					
						
							| 
									
										
										
										
											2021-08-21 23:19:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | 1. follow the [instructions](https://docs.docker.com/desktop/mac/install/) to install docker. | 
					
						
							|  |  |  | 2. open terminal and run: `docker pull postgres` | 
					
						
							|  |  |  |     | 
					
						
							| 
									
										
										
										
											2021-09-02 17:32:30 +08:00
										 |  |  | 3. run `make init_postgres` if you have not run before. You can find out the running container by run `docker ps` | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES | 
					
						
							|  |  |  | bfcdd6369e89   postgres   "docker-entrypoint.s…"   19 minutes ago   Up 19 minutes   0.0.0.0:5433->5432/tcp, :::5433->5432/tcp   brave_bassi | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 17:32:30 +08:00
										 |  |  | 4. run `make init_database`. It will create the database scheme on remote specified by DATABASE_URL. You can connect you database using  | 
					
						
							| 
									
										
										
										
											2021-08-22 09:25:00 +08:00
										 |  |  | pgAdmin. | 
					
						
							| 
									
										
										
										
											2021-09-25 21:47:02 +08:00
										 |  |  |     | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  |  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 17:32:30 +08:00
										 |  |  | The information you enter must be the same as the `make init_postgres`. e.g. | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | ``` | 
					
						
							|  |  |  | export DB_USER=postgres | 
					
						
							|  |  |  | export DB_PASSWORD=password | 
					
						
							|  |  |  | export DB_NAME=flowy | 
					
						
							|  |  |  | export DB_PORT=5433 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 23:19:57 +08:00
										 |  |  |  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 15:32:48 +08:00
										 |  |  | [Docker command](https://docs.docker.com/engine/reference/commandline/builder_prune/) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Run
 | 
					
						
							|  |  |  | By default, Docker images do not expose their ports to the underlying host machine. We need to do it explicitly using the -p flag. | 
					
						
							| 
									
										
										
										
											2021-08-24 13:10:53 +08:00
										 |  |  | `docker run -p 8000:8000 backend` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Sqlx
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-22 10:25:25 +08:00
										 |  |  | **sqlx-cli** | 
					
						
							|  |  |  | *  [install sqlx-cli](https://github.com/launchbadge/sqlx/tree/master/sqlx-cli) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 13:10:53 +08:00
										 |  |  | **Sqlx and Diesel commands**  | 
					
						
							|  |  |  | * create migration | 
					
						
							|  |  |  |     * sqlx: sqlx migrate add $(table) | 
					
						
							|  |  |  |     * diesel: diesel migration generation $(table) | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | * run migration | 
					
						
							|  |  |  |     * sqlx: sqlx migrate run | 
					
						
							|  |  |  |     * diesel: diesel migration run | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | * reset database | 
					
						
							|  |  |  |     * sqlx: sqlx database reset | 
					
						
							|  |  |  |     * diesel: diesel database reset | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  | **offline mode** | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | `cargo sqlx prepare -- --bin backend` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 13:10:53 +08:00
										 |  |  | **Type mapping** | 
					
						
							|  |  |  | * [postgres type map](https://docs.rs/sqlx/0.5.7/sqlx/postgres/types/index.html) | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  | * [postgres and diesel type map](https://kotiri.com/2018/01/31/postgresql-diesel-rust-types.html) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Q&A
 | 
					
						
							|  |  |  | 1. Receive` { code: 24, kind: Other, message: "Too many open files" } on arbiter` after running cargo test on backend. | 
					
						
							|  |  |  | > This is due to a limit enforced by the operating system on the maximum number of open file descriptors (including sockets) for each process.
 | 
					
						
							|  |  |  | > Raising the file descriptor limit using `ulimit -n 2048` to solve this issue. It won't stay after reboot so check on google how to persist 
 | 
					
						
							|  |  |  | > that value if you want to.
 | 
					
						
							|  |  |  | >  | 
					
						
							|  |  |  | > or you can try:
 | 
					
						
							|  |  |  | > `launchctl limit maxfiles 2048 2048`
 | 
					
						
							|  |  |  | > `launchctl limit maxfiles`
 | 
					
						
							|  |  |  | >  | 
					
						
							|  |  |  | > Don't forget to relaunch your terminal.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## More
 | 
					
						
							| 
									
										
										
										
											2021-11-22 10:25:25 +08:00
										 |  |  | * [11-database-drivers](https://blog.logrocket.com/11-database-drivers-and-orms-for-rust-that-are-ready-for-production/)  |