feat: seed data and fix db config
This commit is contained in:
parent
83545fb854
commit
b637762744
@ -1,13 +1,27 @@
|
|||||||
-- I DO NOT KNOW IF THIS WORKS --
|
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||||
-- I AM PLAYING ARROUND WITH START UP SCRIPTS FOR THE CONTAINER --
|
|
||||||
|
|
||||||
-- Drop existing roles/users if they exist
|
DROP TABLE dev_users
|
||||||
DROP ROLE IF EXISTS admin;
|
|
||||||
DROP USER IF EXISTS admin;
|
|
||||||
|
|
||||||
-- Create admin user with strong password using bcrypt
|
CREATE TABLE IF NOT EXISTS dev_users (
|
||||||
CREATE USER admin WITH PASSWORD 'password';
|
id UUID PRIMARY KEY DEFAULT (uuid_generate_v4()),
|
||||||
|
first_name character varying(255) NOT NULL,
|
||||||
|
last_name character varying(255) NOT NULL,
|
||||||
|
email character varying(255) UNIQUE NOT NULL,
|
||||||
|
phone_number character varying(255),
|
||||||
|
created_at timestamp with time zone DEFAULT now(),
|
||||||
|
updated_at timestamp with time zone DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
-- Grant all privileges
|
-- Seed Data
|
||||||
ALTER ROLE admin SUPERUSER;
|
INSERT INTO dev_users (first_name, last_name, email, phone_number, created_at, updated_at)
|
||||||
GRANT ALL PRIVILEGES TO admin;
|
VALUES
|
||||||
|
('John', 'Doe', 'john.doe@example.com', '(555) 123-4567', now(), now()),
|
||||||
|
('Jane', 'Smith', 'jane.smith@example.com', '(555) 123-4568', now(), now()),
|
||||||
|
('Bob', 'Johnson', 'bob.johnson@example.com', '(555) 123-4569', now(), now()),
|
||||||
|
('Alice', 'Brown', 'alice.brown@example.com', '(555) 123-4570', now(), now()),
|
||||||
|
('Charlie', 'Wilson', 'charlie.wilson@example.com', '(555) 123-4571', now(), now()),
|
||||||
|
('Eva', 'Davis', 'eva.davis@example.com', '(555) 123-4572', now(), now()),
|
||||||
|
('Frank', 'Miller', 'frank.miller@example.com', '(555) 123-4573', now(), now()),
|
||||||
|
('Grace', 'Taylor', 'grace.taylor@example.com', '(555) 123-4574', now(), now()),
|
||||||
|
('Henry', 'Anderson', 'henry.anderson@example.com', '(555) 123-4575', now(), now()),
|
||||||
|
('Isabella', 'King', 'isabella.king@example.com', '(555) 123-4576', now(), now());
|
||||||
|
3
.env
3
.env
@ -1,9 +1,10 @@
|
|||||||
DB_USER=admin
|
DB_USER=admin
|
||||||
DB_PASSWORD=password
|
DB_PASSWORD=password
|
||||||
|
DB_ROOT_PASSWORD=rootpassword
|
||||||
DB_NAME=worklog
|
DB_NAME=worklog
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_EXPOSED_PORT=6767
|
DB_EXPOSED_PORT=5432
|
||||||
DB_TZ=America/Chicago
|
DB_TZ=America/Chicago
|
||||||
|
|
||||||
VOLUME_ROOT="./.container_volume"
|
VOLUME_ROOT="./.container_volume"
|
||||||
|
@ -5,9 +5,9 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "${DB_EXPOSED_PORT}:${DB_PORT}"
|
- "${DB_EXPOSED_PORT}:${DB_PORT}"
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_DB: worklog
|
- POSTGRES_DB=worklog
|
||||||
POSTGRES_USER: admin
|
- POSTGRES_USER=${DB_USER}
|
||||||
POSTGRES_PASSWORD: password
|
- POSTGRES_PASSWORD=${DB_PASSWORD}
|
||||||
volumes:
|
volumes:
|
||||||
- "${VOLUME_ROOT}/db:/var/lib/postgresql/data"
|
- "${VOLUME_ROOT}/db:/var/lib/postgresql/data"
|
||||||
- "${VOLUME_ROOT}/scripts:/docker-entrypoint-initdb.d/"
|
- "${VOLUME_ROOT}/scripts:/docker-entrypoint-initdb.d/"
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
gorm.Model
|
|
||||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
|
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()"`
|
||||||
FirstName string `gorm:"size:255"`
|
FirstName string `gorm:"size:255"`
|
||||||
LastName string `gorm:"size:255"`
|
LastName string `gorm:"size:255"`
|
||||||
|
@ -55,11 +55,11 @@ func (r *UserRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|||||||
return r.db.WithContext(ctx).Delete(&user).Error
|
return r.db.WithContext(ctx).Delete(&user).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *UserRepository) List(ctx context.Context, offset int, limit int) ([]entities.User, int64, error) {
|
func (r *UserRepository) List(ctx context.Context, offset int, limit int) ([]entities.DevUser, int64, error) {
|
||||||
var users []entities.User
|
var users []entities.DevUser // TODO: make sure to make this abstractable
|
||||||
var totalUserCount int64
|
var totalUserCount int64
|
||||||
|
|
||||||
if err := r.db.WithContext(ctx).Where("1 = 1").Count(&totalUserCount).Offset(offset).Limit(limit); err != nil {
|
if err := r.db.WithContext(ctx).Find(&users).Count(&totalUserCount).Offset(offset).Limit(limit); err != nil {
|
||||||
return nil, 0, err.Error
|
return nil, 0, err.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user