Skip to content

Commit

Permalink
Added rust/postgres/postgres/firststep.
Browse files Browse the repository at this point in the history
create dev environment.
  • Loading branch information
mikoto2000 committed Jan 7, 2023
1 parent b9f93bc commit 0e3c509
Show file tree
Hide file tree
Showing 9 changed files with 890 additions and 0 deletions.
37 changes: 37 additions & 0 deletions rust/sql/postgres/postgres/firststep/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# rust postgres firststep

## Development

### Start dev environment

```sh
docker compose up -d
```

### Attach application container

```sh
docker compose exec app bash
```


### Setup project

```sh
cargo init
cargo add postgres --features="with-uuid-1"
cargo add uuid
```

### Run

```sh
cargo run
```

## References

- [postgres - crates.io: Rust Package Registry](https://crates.io/crates/postgres)
- [postgres - Rust](https://docs.rs/postgres/latest/postgres/)
- [Config in postgres::config - Rust](https://docs.rs/postgres/latest/postgres/config/struct.Config.html)

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS "device";
DROP TABLE IF EXISTS "user";

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE "user" (
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
, name text NOT NULL
);

CREATE TABLE "device" (
id UUID PRIMARY KEY DEFAULT gen_random_uuid()
, name text NOT NULL
, user_id UUID REFERENCES "user"(id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
INSERT INTO "user" (id, name)
values
('{d35696e8-5aa0-4b35-88cf-ded521b3dede}', 'user1')
, ('{d35696e8-5aa0-4b35-88cf-ded521b3dedd}', 'user2')
, ('{d35696e8-5aa0-4b35-88cf-ded521b3dedc}', 'user3')
, ('{d35696e8-5aa0-4b35-88cf-ded521b3dedb}', 'user4')
, ('{d35696e8-5aa0-4b35-88cf-ded521b3deda}', 'user5')
;

INSERT INTO "device" (id, name, user_id)
values
('{d35696e8-5aa0-4b35-98cf-ded521b3dede}', 'device11', '{d35696e8-5aa0-4b35-88cf-ded521b3dede}')
, ('{d35696e8-5aa0-4b35-98cf-ded521b3dedd}', 'device12', '{d35696e8-5aa0-4b35-88cf-ded521b3dede}')
, ('{d35696e8-5aa0-4b35-98cf-ded521b3dedc}', 'device13', '{d35696e8-5aa0-4b35-88cf-ded521b3dede}')
, ('{d35696e8-5aa0-4b35-98cf-ded521b3dedb}', 'device14', '{d35696e8-5aa0-4b35-88cf-ded521b3dede}')
, ('{d35696e8-5aa0-4b35-98cf-ded521b3deda}', 'device15', '{d35696e8-5aa0-4b35-88cf-ded521b3dede}')
, ('{d35696e8-5aa0-4b35-a8cf-ded521b3dede}', 'device21', '{d35696e8-5aa0-4b35-88cf-ded521b3dedd}')
, ('{d35696e8-5aa0-4b35-a8cf-ded521b3dedd}', 'device22', '{d35696e8-5aa0-4b35-88cf-ded521b3dedd}')
, ('{d35696e8-5aa0-4b35-a8cf-ded521b3dedc}', 'device23', '{d35696e8-5aa0-4b35-88cf-ded521b3dedd}')
, ('{d35696e8-5aa0-4b35-a8cf-ded521b3dedb}', 'device24', '{d35696e8-5aa0-4b35-88cf-ded521b3dedd}')
, ('{d35696e8-5aa0-4b35-a8cf-ded521b3deda}', 'device25', '{d35696e8-5aa0-4b35-88cf-ded521b3dedd}')
;
35 changes: 35 additions & 0 deletions rust/sql/postgres/postgres/firststep/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3'

services:
app:
image: rust
init: true
environment:
POSTGRES_HOST: "postgres:5432"
POSTGRES_DB: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "password"
command:
- sleep
- infinity
volumes:
- "./project:/project"
working_dir: /project
postgres:
image: postgres:15
init: true
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data
- ./conf/postgres/init.d/:/docker-entrypoint-initdb.d
environment:
POSTGRES_DB: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "password"
ports:
- "0.0.0.0:5432:5432"

volumes:
postgres_data:
driver: local

1 change: 1 addition & 0 deletions rust/sql/postgres/postgres/firststep/project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading

0 comments on commit 0e3c509

Please sign in to comment.