Skip to content

feat(connections): compare the structure of two schemas - #104

Merged
sao-coding merged 2 commits into
sao-coding:mainfrom
doraeyi:feat/schema-compare
Dec 4, 2026
Merged

sao-coding merged 2 commits into
sao-coding:mainfrom
doraeyi:feat/schema-compare

Conversation

@doraeyi

@doraeyi doraeyi commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

想確認本機的資料庫跟正式機的結構有沒有對上(migration 有沒有漏跑、本機是不是多開了欄位),現在只能兩邊各開一個 ER 圖或一張一張表看。DBeaver 的結構比對只有付費版有,所以我加了一個簡單版的。

用法:在 Schema 面板的 schema 上按右鍵 →「設為比對基準」,再到另一個 schema 按右鍵 →「與 X(連線)比對結構」。兩個 schema 可以在同一個連線,也可以在不同連線。

比的是 Catalog::schema_columns 讀得到的東西,跟 ER 圖讀的一樣:

  • 只有 A 有、只有 B 有的資料表
  • 兩邊都有的表:多出來和少掉的欄位,還有型別、NULL、主鍵不一樣的欄位

名稱不分大小寫(MySQL 在 Windows 會轉小寫),型別不分大小寫也不管空白(VARCHAR( 255 ) 跟 varchar(255) 算一樣)。欄位順序、索引、外鍵、預設值這版沒有比。不同引擎之間比的話,型別名稱本來就不一樣(INTEGER 對 int),會列成型別不同。

結果是一個對話框,紅色是只有 A 有,綠色是只有 B 有,黃色是不一樣的,可以「複製為文字」貼到 issue 或訊息裡。兩邊的結構是同時讀的,只有讀取,所以 Production 上也可以用。如果某一邊欄位太多、schema_columns 沒讀完,報告最上面會提醒。

改的檔案:

  • crates/locus-core/src/connection/schema_diff.rs:比對和報告文字,純邏輯,附 7 個測試
  • crates/app/src/connections/compare.rs:連線(沒連的話先連)、兩邊同時讀、顯示對話框
  • crates/app/src/connections/objects.rs、objects/load.rs、objects/render.rs:記住比對基準、schema 右鍵選單的兩個項目
  • crates/app/src/shell/mod.rs:接上 ObjectsEvent::CompareSchemas
  • crates/app/locales/*.yml:schema_compare.*,三種語言
  • crates/locus-core/src/connection/mod.rs、docs/architecture.md:新模組
  • .changeset/schema-compare.md(minor)

fmt、clippy -D warnings、typos 都過,cargo test -p locus-core --lib 406 個、cargo test -p app --bins 86 個,都過。

實際開起來試過:兩個 SQLite 連線的 main 互相比,對話框上面有 A、B 分別是哪個連線,只有一邊有的資料表都有列出來。MySQL、PostgreSQL 之間的比對我只有單元測試,還沒接真的資料庫試。

之後可以再加的:比索引和外鍵、從差異產生 ALTER 語法。

doraeyi and others added 2 commits December 3, 2026 23:24
Right-click a schema, set it as the comparison base, then compare another
schema with it, on the same connection or another one. The report lists the
tables only one side has and the columns that were added, removed, or differ
in type, NULL or primary key. Only catalog reads are run.
@sao-coding
sao-coding merged commit be3df11 into sao-coding:main Dec 4, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants