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

Remove unnecessary tables and field from db schema #1623

Merged
merged 5 commits into from
Jul 5, 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
2 changes: 1 addition & 1 deletion src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async def refresh_token(
except Exception as e:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"fail to refresh the access token: {e}",
detail=f"Failed to refresh the access token: {e}",
) from e


Expand Down
181 changes: 0 additions & 181 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
BackgroundTaskStatus,
CommunityType,
MappingLevel,
MappingPermission,
OrganisationType,
ProjectPriority,
ProjectRole,
Expand All @@ -63,9 +62,7 @@
TaskAction,
TaskSplitType,
TaskStatus,
TeamVisibility,
UserRole,
ValidationPermission,
)


Expand Down Expand Up @@ -127,9 +124,6 @@ class DbUser(Base):
# projects_notifications = Column(Boolean, default=True, nullable=False)
# tasks_notifications = Column(Boolean, default=True, nullable=False)
# tasks_comments_notifications = Column(Boolean, default=False, nullable=False)
# teams_announcement_notifications = Column(
# Boolean, default=True, nullable=False
# )

date_registered = cast(datetime, Column(DateTime, default=timestamp))
# Represents the date the user last had one of their tasks validated
Expand Down Expand Up @@ -184,48 +178,6 @@ class DbOrganisation(Base):
)


class DbTeam(Base):
"""Describes a team."""

__tablename__ = "teams"

# Columns
id = cast(int, Column(Integer, primary_key=True))
organisation_id = cast(
int,
Column(
Integer,
ForeignKey("organisations.id", name="fk_organisations"),
nullable=False,
),
)
name = cast(str, Column(String(512), nullable=False))
logo = cast(str, Column(String)) # URL of a logo
description = cast(str, Column(String))
invite_only = cast(bool, Column(Boolean, default=False, nullable=False))
visibility = cast(
TeamVisibility,
Column(Enum(TeamVisibility), default=TeamVisibility.PUBLIC, nullable=False),
)
organisation = relationship(DbOrganisation, backref="teams")


class DbProjectTeams(Base):
"""Link table between teams and projects."""

__tablename__ = "project_teams"
team_id = cast(int, Column(Integer, ForeignKey("teams.id"), primary_key=True))
project_id = cast(int, Column(Integer, ForeignKey("projects.id"), primary_key=True))
role = cast(int, Column(Integer, nullable=False))

project = relationship(
"DbProject", backref=backref("teams", cascade="all, delete-orphan")
)
team = relationship(
DbTeam, backref=backref("projects", cascade="all, delete-orphan")
)


class DbProjectInfo(Base):
"""Contains all project info localized into supported languages."""

Expand All @@ -247,22 +199,6 @@ class DbProjectInfo(Base):
)


class DbProjectChat(Base):
"""Contains all project info localized into supported languages."""

__tablename__ = "project_chat"
id = cast(int, Column(BigInteger, primary_key=True))
project_id = cast(
int, Column(Integer, ForeignKey("projects.id"), index=True, nullable=False)
)
user_id = cast(int, Column(Integer, ForeignKey("users.id"), nullable=False))
time_stamp = cast(datetime, Column(DateTime, nullable=False, default=timestamp))
message = cast(str, Column(String, nullable=False))

# Relationships
posted_by = relationship(DbUser, foreign_keys=[user_id])


class DbXLSForm(Base):
"""XLSForm templates and custom uploads."""

Expand Down Expand Up @@ -291,83 +227,6 @@ class DbXForm(Base):
category = cast(str, Column(String))


class DbTaskInvalidationHistory(Base):
"""Information on task invalidation.

Describes the most recent history of task invalidation and subsequent validation.
"""

