188 gboolean all_ok =
TRUE;
189 const char *failing_query = NULL;
191 sqlite3_stmt *innerstmt;
196 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
207 _SQLITE3_EXEC(db->
handle,
"DROP TABLE IF EXISTS main.mipmap_timestamps", NULL, NULL, NULL);
208 _SQLITE3_EXEC(db->
handle,
"DROP TABLE IF EXISTS main.dt_migration_table", NULL, NULL, NULL);
213 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.db_info (key VARCHAR PRIMARY KEY, value VARCHAR)",
215 _SQLITE3_EXEC(db->
handle,
"INSERT OR REPLACE INTO main.db_info (key, value) VALUES ('version', 1)",
218 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.film_rolls_folder_index ON film_rolls (folder)",
221 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN orientation INTEGER", NULL, NULL, NULL);
222 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN focus_distance REAL", NULL, NULL, NULL);
223 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN group_id INTEGER", NULL, NULL, NULL);
224 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN histogram BLOB", NULL, NULL, NULL);
225 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN lightmap BLOB", NULL, NULL, NULL);
226 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN longitude REAL", NULL, NULL, NULL);
227 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN latitude REAL", NULL, NULL, NULL);
228 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN color_matrix BLOB", NULL, NULL, NULL);
230 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN colorspace INTEGER", NULL, NULL, NULL);
231 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN version INTEGER", NULL, NULL, NULL);
232 sqlite3_exec(db->
handle,
"ALTER TABLE main.images ADD COLUMN max_version INTEGER", NULL, NULL, NULL);
233 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET orientation = -1 WHERE orientation IS NULL", NULL, NULL, NULL);
234 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET focus_distance = -1 WHERE focus_distance IS NULL",
236 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET group_id = id WHERE group_id IS NULL", NULL, NULL, NULL);
237 _SQLITE3_EXEC(db->
handle,
"UPDATE main.images SET max_version = (SELECT COUNT(*)-1 FROM main.images i WHERE "
238 "i.filename = main.images.filename AND "
239 "i.film_id = main.images.film_id) WHERE max_version IS NULL",
243 "UPDATE main.images SET version = (SELECT COUNT(*) FROM main.images i "
244 "WHERE i.filename = main.images.filename AND "
245 "i.film_id = main.images.film_id AND i.id < main.images.id) WHERE version IS NULL",
248 _SQLITE3_EXEC(db->
handle,
"ALTER TABLE main.images RENAME TO dt_migration_table", NULL, NULL, NULL);
249 _SQLITE3_EXEC(db->
handle,
"DROP INDEX IF EXISTS main.images_group_id_index", NULL, NULL, NULL);
250 _SQLITE3_EXEC(db->
handle,
"DROP INDEX IF EXISTS main.images_film_id_index", NULL, NULL, NULL);
253 "CREATE TABLE main.images (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
254 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
255 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
256 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
257 "output_width INTEGER, output_height INTEGER, crop REAL, "
258 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
259 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
260 "caption VARCHAR, description VARCHAR, license VARCHAR, sha1sum CHAR(40), "
261 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
262 "latitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, max_version INTEGER)",
264 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX main.images_group_id_index ON images (group_id)", NULL, NULL, NULL);
265 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX main.images_film_id_index ON images (film_id)", NULL, NULL, NULL);
268 "INSERT INTO main.images (id, group_id, film_id, width, height, filename, maker, model, "
269 "lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags, "
270 "output_width, output_height, crop, raw_parameters, raw_denoise_threshold, "
271 "raw_auto_bright_threshold, raw_black, raw_maximum, caption, description, license, sha1sum, "
272 "orientation, histogram, lightmap, longitude, latitude, color_matrix, colorspace, version, "
274 "SELECT id, group_id, film_id, width, height, filename, maker, model, lens, exposure, aperture, iso, "
275 "focal_length, focus_distance, datetime_taken, flags, output_width, output_height, crop, "
276 "raw_parameters, raw_denoise_threshold, raw_auto_bright_threshold, raw_black, raw_maximum, "
277 "caption, description, license, sha1sum, orientation, histogram, lightmap, longitude, "
278 "latitude, color_matrix, colorspace, version, max_version FROM dt_migration_table",
283 _SQLITE3_EXEC(db->
handle,
"CREATE TEMPORARY TABLE dt_migration_table (imgid INTEGER)", NULL, NULL, NULL);
284 _SQLITE3_EXEC(db->
handle,
"INSERT INTO dt_migration_table SELECT imgid FROM main.selected_images", NULL, NULL,
287 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.selected_images (imgid INTEGER PRIMARY KEY)", NULL, NULL, NULL);
288 _SQLITE3_EXEC(db->
handle,
"INSERT OR IGNORE INTO main.selected_images SELECT imgid FROM dt_migration_table",
292 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
293 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
294 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
295 sqlite3_exec(db->
handle,
"ALTER TABLE main.history ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
296 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.history_imgid_index ON history (imgid)", NULL, NULL,
298 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
300 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET multi_priority = 0 WHERE multi_priority IS NULL", NULL, NULL,
302 _SQLITE3_EXEC(db->
handle,
"UPDATE main.history SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL, NULL);
304 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.mask (imgid INTEGER, formid INTEGER, form INTEGER, "
305 "name VARCHAR(256), version INTEGER, "
306 "points BLOB, points_count INTEGER, source BLOB)",
308 sqlite3_exec(db->
handle,
"ALTER TABLE main.mask ADD COLUMN source BLOB", NULL, NULL,
311 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.tagged_images_tagid_index ON tagged_images (tagid)",
315 "CREATE TABLE IF NOT EXISTS main.styles (id INTEGER, name VARCHAR, description VARCHAR)", NULL,
317 sqlite3_exec(db->
handle,
"ALTER TABLE main.styles ADD COLUMN id INTEGER", NULL, NULL, NULL);
318 _SQLITE3_EXEC(db->
handle,
"UPDATE main.styles SET id = rowid WHERE id IS NULL", NULL, NULL, NULL);
320 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.style_items (styleid INTEGER, num INTEGER, module "
321 "INTEGER, operation VARCHAR(256), op_params BLOB, "
322 "enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, multi_priority "
323 "INTEGER, multi_name VARCHAR(256))",
325 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
326 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
327 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
328 sqlite3_exec(db->
handle,
"ALTER TABLE main.style_items ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
329 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
331 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET multi_priority = 0 WHERE multi_priority IS NULL", NULL,
333 _SQLITE3_EXEC(db->
handle,
"UPDATE main.style_items SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL,
337 _SQLITE3_EXEC(db->
handle,
"CREATE TEMPORARY TABLE dt_migration_table (imgid INTEGER, color INTEGER)", NULL,
339 _SQLITE3_EXEC(db->
handle,
"INSERT INTO dt_migration_table SELECT imgid, color FROM main.color_labels", NULL,
342 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE main.color_labels (imgid INTEGER, color INTEGER)", NULL, NULL, NULL);
343 _SQLITE3_EXEC(db->
handle,
"CREATE UNIQUE INDEX main.color_labels_idx ON color_labels (imgid, color)", NULL, NULL,
345 _SQLITE3_EXEC(db->
handle,
"INSERT OR IGNORE INTO main.color_labels SELECT imgid, color FROM dt_migration_table",
349 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.meta_data (id INTEGER, key INTEGER, value VARCHAR)",
351 _SQLITE3_EXEC(db->
handle,
"CREATE INDEX IF NOT EXISTS main.metadata_index ON meta_data (id, key)", NULL, NULL,
354 _SQLITE3_EXEC(db->
handle,
"CREATE TABLE IF NOT EXISTS main.presets (name VARCHAR, description VARCHAR, "
355 "operation VARCHAR, op_version INTEGER, op_params BLOB, "
356 "enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, multi_priority "
357 "INTEGER, multi_name VARCHAR(256), "
358 "model VARCHAR, maker VARCHAR, lens VARCHAR, iso_min REAL, iso_max REAL, "
359 "exposure_min REAL, exposure_max REAL, "
360 "aperture_min REAL, aperture_max REAL, focal_length_min REAL, focal_length_max "
361 "REAL, writeprotect INTEGER, "
362 "autoapply INTEGER, filter INTEGER, def INTEGER, isldr INTEGER)",
364 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN op_version INTEGER", NULL, NULL, NULL);
365 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN blendop_params BLOB", NULL, NULL, NULL);
366 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN blendop_version INTEGER", NULL, NULL, NULL);
367 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN multi_priority INTEGER", NULL, NULL, NULL);
368 sqlite3_exec(db->
handle,
"ALTER TABLE main.presets ADD COLUMN multi_name VARCHAR(256)", NULL, NULL, NULL);
371 sqlite3_prepare_v2(db->
handle,
372 "SELECT p.rowid, p.name, p.operation, p.op_version FROM main.presets p INNER JOIN "
373 "(SELECT * FROM (SELECT rowid, name, operation, op_version, COUNT(*) AS count "
374 "FROM main.presets GROUP BY name, operation, op_version) WHERE count > 1) s "
375 "ON p.name = s.name AND p.operation = s.operation AND p.op_version = s.op_version",
378 char *last_name = NULL, *last_operation = NULL;
379 int last_op_version = 0;
381 while(sqlite3_step(stmt) == SQLITE_ROW)
383 const int rowid = sqlite3_column_int(stmt, 0);
384 const char *
name = (
const char *)sqlite3_column_text(stmt, 1);
385 const char *operation = (
const char *)sqlite3_column_text(stmt, 2);
386 const int op_version = sqlite3_column_int(stmt, 3);
390 || last_op_version != op_version)
394 last_name = g_strdup(
name);
395 last_operation = g_strdup(operation);
396 last_op_version = op_version;
402 sqlite3_prepare_v2(db->
handle,
"SELECT name FROM main.presets WHERE name = ?1 || ' (' || ?2 || ')' AND "
403 "operation = ?3 AND op_version = ?4",
404 -1, &innerstmt, NULL);
408 sqlite3_bind_text(innerstmt, 1,
name, -1, SQLITE_TRANSIENT);
409 sqlite3_bind_int(innerstmt, 2,
i);
410 sqlite3_bind_text(innerstmt, 3, operation, -1, SQLITE_TRANSIENT);
411 sqlite3_bind_int(innerstmt, 4, op_version);
412 if(sqlite3_step(innerstmt) != SQLITE_ROW)
break;
413 sqlite3_reset(innerstmt);
414 sqlite3_clear_bindings(innerstmt);
417 sqlite3_finalize(innerstmt);
421 const char *query =
"UPDATE main.presets SET name = name || ' (' || ?1 || ')' WHERE rowid = ?2";
423 sqlite3_prepare_v2(db->
handle, query, -1, &innerstmt, NULL);
424 sqlite3_bind_int(innerstmt, 1,
i);
425 sqlite3_bind_int(innerstmt, 2, rowid);
426 if(sqlite3_step(innerstmt) != SQLITE_DONE)
429 failing_query = query;
432 sqlite3_finalize(innerstmt);
434 sqlite3_finalize(stmt);
440 "CREATE UNIQUE INDEX IF NOT EXISTS main.presets_idx ON presets (name, operation, op_version)",
442 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET blendop_version = 1 WHERE blendop_version IS NULL", NULL,
444 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET multi_priority = 0 WHERE multi_priority IS NULL", NULL, NULL,
446 _SQLITE3_EXEC(db->
handle,
"UPDATE main.presets SET multi_name = ' ' WHERE multi_name IS NULL", NULL, NULL, NULL);
454 sqlite3_prepare_v2(db->
handle,
"SELECT id, filename FROM main.images WHERE filename LIKE '/%'", -1, &stmt, NULL);
455 sqlite3_prepare_v2(db->
handle,
"UPDATE main.images SET filename = ?1 WHERE id = ?2", -1, &innerstmt, NULL);
457 while(sqlite3_step(stmt) == SQLITE_ROW)
459 const int id = sqlite3_column_int(stmt, 0);
460 const char *path = (
const char *)sqlite3_column_text(stmt, 1);
461 gchar *filename = g_path_get_basename(path);
462 sqlite3_bind_text(innerstmt, 1, filename, -1, SQLITE_TRANSIENT);
463 sqlite3_bind_int(innerstmt, 2,
id);
464 sqlite3_step(innerstmt);
465 sqlite3_reset(innerstmt);
466 sqlite3_clear_bindings(innerstmt);
469 sqlite3_finalize(stmt);
470 sqlite3_finalize(innerstmt);
478 "UPDATE main.images SET datetime_taken = REPLACE(datetime_taken, '-', ':') WHERE datetime_taken LIKE '%-%'",
484 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
487 fprintf(stderr,
"[init] failing query: `%s'\n", failing_query);
488 fprintf(stderr,
"[init] %s\n", sqlite3_errmsg(db->
handle));
489 sqlite3_exec(db->
handle,
"ROLLBACK TRANSACTION", NULL, NULL, NULL);
543 int new_version = version;
546 else if(version == 0)
553 else if(version == 1)
556 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
558 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN write_timestamp INTEGER",
559 "[init] can't add `write_timestamp' column to database\n");
560 TRY_EXEC(
"UPDATE main.images SET write_timestamp = STRFTIME('%s', 'now') WHERE write_timestamp IS NULL",
561 "[init] can't initialize `write_timestamp' with current point in time\n");
563 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
566 else if(version == 2)
570 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
572 TRY_EXEC(
"UPDATE main.images SET raw_black = 0, raw_maximum = 16384",
573 "[init] can't reset raw_black and raw_maximum\n");
575 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
578 else if(version == 3)
580 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
582 TRY_EXEC(
"CREATE TRIGGER insert_tag AFTER INSERT ON main.tags"
584 " INSERT INTO tagxtag SELECT id, new.id, 0 FROM TAGS;"
585 " UPDATE tagxtag SET count = 1000000 WHERE id1=new.id AND id2=new.id;"
587 "[init] can't create insert_tag trigger\n");
588 TRY_EXEC(
"CREATE TRIGGER delete_tag BEFORE DELETE ON main.tags"
590 " DELETE FROM tagxtag WHERE id1=old.id OR id2=old.id;"
591 " DELETE FROM tagged_images WHERE tagid=old.id;"
593 "[init] can't create delete_tag trigger\n");
594 TRY_EXEC(
"CREATE TRIGGER attach_tag AFTER INSERT ON main.tagged_images"
597 " SET count = count + 1"
598 " WHERE (id1=new.tagid AND id2 IN (SELECT tagid FROM tagged_images WHERE imgid=new.imgid))"
599 " OR (id2=new.tagid AND id1 IN (SELECT tagid FROM tagged_images WHERE imgid=new.imgid));"
601 "[init] can't create attach_tag trigger\n");
602 TRY_EXEC(
"CREATE TRIGGER detach_tag BEFORE DELETE ON main.tagged_images"
605 " SET count = count - 1"
606 " WHERE (id1=old.tagid AND id2 IN (SELECT tagid FROM tagged_images WHERE imgid=old.imgid))"
607 " OR (id2=old.tagid AND id1 IN (SELECT tagid FROM tagged_images WHERE imgid=old.imgid));"
609 "[init] can't create detach_tag trigger\n");
611 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
614 else if(version == 4)
616 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
618 TRY_EXEC(
"ALTER TABLE main.presets RENAME TO tmp_presets",
"[init] can't rename table presets\n");
620 TRY_EXEC(
"CREATE TABLE main.presets (name VARCHAR, description VARCHAR, operation VARCHAR, op_params BLOB,"
621 "enabled INTEGER, blendop_params BLOB, model VARCHAR, maker VARCHAR, lens VARCHAR,"
622 "iso_min REAL, iso_max REAL, exposure_min REAL, exposure_max REAL, aperture_min REAL,"
623 "aperture_max REAL, focal_length_min REAL, focal_length_max REAL, writeprotect INTEGER,"
624 "autoapply INTEGER, filter INTEGER, def INTEGER, format INTEGER, op_version INTEGER,"
625 "blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
626 "[init] can't create new presets table\n");
628 TRY_EXEC(
"INSERT INTO main.presets (name, description, operation, op_params, enabled, blendop_params, model, "
629 "maker, lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, aperture_max,"
630 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, def, format, op_version, "
631 "blendop_version, multi_priority, multi_name) SELECT name, description, operation, op_params, "
632 "enabled, blendop_params, model, maker, lens, iso_min, iso_max, exposure_min, exposure_max, "
633 "aperture_min, aperture_max, focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
634 "def, isldr, op_version, blendop_version, multi_priority, multi_name FROM tmp_presets",
635 "[init] can't populate presets table from tmp_presets\n");
637 TRY_EXEC(
"DROP TABLE tmp_presets",
"[init] can't delete table tmp_presets\n");
639 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
642 else if(version == 5)
644 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
646 TRY_EXEC(
"CREATE INDEX main.images_filename_index ON images (filename)",
647 "[init] can't create index on image filename\n");
649 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
652 else if(version == 6)
655 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
657 if(sqlite3_exec(db->
handle,
"SELECT style_id FROM main.style_items", NULL, NULL, NULL) == SQLITE_OK)
660 TRY_EXEC(
"ALTER TABLE main.style_items RENAME TO tmp_style_items",
661 "[init] can't rename table style_items\n");
663 TRY_EXEC(
"CREATE TABLE main.style_items (styleid INTEGER, num INTEGER, module INTEGER, "
664 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, "
665 "blendop_params BLOB, blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
666 "[init] can't create new style_items table\n");
668 TRY_EXEC(
"INSERT INTO main.style_items (styleid, num, module, operation, op_params, enabled,"
669 " blendop_params, blendop_version, multi_priority, multi_name)"
670 " SELECT style_id, num, module, operation, op_params, enabled,"
671 " blendop_params, blendop_version, multi_priority, multi_name"
672 " FROM tmp_style_items",
673 "[init] can't populate style_items table from tmp_style_items\n");
675 TRY_EXEC(
"DROP TABLE tmp_style_items",
"[init] can't delete table tmp_style_items\n");
679 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
682 else if(version == 7)
685 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
687 TRY_EXEC(
"ALTER TABLE main.film_rolls RENAME TO tmp_film_rolls",
"[init] can't rename table film_rolls\n");
689 TRY_EXEC(
"CREATE TABLE main.film_rolls "
690 "(id INTEGER PRIMARY KEY, datetime_accessed CHAR(20), "
691 "folder VARCHAR(1024) NOT NULL)",
692 "[init] can't create new film_rolls table\n");
694 TRY_EXEC(
"INSERT INTO main.film_rolls (id, datetime_accessed, folder) "
695 "SELECT id, datetime_accessed, folder "
696 "FROM tmp_film_rolls "
697 "WHERE folder IS NOT NULL",
698 "[init] can't populate film_rolls table from tmp_film_rolls\n");
700 TRY_EXEC(
"DROP TABLE tmp_film_rolls",
"[init] can't delete table tmp_film_rolls\n");
702 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
705 else if(version == 8)
708 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
710 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN history_end INTEGER",
711 "[init] can't add `history_end' column to database\n");
713 TRY_EXEC(
"UPDATE main.images SET history_end = (SELECT IFNULL(MAX(num) + 1, 0) FROM main.history "
714 "WHERE imgid = id)",
"[init] can't initialize `history_end' with last history entry\n");
716 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
719 else if(version == 9)
722 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
724 TRY_EXEC(
"UPDATE main.images SET history_end = (SELECT IFNULL(MAX(num) + 1, 0) FROM main.history "
725 "WHERE imgid = id)",
"[init] can't set `history_end' to 0 where it was NULL\n");
727 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
730 else if(version == 10)
733 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
735 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN altitude REAL",
736 "[init] can't add `altitude' column to database\n");
738 TRY_EXEC(
"UPDATE main.images SET altitude = NULL",
"[init] can't initialize `altitude' with NULL\n");
740 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
743 else if(version == 11)
746 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
748 TRY_EXEC(
"DROP TRIGGER main.detach_tag",
"[init] can't drop trigger `detach_tag' from database\n");
750 TRY_EXEC(
"DROP TRIGGER main.attach_tag",
"[init] can't drop trigger `attach_tag' from database\n");
752 TRY_EXEC(
"DROP TRIGGER main.delete_tag",
"[init] can't drop trigger `delete_tag' from database\n");
754 TRY_EXEC(
"DROP TRIGGER main.insert_tag",
"[init] can't drop trigger `insert_tag' from database\n");
756 TRY_EXEC(
"DROP TABLE main.tagxtag",
"[init] can't drop table `tagxtag' from database\n");
758 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
761 else if(version == 12)
764 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
771 sqlite3_finalize(stmt); \
772 sqlite3_finalize(select_stmt); \
773 sqlite3_finalize(count_clashes_stmt); \
774 sqlite3_finalize(update_name_stmt); \
775 sqlite3_finalize(insert_stmt); \
776 sqlite3_finalize(delete_stmt); \
780 sqlite3_stmt *insert_stmt = NULL, *delete_stmt = NULL, *select_stmt = NULL, *count_clashes_stmt = NULL,
781 *update_name_stmt = NULL;
785 TRY_EXEC(
"DELETE FROM main.presets WHERE rowid IN (SELECT p1.rowid FROM main.presets p1 "
786 "JOIN data.presets p2 ON "
787 "p1.name IS p2.name AND "
788 "p1.description IS p2.description AND "
789 "p1.operation IS p2.operation AND "
790 "p1.op_version IS p2.op_version AND "
791 "p1.op_params IS p2.op_params AND "
792 "p1.enabled IS p2.enabled AND "
793 "p1.blendop_params IS p2.blendop_params AND "
794 "p1.blendop_version IS p2.blendop_version AND "
795 "p1.multi_priority IS p2.multi_priority AND "
796 "p1.multi_name IS p2.multi_name AND "
797 "p1.model IS p2.model AND "
798 "p1.maker IS p2.maker AND "
799 "p1.lens IS p2.lens AND "
800 "p1.iso_min IS p2.iso_min AND "
801 "p1.iso_max IS p2.iso_max AND "
802 "p1.exposure_min IS p2.exposure_min AND "
803 "p1.exposure_max IS p2.exposure_max AND "
804 "p1.aperture_min IS p2.aperture_min AND "
805 "p1.aperture_max IS p2.aperture_max AND "
806 "p1.focal_length_min IS p2.focal_length_min AND "
807 "p1.focal_length_max IS p2.focal_length_max AND "
808 "p1.writeprotect IS p2.writeprotect AND "
809 "p1.autoapply IS p2.autoapply AND "
810 "p1.filter IS p2.filter AND "
811 "p1.def IS p2.def AND "
812 "p1.format IS p2.format "
813 "WHERE p1.writeprotect = 0)",
814 "[init] can't delete already migrated presets from database\n");
818 TRY_PREPARE(select_stmt,
"SELECT p.rowid, r FROM main.presets AS p, (SELECT rowid AS r, name, operation, "
819 "op_version FROM main.presets GROUP BY name, operation, op_version HAVING "
820 "COUNT(*) > 1) USING (name, operation, op_version) WHERE p.rowid != r",
821 "[init] can't prepare selecting presets with same name, operation, op_version from database\n");
824 TRY_PREPARE(count_clashes_stmt,
"SELECT COUNT(*) FROM main.presets AS p, (SELECT name, operation, op_version "
825 "FROM main.presets WHERE rowid = ?1) AS i ON p.name = i.name || \" #\" || ?2 "
826 "AND p.operation = i.operation AND p.op_version = i.op_version",
827 "[init] can't prepare selection of preset count by name from database\n");
830 TRY_PREPARE(update_name_stmt,
"UPDATE main.presets SET name = name || \" #\" || ?1 WHERE rowid = ?2",
831 "[init] can't prepare updating of preset name in database\n");
834 TRY_PREPARE(stmt,
"SELECT p1.rowid FROM main.presets p1 INNER JOIN data.presets p2 "
835 "USING (name, operation, op_version) WHERE p1.writeprotect = 0",
836 "[init] can't access table `presets' in database\n");
839 TRY_PREPARE(insert_stmt,
"INSERT OR FAIL INTO data.presets (name, description, operation, op_version, "
840 "op_params, enabled, blendop_params, blendop_version, multi_priority, multi_name, "
841 "model, maker, lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, "
842 "aperture_max, focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
844 "SELECT name || \" #\" || ?1, description, operation, op_version, op_params, "
845 "enabled, blendop_params, blendop_version, multi_priority, multi_name, model, maker, "
846 "lens, iso_min, iso_max, exposure_min, exposure_max, aperture_min, aperture_max, "
847 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, def, format "
848 "FROM main.presets p1 WHERE p1.rowid = ?2",
849 "[init] can't prepare insertion statement\n");
851 TRY_PREPARE(delete_stmt,
"DELETE FROM main.presets WHERE rowid = ?1",
"[init] can't prepare deletion statement\n");
854 while(sqlite3_step(select_stmt) == SQLITE_ROW)
856 const int own_rowid = sqlite3_column_int(select_stmt, 0);
857 const int other_rowid = sqlite3_column_int(select_stmt, 1);
858 int preset_version = 0;
863 sqlite3_reset(count_clashes_stmt);
864 sqlite3_clear_bindings(count_clashes_stmt);
865 sqlite3_bind_int(count_clashes_stmt, 1, other_rowid);
866 sqlite3_bind_int(count_clashes_stmt, 2, preset_version);
868 while(sqlite3_step(count_clashes_stmt) == SQLITE_ROW && sqlite3_column_int(count_clashes_stmt, 0) > 0);
870 sqlite3_bind_int(update_name_stmt, 1, preset_version);
871 sqlite3_bind_int(update_name_stmt, 2, own_rowid);
872 TRY_STEP(update_name_stmt, SQLITE_DONE,
"[init] can't rename preset in database\n");
873 sqlite3_reset(update_name_stmt);
874 sqlite3_reset(update_name_stmt);
878 while(sqlite3_step(stmt) == SQLITE_ROW)
880 int preset_version = 0;
881 const int rowid = sqlite3_column_int(stmt, 0);
886 sqlite3_reset(insert_stmt);
887 sqlite3_clear_bindings(insert_stmt);
888 sqlite3_bind_int(insert_stmt, 1, preset_version);
889 sqlite3_bind_int(insert_stmt, 2, rowid);
890 }
while(sqlite3_step(insert_stmt) != SQLITE_DONE);
892 sqlite3_reset(delete_stmt);
893 sqlite3_clear_bindings(delete_stmt);
894 sqlite3_bind_int(delete_stmt, 1, rowid);
895 TRY_STEP(delete_stmt, SQLITE_DONE,
"[init] can't delete preset from database\n");
899 TRY_EXEC(
"INSERT OR FAIL INTO data.presets SELECT name, description, operation, "
900 "op_version, op_params, enabled, blendop_params, blendop_version, "
901 "multi_priority, multi_name, model, maker, lens, iso_min, iso_max, "
902 "exposure_min, exposure_max, aperture_min, aperture_max, "
903 "focal_length_min, focal_length_max, writeprotect, autoapply, filter, "
904 "def, format FROM main.presets WHERE writeprotect = 0",
905 "[init] can't copy presets to the data database\n");
907 TRY_EXEC(
"DELETE FROM main.presets WHERE writeprotect = 0",
908 "[init] can't copy presets to the data database\n");
918 sqlite3_finalize(stmt); \
919 sqlite3_finalize(insert_stmt); \
920 sqlite3_finalize(select_stmt); \
921 sqlite3_finalize(delete_stmt); \
922 sqlite3_finalize(update_name_stmt); \
923 sqlite3_finalize(select_new_stmt); \
924 sqlite3_finalize(copy_style_items_stmt); \
925 sqlite3_finalize(delete_style_items_stmt); \
930 update_name_stmt = NULL;
933 sqlite3_stmt *select_new_stmt = NULL, *copy_style_items_stmt = NULL, *delete_style_items_stmt = NULL;
935 TRY_PREPARE(stmt,
"SELECT id, name FROM main.styles",
"[init] can't prepare style selection from database\n");
936 TRY_PREPARE(select_stmt,
"SELECT rowid FROM data.styles WHERE name = ?1 LIMIT 1",
937 "[init] can't prepare style item selection from database\n");
938 TRY_PREPARE(update_name_stmt,
"UPDATE main.styles SET name = ?1 WHERE id = ?2",
939 "[init] can't prepare style name update\n");
940 TRY_PREPARE(insert_stmt,
"INSERT INTO data.styles (id, name, description) "
941 "SELECT (SELECT COALESCE(MAX(id),0)+1 FROM data.styles), name, description "
942 "FROM main.styles where id = ?1",
943 "[init] can't prepare style insertion for database\n");
944 TRY_PREPARE(delete_stmt,
"DELETE FROM main.styles WHERE id = ?1",
945 "[init] can't prepare style deletion for database\n");
946 TRY_PREPARE(select_new_stmt,
"SELECT id FROM data.styles WHERE rowid = ?1",
947 "[init] can't prepare style selection from data database\n");
948 TRY_PREPARE(copy_style_items_stmt,
"INSERT INTO data.style_items "
949 "(styleid, num, module, operation, op_params, enabled, blendop_params, "
950 "blendop_version, multi_priority, multi_name) "
951 "SELECT ?1, num, module, operation, op_params, enabled, blendop_params, "
952 "blendop_version, multi_priority, multi_name FROM main.style_items "
953 "WHERE styleid = ?2",
954 "[init] can't prepare style item copy into data database\n");
955 TRY_PREPARE(delete_style_items_stmt,
"DELETE FROM main.style_items WHERE styleid = ?1",
956 "[init] can't prepare style item deletion for database\n");
958 while(sqlite3_step(stmt) == SQLITE_ROW)
960 const int id = sqlite3_column_int(stmt, 0);
961 const char *
name = (
const char *)sqlite3_column_text(stmt, 1);
964 sqlite3_bind_text(select_stmt, 1,
name, -1, SQLITE_TRANSIENT);
965 if(sqlite3_step(select_stmt) == SQLITE_ROW)
968 int style_version = 0;
969 char *new_name = NULL;
974 new_name = g_strdup_printf(
"%s #%d",
name, style_version);
975 sqlite3_reset(select_stmt);
976 sqlite3_clear_bindings(select_stmt);
977 sqlite3_bind_text(select_stmt, 1, new_name, -1, SQLITE_TRANSIENT);
978 }
while(sqlite3_step(select_stmt) == SQLITE_ROW);
981 sqlite3_bind_text(update_name_stmt, 1, new_name, -1, SQLITE_TRANSIENT);
982 sqlite3_bind_int(update_name_stmt, 2,
id);
983 TRY_STEP(update_name_stmt, SQLITE_DONE,
"[init] can't update name of style in database\n");
984 sqlite3_reset(update_name_stmt);
985 sqlite3_clear_bindings(update_name_stmt);
990 sqlite3_bind_int(insert_stmt, 1,
id);
991 TRY_STEP(insert_stmt, SQLITE_DONE,
"[init] can't insert style into data database\n");
992 sqlite3_int64 last_rowid = sqlite3_last_insert_rowid(db->
handle);
995 sqlite3_bind_int(delete_stmt, 1,
id);
996 TRY_STEP(delete_stmt, SQLITE_DONE,
"[init] can't delete style from database\n");
998 sqlite3_bind_int(select_new_stmt, 1, last_rowid);
999 TRY_STEP(select_new_stmt, SQLITE_ROW,
"[init] can't select new style from data database\n");
1000 const int new_id = sqlite3_column_int(select_new_stmt, 0);
1003 sqlite3_bind_int(copy_style_items_stmt, 1, new_id);
1004 sqlite3_bind_int(copy_style_items_stmt, 2,
id);
1005 TRY_STEP(copy_style_items_stmt, SQLITE_DONE,
"[init] can't copy style items into data database\n");
1008 sqlite3_bind_int(delete_style_items_stmt, 1,
id);
1009 TRY_STEP(delete_style_items_stmt, SQLITE_DONE,
"[init] can't delete style items from database\n");
1012 sqlite3_reset(insert_stmt);
1013 sqlite3_clear_bindings(insert_stmt);
1014 sqlite3_reset(select_stmt);
1015 sqlite3_clear_bindings(select_stmt);
1016 sqlite3_reset(delete_stmt);
1017 sqlite3_clear_bindings(delete_stmt);
1018 sqlite3_reset(select_new_stmt);
1019 sqlite3_clear_bindings(select_new_stmt);
1020 sqlite3_reset(copy_style_items_stmt);
1021 sqlite3_clear_bindings(copy_style_items_stmt);
1022 sqlite3_reset(delete_style_items_stmt);
1023 sqlite3_clear_bindings(delete_style_items_stmt);
1032 TRY_EXEC(
"INSERT OR IGNORE INTO data.tags (name, icon, description, flags) "
1033 "SELECT name, icon, description, flags FROM main.tags",
1034 "[init] can't prepare insertion of used tags into data database\n");
1038 TRY_EXEC(
"CREATE TEMPORARY TABLE tagged_images_tmp (imgid INTEGER, tagid INTEGER)",
1039 "[init] can't create temporary table for updating `tagged_images'\n");
1041 TRY_EXEC(
"INSERT INTO tagged_images_tmp (imgid, tagid) "
1042 "SELECT imgid, (SELECT t2.id FROM main.tags t1, data.tags t2 USING (name) WHERE t1.id = tagid) "
1043 "FROM main.tagged_images",
"[init] can't insert into `tagged_images_tmp'\n");
1045 TRY_EXEC(
"DELETE FROM main.tagged_images",
"[init] can't delete tagged images in database\n");
1047 TRY_EXEC(
"INSERT OR IGNORE INTO main.tagged_images (imgid, tagid) SELECT imgid, tagid FROM tagged_images_tmp",
1048 "[init] can't copy updated values back to `tagged_images'\n");
1050 TRY_EXEC(
"DROP TABLE tagged_images_tmp",
"[init] can't drop table `tagged_images_tmp' from database\n");
1053 TRY_EXEC(
"DROP INDEX IF EXISTS main.presets_idx",
"[init] can't drop index `presets_idx' from database\n");
1054 TRY_EXEC(
"DROP TABLE main.presets",
"[init] can't drop table `presets' from database\n");
1055 TRY_EXEC(
"DROP TABLE main.style_items",
"[init] can't drop table `style_items' from database\n");
1056 TRY_EXEC(
"DROP TABLE main.styles",
"[init] can't drop table `styles' from database\n");
1057 TRY_EXEC(
"DROP TABLE main.tags",
"[init] can't drop table `tags' from database\n");
1059 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1061 }
else if(version == 13)
1064 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1066 TRY_EXEC(
"CREATE TABLE main.used_tags (id INTEGER, name VARCHAR NOT NULL)",
1067 "[init] can't create `used_tags` table\n");
1069 TRY_EXEC(
"CREATE INDEX main.used_tags_idx ON used_tags (id, name)",
1070 "[init] can't create index on table `used_tags' in database\n");
1072 TRY_EXEC(
"INSERT INTO main.used_tags (id, name) SELECT t.id, t.name FROM data.tags AS t, main.tagged_images "
1073 "AS i ON t.id = i.tagid GROUP BY t.id",
1074 "[init] can't insert used tags into `used_tags` table in database\n");
1076 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1079 else if(version == 14)
1082 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1084 TRY_EXEC(
"DELETE FROM main.used_tags WHERE rowid NOT IN (SELECT rowid FROM used_tags GROUP BY id)",
1085 "[init] can't delete duplicated entries from `used_tags' in database\n");
1087 TRY_EXEC(
"DROP INDEX main.used_tags_idx",
"[init] can't drop index `used_tags_idx' from database\n");
1089 TRY_EXEC(
"CREATE UNIQUE INDEX main.used_tags_idx ON used_tags (id, name)",
1090 "[init] can't create index `used_tags_idx' in database\n");
1092 TRY_EXEC(
"DELETE FROM main.tagged_images WHERE tagid IS NULL",
1093 "[init] can't delete NULL entries from `tagged_images' in database");
1095 TRY_EXEC(
"DELETE FROM main.used_tags WHERE id NOT IN (SELECT DISTINCT tagid FROM main.tagged_images)",
1096 "[init] can't delete unused tags from `used_tags' in database\n");
1098 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1101 else if(version == 15)
1103 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1106 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN position INTEGER",
1107 "[init] can't add `position' column to images table in database\n");
1108 TRY_EXEC(
"CREATE INDEX main.image_position_index ON images (position)",
1109 "[init] can't create index for custom image order table\n");
1119 TRY_EXEC(
"UPDATE main.images SET position = id << 32",
1120 "[init] can't update positions custom image order table\n");
1122 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1125 else if(version == 16)
1127 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1130 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN aspect_ratio REAL",
1131 "[init] can't add `aspect_ratio' column to images table in database\n");
1132 TRY_EXEC(
"UPDATE main.images SET aspect_ratio = 0.0",
1133 "[init] can't update aspect_ratio in database\n");
1135 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1138 else if(version == 17)
1140 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1144 TRY_EXEC(
"CREATE TABLE main.masks_history (imgid INTEGER, num INTEGER, formid INTEGER, form INTEGER, name VARCHAR(256), "
1145 "version INTEGER, points BLOB, points_count INTEGER, source BLOB)",
1146 "[init] can't create `masks_history` table\n");
1148 TRY_EXEC(
"CREATE INDEX main.masks_history_imgid_index ON masks_history (imgid)",
1149 "[init] can't create index `masks_history_imgid_index' in database\n");
1152 TRY_EXEC(
"CREATE INDEX main.mask_imgid_index ON mask (imgid);",
1153 "[init] can't create index `mask_imgid_index' in database\n");
1157 TRY_EXEC(
"UPDATE main.history SET num=num+1 WHERE imgid IN (SELECT imgid FROM main.mask WHERE main.mask.imgid=main.history.imgid)",
1158 "[init] can't update `num' with num+1\n");
1161 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)",
1162 "[init] can't update `history_end' with history_end+1\n");
1165 TRY_EXEC(
"INSERT INTO main.masks_history (imgid, num, formid, form, name, version, points, points_count, source) SELECT "
1166 "imgid, 0, formid, form, name, version, points, points_count, source FROM main.mask",
1167 "[init] can't insert into masks_history\n");
1170 TRY_EXEC(
"INSERT INTO main.history (imgid, num, operation, op_params, module, enabled, "
1171 "blendop_params, blendop_version, multi_priority, multi_name) "
1172 "SELECT DISTINCT imgid, 0, 'mask_manager', NULL, 1, 0, NULL, 0, 0, '' FROM main.mask "
1174 "[init] can't insert mask manager into history\n");
1176 TRY_EXEC(
"DROP TABLE main.mask",
"[init] can't drop table `mask' from database\n");
1181 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN iop_order_version INTEGER",
1182 "[init] can't add `iop_order_version' column to images table in database\n");
1184 TRY_EXEC(
"UPDATE main.images SET iop_order_version = 0",
1185 "[init] can't update iop_order_version in database\n");
1187 TRY_EXEC(
"UPDATE main.images SET iop_order_version = 1 WHERE "
1188 "EXISTS(SELECT * FROM main.history WHERE main.history.imgid = main.images.id)",
1189 "[init] can't update iop_order_version in database\n");
1191 TRY_EXEC(
"ALTER TABLE main.history ADD COLUMN iop_order REAL",
1192 "[init] can't add `iop_order' column to history table in database\n");
1195 TRY_EXEC(
"CREATE TEMPORARY TABLE iop_order_tmp (iop_order REAL, operation VARCHAR(256))",
1196 "[init] can't create temporary table for updating `main.history'\n");
1200 for(GList *priorities = prior_v1; priorities; priorities = g_list_next(priorities))
1206 "INSERT INTO iop_order_tmp (iop_order, operation) VALUES (?1, ?2)",
1209 sqlite3_bind_text(stmt, 2, prior->
operation, -1, SQLITE_TRANSIENT);
1210 TRY_STEP(stmt, SQLITE_DONE,
"[init] can't insert default value in iop_order_tmp\n");
1211 sqlite3_finalize(stmt);
1224 TRY_EXEC(
"UPDATE main.history SET iop_order = ((("
1225 "SELECT MAX(multi_priority) FROM main.history hist1 WHERE hist1.imgid = main.history.imgid AND hist1.operation = main.history.operation "
1226 ") + 1. - multi_priority) / 1000.) + "
1227 "IFNULL((SELECT iop_order FROM iop_order_tmp WHERE iop_order_tmp.operation = "
1228 "main.history.operation), -999999.) ",
1229 "[init] can't update iop_order in history table\n");
1232 sqlite3_stmt *sel_stmt;
1233 TRY_PREPARE(sel_stmt,
"SELECT DISTINCT operation FROM main.history WHERE iop_order <= 0 OR iop_order IS NULL",
1234 "[init] can't prepare selecting history iop_order\n");
1236 while(sqlite3_step(sel_stmt) == SQLITE_ROW)
1238 const char *op_name = (
const char *)sqlite3_column_text(sel_stmt, 0);
1239 printf(
"operation %s with no iop_order while upgrading database\n", op_name);
1241 sqlite3_finalize(sel_stmt);
1243 TRY_EXEC(
"DROP TABLE iop_order_tmp",
"[init] can't drop table `iop_order_tmp' from database\n");
1245 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1254 else if(version == 18)
1256 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1258 TRY_EXEC(
"UPDATE images SET orientation=-2 WHERE orientation=1;",
1259 "[init] can't update images orientation 1 from database\n");
1261 TRY_EXEC(
"UPDATE images SET orientation=1 WHERE orientation=2;",
1262 "[init] can't update images orientation 2 from database\n");
1264 TRY_EXEC(
"UPDATE images SET orientation=-6 WHERE orientation=5;",
1265 "[init] can't update images orientation 5 from database\n");
1267 TRY_EXEC(
"UPDATE images SET orientation=5 WHERE orientation=6;",
1268 "[init] can't update images orientation 6 from database\n");
1270 TRY_EXEC(
"UPDATE images SET orientation=2 WHERE orientation=-2;",
1271 "[init] can't update images orientation -1 from database\n");
1273 TRY_EXEC(
"UPDATE images SET orientation=6 WHERE orientation=-6;",
1274 "[init] can't update images orientation -6 from database\n");
1276 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1279 else if(version == 19)
1281 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1284 TRY_EXEC(
"CREATE TEMPORARY TABLE m_prio (id INTEGER, operation VARCHAR(256), prio INTEGER)",
1285 "[init] can't create temporary table for updating `history and style_items'\n");
1287 TRY_EXEC(
"CREATE INDEX m_prio_id_index ON m_prio (id)",
1288 "[init] can't create temporary index for updating `history and style_items'\n");
1289 TRY_EXEC(
"CREATE INDEX m_prio_op_index ON m_prio (operation)",
1290 "[init] can't create temporary index for updating `history and style_items'\n");
1292 TRY_EXEC(
"INSERT INTO m_prio SELECT imgid, operation, MAX(multi_priority)"
1293 " FROM main.history GROUP BY imgid, operation",
1294 "[init] can't populate m_prio\n");
1296 TRY_EXEC(
"UPDATE main.history SET multi_priority = "
1297 "(SELECT prio FROM m_prio "
1298 " WHERE main.history.operation = operation AND main.history.imgid = id) - main.history.multi_priority",
1299 "[init] can't update multi_priority for history\n");
1301 TRY_EXEC(
"DROP TABLE m_prio",
"[init] can't drop table `m_prio' from database\n");
1303 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1306 else if(version == 20)
1308 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1310 TRY_EXEC(
"DROP INDEX IF EXISTS main.used_tags_idx",
"[init] can't drop index `used_tags_idx' from database\n");
1311 TRY_EXEC(
"DROP TABLE used_tags",
"[init] can't delete table used_tags\n");
1313 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1317 else if(version == 21)
1319 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1322 TRY_EXEC(
"CREATE TABLE module_order (imgid INTEGER PRIMARY KEY, version INTEGER, iop_list VARCHAR)",
1323 "[init] can't create module_order table'\n");
1326 sqlite3_stmt *mig_stmt;
1327 TRY_PREPARE(mig_stmt,
"SELECT imgid, operation, multi_priority, iop_order, mi.iop_order_version"
1328 " FROM main.history AS hi, main.images AS mi"
1329 " WHERE hi.imgid = mi.id"
1330 " GROUP BY imgid, operation, multi_priority"
1331 " ORDER BY imgid, iop_order",
1332 "[init] can't prepare selecting history for iop_order migration (v21)\n");
1334 GList *item_list = NULL;
1336 int current_order_version = -1;
1338 gboolean has_row = (sqlite3_step(mig_stmt) == SQLITE_ROW);
1342 const int32_t imgid = sqlite3_column_int(mig_stmt, 0);
1343 char operation[20] = { 0 };
1344 g_strlcpy(operation, (
const char *)sqlite3_column_text(mig_stmt, 1),
sizeof(operation));
1345 const int multi_priority = sqlite3_column_int(mig_stmt, 2);
1346 const double iop_order = sqlite3_column_double(mig_stmt, 3);
1347 const int iop_order_version = sqlite3_column_int(mig_stmt, 4);
1349 has_row = (sqlite3_step(mig_stmt) == SQLITE_ROW);
1352 if(imgid != current_imgid || !has_row)
1366 GList *e = item_list;
1374 GList *s = iop_order_list;
1381 iop_order_list = g_list_delete_link(iop_order_list, s);
1397 for(e = item_list; e; e = g_list_next(e))
1400 iop_order_list = g_list_prepend(iop_order_list, e_entry);
1411 gboolean has_multiple_instances =
FALSE;
1412 GList *l = iop_order_list;
1416 GList *next = g_list_next(l);
1421 has_multiple_instances =
TRUE;
1429 sqlite3_stmt *ins_stmt = NULL;
1434 sqlite3_prepare_v2(db->
handle,
1435 "INSERT INTO module_order VALUES (?1, ?2, ?3)", -1,
1437 sqlite3_bind_int(ins_stmt, 1, current_imgid);
1438 sqlite3_bind_int(ins_stmt, 2,
kind);
1439 sqlite3_bind_text(ins_stmt, 3, iop_list_txt, -1, SQLITE_TRANSIENT);
1440 TRY_STEP(ins_stmt, SQLITE_DONE,
"[init] can't insert into module_order (custom order)\n");
1441 sqlite3_finalize(ins_stmt);
1447 sqlite3_prepare_v2(db->
handle,
1448 "INSERT INTO module_order VALUES (?1, ?2, NULL)", -1,
1450 sqlite3_bind_int(ins_stmt, 1, current_imgid);
1451 sqlite3_bind_int(ins_stmt, 2,
kind);
1452 TRY_STEP(ins_stmt, SQLITE_DONE,
"[init] can't insert into module_order (standard order)\n");
1453 sqlite3_finalize(ins_stmt);
1456 g_list_free(item_list);
1459 iop_order_list = NULL;
1464 current_imgid = imgid;
1465 current_order_version = iop_order_version;
1472 item_list = g_list_append(item_list, item);
1474 sqlite3_finalize(mig_stmt);
1478 TRY_EXEC(
"CREATE TABLE h (imgid INTEGER, num INTEGER, module INTEGER, "
1479 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, "
1480 "blendop_params BLOB, blendop_version INTEGER, multi_priority INTEGER, multi_name VARCHAR(256))",
1481 "[init] can't create module_order table\n");
1482 TRY_EXEC(
"CREATE INDEX h_imgid_index ON h (imgid)",
1483 "[init] can't create index h_imgid_index\n");
1484 TRY_EXEC(
"INSERT INTO h SELECT imgid, num, module, operation, op_params, enabled, "
1485 "blendop_params, blendop_version, multi_priority, multi_name FROM main.history",
1486 "[init] can't create module_order table\n");
1488 "[init] can't drop table history\n");
1489 TRY_EXEC(
"ALTER TABLE h RENAME TO history",
1490 "[init] can't rename h to history\n");
1491 TRY_EXEC(
"DROP INDEX h_imgid_index",
1492 "[init] can't drop index h_imgid_index\n");
1493 TRY_EXEC(
"CREATE INDEX main.history_imgid_index ON history (imgid)",
1494 "[init] can't create index images_imgid_index\n");
1498 TRY_EXEC(
"CREATE TABLE i (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1499 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
1500 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
1501 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
1502 "output_width INTEGER, output_height INTEGER, crop REAL, "
1503 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
1504 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
1505 "caption VARCHAR, description VARCHAR, license VARCHAR, sha1sum CHAR(40), "
1506 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
1507 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
1508 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, aspect_ratio REAL)",
1509 "[init] can't create table i\n");
1510 TRY_EXEC(
"INSERT INTO i SELECT id, group_id, film_id, width, height, filename, maker, model,"
1511 " lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1512 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1513 " raw_auto_bright_threshold, raw_black, raw_maximum, caption, description, license, sha1sum,"
1514 " orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version,"
1515 " max_version, write_timestamp, history_end, position, aspect_ratio "
1517 "[init] can't populate table h\n");
1519 "[init] can't drop table images\n");
1520 TRY_EXEC(
"ALTER TABLE i RENAME TO images",
1521 "[init] can't rename i to images\n");
1523 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1527 else if(version == 22)
1529 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1531 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_group_id_index ON images (group_id)",
1532 "[init] can't create group_id index on image\n");
1533 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_film_id_index ON images (film_id)",
1534 "[init] can't create film_id index on image\n");
1535 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_filename_index ON images (filename)",
1536 "[init] can't create filename index on image\n");
1537 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.image_position_index ON images (position)",
1538 "[init] can't create position index on image\n");
1540 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.film_rolls_folder_index ON film_rolls (folder)",
1541 "[init] can't create folder index on film_rolls\n");
1543 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1547 else if(version == 23)
1549 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1551 TRY_EXEC(
"CREATE TABLE main.history_hash (imgid INTEGER PRIMARY KEY, "
1552 "basic_hash BLOB, auto_hash BLOB, current_hash BLOB)",
1553 "[init] can't create table history_hash\n");
1558 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1562 else if(version == 24)
1565 TRY_EXEC(
"ALTER TABLE main.history_hash ADD COLUMN mipmap_hash BLOB",
1566 "[init] can't add `mipmap_hash' column to history_hash table in database\n");
1571 else if(version == 25)
1574 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN exposure_bias REAL",
1575 "[init] can't add `exposure_bias' column to images table in database\n");
1580 else if(version == 26)
1582 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1584 TRY_EXEC(
"CREATE TABLE main.new_film_rolls "
1585 "(id INTEGER PRIMARY KEY, "
1586 "access_timestamp INTEGER, "
1587 "folder VARCHAR(1024) NOT NULL)",
1588 "[init] can't create new_film_rolls table\n");
1590 TRY_EXEC(
"INSERT INTO main.new_film_rolls"
1591 "(id, access_timestamp, folder) "
1593 "strftime('%s', replace(substr(datetime_accessed, 1, 10), ':', '-') || substr(datetime_accessed, 11), 'utc'), "
1596 "WHERE folder IS NOT NULL",
1597 "[init] can't populate new_film_rolls table from film_rolls\n");
1600 "[init] can't delete table film_rolls\n");
1602 TRY_EXEC(
"ALTER TABLE main.new_film_rolls RENAME TO film_rolls",
1603 "[init] can't rename table new_film_rolls to film_rolls\n");
1605 TRY_EXEC(
"CREATE INDEX main.film_rolls_folder_index ON film_rolls (folder)",
1606 "[init] can't create index `film_rolls_folder_index' on table `film_rolls'\n");
1608 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1611 else if(version == 27)
1613 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1615 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN import_timestamp INTEGER DEFAULT -1",
1616 "[init] can't add `import_timestamp' column to images table in database\n");
1617 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN change_timestamp INTEGER DEFAULT -1",
1618 "[init] can't add `change_timestamp' column to images table in database\n");
1619 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN export_timestamp INTEGER DEFAULT -1",
1620 "[init] can't add `export_timestamp' column to images table in database\n");
1621 TRY_EXEC(
"ALTER TABLE main.images ADD COLUMN print_timestamp INTEGER DEFAULT -1",
1622 "[init] can't add `print_timestamp' column to images table in database\n");
1624 TRY_EXEC(
"UPDATE main.images SET import_timestamp = (SELECT access_timestamp "
1625 "FROM main.film_rolls WHERE film_rolls.id = images.film_id)",
1626 "[init] can't populate import_timestamp column from film_rolls.access_timestamp.\n");
1628 TRY_EXEC(
"UPDATE main.images SET change_timestamp = images.write_timestamp "
1629 "WHERE images.write_timestamp IS NOT NULL "
1630 "AND images.id = (SELECT imgid FROM tagged_images "
1631 "JOIN data.tags ON tags.id = tagged_images.tagid "
1632 "WHERE data.tags.name = 'darktable|changed')",
1633 "[init] can't populate change_timestamp column from images.write_timestamp.\n");
1635 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1638 else if(version == 28)
1640 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1643 TRY_EXEC(
"UPDATE main.images SET flags = (flags & ~8)",
1644 "[init] can't clear rejected flags");
1647 TRY_EXEC(
"UPDATE main.images SET flags = (flags | 8) & ~7 WHERE (flags & 7) = 6",
1648 "[init] can't set rejected flags");
1650 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1653 else if(version == 29)
1655 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1658 TRY_EXEC(
"ALTER TABLE main.tagged_images ADD COLUMN position INTEGER",
1659 "[init] can't add `position' column to tagged_images table in database\n");
1661 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.tagged_images_imgid_index ON tagged_images (imgid)",
1662 "[init] can't create image index on tagged_images\n");
1663 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.tagged_images_position_index ON tagged_images (position)",
1664 "[init] can't create position index on tagged_images\n");
1665 TRY_EXEC(
"UPDATE main.tagged_images SET position = (tagid + imgid) << 32",
1666 "[init] can't populate position on tagged_images\n");
1670 TRY_EXEC(
"CREATE TABLE main.i (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1671 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
1672 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
1673 "focus_distance REAL, datetime_taken CHAR(20), flags INTEGER, "
1674 "output_width INTEGER, output_height INTEGER, crop REAL, "
1675 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
1676 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
1677 "license VARCHAR, sha1sum CHAR(40), "
1678 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
1679 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
1680 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, "
1681 "aspect_ratio REAL, exposure_bias REAL, "
1682 "import_timestamp INTEGER DEFAULT -1, change_timestamp INTEGER DEFAULT -1, "
1683 "export_timestamp INTEGER DEFAULT -1, print_timestamp INTEGER DEFAULT -1)",
1684 "[init] can't create table i\n");
1686 TRY_EXEC(
"INSERT INTO main.i SELECT id, group_id, film_id, width, height, filename, maker, model,"
1687 " lens, exposure, aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1688 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1689 " raw_auto_bright_threshold, raw_black, raw_maximum, license, sha1sum,"
1690 " orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version,"
1691 " max_version, write_timestamp, history_end, position, aspect_ratio, exposure_bias,"
1692 " import_timestamp, change_timestamp, export_timestamp, print_timestamp "
1694 "[init] can't populate table i\n");
1696 "[init] can't drop table images\n");
1697 TRY_EXEC(
"ALTER TABLE main.i RENAME TO images",
1698 "[init] can't rename i to images\n");
1700 TRY_EXEC(
"CREATE INDEX main.images_group_id_index ON images (group_id)",
1701 "[init] can't create group_id index on images table\n");
1702 TRY_EXEC(
"CREATE INDEX main.images_film_id_index ON images (film_id)",
1703 "[init] can't create film_id index on images table\n");
1704 TRY_EXEC(
"CREATE INDEX main.images_filename_index ON images (filename)",
1705 "[init] can't create filename index on images table\n");
1706 TRY_EXEC(
"CREATE INDEX main.image_position_index ON images (position)",
1707 "[init] can't create position index on images table\n");
1709 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1712 else if(version == 30)
1714 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1717 TRY_EXEC(
"DROP INDEX IF EXISTS `history_imgid_index`",
1718 "[init] can't drop history_imgid_index\n");
1719 TRY_EXEC(
"CREATE INDEX `history_imgid_index` ON `history` ( `imgid`, `operation` )",
1720 "[init] can't recreate history_imgid_index\n");
1722 TRY_EXEC(
"DROP INDEX IF EXISTS `images_filename_index`",
1723 "[init] can't drop images_filename_index\n");
1724 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
1725 "[init] can't recreate images_filename_index\n");
1727 TRY_EXEC(
"DROP INDEX IF EXISTS `images_film_id_index`",
1728 "[init] can't drop images_film_id_index\n");
1729 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
1730 "[init] can't recreate images_film_id_index\n");
1732 TRY_EXEC(
"DROP INDEX IF EXISTS `images_group_id_index`",
1733 "[init] can't drop images_group_id_index\n");
1734 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
1735 "[init] can't recreate images_group_id_index\n");
1737 TRY_EXEC(
"DROP INDEX IF EXISTS `masks_history_imgid_index`",
1738 "[init] can't drop masks_history_imgid_index\n");
1739 TRY_EXEC(
"CREATE INDEX `masks_history_imgid_index` ON `masks_history` ( `imgid`, `num` )",
1740 "[init] can't recreate masks_history_imgid_index\n");
1743 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( `latitude` DESC, `longitude` DESC )",
1744 "[init] can't create images_latlong_index\n");
1746 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1749 else if(version == 31)
1751 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1754 TRY_EXEC(
"DELETE FROM main.meta_data WHERE rowid NOT IN (SELECT MIN(rowid) "
1755 "FROM main.meta_data GROUP BY id, key)",
1756 "[init] can't remove duplicates from meta_data\n");
1759 TRY_EXEC(
"DROP INDEX IF EXISTS metadata_index",
1760 "[init] can't drop metadata_index\n");
1761 TRY_EXEC(
"CREATE UNIQUE INDEX main.metadata_index ON meta_data (id, key)",
1762 "[init] can't create metadata_index\n");
1764 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
1767 else if(version == 32)
1769 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
1777 TRY_EXEC(
"ALTER TABLE `images` RENAME TO `images_old`",
1778 "[init] can't rename images\n");
1780 TRY_EXEC(
"CREATE TABLE `images` (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
1781 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, lens VARCHAR, "
1782 "exposure REAL, aperture REAL, iso REAL, focal_length REAL, focus_distance REAL, datetime_taken CHAR(20), "
1783 "flags INTEGER, output_width INTEGER, output_height INTEGER, crop REAL, "
1784 "raw_parameters INTEGER, raw_denoise_threshold REAL, raw_auto_bright_threshold REAL, "
1785 "raw_black INTEGER, raw_maximum INTEGER, license VARCHAR, sha1sum CHAR(40), "
1786 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, latitude REAL, altitude REAL, "
1787 "color_matrix BLOB, colorspace INTEGER, version INTEGER, max_version INTEGER, write_timestamp INTEGER, "
1788 "history_end INTEGER, position INTEGER, aspect_ratio REAL, exposure_bias REAL, "
1789 "import_timestamp INTEGER DEFAULT -1, change_timestamp INTEGER DEFAULT -1, export_timestamp INTEGER DEFAULT -1, print_timestamp INTEGER DEFAULT -1, "
1790 "FOREIGN KEY(film_id) REFERENCES film_rolls(id) ON DELETE CASCADE ON UPDATE CASCADE, "
1791 "FOREIGN KEY(group_id) REFERENCES images(id) ON DELETE RESTRICT ON UPDATE CASCADE)",
1792 "[init] can't create new images table\n");
1795 TRY_EXEC(
"DELETE FROM `images_old` WHERE film_id NOT IN (SELECT id FROM `film_rolls`)",
1796 "[init] can't delete images with invalid film id\n");
1798 TRY_EXEC(
"UPDATE `images_old` SET group_id=id WHERE group_id NOT IN (SELECT id from `images_old`)",
1799 "[init] can't fix invalid group ids\n");
1801 TRY_EXEC(
"INSERT INTO `images` SELECT * FROM `images_old`",
1802 "[init] can't copy back from images_old\n");
1805 TRY_EXEC(
"DROP INDEX IF EXISTS `image_position_index`",
1806 "[init] can't drop image_position_index\n");
1807 TRY_EXEC(
"CREATE INDEX `image_position_index` ON `images` (position)",
1808 "[init] can't add image_position_index\n");
1811 TRY_EXEC(
"DROP INDEX IF EXISTS `images_filename_index`",
1812 "[init] can't drop images_filename_index\n");
1813 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
1814 "[init] can't recreate images_filename_index\n");
1816 TRY_EXEC(
"DROP INDEX IF EXISTS `images_film_id_index`",
1817 "[init] can't drop images_film_id_index\n");
1818 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
1819 "[init] can't recreate images_film_id_index\n");
1821 TRY_EXEC(
"DROP INDEX IF EXISTS `images_group_id_index`",
1822 "[init] can't drop images_group_id_index\n");
1823 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
1824 "[init] can't recreate images_group_id_index\n");
1826 TRY_EXEC(
"DROP INDEX IF EXISTS `images_latlong_index`",
1827 "[init] can't drop images_latlong_index\n");
1828 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( latitude DESC, longitude DESC )",
1829 "[init] can't add images_latlong_index\n");
1831 TRY_EXEC(
"DROP TABLE `images_old`",
1832 "[init] can't drop table images_old\n");
1835 TRY_EXEC(
"ALTER TABLE `history` RENAME TO `history_old`",
1836 "[init] can't rename history\n");
1838 TRY_EXEC(
"CREATE TABLE `history` (imgid INTEGER, num INTEGER, module INTEGER, "
1839 "operation VARCHAR(256), op_params BLOB, enabled INTEGER, blendop_params BLOB, blendop_version INTEGER, "
1840 "multi_priority INTEGER, multi_name VARCHAR(256), "
1841 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1842 "[init] can't create new history table\n");
1844 TRY_EXEC(
"DELETE FROM `history_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1845 "[init] can't delete orphaned history elements\n");
1847 TRY_EXEC(
"INSERT INTO history SELECT * FROM history_old",
1848 "[init] can't copy back from history_old\n");
1850 TRY_EXEC(
"DROP INDEX IF EXISTS `history_imgid_index`",
1851 "[init] can't drop history_imgid_index\n");
1852 TRY_EXEC(
"CREATE INDEX `history_imgid_op_index` ON `history` ( `imgid`, `operation` )",
1853 "[init] can't recreate history_imgid_index\n");
1854 TRY_EXEC(
"CREATE INDEX `history_imgid_num_index` ON `history` ( `imgid`, `num` DESC )",
1855 "[init] can't recreate history_imgid_index\n");
1857 TRY_EXEC(
"DROP TABLE `history_old`",
1858 "[init] can't drop table history_old\n");
1861 TRY_EXEC(
"ALTER TABLE `history_hash` RENAME TO `history_hash_old`",
1862 "[init] can't rename history_hash\n");
1864 TRY_EXEC(
"CREATE TABLE `history_hash` (imgid INTEGER PRIMARY KEY, basic_hash BLOB, auto_hash BLOB, current_hash BLOB, "
1865 "mipmap_hash BLOB, FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1866 "[init] can't create new history_hash table\n");
1868 TRY_EXEC(
"DELETE FROM `history_hash_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1869 "[init] can't delete orphaned history_hash elements\n");
1871 TRY_EXEC(
"INSERT INTO `history_hash` SELECT * FROM `history_hash_old`",
1872 "[init] can't copy back from history_hash_old\n");
1874 TRY_EXEC(
"DROP TABLE `history_hash_old`",
1875 "[init] can't drop table history_hash_old\n");
1878 TRY_EXEC(
"ALTER TABLE `tagged_images` RENAME TO `tagged_images_old`",
1879 "[init] can't rename tagged_images\n");
1881 TRY_EXEC(
"CREATE TABLE `tagged_images` (imgid integer, tagid integer, position INTEGER, "
1882 "primary key(imgid, tagid), FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1883 "[init] can't create new tagged_images table\n");
1885 TRY_EXEC(
"DELETE FROM `tagged_images_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1886 "[init] can't delete orphaned tagged_images elements\n");
1888 TRY_EXEC(
"INSERT INTO `tagged_images` SELECT * FROM `tagged_images_old`",
1889 "[init] can't copy back from tagged_images_old\n");
1892 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_imgid_index",
1893 "[init] can't drop tagged_images_imgid_index\n");
1894 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_position_index",
1895 "[init] can't drop tagged_images_position_index\n");
1896 TRY_EXEC(
"CREATE INDEX tagged_images_position_index ON tagged_images (position)",
1897 "[init] can't add index tagged_images_position_index\n");
1898 TRY_EXEC(
"DROP INDEX IF EXISTS tagged_images_tagid_index",
1899 "[init] can't drop tagged_images_tagid_index\n");
1900 TRY_EXEC(
"CREATE INDEX tagged_images_tagid_index ON tagged_images (tagid)",
1901 "[init] can't add index tagged_images_tagid_index\n");
1903 TRY_EXEC(
"DROP TABLE `tagged_images_old`",
1904 "[init] can't drop table tagged_images_old\n");
1907 TRY_EXEC(
"ALTER TABLE `masks_history` RENAME TO `masks_history_old`",
1908 "[init] can't rename masks_history\n");
1910 TRY_EXEC(
"CREATE TABLE masks_history (imgid INTEGER, num INTEGER, formid INTEGER, form INTEGER, "
1911 "name VARCHAR(256), version INTEGER, points BLOB, points_count INTEGER, source BLOB, "
1912 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1913 "[init] can't create new masks_history table\n");
1915 TRY_EXEC(
"DELETE FROM `masks_history_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1916 "[init] can't delete orphaned masks_history elements\n");
1918 TRY_EXEC(
"INSERT INTO `masks_history` SELECT * FROM `masks_history_old`",
1919 "[init] can't copy back from masks_history\n");
1921 TRY_EXEC(
"DROP INDEX IF EXISTS `masks_history_imgid_index`",
1922 "[init] can't drop masks_history_imgid_index\n");
1923 TRY_EXEC(
"CREATE INDEX `masks_history_imgid_index` ON `masks_history` ( imgid, num )",
1924 "[init] can't recreate masks_history_imgid_index\n");
1926 TRY_EXEC(
"DROP TABLE masks_history_old",
1927 "[init] can't drop table masks_history_old\n");
1930 TRY_EXEC(
"ALTER TABLE `color_labels` RENAME TO `color_labels_old`",
1931 "[init] can't rename color_labels\n");
1932 TRY_EXEC(
"CREATE TABLE `color_labels` (imgid INTEGER, color INTEGER, "
1933 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1934 "[init] can't create new color_labels table\n");
1936 TRY_EXEC(
"DELETE FROM `color_labels_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1937 "[init] can't delete orphaned color_labels elements\n");
1939 TRY_EXEC(
"INSERT INTO `color_labels` SELECT * FROM `color_labels_old`",
1940 "[init] can't copy back from color_labels\n");
1942 TRY_EXEC(
"DROP TABLE color_labels_old",
1943 "[init] can't drop table color_labels_old\n");
1945 TRY_EXEC(
"CREATE UNIQUE INDEX `color_labels_idx` ON `color_labels` (imgid, color)",
1946 "[init] can't recreate color_labels_idx\n");
1949 TRY_EXEC(
"ALTER TABLE `meta_data` RENAME TO `meta_data_old`",
1950 "[init] can't rename meta_data\n");
1951 TRY_EXEC(
"CREATE TABLE `meta_data` (id integer, key integer, value varchar, "
1952 "FOREIGN KEY(id) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1953 "[init] can't create new meta_data table\n");
1955 TRY_EXEC(
"DELETE FROM `meta_data_old` WHERE id NOT IN (SELECT id FROM `images`)",
1956 "[init] can't delete orphaned meta_data elements\n");
1958 TRY_EXEC(
"INSERT INTO `meta_data` SELECT * FROM `meta_data_old`",
1959 "[init] can't copy back from meta_data\n");
1961 TRY_EXEC(
"DROP TABLE meta_data_old",
1962 "[init] can't drop table meta_data_old\n");
1964 TRY_EXEC(
"CREATE UNIQUE INDEX `metadata_index` ON `meta_data` (id, key, value)",
1965 "[init] can't recreate metadata_index\n");
1968 TRY_EXEC(
"ALTER TABLE `selected_images` RENAME TO `selected_images_old`",
1969 "[init] can't rename selected_images\n");
1970 TRY_EXEC(
"CREATE TABLE `selected_images` (imgid INTEGER PRIMARY KEY, "
1971 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1972 "[init] can't create new selected_images table\n");
1974 TRY_EXEC(
"DELETE FROM `selected_images_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1975 "[init] can't delete orphaned selected_images elements\n");
1977 TRY_EXEC(
"INSERT INTO `selected_images` SELECT * FROM `selected_images_old`",
1978 "[init] can't copy back selected_images meta_data\n");
1980 TRY_EXEC(
"DROP TABLE selected_images_old",
1981 "[init] can't drop table selected_images_old\n");
1984 TRY_EXEC(
"ALTER TABLE `module_order` RENAME TO `module_order_old`",
1985 "[init] can't rename module_order\n");
1986 TRY_EXEC(
"CREATE TABLE `module_order` (imgid INTEGER PRIMARY KEY, version INTEGER, iop_list VARCHAR, "
1987 "FOREIGN KEY(imgid) REFERENCES images(id) ON DELETE CASCADE ON UPDATE CASCADE)",
1988 "[init] can't create new module_order table\n");
1990 TRY_EXEC(
"DELETE FROM `module_order_old` WHERE imgid NOT IN (SELECT id FROM `images`)",
1991 "[init] can't delete orphaned module_order elements\n");
1993 TRY_EXEC(
"INSERT INTO `module_order` SELECT * FROM `module_order_old`",
1994 "[init] can't copy back module_order meta_data\n");
1996 TRY_EXEC(
"DROP TABLE module_order_old",
1997 "[init] can't drop table module_order_old\n");
1999 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
2002 else if(version == 33)
2004 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
2006 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.images_datetime_taken_nc ON images (datetime_taken COLLATE NOCASE)",
2007 "[init] can't create images_datetime_taken\n");
2008 TRY_EXEC(
"CREATE INDEX IF NOT EXISTS main.metadata_index_key ON meta_data (key)",
2009 "[init] can't create metadata_index_key\n");
2011 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
2014 else if(version == 34)
2016 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
2017 sqlite3_exec(db->
handle,
"BEGIN TRANSACTION", NULL, NULL, NULL);
2019 TRY_EXEC(
"CREATE TABLE main.images_new (id INTEGER PRIMARY KEY AUTOINCREMENT, group_id INTEGER, film_id INTEGER, "
2020 "width INTEGER, height INTEGER, filename VARCHAR, maker VARCHAR, model VARCHAR, "
2021 "lens VARCHAR, exposure REAL, aperture REAL, iso REAL, focal_length REAL, "
2022 "focus_distance REAL, datetime_taken INTEGER, flags INTEGER, "
2023 "output_width INTEGER, output_height INTEGER, crop REAL, "
2024 "raw_parameters INTEGER, raw_denoise_threshold REAL, "
2025 "raw_auto_bright_threshold REAL, raw_black INTEGER, raw_maximum INTEGER, "
2026 "license VARCHAR, sha1sum CHAR(40), "
2027 "orientation INTEGER, histogram BLOB, lightmap BLOB, longitude REAL, "
2028 "latitude REAL, altitude REAL, color_matrix BLOB, colorspace INTEGER, version INTEGER, "
2029 "max_version INTEGER, write_timestamp INTEGER, history_end INTEGER, position INTEGER, "
2030 "aspect_ratio REAL, exposure_bias REAL, "
2031 "import_timestamp INTEGER, change_timestamp INTEGER, "
2032 "export_timestamp INTEGER, print_timestamp INTEGER, "
2033 "FOREIGN KEY(film_id) REFERENCES film_rolls(id) ON DELETE CASCADE ON UPDATE CASCADE, "
2034 "FOREIGN KEY(group_id) REFERENCES images(id) ON DELETE RESTRICT ON UPDATE CASCADE)",
2035 "[init] can't create new images table\n");
2037 TRY_EXEC(
"INSERT INTO `images_new` SELECT "
2038 "id, group_id, film_id, width, height, filename, maker, model, "
2039 "lens, exposure, aperture, iso, focal_length, focus_distance, NULL AS datetime_taken, flags, "
2040 "output_width, output_height, crop, raw_parameters, raw_denoise_threshold, raw_auto_bright_threshold, raw_black, raw_maximum, "
2041 "license, sha1sum, orientation, histogram, lightmap, longitude, latitude, altitude, color_matrix, colorspace, version, "
2042 "max_version, write_timestamp, history_end, position, aspect_ratio, exposure_bias, "
2043 "NULL AS import_timestamp, NULL AS change_timestamp, NULL AS export_timestamp, NULL AS print_timestamp "
2045 "[init] can't copy back from images\n");
2048 " CASE WHEN datetime_taken = '' THEN NULL ELSE datetime_taken END,"
2049 " CASE WHEN import_timestamp = -1 THEN NULL ELSE import_timestamp END,"
2050 " CASE WHEN change_timestamp = -1 THEN NULL ELSE change_timestamp END,"
2051 " CASE WHEN export_timestamp = -1 THEN NULL ELSE export_timestamp END,"
2052 " CASE WHEN print_timestamp = -1 THEN NULL ELSE print_timestamp END "
2054 "[init] can't get datetime from images\n");
2055 while(sqlite3_step(stmt) == SQLITE_ROW)
2057 sqlite3_stmt *stmt2;
2058 sqlite3_prepare_v2(db->
handle,
2059 "UPDATE `images_new` SET"
2060 " (datetime_taken, import_timestamp,"
2061 " change_timestamp, export_timestamp, print_timestamp) = "
2062 " (?2, ?3, ?4, ?5, ?6) WHERE id = ?1",
2064 sqlite3_bind_int(stmt2, 1, sqlite3_column_int(stmt, 0));
2065 if(sqlite3_column_type(stmt, 1) != SQLITE_NULL)
2071 g_date_time_unref(gdt);
2074 for(
int i = 0;
i < 4;
i++)
2076 if(sqlite3_column_type(stmt,
i + 2) != SQLITE_NULL)
2078 GDateTime *gdt = g_date_time_new_from_unix_utc(sqlite3_column_int(stmt,
i + 2));
2082 g_date_time_unref(gdt);
2086 TRY_STEP(stmt2, SQLITE_DONE,
"[init] can't update datetimes into images_new table\n");
2087 sqlite3_finalize(stmt2);
2089 sqlite3_finalize(stmt);
2091 TRY_EXEC(
"DROP TABLE `images`",
"[init] can't drop images table\n");
2093 TRY_EXEC(
"ALTER TABLE `images_new` RENAME TO `images`",
"[init] can't rename images_new table to images");
2096 TRY_EXEC(
"CREATE INDEX `image_position_index` ON `images` (position)",
2097 "[init] can't add image_position_index\n");
2098 TRY_EXEC(
"CREATE INDEX `images_filename_index` ON `images` ( `filename`, `version` )",
2099 "[init] can't recreate images_filename_index\n");
2100 TRY_EXEC(
"CREATE INDEX `images_film_id_index` ON `images` ( `film_id`, `filename` )",
2101 "[init] can't recreate images_film_id_index\n");
2102 TRY_EXEC(
"CREATE INDEX `images_group_id_index` ON `images` ( `group_id`, `id` )",
2103 "[init] can't recreate images_group_id_index\n");
2104 TRY_EXEC(
"CREATE INDEX `images_latlong_index` ON `images` ( latitude DESC, longitude DESC )",
2105 "[init] can't add images_latlong_index\n");
2106 TRY_EXEC(
"CREATE INDEX `images_datetime_taken` ON images (datetime_taken)",
2107 "[init] can't create images_datetime_taken\n");
2109 sqlite3_exec(db->
handle,
"COMMIT", NULL, NULL, NULL);
2110 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = ON", NULL, NULL, NULL);
2113 else if(version == 35)
2115 TRY_EXEC(
"CREATE TABLE main.images_new (id INTEGER, filename VARCHAR, flags INTEGER)",
2116 "[init] can't create new images table\n");
2118 gchar *query = g_strdup_printf(
"INSERT INTO `images_new` "
2119 "SELECT id, filename, flags"
2121 " WHERE (flags & %d == 0)",
2123 TRY_EXEC(query,
"[init] can't copy back from images\n");
2125 TRY_PREPARE(stmt,
"SELECT id, filename, flags FROM `images_new`",
2126 "[init] can't prepare selecting images flags\n");
2128 while(sqlite3_step(stmt) == SQLITE_ROW)
2130 sqlite3_stmt *stmt2;
2131 sqlite3_prepare_v2(db->
handle,
2132 "UPDATE `images` SET"
2134 " (?2) WHERE id = ?1",
2136 sqlite3_bind_int(stmt2, 1, sqlite3_column_int(stmt, 0));
2139 gchar *ext = g_strrstr((
const char *)sqlite3_column_text(stmt, 1),
".");
2141 sqlite3_bind_int(stmt2, 2,
flags);
2143 TRY_STEP(stmt2, SQLITE_DONE,
"[init] can't update flags\n");
2144 sqlite3_finalize(stmt2);
2146 sqlite3_finalize(stmt);
2148 TRY_EXEC(
"DROP TABLE `images_new`",
"[init] can't drop temp images table\n");
2152 new_version = version;
2155 sqlite3_prepare_v2(db->
handle,
"INSERT OR REPLACE INTO main.db_info (key, value) VALUES ('version', ?1)", -1, &stmt,
2157 sqlite3_bind_int(stmt, 1, new_version);
2159 sqlite3_finalize(stmt);
3111 const char *
const alternative = params->alternative;
3112 const gboolean load_data = params->load_data;
3113 const gboolean has_gui = params->has_gui;
3116 sqlite3_config(SQLITE_CONFIG_SERIALIZED);
3118 sqlite3_initialize();
3128 gchar *migrated_name = NULL;
3129 const char *library = params->library;
3138 gchar *previous_name = migrated_name;
3142 library = migrated_name;
3147 migrated_name = previous_name;
3155 gchar *dbname = NULL;
3164 dbname = g_strdup(library);
3167 else if(!strcmp(dbname,
":memory:"))
3168 g_strlcpy(dbfilename_library, dbname,
sizeof(dbfilename_library));
3169 else if(dbname[0] !=
'/')
3172 g_strlcpy(dbfilename_library, dbname,
sizeof(dbfilename_library));
3176 g_strlcpy(dbfilename_library, alternative,
sizeof(dbfilename_library));
3178 GFile *galternative = g_file_new_for_path(alternative);
3179 dbname = g_file_get_basename(galternative);
3180 g_object_unref(galternative);
3188 snprintf(dbfilename_data,
sizeof(dbfilename_data),
":memory:");
3201 if(g_strcmp0(dbfilename_data,
":memory:"))
3203 char *data_path = g_path_get_dirname(dbfilename_data);
3204 g_mkdir_with_parents(data_path, 0750);
3208 if(g_strcmp0(dbfilename_library,
":memory:"))
3210 char *library_path = g_path_get_dirname(dbfilename_library);
3211 g_mkdir_with_parents(library_path, 0750);
3216 _db_print(
"[init sql] library: %s, data: %s\n", dbfilename_library, dbfilename_data);
3224 fprintf(stderr,
"[init] database is locked, probably another process is already using it\n");
3233 fprintf(stderr,
"[init] could not find database ");
3235 fprintf(stderr,
"`%s'!\n", dbname);
3237 fprintf(stderr,
"\n");
3238 fprintf(stderr,
"[init] maybe your %s/anselrc is corrupt?\n", datadir);
3240 fprintf(stderr,
"[init] try `cp %s/anselrc %s/anselrc'\n", dbfilename_library, datadir);
3241 sqlite3_close(db->
handle);
3257 if(sqlite3_db_readonly(db->
handle,
"main") != 0)
3275 sqlite3_exec(db->
handle,
"attach database ':memory:' as memory", NULL, NULL, NULL);
3278 gboolean have_data_db = load_data && g_file_test(dbfilename_data, G_FILE_TEST_EXISTS);
3279 rc = sqlite3_prepare_v2(db->
handle,
"ATTACH DATABASE ?1 AS data", -1, &stmt, NULL);
3280 sqlite3_bind_text(stmt, 1, dbfilename_data, -1, SQLITE_TRANSIENT);
3281 if(rc != SQLITE_OK || sqlite3_step(stmt) != SQLITE_DONE)
3283 sqlite3_finalize(stmt);
3284 fprintf(stderr,
"[init] database `%s' couldn't be opened. aborting\n", dbfilename_data);
3289 sqlite3_finalize(stmt);
3291 _db_print(
"[sql] Opened database: '%s'\n", dbfilename_data);
3294 sqlite3_exec(db->
handle,
"PRAGMA synchronous = OFF", NULL, NULL, NULL);
3295 sqlite3_exec(db->
handle,
"PRAGMA journal_mode = MEMORY", NULL, NULL, NULL);
3296 sqlite3_exec(db->
handle,
"PRAGMA page_size = 32768", NULL, NULL, NULL);
3300 sqlite3_exec(db->
handle,
"PRAGMA foreign_keys = ON", NULL, NULL, NULL);
3302 _db_print(
"[sql] Configured database\n");
3315 rc = sqlite3_prepare_v2(db->
handle,
"select value from data.db_info where key = 'version'", -1, &stmt, NULL);
3316 if(!g_strcmp0(data_status,
"ok") && rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW)
3321 const int db_version = sqlite3_column_int(stmt, 0);
3322 sqlite3_finalize(stmt);
3331 fprintf(stderr,
"[init] database `%s' couldn't be upgraded from version %d to %d. aborting\n",
3341 fprintf(stderr,
"[init] database version of `%s' is too new for this build of darktable. aborting\n",
3354 gchar* quick_check_text = NULL;
3355 if(g_strcmp0(data_status,
"ok"))
3357 quick_check_text = g_strdup_printf(_(
"quick_check said:\n"
3358 "%s \n"), data_status);
3362 quick_check_text = g_strdup(
"");
3371 =
_ask_corrupted(dbfilename_data, quick_check_text, data_snap != NULL);
3380 fprintf(stderr,
"[init] database `%s' is corrupt and can't be opened! either replace it from a backup or "
3381 "delete the file so that darktable can create a new one the next time. aborting\n", dbfilename_data);
3388 fprintf(stderr,
"[init] deleting `%s' on user request", dbfilename_data);
3390 if(g_unlink(dbfilename_data) == 0)
3391 fprintf(stderr,
" ... ok\n");
3393 fprintf(stderr,
" ... failed\n");
3397 fprintf(stderr,
"[init] restoring `%s' from `%s'...", dbfilename_data, data_snap);
3398 GError *gerror = NULL;
3399 if(!g_file_test(dbfilename_data, G_FILE_TEST_EXISTS))
3401 GFile *src = g_file_new_for_path(data_snap);
3402 GFile *dest = g_file_new_for_path(dbfilename_data);
3403 gboolean copy_status =
TRUE;
3404 if(g_file_test(data_snap, G_FILE_TEST_EXISTS))
3406 copy_status = g_file_copy(src, dest, G_FILE_COPY_NONE, NULL, NULL, NULL, &gerror);
3407 if(copy_status) copy_status = g_chmod(dbfilename_data, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == 0;
3412 const int fd = g_open(dbfilename_data, O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
3413 if(fd < 0 || !g_close(fd, &gerror)) copy_status =
FALSE;
3416 fprintf(stderr,
" success!\n");
3418 fprintf(stderr,
" failed!\n");
3420 g_object_unref(src);
3421 g_object_unref(dest);
3430 _db_print(
"[sql] Checked database schema and migrated if needed\n");
3435 rc = sqlite3_prepare_v2(db->
handle,
"select value from main.db_info where key = 'version'", -1, &stmt, NULL);
3436 if(!g_strcmp0(libdb_status,
"ok") && rc == SQLITE_OK && sqlite3_step(stmt) == SQLITE_ROW)
3441 const int db_version = sqlite3_column_int(stmt, 0);
3442 sqlite3_finalize(stmt);
3452 fprintf(stderr,
"[init] database `%s' couldn't be upgraded from version %d to %d. aborting\n", dbname,
3462 fprintf(stderr,
"[init] database version of `%s' is too new for this build of darktable. aborting\n",
3470 else if(g_strcmp0(libdb_status,
"ok") || rc == SQLITE_CORRUPT || rc == SQLITE_NOTADB)
3475 gchar* quick_check_text = NULL;
3476 if(g_strcmp0(libdb_status,
"ok"))
3478 quick_check_text = g_strdup_printf(_(
"quick_check said:\n"
3479 "%s \n"), libdb_status);
3483 quick_check_text = g_strdup(
"");
3492 =
_ask_corrupted(dbfilename_library, quick_check_text, data_snap != NULL);
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");
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);
3591 _db_print(
"[sql] Created in-memory DB\n");
3596 _db_print(
"[sql] Loaded legacy presets\n");
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);
4236 GTimeSpan span_from_last_snap_required = 0;
4237 gboolean decided_answer =
FALSE;
4239 return decided_answer;
4246 _db_print(
"[db backup] checking snapshots existence.\n");
4248 GFile *parent = g_file_get_parent(library);
4252 _db_print(
"[db backup] couldn't get library parent!.\n");
4253 g_object_unref(library);
4257 GError *
error = NULL;
4258 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);
4262 _db_print(
"[db backup] couldn't enumerate library parent: %s.\n",
error->message);
4263 g_object_unref(parent);
4264 g_object_unref(library);
4265 g_error_free(
error);
4269 gchar *lib_basename = g_file_get_basename(library);
4270 g_object_unref(library);
4272 gchar *lib_snap_format = g_strdup_printf(
"%s-snp-", lib_basename);
4273 gchar *lib_backup_format = g_strdup_printf(
"%s-pre-", lib_basename);
4276 GFileInfo *info = NULL;
4277 guint64 last_snap = 0;
4279 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4281 const char* fname = g_file_info_get_name(info);
4282 if(g_str_has_prefix(fname, lib_snap_format) || g_str_has_prefix(fname, lib_backup_format))
4284 _db_print(
"[db backup] found file: %s.\n", fname);
4287 last_snap = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
4288 g_object_unref(info);
4291 const guint64 try_snap = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
4292 if(try_snap > last_snap)
4294 last_snap = try_snap;
4297 g_object_unref(info);
4299 g_object_unref(parent);
4305 _db_print(
"[db backup] problem enumerating library parent: %s.\n",
error->message);
4306 g_file_enumerator_close(library_dir_files, NULL, NULL);
4307 g_object_unref(library_dir_files);
4308 g_error_free(
error);
4312 g_file_enumerator_close(library_dir_files, NULL, NULL);
4313 g_object_unref(library_dir_files);
4315 GDateTime *date_now = g_date_time_new_now_local();
4318 GDateTime *date_last_snap = g_date_time_new_from_unix_local(last_snap);
4320 gchar *now_txt = g_date_time_format(date_now,
"%Y%m%d%H%M%S");
4321 gchar *ls_txt = g_date_time_format(date_last_snap,
"%Y%m%d%H%M%S");
4322 _db_print(
"[db backup] last snap: %s; curr date: %s.\n", ls_txt, now_txt);
4326 GTimeSpan span_from_last_snap = g_date_time_difference(date_now, date_last_snap);
4328 g_date_time_unref(date_now);
4329 g_date_time_unref(date_last_snap);
4331 return span_from_last_snap > span_from_last_snap_required;
4424 _db_print(
"[db backup] checking snapshots existence.\n");
4426 GFile *lib_parent = g_file_get_parent(lib_file);
4430 _db_print(
"[db backup] couldn't get library parent!.\n");
4431 g_object_unref(lib_file);
4436 GFile *dat_parent = g_file_get_parent(dat_file);
4440 _db_print(
"[db backup] couldn't get data parent!.\n");
4441 g_object_unref(dat_file);
4442 g_object_unref(lib_file);
4443 g_object_unref(lib_parent);
4446 gchar *lib_basename = g_file_get_basename(lib_file);
4447 g_object_unref(lib_file);
4448 gchar *lib_snap_format = g_strdup_printf(
"%s-snp-", lib_basename);
4449 gchar *lib_tmp_format = g_strdup_printf(
"%s-tmp-", lib_basename);
4452 gchar *dat_basename = g_file_get_basename(dat_file);
4453 g_object_unref(dat_file);
4454 gchar *dat_snap_format = g_strdup_printf(
"%s-snp-", dat_basename);
4455 gchar *dat_tmp_format = g_strdup_printf(
"%s-tmp-", dat_basename);
4458 GQueue *lib_snaps = g_queue_new();
4459 GQueue *dat_snaps = g_queue_new();
4460 GQueue *tmplib_snaps = g_queue_new();
4461 GQueue *tmpdat_snaps = g_queue_new();
4463 if(g_file_equal(lib_parent, dat_parent))
4466 GError *
error = NULL;
4467 GFileEnumerator *library_dir_files = g_file_enumerate_children(lib_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4471 _db_print(
"[db backup] couldn't enumerate library parent: %s.\n",
error->message);
4472 g_object_unref(lib_parent);
4473 g_object_unref(dat_parent);
4478 g_queue_free(lib_snaps);
4479 g_queue_free(dat_snaps);
4480 g_queue_free(tmplib_snaps);
4481 g_queue_free(tmpdat_snaps);
4482 g_error_free(
error);
4486 GFileInfo *info = NULL;
4488 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4490 const char* fname = g_file_info_get_name(info);
4491 if(g_str_has_prefix(fname, lib_snap_format))
4493 _db_print(
"[db backup] found file: %s.\n", fname);
4494 g_queue_insert_sorted(lib_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4496 else if(g_str_has_prefix(fname, dat_snap_format))
4498 _db_print(
"[db backup] found file: %s.\n", fname);
4499 g_queue_insert_sorted(dat_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4501 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4504 g_queue_push_head(tmplib_snaps, g_strdup(fname));
4506 g_object_unref(info);
4513 _db_print(
"[db backup] problem enumerating library parent: %s.\n",
error->message);
4514 g_object_unref(lib_parent);
4515 g_object_unref(dat_parent);
4518 g_queue_free_full(lib_snaps, g_free);
4519 g_queue_free_full(dat_snaps, g_free);
4520 g_queue_free_full(tmplib_snaps, g_free);
4521 g_queue_free_full(tmpdat_snaps, g_free);
4522 g_file_enumerator_close(library_dir_files, NULL, NULL);
4523 g_object_unref(library_dir_files);
4524 g_error_free(
error);
4527 g_file_enumerator_close(library_dir_files, NULL, NULL);
4528 g_object_unref(library_dir_files);
4534 GError *
error = NULL;
4535 GFileEnumerator *library_dir_files = g_file_enumerate_children(lib_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4538 _db_print(
"[db backup] couldn't enumerate library parent: %s.\n",
error->message);
4539 g_object_unref(lib_parent);
4540 g_object_unref(dat_parent);
4545 g_error_free(
error);
4546 g_queue_free(lib_snaps);
4547 g_queue_free(dat_snaps);
4548 g_queue_free(tmplib_snaps);
4549 g_queue_free(tmpdat_snaps);
4553 GFileEnumerator *data_dir_files = g_file_enumerate_children(dat_parent, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, NULL, &
error);
4556 _db_print(
"[db backup] couldn't enumerate data parent: %s.\n",
error->message);
4557 g_object_unref(lib_parent);
4558 g_object_unref(dat_parent);
4563 g_file_enumerator_close(library_dir_files, NULL, NULL);
4564 g_object_unref(library_dir_files);
4565 g_error_free(
error);
4566 g_queue_free(lib_snaps);
4567 g_queue_free(dat_snaps);
4568 g_queue_free(tmplib_snaps);
4569 g_queue_free(tmpdat_snaps);
4573 GFileInfo *info = NULL;
4575 while ((info = g_file_enumerator_next_file(library_dir_files, NULL, &
error)))
4577 const char* fname = g_file_info_get_name(info);
4578 if(g_str_has_prefix(fname, lib_snap_format))
4580 _db_print(
"[db backup] found file: %s.\n", fname);
4581 g_queue_insert_sorted(lib_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4583 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4586 g_queue_push_head(tmplib_snaps, g_strdup(fname));
4588 g_object_unref(info);
4594 _db_print(
"[db backup] problem enumerating library parent: %s.\n",
error->message);
4595 g_object_unref(lib_parent);
4596 g_object_unref(dat_parent);
4599 g_queue_free_full(lib_snaps, g_free);
4600 g_queue_free(dat_snaps);
4601 g_queue_free_full(tmplib_snaps, g_free);
4602 g_queue_free(tmpdat_snaps);
4603 g_file_enumerator_close(library_dir_files, NULL, NULL);
4604 g_object_unref(library_dir_files);
4605 g_file_enumerator_close(data_dir_files, NULL, NULL);
4606 g_object_unref(data_dir_files);
4607 g_error_free(
error);
4610 g_file_enumerator_close(library_dir_files, NULL, NULL);
4611 g_object_unref(library_dir_files);
4613 while ((info = g_file_enumerator_next_file(data_dir_files, NULL, &
error)))
4615 const char* fname = g_file_info_get_name(info);
4616 if(g_str_has_prefix(fname, dat_snap_format))
4618 _db_print(
"[db backup] found file: %s.\n", fname);
4619 g_queue_insert_sorted(dat_snaps, g_strdup(fname),
_db_snap_sort, NULL);
4621 else if(g_str_has_prefix(fname, lib_tmp_format) || g_str_has_prefix(fname, dat_tmp_format))
4624 g_queue_push_head(tmpdat_snaps, g_strdup(fname));
4626 g_object_unref(info);
4634 _db_print(
"[db backup] problem enumerating data parent: %s.\n",
error->message);
4635 g_object_unref(lib_parent);
4636 g_object_unref(dat_parent);
4637 g_queue_free_full(lib_snaps, g_free);
4638 g_queue_free_full(dat_snaps, g_free);
4639 g_queue_free_full(tmplib_snaps, g_free);
4640 g_queue_free_full(tmpdat_snaps, g_free);
4641 g_file_enumerator_close(data_dir_files, NULL, NULL);
4642 g_object_unref(data_dir_files);
4643 g_error_free(
error);
4647 g_file_enumerator_close(data_dir_files, NULL, NULL);
4648 g_object_unref(data_dir_files);
4654 GPtrArray *ret = g_ptr_array_new();
4656 gchar *lib_parent_path = g_file_get_path(lib_parent);
4657 g_object_unref(lib_parent);
4659 while(g_queue_get_length(lib_snaps) > keep_snaps)
4661 gchar *head = g_queue_pop_head(lib_snaps);
4662 g_ptr_array_add(ret, g_strconcat(lib_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4665 while(!g_queue_is_empty(tmplib_snaps))
4667 gchar *head = g_queue_pop_head(tmplib_snaps);
4668 g_ptr_array_add(ret, g_strconcat(lib_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4672 g_queue_free_full(lib_snaps, g_free);
4673 g_queue_free_full(tmplib_snaps, g_free);
4675 gchar *dat_parent_path = g_file_get_path(dat_parent);
4676 g_object_unref(dat_parent);
4678 while(g_queue_get_length(dat_snaps) > keep_snaps)
4680 gchar *head = g_queue_pop_head(dat_snaps);
4681 g_ptr_array_add(ret, g_strconcat(dat_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4684 while(!g_queue_is_empty(tmpdat_snaps))
4686 gchar *head = g_queue_pop_head(tmpdat_snaps);
4687 g_ptr_array_add(ret, g_strconcat(dat_parent_path, G_DIR_SEPARATOR_S, head, NULL));
4691 g_queue_free_full(dat_snaps, g_free);
4692 g_queue_free_full(tmpdat_snaps, g_free);
4694 g_ptr_array_add (ret, NULL);
4696 return (
char**)g_ptr_array_free(ret,
FALSE);