Skip to content

Commit

Permalink
Fix migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
varmar05 committed Nov 13, 2024
1 parent 5bdeef7 commit 8277055
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion server/mergin/sync/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Project(db.Model):
removed_by = db.Column(
db.Integer, db.ForeignKey("user.id"), nullable=True, index=True
)
public = db.Column(db.Boolean, default=False, index=True)
public = db.Column(db.Boolean, default=False, index=True, nullable=False)

creator = db.relationship(
"User", uselist=False, backref=db.backref("projects"), foreign_keys=[creator_id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def upgrade():
)

op.add_column("project", sa.Column("public", sa.Boolean(), nullable=True))
op.create_index(op.f("ix_project_public"), "project", ["public"], unique=False)

data_upgrade()

op.alter_column("project", "public", nullable=False)
op.create_index(op.f("ix_project_public"), "project", ["public"], unique=False)
op.drop_table("project_access")
conn.execute(sa.text("DROP EXTENSION IF EXISTS intarray;"))


def downgrade():
Expand Down Expand Up @@ -97,6 +97,9 @@ def downgrade():
),
sa.PrimaryKeyConstraint("project_id", name="pk_project_access"),
)

data_downgrade()

op.create_index(
"ix_project_access_writers",
"project_access",
Expand Down Expand Up @@ -132,8 +135,6 @@ def downgrade():
postgresql_using="gin",
)

data_downgrade()

op.drop_index(op.f("ix_project_public"), table_name="project")
op.drop_column("project", "public")
op.drop_table("project_member")
Expand Down Expand Up @@ -172,7 +173,7 @@ def data_upgrade():
FROM project_access
)
INSERT INTO project_member (project_id, user_id, role)
SELECT DISTINCT project_id, user_id, role::project_role FROM members;
SELECT project_id, user_id, role::project_role FROM members;
"""
)
)
Expand All @@ -182,7 +183,7 @@ def data_upgrade():
"""
UPDATE project p
SET
public = pa.public
public = coalesce(pa.public, 'false')
FROM project_access pa
WHERE pa.project_id = p.id;
"""
Expand All @@ -192,6 +193,17 @@ def data_upgrade():

def data_downgrade():
conn = op.get_bind()
# recreate 1:1 relationship for project
conn.execute(
sa.text(
"""
INSERT INTO project_access (project_id, public)
SELECT id, coalesce(public, 'false') FROM project;
"""
)
)

# update access fields from assigned roles
conn.execute(
sa.text(
"""
Expand All @@ -216,20 +228,14 @@ def data_downgrade():
LEFT OUTER JOIN (SELECT * FROM agg WHERE role = 'editor') AS e ON o.project_id = e.project_id
LEFT OUTER JOIN (SELECT * FROM agg WHERE role = 'writer') AS w ON o.project_id = w.project_id
)
INSERT INTO project_access (project_id, owners, writers, editors, readers)
SELECT DISTINCT project_id, owners, writers, editors, readers FROM members m;
"""
)
)

conn.execute(
sa.text(
"""
UPDATE project_access pa
SET
public = p.public
FROM project p
WHERE pa.project_id = p.id;
owners = m.owners,
writers = m.writers,
editors = m.editors,
readers = m.readers
FROM members m
WHERE m.project_id = pa.project_id;
"""
)
)

0 comments on commit 8277055

Please sign in to comment.