__tablename__ = "task_invalidation_history"
id = cast(int, Column(Integer, primary_key=True))
project_id = cast(int, Column(Integer, ForeignKey("projects.id"), nullable=False))
task_id = cast(int, Column(Integer, nullable=False))
is_closed = cast(bool, Column(Boolean, default=False))
mapper_id = cast(int, Column(BigInteger, ForeignKey("users.id", name="fk_mappers")))
mapped_date = cast(datetime, Column(DateTime))
invalidator_id = cast(
int, Column(BigInteger, ForeignKey("users.id", name="fk_invalidators"))
)
invalidated_date = cast(datetime, Column(DateTime))
invalidation_history_id = cast(
int,
Column(Integer, ForeignKey("task_history.id", name="fk_invalidation_history")),
)
validator_id = cast(
int, Column(BigInteger, ForeignKey("users.id", name="fk_validators"))
)
validated_date = cast(datetime, Column(DateTime))
updated_date = cast(datetime, Column(DateTime, default=timestamp))

__table_args__ = (
ForeignKeyConstraint(
[task_id, project_id], ["tasks.id", "tasks.project_id"], name="fk_tasks"
),
Index("idx_task_validation_history_composite", "task_id", "project_id"),
Index(
"idx_task_validation_validator_status_composite",
"invalidator_id",
"is_closed",
),
Index("idx_task_validation_mapper_status_composite", "mapper_id", "is_closed"),
{},
)


class DbTaskMappingIssue(Base):
"""Describes mapping issues.

An issue (along with an occurrence count) with a
task mapping that contributed to invalidation of the task.
"""

__tablename__ = "task_mapping_issues"
id = cast(int, Column(Integer, primary_key=True))
task_history_id = cast(
int, Column(Integer, ForeignKey("task_history.id"), nullable=False, index=True)
)
issue = cast(str, Column(String, nullable=False))
mapping_issue_category_id = cast(
int,
Column(
Integer,
ForeignKey("mapping_issue_categories.id", name="fk_issue_category"),
nullable=False,
),
)
count = cast(int, Column(Integer, nullable=False))


class DbMappingIssueCategory(Base):
"""Represents a category of task mapping issues identified during validation."""

__tablename__ = "mapping_issue_categories"

id = cast(int, Column(Integer, primary_key=True))
name = cast(str, Column(String, nullable=False, unique=True))
description = cast(str, Column(String, nullable=True))
archived = cast(bool, Column(Boolean, default=False, nullable=False))


class DbTaskHistory(Base):
"""Describes the history associated with a task."""

Expand All @@ -391,11 +250,7 @@ class DbTaskHistory(Base):

# Define relationships
user = relationship(DbUser, uselist=False, backref="task_history_user")
invalidation_history = relationship(
DbTaskInvalidationHistory, lazy="dynamic", cascade="all"
)
actioned_by = relationship(DbUser, overlaps="task_history_user,user")
task_mapping_issues = relationship(DbTaskMappingIssue, cascade="all")

