From 83efe2447180492181fc79424487d03777268002 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 08 2016 10:17:02 +0000 Subject: [PATCH 1/2] Add an alembic migration to set the default hook to all existing projects --- diff --git a/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py b/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py new file mode 100644 index 0000000..b51eb89 --- /dev/null +++ b/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py @@ -0,0 +1,71 @@ +"""Add the default hook to all projects + +Revision ID: 26af5c3602a0 +Revises: 644ef887bb6f +Create Date: 2016-10-08 12:14:31.155018 + +""" + +# revision identifiers, used by Alembic. +revision = '26af5c3602a0' +down_revision = '644ef887bb6f' + +from alembic import op +import sqlalchemy as sa + + +try: + import pagure.lib.plugins + from pagure.lib import model +except ImportError: + import sys + sys.path.insert(0, '.') + import pagure.lib.plugins + from pagure.lib import model + + + +def upgrade(): + ''' Add the default hook to all existing projects. + ''' + + engine = op.get_bind() + Session = sa.orm.scoped_session(sa.orm.sessionmaker()) + Session.configure(bind=engine) + session = Session() + + # Update all the existing projects + for project in session.query(model.Project).all(): + # Install the default hook + plugin = pagure.lib.plugins.get_plugin('default') + dbobj = plugin.db_object() + dbobj.active = True + dbobj.project_id = project.id + session.add(dbobj) + session.flush() + plugin.set_up(project) + plugin.install(project, dbobj) + + # create the project in the db + session.commit() + + +def downgrade(): + engine = op.get_bind() + Session = sa.orm.scoped_session(sa.orm.sessionmaker()) + Session.configure(bind=engine) + session = Session() + + # Update all the existing projects + for project in session.query(model.Project).all(): + # Install the default hook + plugin = pagure.lib.plugins.get_plugin('default') + dbobj = plugin.db_object() + dbobj.active = False + dbobj.project_id = project.id + session.add(dbobj) + session.flush() + plugin.remove(project, dbobj) + + # create the project in the db + session.commit() From 9ebb182cfe561182a65c1ba3e0979e8a9edf3573 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 10 2016 14:20:28 +0000 Subject: [PATCH 2/2] Save the changes as we go --- diff --git a/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py b/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py index b51eb89..eec789f 100644 --- a/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py +++ b/alembic/versions/26af5c3602a0_add_the_default_hook_to_all_projects.py @@ -45,9 +45,8 @@ def upgrade(): session.flush() plugin.set_up(project) plugin.install(project, dbobj) - - # create the project in the db - session.commit() + # Save the change + session.commit() def downgrade(): @@ -67,5 +66,5 @@ def downgrade(): session.flush() plugin.remove(project, dbobj) - # create the project in the db - session.commit() + # Save the change + session.commit()