987edda096f5_access_id_in_user_projects.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """access_id in user_projects
  2. Revision ID: 987edda096f5
  3. Revises: 8a3b10926153
  4. Create Date: 2016-07-05 18:21:14.771273
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '987edda096f5'
  8. down_revision = '8a3b10926153'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. from sqlalchemy.sql import table, column
  12. acl = table (
  13. 'access_levels',
  14. column('access', sa.String(255))
  15. )
  16. def upgrade():
  17. ''' Add a foreign key in user_projects and projects_groups
  18. table for access_levels
  19. '''
  20. # To allow N + 2 migrations easier
  21. # without going through N + 1
  22. # Make sure, we have all the tables
  23. op.create_table(
  24. 'access_levels',
  25. sa.Column('access', sa.String(255), primary_key=True)
  26. )
  27. op.bulk_insert(
  28. acl,
  29. [
  30. {'access': 'ticket'},
  31. {'access': 'commit'},
  32. {'access': 'admin'},
  33. ],
  34. )
  35. op.add_column(
  36. 'user_projects',
  37. sa.Column(
  38. 'access',
  39. sa.String(255),
  40. sa.ForeignKey(
  41. 'access_levels.access',
  42. onupdate='CASCADE',
  43. ondelete='CASCADE',
  44. ),
  45. nullable=True,
  46. ),
  47. )
  48. op.execute('UPDATE "user_projects" SET access=\'admin\'')
  49. op.alter_column(
  50. 'user_projects',
  51. 'access',
  52. nullable=False,
  53. existing_nullable=True,
  54. )
  55. # for groups
  56. op.add_column(
  57. 'projects_groups',
  58. sa.Column(
  59. 'access',
  60. sa.String(255),
  61. sa.ForeignKey(
  62. 'access_levels.access',
  63. onupdate='CASCADE',
  64. ondelete='CASCADE',
  65. ),
  66. nullable=True,
  67. ),
  68. )
  69. op.execute('UPDATE "projects_groups" SET access=\'admin\'')
  70. op.alter_column(
  71. 'projects_groups',
  72. 'access',
  73. nullable=False,
  74. existing_nullable=True,
  75. )
  76. # alter the constraints
  77. op.drop_constraint('user_projects_project_id_fkey', 'user_projects')
  78. op.create_unique_constraint(
  79. None,
  80. 'user_projects',
  81. ["project_id", "user_id", "access"]
  82. )
  83. op.drop_constraint('projects_groups_pkey', 'projects_groups')
  84. op.create_primary_key(
  85. None,
  86. 'projects_groups',
  87. ['project_id', 'group_id', 'access'],
  88. )
  89. def downgrade():
  90. ''' Remove column access_id from user_projects and projects_groups '''
  91. # this removes the current constraints as well.
  92. op.drop_column('user_projects', 'access')
  93. op.drop_column('projects_groups', 'access')
  94. # recreate the previous constraints
  95. op.create_unique_constraint(
  96. None,
  97. 'user_projects',
  98. ['project_id', 'user_id'],
  99. )
  100. op.create_primary_key(
  101. None,
  102. 'projects_groups',
  103. ['project_id', 'group_id'],
  104. )
  105. op.drop_table('access_levels')