react-rust-postgres: add connection to postgres with migrations
Signed-off-by: Jérémie Drouet <jeremie.drouet@gmail.com>master
parent
bed22c74f5
commit
b73e252639
@ -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"
|
@ -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();
|
@ -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;
|
@ -0,0 +1 @@
|
||||
DROP TABLE users;
|
@ -0,0 +1,4 @@
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
login TEXT UNIQUE NOT NULL
|
||||
);
|
@ -0,0 +1,6 @@
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Int4,
|
||||
login -> Text,
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
#![allow(proc_macro_derive_resolution_fallback)]
|
||||
|
||||
use diesel;
|
||||
use diesel::prelude::*;
|
||||
use super::schema::users;
|
||||
|
||||
#[derive(Queryable, AsChangeset, Serialize, Deserialize)]
|
||||
#[table_name = "users"]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub login: String,
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn all(connection: &PgConnection) -> QueryResult<Vec<User>> {
|
||||
users::table.load::<User>(&*connection)
|
||||
}
|
||||
}
|
@ -1,16 +1,43 @@
|
||||
version: "3.7"
|
||||
services:
|
||||
frontend:
|
||||
build:
|
||||
context: frontend
|
||||
target: development
|
||||
networks:
|
||||
- client-side
|
||||
ports:
|
||||
- 3000:3000
|
||||
volumes:
|
||||
- ./frontend/src:/code/src:ro
|
||||
backend:
|
||||
build:
|
||||
context: backend
|
||||
target: development
|
||||
environment:
|
||||
- DATABASE_URL=postgres://postgres:mysecretpassword@db/postgres
|
||||
networks:
|
||||
- client-side
|
||||
- server-side
|
||||
volumes:
|
||||
- ./backend/src:/code/src:ro
|
||||
- ./backend/src:/code/src
|
||||
- backend-cache:/code/target
|
||||
depends_on:
|
||||
- db
|
||||
db:
|
||||
image: postgres:12-alpine
|
||||
restart: always
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=mysecretpassword
|
||||
networks:
|
||||
- server-side
|
||||
ports:
|
||||
- 5432:5432
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
networks:
|
||||
client-side: {}
|
||||
server-side: {}
|
||||
volumes:
|
||||
backend-cache: {}
|
||||
db-data: {}
|
||||
|
Loading…
Reference in New Issue