__table_args__ = (
ForeignKeyConstraint(
Expand Down Expand Up @@ -623,14 +478,6 @@ def tasks_bad(self):
featured = cast(
bool, Column(Boolean, default=False)
) # Only admins can set a project as featured
mapping_permission = cast(
MappingPermission,
Column(Enum(MappingPermission), default=MappingPermission.ANY),
)
validation_permission = cast(
ValidationPermission,
Column(Enum(ValidationPermission), default=ValidationPermission.LEVEL),
) # Means only users with validator role can validate
changeset_comment = cast(str, Column(String))

# Odk central server
Expand Down Expand Up @@ -665,9 +512,6 @@ def tasks_bad(self):
josm_preset = cast(str, Column(String))
id_presets = cast(list, Column(ARRAY(String)))
extra_id_params = cast(str, Column(String))
license_id = cast(
int, Column(Integer, ForeignKey("licenses.id", name="fk_licenses"))
)

# GEOMETRY
# country = Column(ARRAY(String), default=[])
Expand All @@ -679,31 +523,6 @@ def tasks_bad(self):
due_date = cast(datetime, Column(DateTime))


# Secondary table defining the many-to-many join
user_licenses_table = Table(
"user_licenses",
FmtmMetadata,
Column("user", BigInteger, ForeignKey("users.id")),
Column("license", Integer, ForeignKey("licenses.id")),
)


class DbLicense(Base):
"""Describes an individual license."""

__tablename__ = "licenses"

id = cast(int, Column(Integer, primary_key=True))
name = cast(str, Column(String, unique=True))
description = cast(str, Column(String))
plain_text = cast(str, Column(String))

projects = relationship(DbProject, backref="license")
users = relationship(
DbUser, secondary=user_licenses_table
) # Many to Many relationship


class BackgroundTasks(Base):
"""Table managing long running background tasks."""

Expand Down
25 changes: 0 additions & 25 deletions src/backend/app/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ class HTTPStatus(IntEnum):
NOT_IMPLEMENTED = 501


class TeamVisibility(IntEnum, Enum):
"""Describes the visibility associated with an Team."""

PUBLIC = 0
PRIVATE = 1


class OrganisationType(IntEnum, Enum):
"""Describes an organisation's subscription type."""

Expand Down Expand Up @@ -126,24 +119,6 @@ class MappingLevel(IntEnum, Enum):
ADVANCED = 3


class MappingPermission(IntEnum, Enum):
"""Describes a set of permissions for mapping on a project."""

ANY = 0
LEVEL = 1
TEAMS = 2
TEAMS_LEVEL = 3


class ValidationPermission(IntEnum, Enum):
"""Describes a set of permissions for validating on a project."""

ANY = 0
LEVEL = 1
TEAMS = 2
TEAMS_LEVEL = 3


class TaskStatus(IntEnum, Enum):
"""Enum describing available Task Statuses."""

Expand Down
4 changes: 2 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def delete_one_project(db: Session, db_project: db_models.DbProject) -> No
log.info(f"Deleted project with ID: {project_id}")
except Exception as e:
log.exception(e)
raise HTTPException(e) from e
raise HTTPException(status_code=HTTPStatus.CONFLICT, detail=e) from e


async def partial_update_project_info(
Expand Down Expand Up @@ -395,7 +395,7 @@ async def create_tasks_from_geojson(
return True
except Exception as e:
log.exception(e)
raise HTTPException(e) from e
raise HTTPException(HTTPStatus.UNPROCESSABLE_ENTITY, detail=e) from e


async def preview_split_by_square(boundary: str, meters: int):
Expand Down
22 changes: 1 addition & 21 deletions src/backend/migrations/004-update-default-values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,27 @@
-- Start a transaction
BEGIN;

-- Update mapping_issue_categories table
ALTER TABLE public.mapping_issue_categories
ALTER COLUMN archived SET DEFAULT false;

-- Update mbtiles_path table
ALTER TABLE public.mbtiles_path
ALTER COLUMN created_at SET DEFAULT now();

-- Update project_chat table
ALTER TABLE public.project_chat
ALTER COLUMN time_stamp SET DEFAULT now();

-- Update projects table
ALTER TABLE public.projects
ALTER COLUMN created SET DEFAULT now(),
ALTER COLUMN last_updated SET DEFAULT now(),
ALTER COLUMN status SET DEFAULT 'DRAFT',
ALTER COLUMN mapper_level SET DEFAULT 'INTERMEDIATE',
ALTER COLUMN priority SET DEFAULT 'MEDIUM',
ALTER COLUMN featured SET DEFAULT false,
ALTER COLUMN mapping_permission SET DEFAULT 'ANY',
ALTER COLUMN validation_permission SET DEFAULT 'LEVEL';
ALTER COLUMN featured SET DEFAULT false;

-- Update task_history table
ALTER TABLE public.task_history
ALTER COLUMN action_date SET DEFAULT now();

-- Update task_invalidation_history table
ALTER TABLE public.task_invalidation_history
ALTER COLUMN is_closed SET DEFAULT false,
ALTER COLUMN updated_date SET DEFAULT now();

-- Update tasks table
ALTER TABLE public.tasks
ALTER COLUMN task_status SET DEFAULT 'READY';

-- Update teams table
ALTER TABLE public.teams
ALTER COLUMN invite_only SET DEFAULT false,
ALTER COLUMN visibility SET DEFAULT 'PUBLIC';

-- Update user_roles table
ALTER TABLE public.user_roles
ALTER COLUMN role SET DEFAULT 'MAPPER';
Expand Down
Loading
Loading