Skip to content

Commit

Permalink
fix: don't break column references when modifying tables
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Dec 14, 2023
1 parent 4daa937 commit 2faa2ad
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/sqlitediff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ def to_sql(self) -> str:
raise ValueError(f"No source SQL available for {self.new.raw_name} table")

columns = ", ".join(c.raw_name for c in self.old.columns.values())

new_sql_temp = self.new.sql.replace(
f"CREATE TABLE {self.new.raw_name}",
"CREATE TABLE sqlitediff_temp",
1
)
if new_sql_temp == self.new.sql:
raise ValueError(f"Table {self.new.name} SQL does not match name")

sql = [
f"-- Previous table schema for {self.old.raw_name}:",
f"{sql_comment(self.old.sql + ';')}",
f"ALTER TABLE {self.old.raw_name} RENAME TO sqlitediff_temp;",
f"{self.new.sql};",
f"{new_sql_temp};",
f"INSERT INTO {self.new.raw_name} ({columns}) SELECT * FROM sqlitediff_temp;",
f"DROP TABLE sqlitediff_temp;",
f"DROP TABLE {self.old.raw_name};",
f"ALTER TABLE sqlitediff_temp RENAME TO {self.new.raw_name};",
]

if len(self.references) > 0:
Expand Down

0 comments on commit 2faa2ad

Please sign in to comment.