| 118 | } |
| 119 | |
| 120 | func TestCheckpointerRunRecordsFailure(t *testing.T) { |
| 121 | db, err := sql.Open("duckdb", ":memory:") |
| 122 | if err != nil { |
| 123 | t.Fatalf("open duckdb: %v", err) |
| 124 | } |
| 125 | defer func() { _ = db.Close() }() |
| 126 | |
| 127 | // Attach an in-memory database as "ducklake" and create the table, |
| 128 | // then detach and reattach as read-only so CHECKPOINT fails. |
| 129 | if _, err := db.Exec("ATTACH ':memory:' AS ducklake"); err != nil { |
| 130 | t.Fatalf("attach: %v", err) |
| 131 | } |
| 132 | if _, err := db.Exec("CREATE SCHEMA ducklake.system"); err != nil { |
| 133 | t.Fatalf("create schema: %v", err) |
| 134 | } |
| 135 | createTable := `CREATE TABLE ducklake.system.checkpoints ( |
| 136 | started_at TIMESTAMPTZ NOT NULL, |
| 137 | finished_at TIMESTAMPTZ NOT NULL, |
| 138 | duration_ms BIGINT NOT NULL, |
| 139 | status VARCHAR NOT NULL, |
| 140 | error VARCHAR |
| 141 | )` |
| 142 | if _, err := db.Exec(createTable); err != nil { |
| 143 | t.Fatalf("create table: %v", err) |
| 144 | } |
| 145 | |
| 146 | // Write a temp file so we can reattach read-only |
| 147 | tmpDir := t.TempDir() |
| 148 | tmpDB := tmpDir + "/test.db" |
| 149 | if _, err := db.Exec("DETACH ducklake"); err != nil { |
| 150 | t.Fatalf("detach: %v", err) |
| 151 | } |
| 152 | // Create a persistent DB, set up the table, detach, reattach read-only |
| 153 | if _, err := db.Exec("ATTACH '" + tmpDB + "' AS ducklake"); err != nil { |
| 154 | t.Fatalf("attach persistent: %v", err) |
| 155 | } |
| 156 | if _, err := db.Exec("CREATE SCHEMA ducklake.system"); err != nil { |
| 157 | t.Fatalf("create schema persistent: %v", err) |
| 158 | } |
| 159 | if _, err := db.Exec(createTable); err != nil { |
| 160 | t.Fatalf("create table persistent: %v", err) |
| 161 | } |
| 162 | if _, err := db.Exec("DETACH ducklake"); err != nil { |
| 163 | t.Fatalf("detach persistent: %v", err) |
| 164 | } |
| 165 | if _, err := db.Exec("ATTACH '" + tmpDB + "' AS ducklake (READ_ONLY)"); err != nil { |
| 166 | t.Fatalf("attach read-only: %v", err) |
| 167 | } |
| 168 | |
| 169 | c := &DuckLakeCheckpointer{db: db} |
| 170 | c.run() |
| 171 | |
| 172 | // On a read-only DB the INSERT fails — 0 rows expected. |
| 173 | // The test validates that run() handles INSERT errors gracefully without panicking. |
| 174 | var count int |
| 175 | if err := db.QueryRow("SELECT COUNT(*) FROM ducklake.system.checkpoints").Scan(&count); err != nil { |
| 176 | t.Fatalf("count: %v", err) |
| 177 | } |