7f8b8920355f_create_annotation_table.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """create annotation table
  2. Revision ID: 7f8b8920355f
  3. Revises: c1d316c60985
  4. Create Date: 2022-01-29 20:23:29.996133
  5. """
  6. from alembic import op
  7. import click
  8. import sqlalchemy as sa
  9. # revision identifiers, used by Alembic.
  10. revision = "7f8b8920355f"
  11. down_revision = "c1d316c60985"
  12. branch_labels = None
  13. depends_on = None
  14. def upgrade():
  15. create_annotation_table()
  16. create_annotation_account_relationship_table()
  17. create_annotation_asset_relationship_table()
  18. create_annotation_sensor_relationship_table()
  19. create_user_roles_unique_constraints()
  20. create_account_roles_unique_constraints()
  21. def downgrade():
  22. click.confirm(
  23. "This downgrade drops the tables 'annotations_accounts', 'annotations_assets', 'annotations_sensors' and 'annotation'. Continue?",
  24. abort=True,
  25. )
  26. op.drop_constraint(
  27. op.f("roles_accounts_role_id_key"),
  28. "roles_accounts",
  29. type_="unique",
  30. )
  31. op.drop_constraint(
  32. op.f("roles_users_role_id_key"),
  33. "roles_users",
  34. type_="unique",
  35. )
  36. op.drop_constraint(
  37. op.f("annotations_accounts_annotation_id_key"),
  38. "annotations_accounts",
  39. type_="unique",
  40. )
  41. op.drop_constraint(
  42. op.f("annotations_assets_annotation_id_key"),
  43. "annotations_assets",
  44. type_="unique",
  45. )
  46. op.drop_constraint(
  47. op.f("annotations_sensors_annotation_id_key"),
  48. "annotations_sensors",
  49. type_="unique",
  50. )
  51. op.drop_table("annotations_accounts")
  52. op.drop_table("annotations_assets")
  53. op.drop_table("annotations_sensors")
  54. op.drop_constraint(op.f("annotation_content_key"), "annotation", type_="unique")
  55. op.drop_table("annotation")
  56. op.execute("DROP TYPE annotation_type;")
  57. def create_account_roles_unique_constraints():
  58. """Remove any duplicate relationships, then constrain any new relationships to be unique."""
  59. op.execute(
  60. "DELETE FROM roles_accounts WHERE id in (SELECT r1.id FROM roles_accounts r1, roles_accounts r2 WHERE r1.id > r2.id AND r1.role_id = r2.role_id and r1.account_id = r2.account_id);"
  61. )
  62. op.create_unique_constraint(
  63. op.f("roles_accounts_role_id_key"),
  64. "roles_accounts",
  65. ["role_id", "account_id"],
  66. )
  67. def create_user_roles_unique_constraints():
  68. """Remove any duplicate relationships, then constrain any new relationships to be unique."""
  69. op.execute(
  70. "DELETE FROM roles_users WHERE id in (SELECT r1.id FROM roles_users r1, roles_users r2 WHERE r1.id > r2.id AND r1.role_id = r2.role_id and r1.user_id = r2.user_id);"
  71. )
  72. op.create_unique_constraint(
  73. op.f("roles_users_role_id_key"),
  74. "roles_users",
  75. ["role_id", "user_id"],
  76. )
  77. def create_annotation_sensor_relationship_table():
  78. op.create_table(
  79. "annotations_sensors",
  80. sa.Column("id", sa.Integer(), primary_key=True),
  81. sa.Column("sensor_id", sa.Integer()),
  82. sa.Column("annotation_id", sa.Integer()),
  83. sa.ForeignKeyConstraint(("sensor_id",), ["sensor.id"]),
  84. sa.ForeignKeyConstraint(("annotation_id",), ["annotation.id"]),
  85. )
  86. op.create_unique_constraint(
  87. op.f("annotations_sensors_annotation_id_key"),
  88. "annotations_sensors",
  89. ["annotation_id", "sensor_id"],
  90. )
  91. def create_annotation_asset_relationship_table():
  92. op.create_table(
  93. "annotations_assets",
  94. sa.Column("id", sa.Integer(), primary_key=True),
  95. sa.Column("generic_asset_id", sa.Integer()),
  96. sa.Column("annotation_id", sa.Integer()),
  97. sa.ForeignKeyConstraint(("generic_asset_id",), ["generic_asset.id"]),
  98. sa.ForeignKeyConstraint(("annotation_id",), ["annotation.id"]),
  99. )
  100. op.create_unique_constraint(
  101. op.f("annotations_assets_annotation_id_key"),
  102. "annotations_assets",
  103. ["annotation_id", "generic_asset_id"],
  104. )
  105. def create_annotation_account_relationship_table():
  106. op.create_table(
  107. "annotations_accounts",
  108. sa.Column("id", sa.Integer(), primary_key=True),
  109. sa.Column("account_id", sa.Integer()),
  110. sa.Column("annotation_id", sa.Integer()),
  111. sa.ForeignKeyConstraint(("account_id",), ["account.id"]),
  112. sa.ForeignKeyConstraint(("annotation_id",), ["annotation.id"]),
  113. )
  114. op.create_unique_constraint(
  115. op.f("annotations_accounts_annotation_id_key"),
  116. "annotations_accounts",
  117. ["annotation_id", "account_id"],
  118. )
  119. def create_annotation_table():
  120. op.create_table(
  121. "annotation",
  122. sa.Column(
  123. "id", sa.Integer(), nullable=False, autoincrement=True, primary_key=True
  124. ),
  125. sa.Column("start", sa.DateTime(timezone=True), nullable=False),
  126. sa.Column("end", sa.DateTime(timezone=True), nullable=False),
  127. sa.Column("belief_time", sa.DateTime(timezone=True), nullable=True),
  128. sa.Column("source_id", sa.Integer(), nullable=False),
  129. sa.Column(
  130. "type",
  131. sa.Enum("alert", "holiday", "label", "feedback", name="annotation_type"),
  132. nullable=False,
  133. ),
  134. sa.Column("content", sa.String(1024), nullable=False),
  135. sa.ForeignKeyConstraint(("source_id",), ["data_source.id"]),
  136. )
  137. op.create_unique_constraint(
  138. op.f("annotation_content_key"),
  139. "annotation",
  140. ["content", "start", "belief_time", "source_id", "type"],
  141. )