Compare commits
2 commits
f3cab87592
...
1652419d94
Author | SHA1 | Date | |
---|---|---|---|
|
1652419d94 | ||
|
13c5a05bd6 |
7 changed files with 69 additions and 138 deletions
2
justfile
2
justfile
|
@ -39,7 +39,7 @@ start-release: start-postgres-dev clean-podman init-sqlx build-container-release
|
||||||
init-sqlx:
|
init-sqlx:
|
||||||
echo {{env_local}} > server/.env
|
echo {{env_local}} > server/.env
|
||||||
cd server && sqlx database create --connect-timeout 40 # Postgres takes a while to start up
|
cd server && sqlx database create --connect-timeout 40 # Postgres takes a while to start up
|
||||||
cd server && sqlx migrate run --source migrations_pg
|
cd server && sqlx migrate run
|
||||||
cd server && cargo sqlx prepare
|
cd server && cargo sqlx prepare
|
||||||
|
|
||||||
# Starts a postgres container for development
|
# Starts a postgres container for development
|
||||||
|
|
|
@ -1,31 +1,38 @@
|
||||||
CREATE TABLE
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
IF NOT EXISTS users (
|
id BIGSERIAL PRIMARY KEY,
|
||||||
id INTEGER PRIMARY KEY NOT NULL,
|
username TEXT NOT NULL UNIQUE,
|
||||||
username TEXT NOT NULL UNIQUE,
|
password TEXT NOT NULL,
|
||||||
password TEXT NOT NULL,
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
);
|
||||||
);
|
|
||||||
|
|
||||||
-- Create a trigger to set created_at and updated_at on INSERT
|
|
||||||
CREATE TRIGGER IF NOT EXISTS set_created_at AFTER INSERT ON users BEGIN
|
|
||||||
UPDATE users
|
|
||||||
SET
|
|
||||||
created_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE
|
|
||||||
id = NEW.id;
|
|
||||||
|
|
||||||
|
-- Create a function to set created_at and updated_at on INSERT
|
||||||
|
CREATE OR REPLACE FUNCTION set_timestamps_on_insert() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.created_at = NOW();
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
-- Create a trigger to set updated_at on UPDATE
|
-- Create a trigger to call the function after INSERT
|
||||||
CREATE TRIGGER IF NOT EXISTS set_updated_at AFTER
|
CREATE TRIGGER set_timestamps_on_insert
|
||||||
UPDATE ON users BEGIN
|
BEFORE INSERT ON users
|
||||||
UPDATE users
|
FOR EACH ROW
|
||||||
SET
|
EXECUTE FUNCTION set_timestamps_on_insert();
|
||||||
updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE
|
|
||||||
id = NEW.id;
|
|
||||||
|
|
||||||
|
-- Create a function to set updated_at on UPDATE
|
||||||
|
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
-- Create a trigger to call the function after UPDATE
|
||||||
|
CREATE TRIGGER set_updated_at
|
||||||
|
BEFORE UPDATE ON users
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION set_updated_at();
|
||||||
|
|
||||||
CREATE INDEX users_username_index ON users (username);
|
CREATE INDEX users_username_index ON users (username);
|
|
@ -1,38 +1,43 @@
|
||||||
CREATE TABLE
|
CREATE TABLE IF NOT EXISTS posts (
|
||||||
IF NOT EXISTS posts (
|
id BIGSERIAL PRIMARY KEY,
|
||||||
id INTEGER PRIMARY KEY NOT NULL,
|
user_id BIGINT NOT NULL,
|
||||||
user_id INTEGER NOT NULL,
|
content TEXT NOT NULL,
|
||||||
content TEXT NOT NULL,
|
upvotes INTEGER NOT NULL DEFAULT 0,
|
||||||
upvotes INTEGER NOT NULL DEFAULT 0,
|
downvotes INTEGER NOT NULL DEFAULT 0,
|
||||||
downvotes INTEGER NOT NULL DEFAULT 0,
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
);
|
||||||
);
|
|
||||||
|
|
||||||
-- Create a trigger to set created_at and updated_at on INSERT
|
|
||||||
CREATE TRIGGER IF NOT EXISTS set_created_at AFTER INSERT ON posts BEGIN
|
|
||||||
UPDATE posts
|
|
||||||
SET
|
|
||||||
created_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE
|
|
||||||
id = NEW.id;
|
|
||||||
|
|
||||||
|
-- Create a function to set created_at and updated_at on INSERT
|
||||||
|
CREATE OR REPLACE FUNCTION set_timestamps_on_insert() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.created_at = NOW();
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
-- Create a trigger to set updated_at on UPDATE
|
-- Create a trigger to call the function after INSERT
|
||||||
CREATE TRIGGER IF NOT EXISTS set_updated_at AFTER
|
CREATE TRIGGER set_timestamps_on_insert
|
||||||
UPDATE ON posts BEGIN
|
BEFORE INSERT ON posts
|
||||||
UPDATE posts
|
FOR EACH ROW
|
||||||
SET
|
EXECUTE FUNCTION set_timestamps_on_insert();
|
||||||
updated_at = CURRENT_TIMESTAMP
|
|
||||||
WHERE
|
|
||||||
id = NEW.id;
|
|
||||||
|
|
||||||
|
-- Create a function to set updated_at on UPDATE
|
||||||
|
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
END;
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
create INDEX IF NOT EXISTS posts_user_id_index ON posts (user_id);
|
-- Create a trigger to call the function after UPDATE
|
||||||
|
CREATE TRIGGER set_updated_at
|
||||||
create INDEX IF NOT EXISTS posts_id_index ON posts (id);
|
BEFORE UPDATE ON posts
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION set_updated_at();
|
||||||
|
|
||||||
|
CREATE INDEX posts_user_id_index ON posts (user_id);
|
||||||
|
CREATE INDEX posts_id_index ON posts (id);
|
||||||
CREATE INDEX idx_created_at_desc ON posts (created_at DESC);
|
CREATE INDEX idx_created_at_desc ON posts (created_at DESC);
|
|
@ -1,38 +0,0 @@
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
username TEXT NOT NULL UNIQUE,
|
|
||||||
password TEXT NOT NULL,
|
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create a function to set created_at and updated_at on INSERT
|
|
||||||
CREATE OR REPLACE FUNCTION set_timestamps_on_insert() RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.created_at = NOW();
|
|
||||||
NEW.updated_at = NOW();
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
-- Create a trigger to call the function after INSERT
|
|
||||||
CREATE TRIGGER set_timestamps_on_insert
|
|
||||||
BEFORE INSERT ON users
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION set_timestamps_on_insert();
|
|
||||||
|
|
||||||
-- Create a function to set updated_at on UPDATE
|
|
||||||
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.updated_at = NOW();
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
-- Create a trigger to call the function after UPDATE
|
|
||||||
CREATE TRIGGER set_updated_at
|
|
||||||
BEFORE UPDATE ON users
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION set_updated_at();
|
|
||||||
|
|
||||||
CREATE INDEX users_username_index ON users (username);
|
|
|
@ -1,43 +0,0 @@
|
||||||
CREATE TABLE IF NOT EXISTS posts (
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
user_id BIGINT NOT NULL,
|
|
||||||
content TEXT NOT NULL,
|
|
||||||
upvotes INTEGER NOT NULL DEFAULT 0,
|
|
||||||
downvotes INTEGER NOT NULL DEFAULT 0,
|
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create a function to set created_at and updated_at on INSERT
|
|
||||||
CREATE OR REPLACE FUNCTION set_timestamps_on_insert() RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.created_at = NOW();
|
|
||||||
NEW.updated_at = NOW();
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
-- Create a trigger to call the function after INSERT
|
|
||||||
CREATE TRIGGER set_timestamps_on_insert
|
|
||||||
BEFORE INSERT ON posts
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION set_timestamps_on_insert();
|
|
||||||
|
|
||||||
-- Create a function to set updated_at on UPDATE
|
|
||||||
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.updated_at = NOW();
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
-- Create a trigger to call the function after UPDATE
|
|
||||||
CREATE TRIGGER set_updated_at
|
|
||||||
BEFORE UPDATE ON posts
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION set_updated_at();
|
|
||||||
|
|
||||||
CREATE INDEX posts_user_id_index ON posts (user_id);
|
|
||||||
CREATE INDEX posts_id_index ON posts (id);
|
|
||||||
CREATE INDEX idx_created_at_desc ON posts (created_at DESC);
|
|
|
@ -43,7 +43,7 @@ impl ServerState {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
sqlx::migrate!("./migrations_pg").run(&pool).await.unwrap();
|
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
|
||||||
|
|
||||||
match crate::db::db_new_user("imbus".to_string(), "kartellen1234".to_string(), &pool).await
|
match crate::db::db_new_user("imbus".to_string(), "kartellen1234".to_string(), &pool).await
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue