Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: defined sql files for schema #587

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions backend/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ func ConfigureDB(settings config.Settings) (*gorm.DB, error) {
return nil, err
}

if err := MigrateDB(settings, db); err != nil {
return nil, err
// if err := MigrateDB(settings, db); err != nil {
// return nil, err
// }

var superUser models.User
if err := db.Where("role = ?", models.Super).First(&superUser).Error; err != nil {
if err := createSuperUser(settings, db); err != nil {
return nil, err
}
}

return db, nil
Expand Down
2 changes: 1 addition & 1 deletion backend/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func getExcludedPaths() []map[string]string {
{"/api/v1/auth/login": "POST"},
{"/api/v1/auth/logout": "POST"},
{"/api/v1/auth/refresh": "POST"},
{"/api/v1/auth/send-code/": "POST"},
{"/api/v1/auth/send-code": "POST"},
{"/api/v1/auth/verify-email": "POST"},
{"/api/v1/auth/verify-reset": "POST"},
{"/api/v1/auth/forgot-password": "POST"},
Expand Down
11 changes: 11 additions & 0 deletions backend/migrations/categories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS categories;

CREATE TABLE categories(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
name varchar(255) NOT NULL,
PRIMARY KEY(id)
);

CREATE UNIQUE INDEX uni_categories_name ON categories USING btree ("name");
9 changes: 9 additions & 0 deletions backend/migrations/club_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS club_events CASCADE;

CREATE TABLE club_events(
club_id uuid NOT NULL DEFAULT uuid_generate_v4(),
event_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(club_id,event_id),
CONSTRAINT fk_club_events_event FOREIGN key(event_id) REFERENCES events(id),
CONSTRAINT fk_club_events_club FOREIGN key(club_id) REFERENCES clubs(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/club_tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS club_tags;

CREATE TABLE club_tags(
tag_id uuid NOT NULL DEFAULT uuid_generate_v4(),
club_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(tag_id,club_id),
CONSTRAINT fk_club_tags_tag FOREIGN key(tag_id) REFERENCES tags(id),
CONSTRAINT fk_club_tags_club FOREIGN key(club_id) REFERENCES clubs(id)
);
21 changes: 21 additions & 0 deletions backend/migrations/clubs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
DROP TABLE IF EXISTS clubs;

CREATE TABLE clubs(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
soft_deleted_at timestamp with time zone,
name varchar(255) NOT NULL,
preview varchar(255) NOT NULL,
description text NOT NULL,
num_members bigint NOT NULL,
is_recruiting boolean NOT NULL DEFAULT false,
recruitment_cycle varchar(255) NOT NULL DEFAULT 'always'::character varying,
recruitment_type varchar(255) NOT NULL DEFAULT 'unrestricted'::character varying,
weekly_time_commitment bigint,
one_word_to_describe_us varchar(255) DEFAULT NULL::character varying,
application_link varchar(255) DEFAULT NULL::character varying,
logo varchar(255) DEFAULT NULL::character varying,
parent text,
PRIMARY KEY(id)
);
14 changes: 14 additions & 0 deletions backend/migrations/contacts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
DROP TABLE IF EXISTS contacts;

CREATE TABLE contacts(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
"type" varchar(255) NOT NULL,
content varchar(255) NOT NULL,
club_id uuid NOT NULL,
PRIMARY KEY(id),
CONSTRAINT fk_clubs_contact FOREIGN key(club_id) REFERENCES clubs(id)
);

CREATE UNIQUE INDEX idx_contact_type ON contacts USING btree ("type","club_id");
8 changes: 8 additions & 0 deletions backend/migrations/event_series.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DROP TABLE IF EXISTS event_series;

CREATE TABLE event_series(
event_id uuid NOT NULL,
series_id uuid NOT NULL,
CONSTRAINT fk_event_series_event FOREIGN key(event_id) REFERENCES events(id),
CONSTRAINT fk_event_series_series FOREIGN key(series_id) REFERENCES series(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/event_tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS event_tags;

CREATE TABLE event_tags(
tag_id uuid NOT NULL DEFAULT uuid_generate_v4(),
event_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(tag_id,event_id),
CONSTRAINT fk_event_tags_tag FOREIGN key(tag_id) REFERENCES tags(id),
CONSTRAINT fk_event_tags_event FOREIGN key(event_id) REFERENCES events(id)
);
19 changes: 19 additions & 0 deletions backend/migrations/events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DROP TABLE IF EXISTS events;

CREATE TABLE events(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
name varchar(255) NOT NULL,
preview varchar(255) NOT NULL,
content text NOT NULL,
start_time timestamp with time zone NOT NULL,
end_time timestamp with time zone NOT NULL,
location varchar(255) NOT NULL,
event_type varchar(255) NOT NULL DEFAULT 'open'::character varying,
is_recurring boolean NOT NULL DEFAULT false,
meeting_link varchar(255),
host uuid NOT NULL,
PRIMARY KEY(id),
CONSTRAINT fk_clubs_host_event FOREIGN key(host) REFERENCES clubs(id)
);
17 changes: 17 additions & 0 deletions backend/migrations/files.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DROP TABLE IF EXISTS files;

CREATE TABLE files(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
owner_id uuid NOT NULL,
owner_type varchar(255) NOT NULL,
file_name varchar(255) NOT NULL,
file_type varchar(255) NOT NULL,
file_size bigint NOT NULL,
file_url varchar(255) NOT NULL,
object_key varchar(255) NOT NULL,
PRIMARY KEY(id)
);
CREATE INDEX idx_files_owner_type ON files USING btree ("owner_type");
CREATE INDEX idx_files_owner_id ON files USING btree ("owner_id");
13 changes: 13 additions & 0 deletions backend/migrations/notifications.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CREATE TABLE notifications(
-- id uuid NOT NULL DEFAULT uuid_generate_v4(),
-- created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- send_at timestamp with time zone NOT NULL,
-- title varchar(255) NOT NULL,
-- content varchar(255) NOT NULL,
-- deep_link varchar(255) NOT NULL,
-- icon varchar(255) NOT NULL,
-- reference_id uuid NOT NULL,
-- reference_type varchar(255) NOT NULL,
-- PRIMARY KEY(id)
-- );
16 changes: 16 additions & 0 deletions backend/migrations/point_of_contact.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DROP TABLE IF EXISTS point_of_contacts;

CREATE TABLE point_of_contacts(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
position varchar(255) NOT NULL,
club_id uuid NOT NULL,
PRIMARY KEY(id),
CONSTRAINT fk_clubs_point_of_contact FOREIGN key(club_id) REFERENCES clubs(id)
);
CREATE UNIQUE INDEX compositeindex ON point_of_contacts USING btree ("email","club_id");
CREATE INDEX idx_point_of_contacts_club_id ON point_of_contacts USING btree ("club_id");
CREATE INDEX idx_point_of_contacts_email ON point_of_contacts USING btree ("email");
10 changes: 10 additions & 0 deletions backend/migrations/series.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS series;

CREATE TABLE series(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
recurring_type varchar(255) NOT NULL DEFAULT 'open'::character varying,
max_occurrences bigint NOT NULL,
PRIMARY KEY(id)
);
11 changes: 11 additions & 0 deletions backend/migrations/tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS tags;

CREATE TABLE tags(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
name varchar(255) NOT NULL,
category_id uuid NOT NULL,
PRIMARY KEY(id),
CONSTRAINT fk_categories_tag FOREIGN key(category_id) REFERENCES categories(id)
);
25 changes: 25 additions & 0 deletions backend/migrations/user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
DROP TABLE IF EXISTS users;

-- Create User table
CREATE TABLE users(
id uuid NOT NULL DEFAULT uuid_generate_v4(),
created_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
role varchar(255) NOT NULL DEFAULT 'student'::character varying,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
email varchar(255) NOT NULL,
password_hash varchar(97) NOT NULL,
major0 varchar(255),
major1 varchar(255),
major2 varchar(255),
college varchar(255),
graduation_cycle varchar(255),
graduation_year smallint,
is_verified boolean NOT NULL DEFAULT false,
PRIMARY KEY(id)
);

CREATE UNIQUE INDEX uni_users_email ON users USING btree ("email");

-- Insert sample data into "users" table
9 changes: 9 additions & 0 deletions backend/migrations/user_club_followers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS user_club_followers;

CREATE TABLE user_club_followers(
user_id uuid NOT NULL DEFAULT uuid_generate_v4(),
club_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(user_id,club_id),
CONSTRAINT fk_user_club_followers_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_club_followers_club FOREIGN key(club_id) REFERENCES clubs(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/user_club_intended_applicants.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS user_club_intended_applicants;

CREATE TABLE user_club_intended_applicants(
user_id uuid NOT NULL DEFAULT uuid_generate_v4(),
club_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(user_id,club_id),
CONSTRAINT fk_user_club_intended_applicants_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_club_intended_applicants_club FOREIGN key(club_id) REFERENCES clubs(id)
);
10 changes: 10 additions & 0 deletions backend/migrations/user_club_members.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS user_club_members;

CREATE TABLE user_club_members(
user_id uuid NOT NULL,
club_id uuid NOT NULL,
membership_type varchar(255) NOT NULL DEFAULT 'member'::character varying,
PRIMARY KEY(user_id,club_id),
CONSTRAINT fk_user_club_members_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_club_members_club FOREIGN key(club_id) REFERENCES clubs(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/user_event_rsvp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS user_event_rsvps;

CREATE TABLE user_event_rsvps(
user_id uuid NOT NULL DEFAULT uuid_generate_v4(),
event_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(user_id,event_id),
CONSTRAINT fk_user_event_rsvps_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_event_rsvps_event FOREIGN key(event_id) REFERENCES events(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/user_event_waitlist.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS user_event_waitlists;

CREATE TABLE user_event_waitlists(
user_id uuid NOT NULL DEFAULT uuid_generate_v4(),
event_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(user_id,event_id),
CONSTRAINT fk_user_event_waitlists_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_event_waitlists_event FOREIGN key(event_id) REFERENCES events(id)
);
9 changes: 9 additions & 0 deletions backend/migrations/user_tags.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DROP TABLE IF EXISTS user_tags;

CREATE TABLE user_tags(
user_id uuid NOT NULL DEFAULT uuid_generate_v4(),
tag_id uuid NOT NULL DEFAULT uuid_generate_v4(),
PRIMARY KEY(user_id,tag_id),
CONSTRAINT fk_user_tags_user FOREIGN key(user_id) REFERENCES users(id),
CONSTRAINT fk_user_tags_tag FOREIGN key(tag_id) REFERENCES tags(id)
);
10 changes: 10 additions & 0 deletions backend/migrations/verifications.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS verifications CASCADE;

CREATE TABLE verifications(
user_id varchar(36) NOT NULL,
token varchar(255),
expires_at timestamp without time zone NOT NULL,
"type" varchar(255) NOT NULL,
PRIMARY KEY(user_id,expires_at)
);
CREATE UNIQUE INDEX uni_verifications_token ON verifications USING btree ("token");
17 changes: 8 additions & 9 deletions backend/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,14 @@ type User struct {
GraduationYear int16 `gorm:"type:smallint;" json:"graduation_year" validate:"required"`
IsVerified bool `gorm:"type:boolean;default:false;not null" json:"is_verified"`

Tag []Tag `gorm:"many2many:user_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Admin []Club `gorm:"many2many:user_club_admins;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Member []Club `gorm:"many2many:user_club_members;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Follower []Club `gorm:"many2many:user_club_followers;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
IntendedApplicant []Club `gorm:"many2many:user_club_intended_applicants;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Asked []Comment `gorm:"foreignKey:AskedByID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"-" validate:"-"`
Answered []Comment `gorm:"foreignKey:AnsweredByID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"-" validate:"-"`
RSVP []Event `gorm:"many2many:user_event_rsvps;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Waitlist []Event `gorm:"many2many:user_event_waitlists;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Tag []Tag `gorm:"many2many:user_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Member []Club `gorm:"many2many:user_club_members;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Follower []Club `gorm:"many2many:user_club_followers;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
// IntendedApplicant []Club `gorm:"many2many:user_club_intended_applicants;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
// Asked []Comment `gorm:"foreignKey:AskedByID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"-" validate:"-"`
// Answered []Comment `gorm:"foreignKey:AnsweredByID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"-" validate:"-"`
RSVP []Event `gorm:"many2many:user_event_rsvps;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Waitlist []Event `gorm:"many2many:user_event_waitlists;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
}

type CreateUserRequestBody struct {
Expand Down
Loading