Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ddl_references.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. """
  2. Helpers to manipulate deferred DDL statements that might need to be adjusted or
  3. discarded within when executing a migration.
  4. """
  5. class Reference:
  6. """Base class that defines the reference interface."""
  7. def references_table(self, table):
  8. """
  9. Return whether or not this instance references the specified table.
  10. """
  11. return False
  12. def references_column(self, table, column):
  13. """
  14. Return whether or not this instance references the specified column.
  15. """
  16. return False
  17. def rename_table_references(self, old_table, new_table):
  18. """
  19. Rename all references to the old_name to the new_table.
  20. """
  21. pass
  22. def rename_column_references(self, table, old_column, new_column):
  23. """
  24. Rename all references to the old_column to the new_column.
  25. """
  26. pass
  27. def __repr__(self):
  28. return '<%s %r>' % (self.__class__.__name__, str(self))
  29. def __str__(self):
  30. raise NotImplementedError('Subclasses must define how they should be converted to string.')
  31. class Table(Reference):
  32. """Hold a reference to a table."""
  33. def __init__(self, table, quote_name):
  34. self.table = table
  35. self.quote_name = quote_name
  36. def references_table(self, table):
  37. return self.table == table
  38. def rename_table_references(self, old_table, new_table):
  39. if self.table == old_table:
  40. self.table = new_table
  41. def __str__(self):
  42. return self.quote_name(self.table)
  43. class TableColumns(Table):
  44. """Base class for references to multiple columns of a table."""
  45. def __init__(self, table, columns):
  46. self.table = table
  47. self.columns = columns
  48. def references_column(self, table, column):
  49. return self.table == table and column in self.columns
  50. def rename_column_references(self, table, old_column, new_column):
  51. if self.table == table:
  52. for index, column in enumerate(self.columns):
  53. if column == old_column:
  54. self.columns[index] = new_column
  55. class Columns(TableColumns):
  56. """Hold a reference to one or many columns."""
  57. def __init__(self, table, columns, quote_name, col_suffixes=()):
  58. self.quote_name = quote_name
  59. self.col_suffixes = col_suffixes
  60. super().__init__(table, columns)
  61. def __str__(self):
  62. def col_str(column, idx):
  63. try:
  64. return self.quote_name(column) + self.col_suffixes[idx]
  65. except IndexError:
  66. return self.quote_name(column)
  67. return ', '.join(col_str(column, idx) for idx, column in enumerate(self.columns))
  68. class IndexName(TableColumns):
  69. """Hold a reference to an index name."""
  70. def __init__(self, table, columns, suffix, create_index_name):
  71. self.suffix = suffix
  72. self.create_index_name = create_index_name
  73. super().__init__(table, columns)
  74. def __str__(self):
  75. return self.create_index_name(self.table, self.columns, self.suffix)
  76. class ForeignKeyName(TableColumns):
  77. """Hold a reference to a foreign key name."""
  78. def __init__(self, from_table, from_columns, to_table, to_columns, suffix_template, create_fk_name):
  79. self.to_reference = TableColumns(to_table, to_columns)
  80. self.suffix_template = suffix_template
  81. self.create_fk_name = create_fk_name
  82. super().__init__(from_table, from_columns,)
  83. def references_table(self, table):
  84. return super().references_table(table) or self.to_reference.references_table(table)
  85. def references_column(self, table, column):
  86. return (
  87. super().references_column(table, column) or
  88. self.to_reference.references_column(table, column)
  89. )
  90. def rename_table_references(self, old_table, new_table):
  91. super().rename_table_references(old_table, new_table)
  92. self.to_reference.rename_table_references(old_table, new_table)
  93. def rename_column_references(self, table, old_column, new_column):
  94. super().rename_column_references(table, old_column, new_column)
  95. self.to_reference.rename_column_references(table, old_column, new_column)
  96. def __str__(self):
  97. suffix = self.suffix_template % {
  98. 'to_table': self.to_reference.table,
  99. 'to_column': self.to_reference.columns[0],
  100. }
  101. return self.create_fk_name(self.table, self.columns, suffix)
  102. class Statement(Reference):
  103. """
  104. Statement template and formatting parameters container.
  105. Allows keeping a reference to a statement without interpolating identifiers
  106. that might have to be adjusted if they're referencing a table or column
  107. that is removed
  108. """
  109. def __init__(self, template, **parts):
  110. self.template = template
  111. self.parts = parts
  112. def references_table(self, table):
  113. return any(
  114. hasattr(part, 'references_table') and part.references_table(table)
  115. for part in self.parts.values()
  116. )
  117. def references_column(self, table, column):
  118. return any(
  119. hasattr(part, 'references_column') and part.references_column(table, column)
  120. for part in self.parts.values()
  121. )
  122. def rename_table_references(self, old_table, new_table):
  123. for part in self.parts.values():
  124. if hasattr(part, 'rename_table_references'):
  125. part.rename_table_references(old_table, new_table)
  126. def rename_column_references(self, table, old_column, new_column):
  127. for part in self.parts.values():
  128. if hasattr(part, 'rename_column_references'):
  129. part.rename_column_references(table, old_column, new_column)
  130. def __str__(self):
  131. return self.template % self.parts