extractSchema extracts the complete schema by querying sqlite_master
(db *database.DB)
| 126 | |
| 127 | // extractSchema extracts the complete schema by querying sqlite_master |
| 128 | func extractSchema(db *database.DB) (string, error) { |
| 129 | // Query sqlite_master for all schema objects, excluding FTS shadow tables |
| 130 | // FTS shadow tables are internal tables automatically created by FTS virtual tables |
| 131 | rows, err := db.Conn.Query(`SELECT sql FROM sqlite_master |
| 132 | WHERE sql IS NOT NULL |
| 133 | AND name NOT LIKE 'sqlite_%' |
| 134 | AND (type != 'table' |
| 135 | OR (type = 'table' AND name NOT IN ( |
| 136 | SELECT m1.name FROM sqlite_master m1 |
| 137 | JOIN sqlite_master m2 ON m1.name LIKE m2.name || '_%' |
| 138 | WHERE m2.type = 'table' AND m2.sql LIKE '%VIRTUAL TABLE%' |
| 139 | )))`) |
| 140 | if err != nil { |
| 141 | return "", fmt.Errorf("querying sqlite_master: %w", err) |
| 142 | } |
| 143 | defer rows.Close() |
| 144 | |
| 145 | var schemas []string |
| 146 | for rows.Next() { |
| 147 | var sql string |
| 148 | if err := rows.Scan(&sql); err != nil { |
| 149 | return "", fmt.Errorf("scanning row: %w", err) |
| 150 | } |
| 151 | schemas = append(schemas, sql) |
| 152 | } |
| 153 | |
| 154 | if err := rows.Err(); err != nil { |
| 155 | return "", fmt.Errorf("iterating rows: %w", err) |
| 156 | } |
| 157 | |
| 158 | // Add autogenerated header comment |
| 159 | header := `-- This is the final state of the CLI database after all migrations. |
| 160 | -- Auto-generated by generate-schema.go. Do not edit manually. |
| 161 | ` |
| 162 | return header + strings.Join(schemas, ";\n") + ";\n", nil |
| 163 | } |
no test coverage detected