Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/datajoint/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,46 @@ def alter_table_sql(
"""
...

@abstractmethod
def alter_table_ddl(
self,
full_table_name: str,
drop: list[str],
changes: list[dict[str, Any]],
table_comment: str | None = None,
) -> list[str]:
"""
Generate the statements that apply ``Table.alter()`` changes.

Parameters
----------
full_table_name : str
Quoted name of the table.
drop : list[str]
Names of the attributes to drop.
changes : list[dict]
Attributes to add or change, in declaration order. Each has keys
``new`` and ``old`` (the parts of the column after and before the
change, ``old`` None for an added column), ``renamed_from`` (the
name in a rename marker, or None) and ``after`` (the attribute to
place it after, or None). The parts are dicts with keys ``name``,
``type``, ``nullable``, ``default`` (an SQL expression or None),
``comment`` and ``sql`` (the compiled column declaration).
table_comment : str, optional
The new table comment, or None when it is unchanged.

Returns
-------
list[str]
Statements to run in order; empty when there is nothing to change.

Raises
------
DataJointError
If the backend cannot convert an attribute to its new type.
"""
...

@abstractmethod
def add_comment_sql(
self,
Expand Down Expand Up @@ -677,6 +717,22 @@ def supports_column_position(self) -> bool:
"""
return True # Default for MySQL, override in PostgreSQL

@property
def supports_transactional_ddl(self) -> bool:
"""
Whether DDL statements can run inside a transaction and roll back.

MySQL commits implicitly before and after each DDL statement.
PostgreSQL runs DDL inside transactions, so a change made of several
statements can be applied completely or not at all.

Returns
-------
bool
False for MySQL, True for PostgreSQL.
"""
return False # Default for MySQL, override in PostgreSQL

@property
def auto_indexes_foreign_keys(self) -> bool:
"""
Expand Down
44 changes: 44 additions & 0 deletions src/datajoint/adapters/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,50 @@ def alter_table_sql(

return f"ALTER TABLE {table_name} {', '.join(clauses)}"

def alter_table_ddl(
self,
full_table_name: str,
drop: list[str],
changes: list[dict[str, Any]],
table_comment: str | None = None,
) -> list[str]:
"""
Generate the ALTER TABLE statement that applies ``Table.alter()`` changes.

MySQL makes every change in one statement: ``MODIFY`` restates a
column's whole declaration, comment included, and ``CHANGE`` also
renames it.

Parameters
----------
full_table_name : str
Quoted name of the table.
drop : list[str]
Names of the attributes to drop.
changes : list[dict]
Attributes to add or change; see ``DatabaseAdapter.alter_table_ddl``.
table_comment : str, optional
The new table comment, or None when it is unchanged.

Returns
-------
list[str]
One ALTER TABLE statement, or none when there is nothing to change.
"""
clauses = [f"DROP {self.quote_identifier(name)}" for name in drop]
for change in changes:
if change["old"] is None:
command = "ADD"
elif change["renamed_from"] is None:
command = "MODIFY"
else:
command = f"CHANGE {self.quote_identifier(change['renamed_from'])}"
after = "" if change["after"] is None else f"AFTER {self.quote_identifier(change['after'])}"
clauses.append(f"{command} {change['new']['sql']} {after}")
if table_comment is not None:
clauses.append(f'COMMENT="{table_comment}"')
return [f"ALTER TABLE {full_table_name}\n\t" + ",\n\t".join(clauses)] if clauses else []

def add_comment_sql(
self,
object_type: str,
Expand Down
Loading
Loading