133 gboolean all_ok =
TRUE;
134 const char *failing_query = NULL;
136 sqlite3_stmt *innerstmt;
141 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
152 _SQLITE3_EXEC(db->
handle,
"DROP TABLE IF EXISTS main.mipmap_timestamps", NULL, NULL, NULL);
153 _SQLITE3_EXEC(db->
handle,
"DROP TABLE IF EXISTS main.dt_migration_table", NULL, NULL, NULL);
158 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.db_info (key VARCHAR PRIMARY KEY, value VARCHAR)",
160 _SQLITE3_EXEC(db->
handle,
"INSERT OR REPLACE INTO main.db_info (key, value) VALUES ('version', 1)",
163 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.film_rolls_folder_index ON film_rolls (folder)",
166 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN orientation INTEGER", NULL, NULL, NULL);
167 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN focus_distance REAL", NULL, NULL, NULL);
168 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN group_id INTEGER", NULL, NULL, NULL);
169 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN histogram BLOB", NULL, NULL, NULL);
170 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN lightmap BLOB", NULL, NULL, NULL);
171 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN longitude REAL", NULL, NULL, NULL);
172 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN latitude REAL", NULL, NULL, NULL);
173 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN color_matrix BLOB", NULL, NULL, NULL);
175 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN colorspace INTEGER", NULL, NULL, NULL);
176 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN version INTEGER", NULL, NULL, NULL);
177 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN max_version INTEGER", NULL, NULL, NULL);
178 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET orientation = -1 WHERE orientation IS NULL", NULL, NULL, NULL);
179 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET focus_distance = -1 WHERE focus_distance IS NULL",
181 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET group_id = id WHERE group_id IS NULL", NULL, NULL, NULL);
182 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET max_version = (SELECT COUNT(*)-1 FROM main.images i WHERE "
183 "i.filename = main.images.filename AND "
184 "i.film_id = main.images.film_id) WHERE max_version IS NULL",
188 "UPDATE main.images SET version = (SELECT COUNT(*) FROM main.images i "
189 "WHERE i.filename = main.images.filename AND "
190 "i.film_id = main.images.film_id AND i.id < main.images.id) WHERE version IS NULL",
193 _SQLITE3_EXEC(db->
handle,
"ALTER TABLE main.images RENAME TO dt_migration_table", NULL, NULL, NULL);
194 _SQLITE3_EXEC(db->
handle,
"DROP INDEX IF EXISTS main.images_group_id_index", NULL, NULL, NULL);
195 _SQLITE3_EXEC(db->
handle,
"DROP INDEX IF EXISTS main.images_film_id_index", NULL, NULL, NULL);
198 "CREATE TABLE main.images (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
199 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
200 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
201 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
202 "output_width INTEGER, output_height INTEGER, crop REAL, "
203 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
204 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
205 "caption VARCHAR, description VARCHAR, license VARCHAR, sha1sum CHAR(40), "
206 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
207 "latitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, max_version INTEGER)",
209 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX main.images_group_id_index ON images (group_id)", NULL, NULL, NULL);
210 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX main.images_film_id_index ON images (film_id)", NULL, NULL, NULL);
213 "INSERT INTO main.images (id, group_id, film_id, width, height, filename, maker, model, "
214 "lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags, "
215 "output_width, output_height, crop, raw_parameters, raw_denoise_threshold, "
216 "raw_auto_bright_threshold, raw_black, raw_maximum, caption, description, license, sha1sum, "
217 "orientation, histogram, lightmap, longitude, latitude, color_matrix, colorspace, version, "
219 "SELECT id, group_id, film_id, width, height, filename, maker, model, lens, exposure, aperture, iso, "
220 "focal_length, focus_distance, datetime_taken, flags, output_width, output_height, crop, "
221 "raw_parameters, raw_denoise_threshold, raw_auto_bright_threshold, raw_black, raw_maximum, "
222 "caption, description, license, sha1sum, orientation, histogram, lightmap, longitude, "
223 "latitude, color_matrix, colorspace, version, max_version FROM dt_migration_table",
228 _SQLITE3_EXEC(db->
handle,
"CREATE TEMPORARY TABLE dt_migration_table (imgid INTEGER)", NULL, NULL, NULL);
229 _SQLITE3_EXEC(db->
handle,
"INSERT INTO dt_migration_table SELECT imgid FROM main.selected_images", NULL, NULL,
232 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.selected_images (imgid INTEGER PRIMARY KEY)", NULL, NULL, NULL);
233 _SQLITE3_EXEC(db->
handle,
"INSERT OR IGNORE INTO main.selected_images SELECT imgid FROM dt_migration_table",
237 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
238 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
239 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
240 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
241 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.history_imgid_index ON history (imgid)", NULL, NULL,
243 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
245 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET multi_priority = 0 WHERE multi_priority IS NULL", NULL, NULL,
247 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL, NULL);
249 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.mask (imgid INTEGER, formid INTEGER, form INTEGER, "
250 "name VARCHAR(256), version INTEGER, "
251 "points BLOB, points_count INTEGER, source BLOB)",
253 sqlite3_exec(db->
handle,
"ALTER TABLE main.mask ADD COLUMN source BLOB", NULL, NULL,
256 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.tagged_images_tagid_index ON tagged_images (tagid)",
260 "CREATE TABLE IF NOT EXISTS main.styles (id INTEGER, name VARCHAR, description VARCHAR)", NULL,
262 sqlite3_exec(db->
handle,
"ALTER TABLE main.styles ADD COLUMN id INTEGER", NULL, NULL, NULL);
263 _SQLITE3_EXEC(db->
handle,
"UPDATE main.styles SET id = rowid WHERE id IS NULL", NULL, NULL, NULL);
265 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.style_items (styleid INTEGER, num INTEGER, module "
266 "INTEGER, operation VARCHAR(256), op_params BLOB, "
267 "enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, multi_priority "
268 "INTEGER, multi_name VARCHAR(256))",
270 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
271 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
272 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
273 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
274 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
276 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET multi_priority = 0 WHERE multi_priority IS NULL", NULL,
278 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL,
282 _SQLITE3_EXEC(db->
handle,
"CREATE TEMPORARY TABLE dt_migration_table (imgid INTEGER, color INTEGER)", NULL,
284 _SQLITE3_EXEC(db->
handle,
"INSERT INTO dt_migration_table SELECT imgid, color FROM main.color_labels", NULL,
287 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.color_labels (imgid INTEGER, color INTEGER)", NULL, NULL, NULL);
288 _SQLITE3_EXEC(db->
handle,
"CREATE UNIQUE INDEX main.color_labels_idx ON color_labels (imgid, color)", NULL, NULL,
290 _SQLITE3_EXEC(db->
handle,
"INSERT OR IGNORE INTO main.color_labels SELECT imgid, color FROM dt_migration_table",
294 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.meta_data (id INTEGER, key INTEGER, value VARCHAR)",
296 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.metadata_index ON meta_data (id, key)", NULL, NULL,
299 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.presets (name VARCHAR, description VARCHAR, "
300 "operation VARCHAR, op_version INTEGER, op_params BLOB, "
301 "enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, multi_priority "
302 "INTEGER, multi_name VARCHAR(256), "
303 "model VARCHAR, maker VARCHAR, lens VARCHAR, iso_min REAL, iso_max REAL, "
304 "exposure_min REAL, exposure_max REAL, "
305 "aperture_min REAL, aperture_max REAL, focal_length_min REAL, focal_length_max "
306 "REAL, writeprotect INTEGER, "
307 "autoapply INTEGER, filter INTEGER, def INTEGER, isldr INTEGER)",
309 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN op_version INTEGER", NULL, NULL, NULL);
310 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
311 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
312 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
313 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
316 sqlite3_prepare_v2(db->
handle,
317 "SELECT p.rowid, p.name, p.operation, p.op_version FROM main.presets p INNER JOIN "
318 "(SELECT * FROM (SELECT rowid, name, operation, op_version, COUNT(*) AS count "
319 "FROM main.presets GROUP BY name, operation, op_version) WHERE count > 1) s "
320 "ON p.name = s.name AND p.operation = s.operation AND p.op_version = s.op_version",
323 char *last_name = NULL, *last_operation = NULL;
324 int last_op_version = 0;
326 while(sqlite3_step(stmt) == SQLITE_ROW)
328 const int rowid = sqlite3_column_int(stmt, 0);
329 const char *
name = (
const char *)sqlite3_column_text(stmt, 1);
330 const char *operation = (
const char *)sqlite3_column_text(stmt, 2);
331 const int op_version = sqlite3_column_int(stmt, 3);
335 || last_op_version != op_version)
339 last_name = g_strdup(
name);
340 last_operation = g_strdup(operation);
341 last_op_version = op_version;
347 sqlite3_prepare_v2(db->
handle,
"SELECT name FROM main.presets WHERE name = ?1 || ' (' || ?2 || ')' AND "
348 "operation = ?3 AND op_version = ?4",
349 -1, &innerstmt, NULL);
353 sqlite3_bind_text(innerstmt, 1,
name, -1, SQLITE_TRANSIENT);
354 sqlite3_bind_int(innerstmt, 2,
i);
355 sqlite3_bind_text(innerstmt, 3, operation, -1, SQLITE_TRANSIENT);
356 sqlite3_bind_int(innerstmt, 4, op_version);
357 if(sqlite3_step(innerstmt) != SQLITE_ROW)
break;
358 sqlite3_reset(innerstmt);
359 sqlite3_clear_bindings(innerstmt);
362 sqlite3_finalize(innerstmt);
366 const char *query =
"UPDATE main.presets SET name = name || ' (' || ?1 || ')' WHERE rowid = ?2";
368 sqlite3_prepare_v2(db->
handle, query, -1, &innerstmt, NULL);
369 sqlite3_bind_int(innerstmt, 1,
i);
370 sqlite3_bind_int(innerstmt, 2, rowid);
371 if(sqlite3_step(innerstmt) != SQLITE_DONE)
374 failing_query = query;
377 sqlite3_finalize(innerstmt);
379 sqlite3_finalize(stmt);
385 "CREATE UNIQUE INDEX IF NOT EXISTS main.presets_idx ON presets (name, operation, op_version)",
387 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
389 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET multi_priority = 0 WHERE multi_priority IS NULL", NULL, NULL,
391 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL, NULL);
399 sqlite3_prepare_v2(db->
handle,
"SELECT id, filename FROM main.images WHERE filename LIKE '/%'", -1, &stmt, NULL);
400 sqlite3_prepare_v2(db->
handle,
"UPDATE main.images SET filename = ?1 WHERE id = ?2", -1, &innerstmt, NULL);
402 while(sqlite3_step(stmt) == SQLITE_ROW)
404 const int id = sqlite3_column_int(stmt, 0);
405 const char *path = (
const char *)sqlite3_column_text(stmt, 1);
406 gchar *filename = g_path_get_basename(path);
407 sqlite3_bind_text(innerstmt, 1, filename, -1, SQLITE_TRANSIENT);
408 sqlite3_bind_int(innerstmt, 2,
id);
409 sqlite3_step(innerstmt);
410 sqlite3_reset(innerstmt);
411 sqlite3_clear_bindings(innerstmt);
414 sqlite3_finalize(stmt);
415 sqlite3_finalize(innerstmt);
423 "UPDATE main.images SET datetime_taken = REPLACE(datetime_taken, '-', ':') WHERE datetime_taken LIKE '%-%'",
429 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
432 fprintf(stderr,
"[init] failing query: `%s'\n", failing_query);
433 fprintf(stderr,
"[init] %s\n", sqlite3_errmsg(db->
handle));
434 sqlite3_exec(db->
handle,
"ROLLBACK TRANSACTION", NULL, NULL, NULL);
488 int new_version = version;
491 else if(version == 0)
498 else if(version == 1)
501 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
503 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN write_timestamp INTEGER",
504 "[init] can't add `write_timestamp' column to database\n");
505 TRY_EXEC(
"UPDATE main.images SET write_timestamp = STRFTIME('%s', 'now') WHERE write_timestamp IS NULL",
506 "[init] can't initialize `write_timestamp' with current point in time\n");
508 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
511 else if(version == 2)
515 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
517 TRY_EXEC(
"UPDATE main.images SET raw_black = 0, raw_maximum = 16384",
518 "[init] can't reset raw_black and raw_maximum\n");
520 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
523 else if(version == 3)
525 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
527 TRY_EXEC(
"CREATE TRIGGER insert_tag AFTER INSERT ON main.tags"
529 " INSERT INTO tagxtag SELECT id, new.id, 0 FROM TAGS;"
530 " UPDATE tagxtag SET count = 1000000 WHERE id1=new.id AND id2=new.id;"
532 "[init] can't create insert_tag trigger\n");
533 TRY_EXEC(
"CREATE TRIGGER delete_tag BEFORE DELETE ON main.tags"
535 " DELETE FROM tagxtag WHERE id1=old.id OR id2=old.id;"
536 " DELETE FROM tagged_images WHERE tagid=old.id;"
538 "[init] can't create delete_tag trigger\n");
539 TRY_EXEC(
"CREATE TRIGGER attach_tag AFTER INSERT ON main.tagged_images"
542 " SET count = count + 1"
543 " WHERE (id1=new.tagid AND id2 IN (SELECT tagid FROM tagged_images WHERE imgid=new.imgid))"
544 " OR (id2=new.tagid AND id1 IN (SELECT tagid FROM tagged_images WHERE imgid=new.imgid));"
546 "[init] can't create attach_tag trigger\n");
547 TRY_EXEC(
"CREATE TRIGGER detach_tag BEFORE DELETE ON main.tagged_images"
550 " SET count = count - 1"
551 " WHERE (id1=old.tagid AND id2 IN (SELECT tagid FROM tagged_images WHERE imgid=old.imgid))"
552 " OR (id2=old.tagid AND id1 IN (SELECT tagid FROM tagged_images WHERE imgid=old.imgid));"
554 "[init] can't create detach_tag trigger\n");
556 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
559 else if(version == 4)
561 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
563 TRY_EXEC(
"ALTER TABLE main.presets RENAME TO tmp_presets",
"[init] can't rename table presets\n");
565 TRY_EXEC(
"CREATE TABLE main.presets (name VARCHAR, description VARCHAR, operation VARCHAR, op_params BLOB,"
566 "enabled INTEGER, blendop_params BLOB, model VARCHAR, maker VARCHAR, lens VARCHAR,"
567 "iso_min REAL, iso_max REAL, exposure_min REAL, exposure_max REAL, aperture_min REAL,"
568 "aperture_max REAL, focal_length_min REAL, focal_length_max REAL, writeprotect INTEGER,"
569 "autoapply INTEGER, filter INTEGER, def INTEGER, format INTEGER, op_version INTEGER,"
570 "blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
571 "[init] can't create new presets table\n");
573 TRY_EXEC(
"INSERT INTO main.presets (name, description, operation, op_params, enabled, blendop_params, model, "
574 "maker, lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, aperture_max,"
575 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, def, format, op_version, "
576 "blendop_version, multi_priority, multi_name) SELECT name, description, operation, op_params, "
577 "enabled, blendop_params, model, maker, lens, iso_min, iso_max, exposure_min, exposure_max, "
578 "aperture_min, aperture_max, focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
579 "def, isldr, op_version, blendop_version, multi_priority, multi_name FROM tmp_presets",
580 "[init] can't populate presets table from tmp_presets\n");
582 TRY_EXEC(
"DROP TABLE tmp_presets",
"[init] can't delete table tmp_presets\n");
584 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
587 else if(version == 5)
589 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
591 TRY_EXEC(
"CREATE INDEX main.images_filename_index ON images (filename)",
592 "[init] can't create index on image filename\n");
594 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
597 else if(version == 6)
600 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
602 if(sqlite3_exec(db->
handle,
"SELECT style_id FROM main.style_items", NULL, NULL, NULL) == SQLITE_OK)
605 TRY_EXEC(
"ALTER TABLE main.style_items RENAME TO tmp_style_items",
606 "[init] can't rename table style_items\n");
608 TRY_EXEC(
"CREATE TABLE main.style_items (styleid INTEGER, num INTEGER, module INTEGER, "
609 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, "
610 "blendop_params BLOB, blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
611 "[init] can't create new style_items table\n");
613 TRY_EXEC(
"INSERT INTO main.style_items (styleid, num, module, operation, op_params, enabled,"
614 " blendop_params, blendop_version, multi_priority, multi_name)"
615 " SELECT style_id, num, module, operation, op_params, enabled,"
616 " blendop_params, blendop_version, multi_priority, multi_name"
617 " FROM tmp_style_items",
618 "[init] can't populate style_items table from tmp_style_items\n");
620 TRY_EXEC(
"DROP TABLE tmp_style_items",
"[init] can't delete table tmp_style_items\n");
624 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
627 else if(version == 7)
630 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
632 TRY_EXEC(
"ALTER TABLE main.film_rolls RENAME TO tmp_film_rolls",
"[init] can't rename table film_rolls\n");
634 TRY_EXEC(
"CREATE TABLE main.film_rolls "
635 "(id INTEGER PRIMARY KEY, datetime_accessed CHAR(20), "
636 "folder VARCHAR(1024) NOT NULL)",
637 "[init] can't create new film_rolls table\n");
639 TRY_EXEC(
"INSERT INTO main.film_rolls (id, datetime_accessed, folder) "
640 "SELECT id, datetime_accessed, folder "
641 "FROM tmp_film_rolls "
642 "WHERE folder IS NOT NULL",
643 "[init] can't populate film_rolls table from tmp_film_rolls\n");
645 TRY_EXEC(
"DROP TABLE tmp_film_rolls",
"[init] can't delete table tmp_film_rolls\n");
647 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
650 else if(version == 8)
653 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
655 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN history_end INTEGER",
656 "[init] can't add `history_end' column to database\n");
658 TRY_EXEC(
"UPDATE main.images SET history_end = (SELECT IFNULL(MAX(num) + 1, 0) FROM main.history "
659 "WHERE imgid = id)",
"[init] can't initialize `history_end' with last history entry\n");
661 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
664 else if(version == 9)
667 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
669 TRY_EXEC(
"UPDATE main.images SET history_end = (SELECT IFNULL(MAX(num) + 1, 0) FROM main.history "
670 "WHERE imgid = id)",
"[init] can't set `history_end' to 0 where it was NULL\n");
672 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
675 else if(version == 10)
678 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
680 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN altitude REAL",
681 "[init] can't add `altitude' column to database\n");
683 TRY_EXEC(
"UPDATE main.images SET altitude = NULL",
"[init] can't initialize `altitude' with NULL\n");
685 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
688 else if(version == 11)
691 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
693 TRY_EXEC(
"DROP TRIGGER main.detach_tag",
"[init] can't drop trigger `detach_tag' from database\n");
695 TRY_EXEC(
"DROP TRIGGER main.attach_tag",
"[init] can't drop trigger `attach_tag' from database\n");
697 TRY_EXEC(
"DROP TRIGGER main.delete_tag",
"[init] can't drop trigger `delete_tag' from database\n");
699 TRY_EXEC(
"DROP TRIGGER main.insert_tag",
"[init] can't drop trigger `insert_tag' from database\n");
701 TRY_EXEC(
"DROP TABLE main.tagxtag",
"[init] can't drop table `tagxtag' from database\n");
703 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
706 else if(version == 12)
709 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
716 sqlite3_finalize(stmt); \
717 sqlite3_finalize(select_stmt); \
718 sqlite3_finalize(count_clashes_stmt); \
719 sqlite3_finalize(update_name_stmt); \
720 sqlite3_finalize(insert_stmt); \
721 sqlite3_finalize(delete_stmt); \
725 sqlite3_stmt *insert_stmt = NULL, *delete_stmt = NULL, *select_stmt = NULL, *count_clashes_stmt = NULL,
726 *update_name_stmt = NULL;
730 TRY_EXEC(
"DELETE FROM main.presets WHERE rowid IN (SELECT p1.rowid FROM main.presets p1 "
731 "JOIN data.presets p2 ON "
732 "p1.name IS p2.name AND "
733 "p1.description IS p2.description AND "
734 "p1.operation IS p2.operation AND "
735 "p1.op_version IS p2.op_version AND "
736 "p1.op_params IS p2.op_params AND "
737 "p1.enabled IS p2.enabled AND "
738 "p1.blendop_params IS p2.blendop_params AND "
739 "p1.blendop_version IS p2.blendop_version AND "
740 "p1.multi_priority IS p2.multi_priority AND "
741 "p1.multi_name IS p2.multi_name AND "
742 "p1.model IS p2.model AND "
743 "p1.maker IS p2.maker AND "
744 "p1.lens IS p2.lens AND "
745 "p1.iso_min IS p2.iso_min AND "
746 "p1.iso_max IS p2.iso_max AND "
747 "p1.exposure_min IS p2.exposure_min AND "
748 "p1.exposure_max IS p2.exposure_max AND "
749 "p1.aperture_min IS p2.aperture_min AND "
750 "p1.aperture_max IS p2.aperture_max AND "
751 "p1.focal_length_min IS p2.focal_length_min AND "
752 "p1.focal_length_max IS p2.focal_length_max AND "
753 "p1.writeprotect IS p2.writeprotect AND "
754 "p1.autoapply IS p2.autoapply AND "
755 "p1.filter IS p2.filter AND "
756 "p1.def IS p2.def AND "
757 "p1.format IS p2.format "
758 "WHERE p1.writeprotect = 0)",
759 "[init] can't delete already migrated presets from database\n");
763 TRY_PREPARE(select_stmt,
"SELECT p.rowid, r FROM main.presets AS p, (SELECT rowid AS r, name, operation, "
764 "op_version FROM main.presets GROUP BY name, operation, op_version HAVING "
765 "COUNT(*) > 1) USING (name, operation, op_version) WHERE p.rowid != r",
766 "[init] can't prepare selecting presets with same name, operation, op_version from database\n");
769 TRY_PREPARE(count_clashes_stmt,
"SELECT COUNT(*) FROM main.presets AS p, (SELECT name, operation, op_version "
770 "FROM main.presets WHERE rowid = ?1) AS i ON p.name = i.name || \" #\" || ?2 "
771 "AND p.operation = i.operation AND p.op_version = i.op_version",
772 "[init] can't prepare selection of preset count by name from database\n");
775 TRY_PREPARE(update_name_stmt,
"UPDATE main.presets SET name = name || \" #\" || ?1 WHERE rowid = ?2",
776 "[init] can't prepare updating of preset name in database\n");
779 TRY_PREPARE(stmt,
"SELECT p1.rowid FROM main.presets p1 INNER JOIN data.presets p2 "
780 "USING (name, operation, op_version) WHERE p1.writeprotect = 0",
781 "[init] can't access table `presets' in database\n");
784 TRY_PREPARE(insert_stmt,
"INSERT OR FAIL INTO data.presets (name, description, operation, op_version, "
785 "op_params, enabled, blendop_params, blendop_version, multi_priority, multi_name, "
786 "model, maker, lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, "
787 "aperture_max, focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
789 "SELECT name || \" #\" || ?1, description, operation, op_version, op_params, "
790 "enabled, blendop_params, blendop_version, multi_priority, multi_name, model, maker, "
791 "lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, aperture_max, "
792 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, def, format "
793 "FROM main.presets p1 WHERE p1.rowid = ?2",
794 "[init] can't prepare insertion statement\n");
796 TRY_PREPARE(delete_stmt,
"DELETE FROM main.presets WHERE rowid = ?1",
"[init] can't prepare deletion statement\n");
799 while(sqlite3_step(select_stmt) == SQLITE_ROW)
801 const int own_rowid = sqlite3_column_int(select_stmt, 0);
802 const int other_rowid = sqlite3_column_int(select_stmt, 1);
803 int preset_version = 0;
808 sqlite3_reset(count_clashes_stmt);
809 sqlite3_clear_bindings(count_clashes_stmt);
810 sqlite3_bind_int(count_clashes_stmt, 1, other_rowid);
811 sqlite3_bind_int(count_clashes_stmt, 2, preset_version);
813 while(sqlite3_step(count_clashes_stmt) == SQLITE_ROW && sqlite3_column_int(count_clashes_stmt, 0) > 0);
815 sqlite3_bind_int(update_name_stmt, 1, preset_version);
816 sqlite3_bind_int(update_name_stmt, 2, own_rowid);
817 TRY_STEP(update_name_stmt, SQLITE_DONE,
"[init] can't rename preset in database\n");
818 sqlite3_reset(update_name_stmt);
819 sqlite3_reset(update_name_stmt);
823 while(sqlite3_step(stmt) == SQLITE_ROW)
825 int preset_version = 0;
826 const int rowid = sqlite3_column_int(stmt, 0);
831 sqlite3_reset(insert_stmt);
832 sqlite3_clear_bindings(insert_stmt);
833 sqlite3_bind_int(insert_stmt, 1, preset_version);
834 sqlite3_bind_int(insert_stmt, 2, rowid);
835 }
while(sqlite3_step(insert_stmt) != SQLITE_DONE);
837 sqlite3_reset(delete_stmt);
838 sqlite3_clear_bindings(delete_stmt);
839 sqlite3_bind_int(delete_stmt, 1, rowid);
840 TRY_STEP(delete_stmt, SQLITE_DONE,
"[init] can't delete preset from database\n");
844 TRY_EXEC(
"INSERT OR FAIL INTO data.presets SELECT name, description, operation, "
845 "op_version, op_params, enabled, blendop_params, blendop_version, "
846 "multi_priority, multi_name, model, maker, lens, iso_min, iso_max, "
847 "exposure_min, exposure_max, aperture_min, aperture_max, "
848 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
849 "def, format FROM main.presets WHERE writeprotect = 0",
850 "[init] can't copy presets to the data database\n");
852 TRY_EXEC(
"DELETE FROM main.presets WHERE writeprotect = 0",
853 "[init] can't copy presets to the data database\n");
863 sqlite3_finalize(stmt); \
864 sqlite3_finalize(insert_stmt); \
865 sqlite3_finalize(select_stmt); \
866 sqlite3_finalize(delete_stmt); \
867 sqlite3_finalize(update_name_stmt); \
868 sqlite3_finalize(select_new_stmt); \
869 sqlite3_finalize(copy_style_items_stmt); \
870 sqlite3_finalize(delete_style_items_stmt); \
875 update_name_stmt = NULL;
878 sqlite3_stmt *select_new_stmt = NULL, *copy_style_items_stmt = NULL, *delete_style_items_stmt = NULL;
880 TRY_PREPARE(stmt,
"SELECT id, name FROM main.styles",
"[init] can't prepare style selection from database\n");
881 TRY_PREPARE(select_stmt,
"SELECT rowid FROM data.styles WHERE name = ?1 LIMIT 1",
882 "[init] can't prepare style item selection from database\n");
883 TRY_PREPARE(update_name_stmt,
"UPDATE main.styles SET name = ?1 WHERE id = ?2",
884 "[init] can't prepare style name update\n");
885 TRY_PREPARE(insert_stmt,
"INSERT INTO data.styles (id, name, description) "
886 "SELECT (SELECT COALESCE(MAX(id),0)+1 FROM data.styles), name, description "
887 "FROM main.styles where id = ?1",
888 "[init] can't prepare style insertion for database\n");
889 TRY_PREPARE(delete_stmt,
"DELETE FROM main.styles WHERE id = ?1",
890 "[init] can't prepare style deletion for database\n");
891 TRY_PREPARE(select_new_stmt,
"SELECT id FROM data.styles WHERE rowid = ?1",
892 "[init] can't prepare style selection from data database\n");
893 TRY_PREPARE(copy_style_items_stmt,
"INSERT INTO data.style_items "
894 "(styleid, num, module, operation, op_params, enabled, blendop_params, "
895 "blendop_version, multi_priority, multi_name) "
896 "SELECT ?1, num, module, operation, op_params, enabled, blendop_params, "
897 "blendop_version, multi_priority, multi_name FROM main.style_items "
898 "WHERE styleid = ?2",
899 "[init] can't prepare style item copy into data database\n");
900 TRY_PREPARE(delete_style_items_stmt,
"DELETE FROM main.style_items WHERE styleid = ?1",
901 "[init] can't prepare style item deletion for database\n");
903 while(sqlite3_step(stmt) == SQLITE_ROW)
905 const int id = sqlite3_column_int(stmt, 0);
906 const char *
name = (
const char *)sqlite3_column_text(stmt, 1);
909 sqlite3_bind_text(select_stmt, 1,
name, -1, SQLITE_TRANSIENT);
910 if(sqlite3_step(select_stmt) == SQLITE_ROW)
913 int style_version = 0;
914 char *new_name = NULL;
919 new_name = g_strdup_printf(
"%s #%d",
name, style_version);
920 sqlite3_reset(select_stmt);
921 sqlite3_clear_bindings(select_stmt);
922 sqlite3_bind_text(select_stmt, 1, new_name, -1, SQLITE_TRANSIENT);
923 }
while(sqlite3_step(select_stmt) == SQLITE_ROW);
926 sqlite3_bind_text(update_name_stmt, 1, new_name, -1, SQLITE_TRANSIENT);
927 sqlite3_bind_int(update_name_stmt, 2,
id);
928 TRY_STEP(update_name_stmt, SQLITE_DONE,
"[init] can't update name of style in database\n");
929 sqlite3_reset(update_name_stmt);
930 sqlite3_clear_bindings(update_name_stmt);
935 sqlite3_bind_int(insert_stmt, 1,
id);
936 TRY_STEP(insert_stmt, SQLITE_DONE,
"[init] can't insert style into data database\n");
937 sqlite3_int64 last_rowid = sqlite3_last_insert_rowid(db->
handle);
940 sqlite3_bind_int(delete_stmt, 1,
id);
941 TRY_STEP(delete_stmt, SQLITE_DONE,
"[init] can't delete style from database\n");
943 sqlite3_bind_int(select_new_stmt, 1, last_rowid);
944 TRY_STEP(select_new_stmt, SQLITE_ROW,
"[init] can't select new style from data database\n");
945 const int new_id = sqlite3_column_int(select_new_stmt, 0);
948 sqlite3_bind_int(copy_style_items_stmt, 1, new_id);
949 sqlite3_bind_int(copy_style_items_stmt, 2,
id);
950 TRY_STEP(copy_style_items_stmt, SQLITE_DONE,
"[init] can't copy style items into data database\n");
953 sqlite3_bind_int(delete_style_items_stmt, 1,
id);
954 TRY_STEP(delete_style_items_stmt, SQLITE_DONE,
"[init] can't delete style items from database\n");
957 sqlite3_reset(insert_stmt);
958 sqlite3_clear_bindings(insert_stmt);
959 sqlite3_reset(select_stmt);
960 sqlite3_clear_bindings(select_stmt);
961 sqlite3_reset(delete_stmt);
962 sqlite3_clear_bindings(delete_stmt);
963 sqlite3_reset(select_new_stmt);
964 sqlite3_clear_bindings(select_new_stmt);
965 sqlite3_reset(copy_style_items_stmt);
966 sqlite3_clear_bindings(copy_style_items_stmt);
967 sqlite3_reset(delete_style_items_stmt);
968 sqlite3_clear_bindings(delete_style_items_stmt);
977 TRY_EXEC(
"INSERT OR IGNORE INTO data.tags (name, icon, description, flags) "
978 "SELECT name, icon, description, flags FROM main.tags",
979 "[init] can't prepare insertion of used tags into data database\n");
983 TRY_EXEC(
"CREATE TEMPORARY TABLE tagged_images_tmp (imgid INTEGER, tagid INTEGER)",
984 "[init] can't create temporary table for updating `tagged_images'\n");
986 TRY_EXEC(
"INSERT INTO tagged_images_tmp (imgid, tagid) "
987 "SELECT imgid, (SELECT t2.id FROM main.tags t1, data.tags t2 USING (name) WHERE t1.id = tagid) "
988 "FROM main.tagged_images",
"[init] can't insert into `tagged_images_tmp'\n");
990 TRY_EXEC(
"DELETE FROM main.tagged_images",
"[init] can't delete tagged images in database\n");
992 TRY_EXEC(
"INSERT OR IGNORE INTO main.tagged_images (imgid, tagid) SELECT imgid, tagid FROM tagged_images_tmp",
993 "[init] can't copy updated values back to `tagged_images'\n");
995 TRY_EXEC(
"DROP TABLE tagged_images_tmp",
"[init] can't drop table `tagged_images_tmp' from database\n");
998 TRY_EXEC(
"DROP INDEX IF EXISTS main.presets_idx",
"[init] can't drop index `presets_idx' from database\n");
999 TRY_EXEC(
"DROP TABLE main.presets",
"[init] can't drop table `presets' from database\n");
1000 TRY_EXEC(
"DROP TABLE main.style_items",
"[init] can't drop table `style_items' from database\n");
1001 TRY_EXEC(
"DROP TABLE main.styles",
"[init] can't drop table `styles' from database\n");
1002 TRY_EXEC(
"DROP TABLE main.tags",
"[init] can't drop table `tags' from database\n");
1004 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1006 }
else if(version == 13)
1009 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1011 TRY_EXEC(
"CREATE TABLE main.used_tags (id INTEGER, name VARCHAR NOT NULL)",
1012 "[init] can't create `used_tags` table\n");
1014 TRY_EXEC(
"CREATE INDEX main.used_tags_idx ON used_tags (id, name)",
1015 "[init] can't create index on table `used_tags' in database\n");
1017 TRY_EXEC(
"INSERT INTO main.used_tags (id, name) SELECT t.id, t.name FROM data.tags AS t, main.tagged_images "
1018 "AS i ON t.id = i.tagid GROUP BY t.id",
1019 "[init] can't insert used tags into `used_tags` table in database\n");
1021 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1024 else if(version == 14)
1027 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1029 TRY_EXEC(
"DELETE FROM main.used_tags WHERE rowid NOT IN (SELECT rowid FROM used_tags GROUP BY id)",
1030 "[init] can't delete duplicated entries from `used_tags' in database\n");
1032 TRY_EXEC(
"DROP INDEX main.used_tags_idx",
"[init] can't drop index `used_tags_idx' from database\n");
1034 TRY_EXEC(
"CREATE UNIQUE INDEX main.used_tags_idx ON used_tags (id, name)",
1035 "[init] can't create index `used_tags_idx' in database\n");
1037 TRY_EXEC(
"DELETE FROM main.tagged_images WHERE tagid IS NULL",
1038 "[init] can't delete NULL entries from `tagged_images' in database");
1040 TRY_EXEC(
"DELETE FROM main.used_tags WHERE id NOT IN (SELECT DISTINCT tagid FROM main.tagged_images)",
1041 "[init] can't delete unused tags from `used_tags' in database\n");
1043 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1046 else if(version == 15)
1048 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1051 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN position INTEGER",
1052 "[init] can't add `position' column to images table in database\n");
1053 TRY_EXEC(
"CREATE INDEX main.image_position_index ON images (position)",
1054 "[init] can't create index for custom image order table\n");
1064 TRY_EXEC(
"UPDATE main.images SET position = id << 32",
1065 "[init] can't update positions custom image order table\n");
1067 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1070 else if(version == 16)
1072 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1075 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN aspect_ratio REAL",
1076 "[init] can't add `aspect_ratio' column to images table in database\n");
1077 TRY_EXEC(
"UPDATE main.images SET aspect_ratio = 0.0",
1078 "[init] can't update aspect_ratio in database\n");
1080 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1083 else if(version == 17)
1085 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1089 TRY_EXEC(
"CREATE TABLE main.masks_history (imgid INTEGER, num INTEGER, formid INTEGER, form INTEGER, name VARCHAR(256), "
1090 "version INTEGER, points BLOB, points_count INTEGER, source BLOB)",
1091 "[init] can't create `masks_history` table\n");
1093 TRY_EXEC(
"CREATE INDEX main.masks_history_imgid_index ON masks_history (imgid)",
1094 "[init] can't create index `masks_history_imgid_index' in database\n");
1097 TRY_EXEC(
"CREATE INDEX main.mask_imgid_index ON mask (imgid);",
1098 "[init] can't create index `mask_imgid_index' in database\n");
1102 TRY_EXEC(
"UPDATE main.history SET num=num+1 WHERE imgid IN (SELECT imgid FROM main.mask WHERE main.mask.imgid=main.history.imgid)",
1103 "[init] can't update `num' with num+1\n");
1106 TRY_EXEC(
"UPDATE main.images SET history_end = history_end+1 WHERE id IN (SELECT imgid FROM main.mask WHERE main.mask.imgid=main.images.id)",
1107 "[init] can't update `history_end' with history_end+1\n");
1110 TRY_EXEC(
"INSERT INTO main.masks_history (imgid, num, formid, form, name, version, points, points_count, source) SELECT "
1111 "imgid, 0, formid, form, name, version, points, points_count, source FROM main.mask",
1112 "[init] can't insert into masks_history\n");
1115 TRY_EXEC(
"INSERT INTO main.history (imgid, num, operation, op_params, module, enabled, "
1116 "blendop_params, blendop_version, multi_priority, multi_name) "
1117 "SELECT DISTINCT imgid, 0, 'mask_manager', NULL, 1, 0, NULL, 0, 0, '' FROM main.mask "
1119 "[init] can't insert mask manager into history\n");
1121 TRY_EXEC(
"DROP TABLE main.mask",
"[init] can't drop table `mask' from database\n");
1126 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN iop_order_version INTEGER",
1127 "[init] can't add `iop_order_version' column to images table in database\n");
1129 TRY_EXEC(
"UPDATE main.images SET iop_order_version = 0",
1130 "[init] can't update iop_order_version in database\n");
1132 TRY_EXEC(
"UPDATE main.images SET iop_order_version = 1 WHERE "
1133 "EXISTS(SELECT * FROM main.history WHERE main.history.imgid = main.images.id)",
1134 "[init] can't update iop_order_version in database\n");
1136 TRY_EXEC(
"ALTER TABLE main.history ADD COLUMN iop_order REAL",
1137 "[init] can't add `iop_order' column to history table in database\n");
1140 TRY_EXEC(
"CREATE TEMPORARY TABLE iop_order_tmp (iop_order REAL, operation VARCHAR(256))",
1141 "[init] can't create temporary table for updating `main.history'\n");
1145 for(GList *priorities = prior_v1; priorities; priorities = g_list_next(priorities))
1151 "INSERT INTO iop_order_tmp (iop_order, operation) VALUES (?1, ?2)",
1154 sqlite3_bind_text(stmt, 2, prior->
operation, -1, SQLITE_TRANSIENT);
1155 TRY_STEP(stmt, SQLITE_DONE,
"[init] can't insert default value in iop_order_tmp\n");
1156 sqlite3_finalize(stmt);
1169 TRY_EXEC(
"UPDATE main.history SET iop_order = ((("
1170 "SELECT MAX(multi_priority) FROM main.history hist1 WHERE hist1.imgid = main.history.imgid AND hist1.operation = main.history.operation "
1171 ") + 1. - multi_priority) / 1000.) + "
1172 "IFNULL((SELECT iop_order FROM iop_order_tmp WHERE iop_order_tmp.operation = "
1173 "main.history.operation), -999999.) ",
1174 "[init] can't update iop_order in history table\n");
1177 sqlite3_stmt *sel_stmt;
1178 TRY_PREPARE(sel_stmt,
"SELECT DISTINCT operation FROM main.history WHERE iop_order <= 0 OR iop_order IS NULL",
1179 "[init] can't prepare selecting history iop_order\n");
1181 while(sqlite3_step(sel_stmt) == SQLITE_ROW)
1183 const char *op_name = (
const char *)sqlite3_column_text(sel_stmt, 0);
1184 printf(
"operation %s with no iop_order while upgrading database\n", op_name);
1186 sqlite3_finalize(sel_stmt);
1188 TRY_EXEC(
"DROP TABLE iop_order_tmp",
"[init] can't drop table `iop_order_tmp' from database\n");
1190 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1199 else if(version == 18)
1201 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1203 TRY_EXEC(
"UPDATE images SET orientation=-2 WHERE orientation=1;",
1204 "[init] can't update images orientation 1 from database\n");
1206 TRY_EXEC(
"UPDATE images SET orientation=1 WHERE orientation=2;",
1207 "[init] can't update images orientation 2 from database\n");
1209 TRY_EXEC(
"UPDATE images SET orientation=-6 WHERE orientation=5;",
1210 "[init] can't update images orientation 5 from database\n");
1212 TRY_EXEC(
"UPDATE images SET orientation=5 WHERE orientation=6;",
1213 "[init] can't update images orientation 6 from database\n");
1215 TRY_EXEC(
"UPDATE images SET orientation=2 WHERE orientation=-2;",
1216 "[init] can't update images orientation -1 from database\n");
1218 TRY_EXEC(
"UPDATE images SET orientation=6 WHERE orientation=-6;",
1219 "[init] can't update images orientation -6 from database\n");
1221 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1224 else if(version == 19)
1226 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1229 TRY_EXEC(
"CREATE TEMPORARY TABLE m_prio (id INTEGER, operation VARCHAR(256), prio INTEGER)",
1230 "[init] can't create temporary table for updating `history and style_items'\n");
1232 TRY_EXEC(
"CREATE INDEX m_prio_id_index ON m_prio (id)",
1233 "[init] can't create temporary index for updating `history and style_items'\n");
1234 TRY_EXEC(
"CREATE INDEX m_prio_op_index ON m_prio (operation)",
1235 "[init] can't create temporary index for updating `history and style_items'\n");
1237 TRY_EXEC(
"INSERT INTO m_prio SELECT imgid, operation, MAX(multi_priority)"
1238 " FROM main.history GROUP BY imgid, operation",
1239 "[init] can't populate m_prio\n");
1241 TRY_EXEC(
"UPDATE main.history SET multi_priority = "
1242 "(SELECT prio FROM m_prio "
1243 " WHERE main.history.operation = operation AND main.history.imgid = id) - main.history.multi_priority",
1244 "[init] can't update multi_priority for history\n");
1246 TRY_EXEC(
"DROP TABLE m_prio",
"[init] can't drop table `m_prio' from database\n");
1248 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1251 else if(version == 20)
1253 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1255 TRY_EXEC(
"DROP INDEX IF EXISTS main.used_tags_idx",
"[init] can't drop index `used_tags_idx' from database\n");
1256 TRY_EXEC(
"DROP TABLE used_tags",
"[init] can't delete table used_tags\n");
1258 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1262 else if(version == 21)
1264 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1267 TRY_EXEC(
"CREATE TABLE module_order (imgid INTEGER PRIMARY KEY, version INTEGER, iop_list VARCHAR)",
1268 "[init] can't create module_order table'\n");
1271 sqlite3_stmt *mig_stmt;
1272 TRY_PREPARE(mig_stmt,
"SELECT imgid, operation, multi_priority, iop_order, mi.iop_order_version"
1273 " FROM main.history AS hi, main.images AS mi"
1274 " WHERE hi.imgid = mi.id"
1275 " GROUP BY imgid, operation, multi_priority"
1276 " ORDER BY imgid, iop_order",
1277 "[init] can't prepare selecting history for iop_order migration (v21)\n");
1279 GList *item_list = NULL;
1281 int current_order_version = -1;
1283 gboolean has_row = (sqlite3_step(mig_stmt) == SQLITE_ROW);
1287 const int32_t imgid = sqlite3_column_int(mig_stmt, 0);
1288 char operation[20] = { 0 };
1289 g_strlcpy(operation, (
const char *)sqlite3_column_text(mig_stmt, 1),
sizeof(operation));
1290 const int multi_priority = sqlite3_column_int(mig_stmt, 2);
1291 const double iop_order = sqlite3_column_double(mig_stmt, 3);
1292 const int iop_order_version = sqlite3_column_int(mig_stmt, 4);
1294 has_row = (sqlite3_step(mig_stmt) == SQLITE_ROW);
1297 if(imgid != current_imgid || !has_row)
1311 GList *e = item_list;
1319 GList *s = iop_order_list;
1326 iop_order_list = g_list_delete_link(iop_order_list, s);
1342 for(e = item_list; e; e = g_list_next(e))
1345 iop_order_list = g_list_prepend(iop_order_list, e_entry);
1356 gboolean has_multiple_instances =
FALSE;
1357 GList *l = iop_order_list;
1361 GList *next = g_list_next(l);
1366 has_multiple_instances =
TRUE;
1374 sqlite3_stmt *ins_stmt = NULL;
1379 sqlite3_prepare_v2(db->
handle,
1380 "INSERT INTO module_order VALUES (?1, ?2, ?3)", -1,
1382 sqlite3_bind_int(ins_stmt, 1, current_imgid);
1383 sqlite3_bind_int(ins_stmt, 2,
kind);
1384 sqlite3_bind_text(ins_stmt, 3, iop_list_txt, -1, SQLITE_TRANSIENT);
1385 TRY_STEP(ins_stmt, SQLITE_DONE,
"[init] can't insert into module_order (custom order)\n");
1386 sqlite3_finalize(ins_stmt);
1392 sqlite3_prepare_v2(db->
handle,
1393 "INSERT INTO module_order VALUES (?1, ?2, NULL)", -1,
1395 sqlite3_bind_int(ins_stmt, 1, current_imgid);
1396 sqlite3_bind_int(ins_stmt, 2,
kind);
1397 TRY_STEP(ins_stmt, SQLITE_DONE,
"[init] can't insert into module_order (standard order)\n");
1398 sqlite3_finalize(ins_stmt);
1401 g_list_free(item_list);
1404 iop_order_list = NULL;
1409 current_imgid = imgid;
1410 current_order_version = iop_order_version;
1417 item_list = g_list_append(item_list, item);
1419 sqlite3_finalize(mig_stmt);
1423 TRY_EXEC(
"CREATE TABLE h (imgid INTEGER, num INTEGER, module INTEGER, "
1424 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, "
1425 "blendop_params BLOB, blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
1426 "[init] can't create module_order table\n");
1427 TRY_EXEC(
"CREATE INDEX h_imgid_index ON h (imgid)",
1428 "[init] can't create index h_imgid_index\n");
1429 TRY_EXEC(
"INSERT INTO h SELECT imgid, num, module, operation, op_params, enabled, "
1430 "blendop_params, blendop_version, multi_priority, multi_name FROM main.history",
1431 "[init] can't create module_order table\n");
1433 "[init] can't drop table history\n");
1434 TRY_EXEC(
"ALTER TABLE h RENAME TO history",
1435 "[init] can't rename h to history\n");
1436 TRY_EXEC(
"DROP INDEX h_imgid_index",
1437 "[init] can't drop index h_imgid_index\n");
1438 TRY_EXEC(
"CREATE INDEX main.history_imgid_index ON history (imgid)",
1439 "[init] can't create index images_imgid_index\n");
1443 TRY_EXEC(
"CREATE TABLE i (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1444 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
1445 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
1446 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
1447 "output_width INTEGER, output_height INTEGER, crop REAL, "
1448 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
1449 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
1450 "caption VARCHAR, description VARCHAR, license VARCHAR, sha1sum CHAR(40), "
1451 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
1452 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
1453 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, aspect_ratio REAL)",
1454 "[init] can't create table i\n");
1455 TRY_EXEC(
"INSERT INTO i SELECT id, group_id, film_id, width, height, filename, maker, model,"
1456 " lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1457 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1458 " raw_auto_bright_threshold, raw_black, raw_maximum, caption, description, license, sha1sum,"
1459 " orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version,"
1460 " max_version, write_timestamp, history_end, position, aspect_ratio "
1462 "[init] can't populate table h\n");
1464 "[init] can't drop table images\n");
1465 TRY_EXEC(
"ALTER TABLE i RENAME TO images",
1466 "[init] can't rename i to images\n");
1468 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1472 else if(version == 22)
1474 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1476 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_group_id_index ON images (group_id)",
1477 "[init] can't create group_id index on image\n");
1478 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_film_id_index ON images (film_id)",
1479 "[init] can't create film_id index on image\n");
1480 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_filename_index ON images (filename)",
1481 "[init] can't create filename index on image\n");
1482 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.image_position_index ON images (position)",
1483 "[init] can't create position index on image\n");
1485 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.film_rolls_folder_index ON film_rolls (folder)",
1486 "[init] can't create folder index on film_rolls\n");
1488 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1492 else if(version == 23)
1494 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1496 TRY_EXEC(
"CREATE TABLE main.history_hash (imgid INTEGER PRIMARY KEY, "
1497 "basic_hash BLOB, auto_hash BLOB, current_hash BLOB)",
1498 "[init] can't create table history_hash\n");
1503 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1507 else if(version == 24)
1510 TRY_EXEC(
"ALTER TABLE main.history_hash ADD COLUMN mipmap_hash BLOB",
1511 "[init] can't add `mipmap_hash' column to history_hash table in database\n");
1516 else if(version == 25)
1519 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN exposure_bias REAL",
1520 "[init] can't add `exposure_bias' column to images table in database\n");
1525 else if(version == 26)
1527 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1529 TRY_EXEC(
"CREATE TABLE main.new_film_rolls "
1530 "(id INTEGER PRIMARY KEY, "
1531 "access_timestamp INTEGER, "
1532 "folder VARCHAR(1024) NOT NULL)",
1533 "[init] can't create new_film_rolls table\n");
1535 TRY_EXEC(
"INSERT INTO main.new_film_rolls"
1536 "(id, access_timestamp, folder) "
1538 "strftime('%s', replace(substr(datetime_accessed, 1, 10), ':', '-') || substr(datetime_accessed, 11), 'utc'), "
1541 "WHERE folder IS NOT NULL",
1542 "[init] can't populate new_film_rolls table from film_rolls\n");
1545 "[init] can't delete table film_rolls\n");
1547 TRY_EXEC(
"ALTER TABLE main.new_film_rolls RENAME TO film_rolls",
1548 "[init] can't rename table new_film_rolls to film_rolls\n");
1550 TRY_EXEC(
"CREATE INDEX main.film_rolls_folder_index ON film_rolls (folder)",
1551 "[init] can't create index `film_rolls_folder_index' on table `film_rolls'\n");
1553 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1556 else if(version == 27)
1558 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1560 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN import_timestamp INTEGER DEFAULT -1",
1561 "[init] can't add `import_timestamp' column to images table in database\n");
1562 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN change_timestamp INTEGER DEFAULT -1",
1563 "[init] can't add `change_timestamp' column to images table in database\n");
1564 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN export_timestamp INTEGER DEFAULT -1",
1565 "[init] can't add `export_timestamp' column to images table in database\n");
1566 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN print_timestamp INTEGER DEFAULT -1",
1567 "[init] can't add `print_timestamp' column to images table in database\n");
1569 TRY_EXEC(
"UPDATE main.images SET import_timestamp = (SELECT access_timestamp "
1570 "FROM main.film_rolls WHERE film_rolls.id = images.film_id)",
1571 "[init] can't populate import_timestamp column from film_rolls.access_timestamp.\n");
1573 TRY_EXEC(
"UPDATE main.images SET change_timestamp = images.write_timestamp "
1574 "WHERE images.write_timestamp IS NOT NULL "
1575 "AND images.id = (SELECT imgid FROM tagged_images "
1576 "JOIN data.tags ON tags.id = tagged_images.tagid "
1577 "WHERE data.tags.name = 'darktable|changed')",
1578 "[init] can't populate change_timestamp column from images.write_timestamp.\n");
1580 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1583 else if(version == 28)
1585 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1588 TRY_EXEC(
"UPDATE main.images SET flags = (flags & ~8)",
1589 "[init] can't clear rejected flags");
1592 TRY_EXEC(
"UPDATE main.images SET flags = (flags | 8) & ~7 WHERE (flags & 7) = 6",
1593 "[init] can't set rejected flags");
1595 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1598 else if(version == 29)
1600 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1603 TRY_EXEC(
"ALTER TABLE main.tagged_images ADD COLUMN position INTEGER",
1604 "[init] can't add `position' column to tagged_images table in database\n");
1606 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.tagged_images_imgid_index ON tagged_images (imgid)",
1607 "[init] can't create image index on tagged_images\n");
1608 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.tagged_images_position_index ON tagged_images (position)",
1609 "[init] can't create position index on tagged_images\n");
1610 TRY_EXEC(
"UPDATE main.tagged_images SET position = (tagid + imgid) << 32",
1611 "[init] can't populate position on tagged_images\n");
1615 TRY_EXEC(
"CREATE TABLE main.i (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1616 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
1617 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
1618 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
1619 "output_width INTEGER, output_height INTEGER, crop REAL, "
1620 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
1621 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
1622 "license VARCHAR, sha1sum CHAR(40), "
1623 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
1624 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
1625 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, "
1626 "aspect_ratio REAL, exposure_bias REAL, "
1627 "import_timestamp INTEGER DEFAULT -1, change_timestamp INTEGER DEFAULT -1, "
1628 "export_timestamp INTEGER DEFAULT -1, print_timestamp INTEGER DEFAULT -1)",
1629 "[init] can't create table i\n");
1631 TRY_EXEC(
"INSERT INTO main.i SELECT id, group_id, film_id, width, height, filename, maker, model,"
1632 " lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1633 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1634 " raw_auto_bright_threshold, raw_black, raw_maximum, license, sha1sum,"
1635 " orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version,"
1636 " max_version, write_timestamp, history_end, position, aspect_ratio, exposure_bias,"
1637 " import_timestamp, change_timestamp, export_timestamp, print_timestamp "
1639 "[init] can't populate table i\n");
1641 "[init] can't drop table images\n");
1642 TRY_EXEC(
"ALTER TABLE main.i RENAME TO images",
1643 "[init] can't rename i to images\n");
1645 TRY_EXEC(
"CREATE INDEX main.images_group_id_index ON images (group_id)",
1646 "[init] can't create group_id index on images table\n");
1647 TRY_EXEC(
"CREATE INDEX main.images_film_id_index ON images (film_id)",
1648 "[init] can't create film_id index on images table\n");
1649 TRY_EXEC(
"CREATE INDEX main.images_filename_index ON images (filename)",
1650 "[init] can't create filename index on images table\n");
1651 TRY_EXEC(
"CREATE INDEX main.image_position_index ON images (position)",
1652 "[init] can't create position index on images table\n");
1654 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1657 else if(version == 30)
1659 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1662 TRY_EXEC(
"DROP INDEX IF EXISTS `history_imgid_index`",
1663 "[init] can't drop history_imgid_index\n");
1664 TRY_EXEC(
"CREATE INDEX `history_imgid_index` ON `history` ( `imgid`, `operation` )",
1665 "[init] can't recreate history_imgid_index\n");
1667 TRY_EXEC(
"DROP INDEX IF EXISTS `images_filename_index`",
1668 "[init] can't drop images_filename_index\n");
1669 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
1670 "[init] can't recreate images_filename_index\n");
1672 TRY_EXEC(
"DROP INDEX IF EXISTS `images_film_id_index`",
1673 "[init] can't drop images_film_id_index\n");
1674 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
1675 "[init] can't recreate images_film_id_index\n");
1677 TRY_EXEC(
"DROP INDEX IF EXISTS `images_group_id_index`",
1678 "[init] can't drop images_group_id_index\n");
1679 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
1680 "[init] can't recreate images_group_id_index\n");
1682 TRY_EXEC(
"DROP INDEX IF EXISTS `masks_history_imgid_index`",
1683 "[init] can't drop masks_history_imgid_index\n");
1684 TRY_EXEC(
"CREATE INDEX `masks_history_imgid_index` ON `masks_history` ( `imgid`, `num` )",
1685 "[init] can't recreate masks_history_imgid_index\n");
1688 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( `latitude` DESC, `longitude` DESC )",
1689 "[init] can't create images_latlong_index\n");
1691 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1694 else if(version == 31)
1696 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1699 TRY_EXEC(
"DELETE FROM main.meta_data WHERE rowid NOT IN (SELECT MIN(rowid) "
1700 "FROM main.meta_data GROUP BY id, key)",
1701 "[init] can't remove duplicates from meta_data\n");
1704 TRY_EXEC(
"DROP INDEX IF EXISTS metadata_index",
1705 "[init] can't drop metadata_index\n");
1706 TRY_EXEC(
"CREATE UNIQUE INDEX main.metadata_index ON meta_data (id, key)",
1707 "[init] can't create metadata_index\n");
1709 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1712 else if(version == 32)
1714 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1722 TRY_EXEC(
"ALTER TABLE `images` RENAME TO `images_old`",
1723 "[init] can't rename images\n");
1725 TRY_EXEC(
"CREATE TABLE `images` (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1726 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, lens VARCHAR, "
1727 "exposure REAL, aperture REAL, iso REAL, focal_length REAL, focus_distance REAL, datetime_taken CHAR(20), "
1728 "flags INTEGER, output_width INTEGER, output_height INTEGER, crop REAL, "
1729 "raw_parameters INTEGER, raw_denoise_threshold REAL, raw_auto_bright_threshold REAL, "
1730 "raw_black INTEGER, raw_maximum INTEGER, license VARCHAR, sha1sum CHAR(40), "
1731 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, latitude REAL, altitude REAL, "
1732 "color_matrix BLOB, colorspace INTEGER, version INTEGER, max_version INTEGER, write_timestamp INTEGER, "
1733 "history_end INTEGER, position INTEGER, aspect_ratio REAL, exposure_bias REAL, "
1734 "import_timestamp INTEGER DEFAULT -1, change_timestamp INTEGER DEFAULT -1, export_timestamp INTEGER DEFAULT -1, print_timestamp INTEGER DEFAULT -1, "
1735 "FOREIGN KEY(film_id) REFERENCES film_rolls(id) ON DELETE CASCADE ON UPDATE CASCADE, "
1736 "FOREIGN KEY(group_id) REFERENCES images(id) ON DELETE RESTRICT ON UPDATE CASCADE)",
1737 "[init] can't create new images table\n");
1740 TRY_EXEC(
"DELETE FROM `images_old` WHERE film_id NOT IN (SELECT id FROM `film_rolls`)",
1741 "[init] can't delete images with invalid film id\n");
1743 TRY_EXEC(
"UPDATE `images_old` SET group_id=id WHERE group_id NOT IN (SELECT id from `images_old`)",
1744 "[init] can't fix invalid group ids\n");
1746 TRY_EXEC(
"INSERT INTO `images` SELECT * FROM `images_old`",
1747 "[init] can't copy back from images_old\n");
1750 TRY_EXEC(
"DROP INDEX IF EXISTS `image_position_index`",
1751 "[init] can't drop image_position_index\n");
1752 TRY_EXEC(
"CREATE INDEX `image_position_index` ON `images` (position)",
1753 "[init] can't add image_position_index\n");
1756 TRY_EXEC(
"DROP INDEX IF EXISTS `images_filename_index`",
1757 "[init] can't drop images_filename_index\n");
1758 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
1759 "[init] can't recreate images_filename_index\n");
1761 TRY_EXEC(
"DROP INDEX IF EXISTS `images_film_id_index`",
1762 "[init] can't drop images_film_id_index\n");
1763 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
1764 "[init] can't recreate images_film_id_index\n");
1766 TRY_EXEC(
"DROP INDEX IF EXISTS `images_group_id_index`",
1767 "[init] can't drop images_group_id_index\n");
1768 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
1769 "[init] can't recreate images_group_id_index\n");
1771 TRY_EXEC(
"DROP INDEX IF EXISTS `images_latlong_index`",
1772 "[init] can't drop images_latlong_index\n");
1773 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( latitude DESC, longitude DESC )",
1774 "[init] can't add images_latlong_index\n");
1776 TRY_EXEC(
"DROP TABLE `images_old`",
1777 "[init] can't drop table images_old\n");
1780 TRY_EXEC(
"ALTER TABLE `history` RENAME TO `history_old`",
1781 "[init] can't rename history\n");
1783 TRY_EXEC(
"CREATE TABLE `history` (imgid INTEGER, num INTEGER, module INTEGER, "
1784 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, "
1785 "multi_priority INTEGER, multi_name VARCHAR(256), "
1786 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1787 "[init] can't create new history table\n");
1789 TRY_EXEC(
"DELETE FROM `history_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1790 "[init] can't delete orphaned history elements\n");
1792 TRY_EXEC(
"INSERT INTO history SELECT * FROM history_old",
1793 "[init] can't copy back from history_old\n");
1795 TRY_EXEC(
"DROP INDEX IF EXISTS `history_imgid_index`",
1796 "[init] can't drop history_imgid_index\n");
1797 TRY_EXEC(
"CREATE INDEX `history_imgid_op_index` ON `history` ( `imgid`, `operation` )",
1798 "[init] can't recreate history_imgid_index\n");
1799 TRY_EXEC(
"CREATE INDEX `history_imgid_num_index` ON `history` ( `imgid`, `num` DESC )",
1800 "[init] can't recreate history_imgid_index\n");
1802 TRY_EXEC(
"DROP TABLE `history_old`",
1803 "[init] can't drop table history_old\n");
1806 TRY_EXEC(
"ALTER TABLE `history_hash` RENAME TO `history_hash_old`",
1807 "[init] can't rename history_hash\n");
1809 TRY_EXEC(
"CREATE TABLE `history_hash` (imgid INTEGER PRIMARY KEY, basic_hash BLOB, auto_hash BLOB, current_hash BLOB, "
1810 "mipmap_hash BLOB, FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1811 "[init] can't create new history_hash table\n");
1813 TRY_EXEC(
"DELETE FROM `history_hash_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1814 "[init] can't delete orphaned history_hash elements\n");
1816 TRY_EXEC(
"INSERT INTO `history_hash` SELECT * FROM `history_hash_old`",
1817 "[init] can't copy back from history_hash_old\n");
1819 TRY_EXEC(
"DROP TABLE `history_hash_old`",
1820 "[init] can't drop table history_hash_old\n");
1823 TRY_EXEC(
"ALTER TABLE `tagged_images` RENAME TO `tagged_images_old`",
1824 "[init] can't rename tagged_images\n");
1826 TRY_EXEC(
"CREATE TABLE `tagged_images` (imgid integer, tagid integer, position INTEGER, "
1827 "primary key(imgid, tagid), FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1828 "[init] can't create new tagged_images table\n");
1830 TRY_EXEC(
"DELETE FROM `tagged_images_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1831 "[init] can't delete orphaned tagged_images elements\n");
1833 TRY_EXEC(
"INSERT INTO `tagged_images` SELECT * FROM `tagged_images_old`",
1834 "[init] can't copy back from tagged_images_old\n");
1837 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_imgid_index",
1838 "[init] can't drop tagged_images_imgid_index\n");
1839 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_position_index",
1840 "[init] can't drop tagged_images_position_index\n");
1841 TRY_EXEC(
"CREATE INDEX tagged_images_position_index ON tagged_images (position)",
1842 "[init] can't add index tagged_images_position_index\n");
1843 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_tagid_index",
1844 "[init] can't drop tagged_images_tagid_index\n");
1845 TRY_EXEC(
"CREATE INDEX tagged_images_tagid_index ON tagged_images (tagid)",
1846 "[init] can't add index tagged_images_tagid_index\n");
1848 TRY_EXEC(
"DROP TABLE `tagged_images_old`",
1849 "[init] can't drop table tagged_images_old\n");
1852 TRY_EXEC(
"ALTER TABLE `masks_history` RENAME TO `masks_history_old`",
1853 "[init] can't rename masks_history\n");
1855 TRY_EXEC(
"CREATE TABLE masks_history (imgid INTEGER, num INTEGER, formid INTEGER, form INTEGER, "
1856 "name VARCHAR(256), version INTEGER, points BLOB, points_count INTEGER, source BLOB, "
1857 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1858 "[init] can't create new masks_history table\n");
1860 TRY_EXEC(
"DELETE FROM `masks_history_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1861 "[init] can't delete orphaned masks_history elements\n");
1863 TRY_EXEC(
"INSERT INTO `masks_history` SELECT * FROM `masks_history_old`",
1864 "[init] can't copy back from masks_history\n");
1866 TRY_EXEC(
"DROP INDEX IF EXISTS `masks_history_imgid_index`",
1867 "[init] can't drop masks_history_imgid_index\n");
1868 TRY_EXEC(
"CREATE INDEX `masks_history_imgid_index` ON `masks_history` ( imgid, num )",
1869 "[init] can't recreate masks_history_imgid_index\n");
1871 TRY_EXEC(
"DROP TABLE masks_history_old",
1872 "[init] can't drop table masks_history_old\n");
1875 TRY_EXEC(
"ALTER TABLE `color_labels` RENAME TO `color_labels_old`",
1876 "[init] can't rename color_labels\n");
1877 TRY_EXEC(
"CREATE TABLE `color_labels` (imgid INTEGER, color INTEGER, "
1878 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1879 "[init] can't create new color_labels table\n");
1881 TRY_EXEC(
"DELETE FROM `color_labels_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1882 "[init] can't delete orphaned color_labels elements\n");
1884 TRY_EXEC(
"INSERT INTO `color_labels` SELECT * FROM `color_labels_old`",
1885 "[init] can't copy back from color_labels\n");
1887 TRY_EXEC(
"DROP TABLE color_labels_old",
1888 "[init] can't drop table color_labels_old\n");
1890 TRY_EXEC(
"CREATE UNIQUE INDEX `color_labels_idx` ON `color_labels` (imgid, color)",
1891 "[init] can't recreate color_labels_idx\n");
1894 TRY_EXEC(
"ALTER TABLE `meta_data` RENAME TO `meta_data_old`",
1895 "[init] can't rename meta_data\n");
1896 TRY_EXEC(
"CREATE TABLE `meta_data` (id integer, key integer, value varchar, "
1897 "FOREIGN KEY(id) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1898 "[init] can't create new meta_data table\n");
1900 TRY_EXEC(
"DELETE FROM `meta_data_old` WHERE id NOT IN (SELECT id FROM `images`)",
1901 "[init] can't delete orphaned meta_data elements\n");
1903 TRY_EXEC(
"INSERT INTO `meta_data` SELECT * FROM `meta_data_old`",
1904 "[init] can't copy back from meta_data\n");
1906 TRY_EXEC(
"DROP TABLE meta_data_old",
1907 "[init] can't drop table meta_data_old\n");
1909 TRY_EXEC(
"CREATE UNIQUE INDEX `metadata_index` ON `meta_data` (id, key, value)",
1910 "[init] can't recreate metadata_index\n");
1913 TRY_EXEC(
"ALTER TABLE `selected_images` RENAME TO `selected_images_old`",
1914 "[init] can't rename selected_images\n");
1915 TRY_EXEC(
"CREATE TABLE `selected_images` (imgid INTEGER PRIMARY KEY, "
1916 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1917 "[init] can't create new selected_images table\n");
1919 TRY_EXEC(
"DELETE FROM `selected_images_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1920 "[init] can't delete orphaned selected_images elements\n");
1922 TRY_EXEC(
"INSERT INTO `selected_images` SELECT * FROM `selected_images_old`",
1923 "[init] can't copy back selected_images meta_data\n");
1925 TRY_EXEC(
"DROP TABLE selected_images_old",
1926 "[init] can't drop table selected_images_old\n");
1929 TRY_EXEC(
"ALTER TABLE `module_order` RENAME TO `module_order_old`",
1930 "[init] can't rename module_order\n");
1931 TRY_EXEC(
"CREATE TABLE `module_order` (imgid INTEGER PRIMARY KEY, version INTEGER, iop_list VARCHAR, "
1932 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1933 "[init] can't create new module_order table\n");
1935 TRY_EXEC(
"DELETE FROM `module_order_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1936 "[init] can't delete orphaned module_order elements\n");
1938 TRY_EXEC(
"INSERT INTO `module_order` SELECT * FROM `module_order_old`",
1939 "[init] can't copy back module_order meta_data\n");
1941 TRY_EXEC(
"DROP TABLE module_order_old",
1942 "[init] can't drop table module_order_old\n");
1944 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1947 else if(version == 33)
1949 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1951 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_datetime_taken_nc ON images (datetime_taken COLLATE NOCASE)",
1952 "[init] can't create images_datetime_taken\n");
1953 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.metadata_index_key ON meta_data (key)",
1954 "[init] can't create metadata_index_key\n");
1956 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1959 else if(version == 34)
1961 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
1962 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1964 TRY_EXEC(
"CREATE TABLE main.images_new (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1965 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
1966 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
1967 "focus_distance REAL, datetime_taken INTEGER, flags INTEGER, "
1968 "output_width INTEGER, output_height INTEGER, crop REAL, "
1969 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
1970 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
1971 "license VARCHAR, sha1sum CHAR(40), "
1972 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
1973 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
1974 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, "
1975 "aspect_ratio REAL, exposure_bias REAL, "
1976 "import_timestamp INTEGER, change_timestamp INTEGER, "
1977 "export_timestamp INTEGER, print_timestamp INTEGER, "
1978 "FOREIGN KEY(film_id) REFERENCES film_rolls(id) ON DELETE CASCADE ON UPDATE CASCADE, "
1979 "FOREIGN KEY(group_id) REFERENCES images(id) ON DELETE RESTRICT ON UPDATE CASCADE)",
1980 "[init] can't create new images table\n");
1982 TRY_EXEC(
"INSERT INTO `images_new` SELECT "
1983 "id, group_id, film_id, width, height, filename, maker, model, "
1984 "lens, exposure, aperture, iso, focal_length, focus_distance, NULL AS datetime_taken, flags, "
1985 "output_width, output_height, crop, raw_parameters, raw_denoise_threshold, raw_auto_bright_threshold, raw_black, raw_maximum, "
1986 "license, sha1sum, orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version, "
1987 "max_version, write_timestamp, history_end, position, aspect_ratio, exposure_bias, "
1988 "NULL AS import_timestamp, NULL AS change_timestamp, NULL AS export_timestamp, NULL AS print_timestamp "
1990 "[init] can't copy back from images\n");
1993 " CASE WHEN datetime_taken = '' THEN NULL ELSE datetime_taken END,"
1994 " CASE WHEN import_timestamp = -1 THEN NULL ELSE import_timestamp END,"
1995 " CASE WHEN change_timestamp = -1 THEN NULL ELSE change_timestamp END,"
1996 " CASE WHEN export_timestamp = -1 THEN NULL ELSE export_timestamp END,"
1997 " CASE WHEN print_timestamp = -1 THEN NULL ELSE print_timestamp END "
1999 "[init] can't get datetime from images\n");
2000 while(sqlite3_step(stmt) == SQLITE_ROW)
2002 sqlite3_stmt *stmt2;
2003 sqlite3_prepare_v2(db->
handle,
2004 "UPDATE `images_new` SET"
2005 " (datetime_taken, import_timestamp,"
2006 " change_timestamp, export_timestamp, print_timestamp) = "
2007 " (?2, ?3, ?4, ?5, ?6) WHERE id = ?1",
2009 sqlite3_bind_int(stmt2, 1, sqlite3_column_int(stmt, 0));
2010 if(sqlite3_column_type(stmt, 1) != SQLITE_NULL)
2016 g_date_time_unref(gdt);
2019 for(
int i = 0;
i < 4;
i++)
2021 if(sqlite3_column_type(stmt,
i + 2) != SQLITE_NULL)
2023 GDateTime *gdt = g_date_time_new_from_unix_utc(sqlite3_column_int(stmt,
i + 2));
2027 g_date_time_unref(gdt);
2031 TRY_STEP(stmt2, SQLITE_DONE,
"[init] can't update datetimes into images_new table\n");
2032 sqlite3_finalize(stmt2);
2034 sqlite3_finalize(stmt);
2036 TRY_EXEC(
"DROP TABLE `images`",
"[init] can't drop images table\n");
2038 TRY_EXEC(
"ALTER TABLE `images_new` RENAME TO `images`",
"[init] can't rename images_new table to images");
2041 TRY_EXEC(
"CREATE INDEX `image_position_index` ON `images` (position)",
2042 "[init] can't add image_position_index\n");
2043 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
2044 "[init] can't recreate images_filename_index\n");
2045 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
2046 "[init] can't recreate images_film_id_index\n");
2047 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
2048 "[init] can't recreate images_group_id_index\n");
2049 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( latitude DESC, longitude DESC )",
2050 "[init] can't add images_latlong_index\n");
2051 TRY_EXEC(
"CREATE INDEX `images_datetime_taken` ON images (datetime_taken)",
2052 "[init] can't create images_datetime_taken\n");
2054 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
2055 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = ON", NULL, NULL, NULL);
2058 else if(version == 35)
2060 TRY_EXEC(
"CREATE TABLE main.images_new (id INTEGER, filename VARCHAR, flags INTEGER)",
2061 "[init] can't create new images table\n");
2063 gchar *query = g_strdup_printf(
"INSERT INTO `images_new` "
2064 "SELECT id, filename, flags"
2066 " WHERE (flags & %d == 0)",
2068 TRY_EXEC(query,
"[init] can't copy back from images\n");
2070 TRY_PREPARE(stmt,
"SELECT id, filename, flags FROM `images_new`",
2071 "[init] can't prepare selecting images flags\n");
2073 while(sqlite3_step(stmt) == SQLITE_ROW)
2075 sqlite3_stmt *stmt2;
2076 sqlite3_prepare_v2(db->
handle,
2077 "UPDATE `images` SET"
2079 " (?2) WHERE id = ?1",
2081 sqlite3_bind_int(stmt2, 1, sqlite3_column_int(stmt, 0));
2084 gchar *ext = g_strrstr((
const char *)sqlite3_column_text(stmt, 1),
".");
2086 sqlite3_bind_int(stmt2, 2,
flags);
2088 TRY_STEP(stmt2, SQLITE_DONE,
"[init] can't update flags\n");
2089 sqlite3_finalize(stmt2);
2091 sqlite3_finalize(stmt);
2093 TRY_EXEC(
"DROP TABLE `images_new`",
"[init] can't drop temp images table\n");
2097 new_version = version;
2100 sqlite3_prepare_v2(db->
handle,
"INSERT OR REPLACE INTO main.db_info (key, value) VALUES ('version', ?1)", -1, &stmt,
2102 sqlite3_bind_int(stmt, 1, new_version);
2104 sqlite3_finalize(stmt);
3011 sqlite3_config(SQLITE_CONFIG_SERIALIZED);
3013 sqlite3_initialize();
3026 gchar *dbname = NULL;
3027 gchar dbfilename_library[
PATH_MAX] = { 0 };
3037 else if(!strcmp(dbname,
":memory:"))
3038 g_strlcpy(dbfilename_library, dbname,
sizeof(dbfilename_library));
3039 else if(dbname[0] !=
'/')
3042 g_strlcpy(dbfilename_library, dbname,
sizeof(dbfilename_library));
3046 g_strlcpy(dbfilename_library, alternative,
sizeof(dbfilename_library));
3048 GFile *galternative = g_file_new_for_path(alternative);
3049 dbname = g_file_get_basename(galternative);
3050 g_object_unref(galternative);
3054 char dbfilename_data[
PATH_MAX] = { 0 };
3058 snprintf(dbfilename_data,
sizeof(dbfilename_data),
":memory:");
3071 if(g_strcmp0(dbfilename_data,
":memory:"))
3073 char *data_path = g_path_get_dirname(dbfilename_data);
3074 g_mkdir_with_parents(data_path, 0750);
3078 if(g_strcmp0(dbfilename_library,
":memory:"))
3080 char *library_path = g_path_get_dirname(dbfilename_library);
3081 g_mkdir_with_parents(library_path, 0750);
3086 dt_print(
DT_DEBUG_SQL,
"[init sql] library: %s, data: %s\n", dbfilename_library, dbfilename_data);
3094 fprintf(stderr,
"[init] database is locked, probably another process is already using it\n");
3102 fprintf(stderr,
"[init] could not find database ");
3104 fprintf(stderr,
"`%s'!\n", dbname);
3106 fprintf(stderr,
"\n");
3107 fprintf(stderr,
"[init] maybe your %s/anselrc is corrupt?\n", datadir);
3109 fprintf(stderr,
"[init] try `cp %s/anselrc %s/anselrc'\n", dbfilename_library, datadir);
3110 sqlite3_close(db->
handle);
3125 if(sqlite3_db_readonly(db->
handle,
"main") != 0)
3130 GtkDialogFlags dflags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
3132 dialog = gtk_dialog_new_with_buttons(_(
"Ansel - Database is read-only"),
3134 _(
"Close Ansel"), GTK_RESPONSE_CLOSE, NULL);
3135 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
3137 = g_strdup_printf(_(
"<span weight='bold'>Ansel library database is read-only</span>\n\n"
3138 "This happens if you don't have permissions to write on the filesystem\n"
3139 "or if you have restored a write-protected backup snapshot.\n\n"
3140 "Please change the filesystem access permissions for:\n\n"
3141 "\t<span style='italic'>%s</span>"),
3144 GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG (dialog));
3146 gtk_label_set_markup(GTK_LABEL(label), label_options);
3147 gtk_container_add(GTK_CONTAINER (content_area), label);
3148 gtk_widget_show_all(content_area);
3149 gtk_dialog_run(GTK_DIALOG(dialog));
3150 gtk_widget_destroy(dialog);
3163 sqlite3_exec(db->
handle,
"attach database ':memory:' as memory", NULL, NULL, NULL);
3166 gboolean have_data_db = load_data && g_file_test(dbfilename_data, G_FILE_TEST_EXISTS);
3167 rc = sqlite3_prepare_v2(db->
handle,
"ATTACH DATABASE ?1 AS data", -1, &stmt, NULL);
3168 sqlite3_bind_text(stmt, 1, dbfilename_data, -1, SQLITE_TRANSIENT);
3169 if(rc != SQLITE_OK || sqlite3_step(stmt) != SQLITE_DONE)
3171 sqlite3_finalize(stmt);
3172 fprintf(stderr,
"[init] database `%s' couldn't be opened. aborting\n", dbfilename_data);
3177 sqlite3_finalize(stmt);
3182 sqlite3_exec(db->
handle,
"PRAGMA synchronous = OFF", NULL, NULL, NULL);
3183 sqlite3_exec(db->
handle,
"PRAGMA journal_mode = MEMORY", NULL, NULL, NULL);
3184 sqlite3_exec(db->
handle,
"PRAGMA page_size = 32768", NULL, NULL, NULL);
3188 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = ON", NULL, NULL, NULL);
3203 rc = sqlite3_prepare_v2(db->
handle,
"select value from data.db_info where key = 'version'", -1, &stmt, NULL);
3204 if(!g_strcmp0(data_status,
"ok") && rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW)
3209 const int db_version = sqlite3_column_int(stmt, 0);
3210 sqlite3_finalize(stmt);
3219 fprintf(stderr,
"[init] database `%s' couldn't be upgraded from version %d to %d. aborting\n",
3229 fprintf(stderr,
"[init] database version of `%s' is too new for this build of darktable. aborting\n",
3242 gchar* quick_check_text = NULL;
3243 if(g_strcmp0(data_status,
"ok"))
3245 quick_check_text = g_strdup_printf(_(
"quick_check said:\n"
3246 "%s \n"), data_status);
3250 quick_check_text = g_strdup(
"");
3256 GtkDialogFlags dflags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
3258 const char* label_options = NULL;
3262 dialog = gtk_dialog_new_with_buttons(_(
"ansel - error opening database"),
3267 _(
"attempt restore"),
3268 GTK_RESPONSE_ACCEPT,
3269 _(
"delete database"),
3270 GTK_RESPONSE_REJECT,
3272 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
3273 label_options = _(
"do you want to close Ansel now to manually restore\n"
3274 "the database from a backup, attempt an automatic restore\n"
3275 "from the most recent snapshot or delete the corrupted database\n"
3276 "and start with a new one?");
3280 dialog = gtk_dialog_new_with_buttons(_(
"ansel - error opening database"),
3285 _(
"delete database"),
3286 GTK_RESPONSE_REJECT,
3288 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
3289 label_options = _(
"do you want to close Ansel now to manually restore\n"
3290 "the database from a backup or delete the corrupted database\n"
3291 "and start with a new one?");
3296 char *label_text = g_markup_printf_escaped(_(
"an error has occurred while trying to open the database from\n"
3298 "<span style='italic'>%s</span>\n"
3300 "it seems that the database is corrupted.\n"
3303 dbfilename_data, quick_check_text, label_options);
3308 GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG (dialog));
3310 gtk_label_set_markup(GTK_LABEL(label), label_text);
3312 gtk_container_add(GTK_CONTAINER (content_area), label);
3314 gtk_widget_show_all(content_area);
3316 const int resp = gtk_dialog_run(GTK_DIALOG(dialog));
3318 gtk_widget_destroy(dialog);
3323 if(resp != GTK_RESPONSE_ACCEPT && resp != GTK_RESPONSE_REJECT)
3325 fprintf(stderr,
"[init] database `%s' is corrupt and can't be opened! either replace it from a backup or "
3326 "delete the file so that darktable can create a new one the next time. aborting\n", dbfilename_data);
3333 fprintf(stderr,
"[init] deleting `%s' on user request", dbfilename_data);
3335 if(g_unlink(dbfilename_data) == 0)
3336 fprintf(stderr,
" ... ok\n");
3338 fprintf(stderr,
" ... failed\n");
3340 if(resp == GTK_RESPONSE_ACCEPT && data_snap)
3342 fprintf(stderr,
"[init] restoring `%s' from `%s'...", dbfilename_data, data_snap);
3343 GError *gerror = NULL;
3344 if(!g_file_test(dbfilename_data, G_FILE_TEST_EXISTS))
3346 GFile *src = g_file_new_for_path(data_snap);
3347 GFile *dest = g_file_new_for_path(dbfilename_data);
3348 gboolean copy_status =
TRUE;
3349 if(g_file_test(data_snap, G_FILE_TEST_EXISTS))
3351 copy_status = g_file_copy(src, dest, G_FILE_COPY_NONE, NULL, NULL, NULL, &gerror);
3352 if(copy_status) copy_status = g_chmod(dbfilename_data, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == 0;
3357 const int fd = g_open(dbfilename_data, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
3358 if(fd < 0 || !g_close(fd, &gerror)) copy_status =
FALSE;
3361 fprintf(stderr,
" success!\n");
3363 fprintf(stderr,
" failed!\n");
3365 g_object_unref(src);
3366 g_object_unref(dest);
3380 rc = sqlite3_prepare_v2(db->
handle,
"select value from main.db_info where key = 'version'", -1, &stmt, NULL);
3381 if(!g_strcmp0(libdb_status,
"ok") && rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW)
3386 const int db_version = sqlite3_column_int(stmt, 0);
3387 sqlite3_finalize(stmt);
3397 fprintf(stderr,
"[init] database `%s' couldn't be upgraded from version %d to %d. aborting\n", dbname,
3407 fprintf(stderr,
"[init] database version of `%s' is too new for this build of darktable. aborting\n",
3415 else if(g_strcmp0(libdb_status,
"ok") || rc == SQLITE_CORRUPT || rc == SQLITE_NOTADB)
3420 gchar* quick_check_text = NULL;
3421 if(g_strcmp0(libdb_status,
"ok"))
3423 quick_check_text = g_strdup_printf(_(
"quick_check said:\n"
3424 "%s \n"), libdb_status);
3428 quick_check_text = g_strdup(
"");
3434 GtkDialogFlags dflags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
3436 const char* label_options = NULL;
3440 dialog = gtk_dialog_new_with_buttons(_(
"ansel - error opening database"),
3445 _(
"attempt restore"),
3446 GTK_RESPONSE_ACCEPT,
3447 _(
"delete database"),
3448 GTK_RESPONSE_REJECT,
3450 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
3451 label_options = _(
"do you want to close Ansel now to manually restore\n"
3452 "the database from a backup, attempt an automatic restore\n"
3453 "from the most recent snapshot or delete the corrupted database\n"
3454 "and start with a new one?");
3458 dialog = gtk_dialog_new_with_buttons(_(
"ansel - error opening database"),
3463 _(
"delete database"),
3464 GTK_RESPONSE_REJECT,
3466 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
3467 label_options = _(
"do you want to close Ansel now to manually restore\n"
3468 "the database from a backup or delete the corrupted database\n"
3469 "and start with a new one?");
3472 char *label_text = g_markup_printf_escaped(_(
"an error has occurred while trying to open the database from\n"
3474 "<span style='italic'>%s</span>\n"
3476 "it seems that the database is corrupted.\n"
3479 dbfilename_data, quick_check_text, label_options);
3484 GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG (dialog));
3486 gtk_label_set_markup(GTK_LABEL(label), label_text);
3488 gtk_container_add(GTK_CONTAINER (content_area), label);
3490 gtk_widget_show_all(content_area);
3492 const int resp = gtk_dialog_run(GTK_DIALOG(dialog));
3494 gtk_widget_destroy(dialog);
3499 if(resp != GTK_RESPONSE_ACCEPT && resp != GTK_RESPONSE_REJECT)
3501 fprintf(stderr,
"[init] database `%s' is corrupt and can't be opened! either replace it from a backup or "
3502 "delete the file so that darktable can create a new one the next time. aborting\n", dbfilename_library);
3509 fprintf(stderr,
"[init] deleting `%s' on user request", dbfilename_library);
3511 if(g_unlink(dbfilename_library) == 0)
3512 fprintf(stderr,
" ... ok\n");
3514 fprintf(stderr,
" ... failed\n");
3516 if(resp == GTK_RESPONSE_ACCEPT && data_snap)
3518 fprintf(stderr,
"[init] restoring `%s' from `%s'...", dbfilename_library, data_snap);
3519 GError *gerror = NULL;
3520 if(!g_file_test(dbfilename_library, G_FILE_TEST_EXISTS))
3522 GFile *src = g_file_new_for_path(data_snap);
3523 GFile *dest = g_file_new_for_path(dbfilename_library);
3524 gboolean copy_status =
TRUE;
3525 if(g_file_test(data_snap, G_FILE_TEST_EXISTS))
3527 copy_status = g_file_copy(src, dest, G_FILE_COPY_NONE, NULL, NULL, NULL, &gerror);
3528 if(copy_status) copy_status = g_chmod(dbfilename_library, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == 0;
3533 const int fd = g_open(dbfilename_library, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
3534 if(fd < 0 || !g_close(fd, &gerror)) copy_status =
FALSE;
3537 fprintf(stderr,
" success!\n");
3539 fprintf(stderr,
" failed!\n");
3540 g_object_unref(src);
3541 g_object_unref(dest);
3551 sqlite3_finalize(stmt);
3552 rc = sqlite3_prepare_v2(db->
handle,
"select settings from main.settings", -1, &stmt, NULL);
3553 if(rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW)
3556 const void *set = sqlite3_column_blob(stmt, 0);
3557 const int db_version = *(
int *)set;
3558 sqlite3_finalize(stmt);
3563 fprintf(stderr,
"[init] database `%s' couldn't be migrated from the legacy version %d. aborting\n",
3564 dbname, db_version);
3572 fprintf(stderr,
"[init] database `%s' couldn't be upgraded from version 1 to %d. aborting\n", dbname,
3581 sqlite3_finalize(stmt);
3599 sqlite3_exec(db->
handle,
"drop table main.settings", NULL, NULL, NULL);
3607 rc = sqlite3_prepare_v2(db->
handle,
3608 "SELECT icu_load_collation('en_US', 'english')",
3610 sqlite3_finalize(stmt);
3616 fprintf(stderr,
"[sqlite] init icu extension error %d\n", rc);
4028 if(!g_strcmp0(config,
"never"))
4034 if(!g_strcmp0(config,
"on close"))
4041 GTimeSpan span_from_last_snap_required;
4043 if(!g_strcmp0(config,
"once a day"))
4045 span_from_last_snap_required = G_TIME_SPAN_DAY;
4047 else if(!g_strcmp0(config,
"once a week"))
4049 span_from_last_snap_required = G_TIME_SPAN_DAY * 7;
4051 else if(!g_strcmp0(config,
"once a month"))
4054 span_from_last_snap_required = G_TIME_SPAN_DAY * 30;
4059 dt_print(
DT_DEBUG_SQL,
"[db backup] invalid timespan requirement expecting never/on close/once a [day/week/month], got %s.\n", config);
4070 GFile *parent = g_file_get_parent(library);
4075 g_object_unref(library);
4079 GError *
error = NULL;
4080 GFileEnumerator *library_dir_files = g_file_enumerate_children(parent, G_FILE_ATTRIBUTE_STANDARD_NAME
"," G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4085 g_object_unref(parent);
4086 g_object_unref(library);
4087 g_error_free(
error);
4091 gchar *lib_basename = g_file_get_basename(library);
4092 g_object_unref(library);
4094 gchar *lib_snap_format = g_strdup_printf(
"%s-snp-", lib_basename);
4095 gchar *lib_backup_format = g_strdup_printf(
"%s-pre-", lib_basename);
4098 GFileInfo *info = NULL;
4099 guint64 last_snap = 0;
4101 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4103 const char* fname = g_file_info_get_name(info);
4104 if(g_str_has_prefix(fname, lib_snap_format) || g_str_has_prefix(fname, lib_backup_format))
4109 last_snap = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
4110 g_object_unref(info);
4113 const guint64 try_snap = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
4114 if(try_snap > last_snap)
4116 last_snap = try_snap;
4119 g_object_unref(info);
4121 g_object_unref(parent);
4128 g_file_enumerator_close(library_dir_files, NULL, NULL);
4129 g_object_unref(library_dir_files);
4130 g_error_free(
error);
4134 g_file_enumerator_close(library_dir_files, NULL, NULL);
4135 g_object_unref(library_dir_files);
4137 GDateTime *date_now = g_date_time_new_now_local();
4140 GDateTime *date_last_snap = g_date_time_new_from_unix_local(last_snap);
4142 gchar *now_txt = g_date_time_format(date_now,
"%Y%m%d%H%M%S");
4143 gchar *ls_txt = g_date_time_format(date_last_snap,
"%Y%m%d%H%M%S");
4148 GTimeSpan span_from_last_snap = g_date_time_difference(date_now, date_last_snap);
4150 g_date_time_unref(date_now);
4151 g_date_time_unref(date_last_snap);
4153 return span_from_last_snap > span_from_last_snap_required;
4242 GFile *lib_parent = g_file_get_parent(lib_file);
4247 g_object_unref(lib_file);
4252 GFile *dat_parent = g_file_get_parent(dat_file);
4257 g_object_unref(dat_file);
4258 g_object_unref(lib_file);
4259 g_object_unref(lib_parent);
4262 gchar *lib_basename = g_file_get_basename(lib_file);
4263 g_object_unref(lib_file);
4264 gchar *lib_snap_format = g_strdup_printf(
"%s-snp-", lib_basename);
4265 gchar *lib_tmp_format = g_strdup_printf(
"%s-tmp-", lib_basename);
4268 gchar *dat_basename = g_file_get_basename(dat_file);
4269 g_object_unref(dat_file);
4270 gchar *dat_snap_format = g_strdup_printf(
"%s-snp-", dat_basename);
4271 gchar *dat_tmp_format = g_strdup_printf(
"%s-tmp-", dat_basename);
4274 GQueue *lib_snaps = g_queue_new();
4275 GQueue *dat_snaps = g_queue_new();
4276 GQueue *tmplib_snaps = g_queue_new();
4277 GQueue *tmpdat_snaps = g_queue_new();
4279 if(g_file_equal(lib_parent, dat_parent))
4282 GError *
error = NULL;
4283 GFileEnumerator *library_dir_files = g_file_enumerate_children(lib_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4288 g_object_unref(lib_parent);
4289 g_object_unref(dat_parent);
4294 g_queue_free(lib_snaps);
4295 g_queue_free(dat_snaps);
4296 g_queue_free(tmplib_snaps);
4297 g_queue_free(tmpdat_snaps);
4298 g_error_free(
error);
4302 GFileInfo *info = NULL;
4304 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4306 const char* fname = g_file_info_get_name(info);
4307 if(g_str_has_prefix(fname, lib_snap_format))
4310 g_queue_insert_sorted(lib_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4312 else if(g_str_has_prefix(fname, dat_snap_format))
4315 g_queue_insert_sorted(dat_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4317 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4320 g_queue_push_head(tmplib_snaps, g_strdup(fname));
4322 g_object_unref(info);
4330 g_object_unref(lib_parent);
4331 g_object_unref(dat_parent);
4334 g_queue_free_full(lib_snaps, g_free);
4335 g_queue_free_full(dat_snaps, g_free);
4336 g_queue_free_full(tmplib_snaps, g_free);
4337 g_queue_free_full(tmpdat_snaps, g_free);
4338 g_file_enumerator_close(library_dir_files, NULL, NULL);
4339 g_object_unref(library_dir_files);
4340 g_error_free(
error);
4343 g_file_enumerator_close(library_dir_files, NULL, NULL);
4344 g_object_unref(library_dir_files);
4350 GError *
error = NULL;
4351 GFileEnumerator *library_dir_files = g_file_enumerate_children(lib_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4355 g_object_unref(lib_parent);
4356 g_object_unref(dat_parent);
4361 g_error_free(
error);
4362 g_queue_free(lib_snaps);
4363 g_queue_free(dat_snaps);
4364 g_queue_free(tmplib_snaps);
4365 g_queue_free(tmpdat_snaps);
4369 GFileEnumerator *data_dir_files = g_file_enumerate_children(dat_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4373 g_object_unref(lib_parent);
4374 g_object_unref(dat_parent);
4379 g_file_enumerator_close(library_dir_files, NULL, NULL);
4380 g_object_unref(library_dir_files);
4381 g_error_free(
error);
4382 g_queue_free(lib_snaps);
4383 g_queue_free(dat_snaps);
4384 g_queue_free(tmplib_snaps);
4385 g_queue_free(tmpdat_snaps);
4389 GFileInfo *info = NULL;
4391 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4393 const char* fname = g_file_info_get_name(info);
4394 if(g_str_has_prefix(fname, lib_snap_format))
4397 g_queue_insert_sorted(lib_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4399 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4402 g_queue_push_head(tmplib_snaps, g_strdup(fname));
4404 g_object_unref(info);
4411 g_object_unref(lib_parent);
4412 g_object_unref(dat_parent);
4415 g_queue_free_full(lib_snaps, g_free);
4416 g_queue_free(dat_snaps);
4417 g_queue_free_full(tmplib_snaps, g_free);
4418 g_queue_free(tmpdat_snaps);
4419 g_file_enumerator_close(library_dir_files, NULL, NULL);
4420 g_object_unref(library_dir_files);
4421 g_file_enumerator_close(data_dir_files, NULL, NULL);
4422 g_object_unref(data_dir_files);
4423 g_error_free(
error);
4426 g_file_enumerator_close(library_dir_files, NULL, NULL);
4427 g_object_unref(library_dir_files);
4429 while ((info = g_file_enumerator_next_file(data_dir_files, NULL, &
error)))
4431 const char* fname = g_file_info_get_name(info);
4432 if(g_str_has_prefix(fname, dat_snap_format))
4435 g_queue_insert_sorted(dat_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4437 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4440 g_queue_push_head(tmpdat_snaps, g_strdup(fname));
4442 g_object_unref(info);
4451 g_object_unref(lib_parent);
4452 g_object_unref(dat_parent);
4453 g_queue_free_full(lib_snaps, g_free);
4454 g_queue_free_full(dat_snaps, g_free);
4455 g_queue_free_full(tmplib_snaps, g_free);
4456 g_queue_free_full(tmpdat_snaps, g_free);
4457 g_file_enumerator_close(data_dir_files, NULL, NULL);
4458 g_object_unref(data_dir_files);
4459 g_error_free(
error);
4463 g_file_enumerator_close(data_dir_files, NULL, NULL);
4464 g_object_unref(data_dir_files);
4470 GPtrArray *ret = g_ptr_array_new();
4472 gchar *lib_parent_path = g_file_get_path(lib_parent);
4473 g_object_unref(lib_parent);
4475 while(g_queue_get_length(lib_snaps) > keep_snaps)
4477 gchar *head = g_queue_pop_head(lib_snaps);
4478 g_ptr_array_add(ret, g_strconcat(lib_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4481 while(!g_queue_is_empty(tmplib_snaps))
4483 gchar *head = g_queue_pop_head(tmplib_snaps);
4484 g_ptr_array_add(ret, g_strconcat(lib_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4488 g_queue_free_full(lib_snaps, g_free);
4489 g_queue_free_full(tmplib_snaps, g_free);
4491 gchar *dat_parent_path = g_file_get_path(dat_parent);
4492 g_object_unref(dat_parent);
4494 while(g_queue_get_length(dat_snaps) > keep_snaps)
4496 gchar *head = g_queue_pop_head(dat_snaps);
4497 g_ptr_array_add(ret, g_strconcat(dat_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4500 while(!g_queue_is_empty(tmpdat_snaps))
4502 gchar *head = g_queue_pop_head(tmpdat_snaps);
4503 g_ptr_array_add(ret, g_strconcat(dat_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4507 g_queue_free_full(dat_snaps, g_free);
4508 g_queue_free_full(tmpdat_snaps, g_free);
4510 g_ptr_array_add (ret, NULL);
4512 return (
char**)g_ptr_array_free(ret,
FALSE);