diff --git a/CMakeLists.txt b/CMakeLists.txt index 80a614e5d3a2c1a0276b220663a62dce818a1e83..722818a4e3c4297610512661c873ce880a562fbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ cmake_policy(SET CMP0009 NEW) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/utils/cmake/") set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(THREADS_PREFER_PTHREAD_FLAG ON) +SET(CMAKE_COLOR_MAKEFILE ON) set(COMMON_C_FLAGS -Wall -Wextra -Werror @@ -46,6 +47,8 @@ set(clang_C_FLAGS -Wno-unknown-warning-option ) +set(gnu_C_FLAGS) + # Find dependencies find_library(MPV_LIBRARY mpv REQUIRED) # tested with 0.32.0 find_library(SQLITE3_LIBRARY sqlite3 REQUIRED version>=3.31.0) # tested with 3.34.1 @@ -100,6 +103,7 @@ set(lektor_db_SOURCES src/database/cache.c src/database/open.c src/database/update.c + src/database/upgrade.c ) set(lektor_module_SOURCES @@ -292,5 +296,14 @@ if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang") target_compile_options(lektord PRIVATE ${clang_C_FLAGS}) target_compile_options(lkt PRIVATE ${clang_C_FLAGS}) target_compile_options(luka PRIVATE ${clang_C_FLAGS}) + + # Force the color output + add_compile_options(-fcolor-diagnostics) elseif (${CMAKE_C_COMPILER_ID} STREQUAL "GNU") + target_compile_options(lektord PRIVATE ${gnu_C_FLAGS}) + target_compile_options(lkt PRIVATE ${gnu_C_FLAGS}) + target_compile_options(luka PRIVATE ${gnu_C_FLAGS}) + + # Force the color output + add_compile_options(-fdiagnostics-color=always) endif() diff --git a/src/database/update.c b/src/database/update.c index e3fabacca0432da8b3031eb7121a8e9760a6c3d6..a441318d9049f331aa08940d752a31f32c1208a4 100644 --- a/src/database/update.c +++ b/src/database/update.c @@ -371,127 +371,6 @@ error: return ret; } -struct table_backup_description { - const char *name; - const char *fields[LKT_TABLE_FIELDS_MAX]; -}; - -#define DCL_SQL_TABLE(tbl_name, ...) \ - UNUSED static struct table_backup_description sql_table_##tbl_name = { \ - .name = #tbl_name, \ - .fields = { __VA_ARGS__, NULL }, \ - }; - -#define DCL_SQL_TABLE_NAMED(var_name, tbl_name, ...) \ - UNUSED static struct table_backup_description sql_table_##var_name = { \ - .name = #tbl_name, \ - .fields = { __VA_ARGS__, NULL }, \ - }; - -DCL_SQL_TABLE(kara, "id", "song_name", "source_name", "category", "song_type", "song_number", - "language", "file_path", "is_new", "author_name", "available"); -DCL_SQL_TABLE(kara_type, "id", "name"); -DCL_SQL_TABLE(kara_category, "id", "name"); -DCL_SQL_TABLE(language, "id", "name"); -DCL_SQL_TABLE(playlist, "id", "name", "last_update"); -DCL_SQL_TABLE(kara_playlist, "kara_id", "playlist_id"); -DCL_SQL_TABLE(queue, "position", "kara_id", "priority"); -DCL_SQL_TABLE(users, "username", "password"); -DCL_SQL_TABLE(updates, "job", "kara_id"); -DCL_SQL_TABLE(misc, "id", "last_update", "last_end_update", "update_job", "opened", "obfuscation"); -DCL_SQL_TABLE_NAMED(stickers, 'stickers', "id", "name") -DCL_SQL_TABLE_NAMED(stickers_kara, 'stickers.kara', "id", "sticker", "value") -DCL_SQL_TABLE_NAMED(stickers_plt, 'stickers.plt', "id", "sticker", "value") - -UNUSED static struct table_backup_description *sql_tables_list[] = { - &sql_table_kara, - &sql_table_kara_type, - &sql_table_kara_category, - &sql_table_language, - &sql_table_playlist, - &sql_table_kara_playlist, - &sql_table_queue, - &sql_table_users, - &sql_table_updates, - &sql_table_misc, - &sql_table_stickers, - &sql_table_stickers_kara, - &sql_table_stickers_plt, - NULL, -}; - -int -database_upgrade_scheme(va_list *db_list_ptr) -{ - lkt_db *db = va_arg((*db_list_ptr), lkt_db *); - LKT_DATABASE_VERSION version = database_get_version(db); - if (version == LKT_DATABASE_VERSION_LATEST) { - LOG_INFO("DB", "Don't upgrade the database scheme, already at the latest version"); - return true; - } - - /* Here we are sure to have the alpha version of things: basic `kara`, - * `playlist` tables. - * Here we need to: - * -> [x] create a tmp file from the disk.sql thing - * -> [x] attach it to the disk_new mount point - * -> copy all the data - * -> [x] detach the disk and disk_new - * -> [x] copy the tmp to the disk file - * -> [x] reattach the new disk file - */ - - /* Create a new db with the new schema and attach it */ - char new_db_path[PATH_MAX]; /* New db, will be moved to the db_path */ - char bak_db_path[PATH_MAX]; /* Backup de db_path */ - char db_path[PATH_MAX]; /* The production database location */ - RETURN_UNLESS(database_config_get_text(db, "database", "db_path", new_db_path, PATH_MAX), - "Failed to get the path to the db file", false) - safe_strncpy(db_path, new_db_path, PATH_MAX); - safe_strncpy(bak_db_path, new_db_path, PATH_MAX); - strncat(new_db_path, ".new", strlen(new_db_path) - sizeof(".new")); /* Includes '\0' */ - strncat(bak_db_path, ".bak", strlen(new_db_path) - sizeof(".bak")); /* Includes '\0' */ - - if (!access(new_db_path, R_OK | W_OK)) { - LOG_WARN("DB", "File '%s' already present, delete it before continuing", new_db_path); - RETURN_IF(unlink(new_db_path), "Failed to unlink already existing file", false); - } - - RETURN_UNLESS(database_init(new_db_path), "Failed to init new DB!", false); - RETURN_UNLESS(database_attach(db, LKT_PROTECTED_DATABASE_NEW, LKT_PROTECTED_DATABASE), - "Failed to attach the new database disk", false); - - /* Copy the content of the old db into the new one */ - // Tables to backup - // - kara - // - kara_type - // - kara_category - // - language - // - playlist - // - kara_playlist - // - queue - // - users - // - 'stickers' - // - 'stickers.kara' - // - 'stickers.plt' - // - 'updates' - // - misc - - /* Use the new DB */ - // database_detach(db, LKT_PROTECTED_DATABASE_NEW); - // database_detach(db, LKT_PROTECTED_DATABASE); - // RETURN_IF(rename(db_path, bak_db_path), "Failed to move old db to the backup location", false); - // if(rename(new_db_path, db_path)) { - // LOG_ERROR("DB", "Failed to move new db to the production location"); - // FAIL_UNLESS(rename(bak_db_path, db_path), "Failed to restore the production database from backup"); - // return false; - // } - // FAIL_UNLESS(database_attach(db, LKT_PROTECTED_DATABASE, db_path), "Failed to reattach database"); - - LOG_ERROR("DB", "Not implemented"); - return false; -} - #define sqlite_just_exec(func, query) \ void func(lkt_db *db) \ { \ diff --git a/src/database/upgrade.c b/src/database/upgrade.c new file mode 100644 index 0000000000000000000000000000000000000000..cbc85e8b884665d01d341da90da5d509d19bb8fb --- /dev/null +++ b/src/database/upgrade.c @@ -0,0 +1,127 @@ +#define _POSIX_C_SOURCE 200809L + +#include <lektor/common.h> +#include <lektor/database.h> +#include <lektor/internal/dbmacro.h> + +struct table_backup_description { + const char *name; + const char *fields[LKT_TABLE_FIELDS_MAX]; +}; + +#define DCL_SQL_TABLE(tbl_name, ...) \ + UNUSED static struct table_backup_description sql_table_##tbl_name = { \ + .name = #tbl_name, \ + .fields = { __VA_ARGS__, NULL }, \ + }; + +#define DCL_SQL_TABLE_NAMED(var_name, tbl_name, ...) \ + UNUSED static struct table_backup_description sql_table_##var_name = { \ + .name = #tbl_name, \ + .fields = { __VA_ARGS__, NULL }, \ + }; + +DCL_SQL_TABLE(kara, "id", "song_name", "source_name", "category", "song_type", "song_number", + "language", "file_path", "is_new", "author_name", "available"); +DCL_SQL_TABLE(kara_type, "id", "name"); +DCL_SQL_TABLE(kara_category, "id", "name"); +DCL_SQL_TABLE(language, "id", "name"); +DCL_SQL_TABLE(playlist, "id", "name", "last_update"); +DCL_SQL_TABLE(kara_playlist, "kara_id", "playlist_id"); +DCL_SQL_TABLE(queue, "position", "kara_id", "priority"); +DCL_SQL_TABLE(users, "username", "password"); +DCL_SQL_TABLE(updates, "job", "kara_id"); +DCL_SQL_TABLE(misc, "id", "last_update", "last_end_update", "update_job", "opened", "obfuscation"); +DCL_SQL_TABLE_NAMED(stickers, 'stickers', "id", "name") +DCL_SQL_TABLE_NAMED(stickers_kara, 'stickers.kara', "id", "sticker", "value") +DCL_SQL_TABLE_NAMED(stickers_plt, 'stickers.plt', "id", "sticker", "value") + +UNUSED static struct table_backup_description *sql_tables_list[] = { + &sql_table_kara, + &sql_table_kara_type, + &sql_table_kara_category, + &sql_table_language, + &sql_table_playlist, + &sql_table_kara_playlist, + &sql_table_queue, + &sql_table_users, + &sql_table_updates, + &sql_table_misc, + &sql_table_stickers, + &sql_table_stickers_kara, + &sql_table_stickers_plt, + NULL, +}; + +int +database_upgrade_scheme(va_list *db_list_ptr) +{ + lkt_db *db = va_arg((*db_list_ptr), lkt_db *); + LKT_DATABASE_VERSION version = database_get_version(db); + if (version == LKT_DATABASE_VERSION_LATEST) { + LOG_INFO("DB", "Don't upgrade the database scheme, already at the latest version"); + return true; + } + + /* Here we are sure to have the alpha version of things: basic `kara`, + * `playlist` tables. + * Here we need to: + * -> [x] create a tmp file from the disk.sql thing + * -> [x] attach it to the disk_new mount point + * -> copy all the data + * -> [x] detach the disk and disk_new + * -> [x] copy the tmp to the disk file + * -> [x] reattach the new disk file + */ + + /* Create a new db with the new schema and attach it */ + char new_db_path[PATH_MAX]; /* New db, will be moved to the db_path */ + char bak_db_path[PATH_MAX]; /* Backup de db_path */ + char db_path[PATH_MAX]; /* The production database location */ + RETURN_UNLESS(database_config_get_text(db, "database", "db_path", new_db_path, PATH_MAX), + "Failed to get the path to the db file", false) + safe_strncpy(db_path, new_db_path, PATH_MAX); + safe_strncpy(bak_db_path, new_db_path, PATH_MAX); + strncat(new_db_path, ".new", strlen(new_db_path) - sizeof(".new")); /* Includes '\0' */ + strncat(bak_db_path, ".bak", strlen(new_db_path) - sizeof(".bak")); /* Includes '\0' */ + + if (!access(new_db_path, R_OK | W_OK)) { + LOG_WARN("DB", "File '%s' already present, delete it before continuing", new_db_path); + RETURN_IF(unlink(new_db_path), "Failed to unlink already existing file", false); + } + + RETURN_UNLESS(database_init(new_db_path), "Failed to init new DB!", false); + RETURN_UNLESS(database_attach(db, LKT_PROTECTED_DATABASE_NEW, LKT_PROTECTED_DATABASE), + "Failed to attach the new database disk", false); + + /* Copy the content of the old db into the new one */ + // Tables to backup + // - kara + // - kara_type + // - kara_category + // - language + // - playlist + // - kara_playlist + // - queue + // - users + // - 'stickers' + // - 'stickers.kara' + // - 'stickers.plt' + // - 'updates' + // - misc + + /* Use the new DB */ + // database_detach(db, LKT_PROTECTED_DATABASE_NEW); + // database_detach(db, LKT_PROTECTED_DATABASE); + // RETURN_IF(rename(db_path, bak_db_path), "Failed to move old db to the backup location", false); + // if(rename(new_db_path, db_path)) { + // LOG_ERROR("DB", "Failed to move new db to the production location"); + // FAIL_UNLESS(rename(bak_db_path, db_path), "Failed to restore the production database from backup"); + // return false; + // } + // FAIL_UNLESS(database_attach(db, LKT_PROTECTED_DATABASE, db_path), "Failed to reattach database"); + + LOG_ERROR("DB", "Not implemented"); + return false; +} +