Skip to content

Commit

Permalink
refactor: remove password field from users table (migration)
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Oct 5, 2023
1 parent b94e774 commit 149b89d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
39 changes: 30 additions & 9 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
"""SQLAlchemy database models for interacting with Postgresql."""

from geoalchemy2 import Geometry
from sqlalchemy import (
Expand Down Expand Up @@ -100,9 +101,6 @@ class DbUser(Base):
# Represents the date the user last had one of their tasks validated
last_validation_date = Column(DateTime, default=timestamp)

# TODO: This changes to use Oath
password = Column(String)


# Secondary table defining many-to-many relationship between organisations and managers
organisation_managers = Table(
Expand Down Expand Up @@ -158,7 +156,8 @@ class DbTeam(Base):
organisation = relationship(DbOrganisation, backref="teams")


# Secondary table defining many-to-many join for private projects that only defined users can map on
# Secondary table defining many-to-many join for
# private projects that only defined users can map on
project_allowed_users = Table(
"project_allowed_users",
FmtmMetadata,
Expand All @@ -168,6 +167,8 @@ class DbTeam(Base):


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

__tablename__ = "project_teams"
team_id = Column(Integer, ForeignKey("teams.id"), primary_key=True)
project_id = Column(Integer, ForeignKey("projects.id"), primary_key=True)
Expand Down Expand Up @@ -231,7 +232,10 @@ class DbXForm(Base):


class DbTaskInvalidationHistory(Base):
"""Describes the most recent history of task invalidation and subsequent validation."""
"""Information on task invalidation.
Describes the most recent history of task invalidation and subsequent validation.
"""

__tablename__ = "task_invalidation_history"
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -265,9 +269,10 @@ class DbTaskInvalidationHistory(Base):


class DbTaskMappingIssue(Base):
"""Describes an issue (along with an occurrence count) with a
task mapping that contributed to invalidation of the task
.
"""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"
Expand Down Expand Up @@ -382,7 +387,8 @@ class DbTask(Base):
# y = Column(Integer)
# zoom = Column(Integer)
# extra_properties = Column(Unicode)
# # Tasks need to be split differently if created from an arbitrary grid or were clipped to the edge of the AOI
# # Tasks need to be split differently if created from an arbitrary grid
# or were clipped to the edge of the AOI
# is_square = Column(Boolean, default=False)


Expand Down Expand Up @@ -439,6 +445,7 @@ class DbProject(Base):

@property
def tasks_mapped(self):
"""Get the number of tasks mapped for a project."""
return (
object_session(self)
.query(DbTask)
Expand All @@ -449,6 +456,7 @@ def tasks_mapped(self):

@property
def tasks_validated(self):
"""Get the number of tasks validated for a project."""
return (
object_session(self)
.query(DbTask)
Expand All @@ -459,6 +467,7 @@ def tasks_validated(self):

@property
def tasks_bad(self):
"""Get the number of tasks marked bad for a project."""
return (
object_session(self)
.query(DbTask)
Expand Down Expand Up @@ -590,6 +599,8 @@ class DbFeatures(Base):


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

__tablename__ = "background_tasks"

id = Column(String, primary_key=True)
Expand All @@ -600,6 +611,8 @@ class BackgroundTasks(Base):


class DbUserRoles(Base):
"""Fine grained user control for projects, described by roles."""

__tablename__ = "user_roles"

user_id = Column(BigInteger, ForeignKey("users.id"), primary_key=True)
Expand All @@ -612,6 +625,8 @@ class DbUserRoles(Base):


class DbProjectAOI(Base):
"""The AOI geometry for a project."""

__tablename__ = "project_aoi"

id = Column(Integer, primary_key=True)
Expand All @@ -621,6 +636,8 @@ class DbProjectAOI(Base):


class DbOsmLines(Base):
"""Associated OSM ways for a project."""

__tablename__ = "ways_line"

id = Column(Integer, primary_key=True)
Expand All @@ -630,6 +647,8 @@ class DbOsmLines(Base):


class DbBuildings(Base):
"""Associated OSM buildings for a project."""

__tablename__ = "ways_poly"

id = Column(Integer, primary_key=True)
Expand All @@ -640,6 +659,8 @@ class DbBuildings(Base):


class DbTilesPath(Base):
"""Keeping track of mbtile basemaps for a project."""

__tablename__ = "mbtiles_path"

id = Column(Integer, primary_key=True)
Expand Down
25 changes: 25 additions & 0 deletions src/backend/migrations/000-remove-user-password.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- ## Migration to remove password field from public.users (replaced with OSM OAuth)


-- ## Apply Migration
-- Start a transaction
BEGIN;
-- Drop the 'password' column if it exists
ALTER TABLE IF EXISTS public.users
DROP COLUMN IF EXISTS password;
-- Commit the transaction
COMMIT;


-- ## Revert Migration (comment above, uncomment below)
-- -- Start a transaction
-- BEGIN;
-- -- Add the 'password' column back if it doesn't exist
-- ALTER TABLE public.users
-- ADD COLUMN IF NOT EXISTS password character varying;
-- -- Commit the transaction
-- COMMIT;


-- ## Print a success message
RAISE NOTICE 'Migration complete for public.users "password" field.';
14 changes: 7 additions & 7 deletions src/backend/migrations/init/fmtm_base_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ SET default_table_access_method = heap;

-- Tables

CREATE TABLE IF NOT EXISTS public._migrations (
CREATE TABLE IF NOT EXISTS public."_migrations" (
date_executed TIMESTAMP,
script_name TEXT
);
ALTER TABLE public._migrations OWNER TO fmtm;
ALTER TABLE public."_migrations" OWNER TO fmtm;


CREATE TABLE public.background_tasks (
Expand Down Expand Up @@ -298,11 +298,11 @@ ALTER TABLE public.mbtiles_path_id_seq OWNER TO fmtm;
ALTER SEQUENCE public.mbtiles_path_id_seq OWNED BY public.mbtiles_path.id;


CREATE TABLE public._migrations (
date_executed timestamp without time zone,
CREATE TABLE public."_migrations" (
script_name text
date_executed timestamp without time zone,
);
ALTER TABLE public._migrations OWNER TO fmtm;
ALTER TABLE public."_migrations" OWNER TO fmtm;


CREATE TABLE public.organisation_managers (
Expand Down Expand Up @@ -732,8 +732,8 @@ ALTER TABLE ONLY public.xlsforms ALTER COLUMN id SET DEFAULT nextval('public.xls

-- Constraints for primary keys

ALTER TABLE public._migrations
ADD CONSTRAINT _migrations_pkey PRIMARY KEY (script_name);
ALTER TABLE public."_migrations"
ADD CONSTRAINT "_migrations_pkey" PRIMARY KEY (script_name);

ALTER TABLE ONLY public.background_tasks
ADD CONSTRAINT background_tasks_pkey PRIMARY KEY (id);
Expand Down

0 comments on commit 149b89d

Please sign in to comment.