Skip to content
Extraits de code Groupes Projets
Vérifiée Valider eb5bc910 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

DB + CMAKE: Isolate the upgrade code + try to force colored output

parent 2dc99778
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!169Solves #90 - Database automatic upgrade
......@@ -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()
......@@ -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) \
{ \
......
#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;
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter