Add db auto-upgrade back in

This commit is contained in:
Ron Pedde 2006-06-29 06:21:41 +00:00
parent c2ae17e635
commit 554c72e1a6
5 changed files with 97 additions and 179 deletions

View File

@ -57,7 +57,7 @@ mt_daapd_SOURCES = main.c daapd.h rend.h uici.c uici.h webserver.c \
scan-xml.c scan-wma.c scan-aac.c scan-aac.h scan-wav.c scan-url.c \
smart-parser.c smart-parser.h xml-rpc.c xml-rpc.h \
os.h ll.c ll.h conf.c conf.h compat.c compat.h \
os-unix.h os-unix.c os.h plugin.c plugin.h \
os-unix.h os-unix.c os.h plugin.c plugin.h db-sql-updates.c \
$(PRENDSRC) $(ORENDSRC) $(HRENDSRC) $(OGGVORBISSRC) $(FLACSRC) \
$(MUSEPACKSRC) $(SQLITEDB) $(SQLITE3DB) $(SQLDB) $(GDBM)

View File

@ -65,7 +65,7 @@ static char *db_sqlite2_enum_query;
static char db_sqlite2_path[PATH_MAX + 1];
#define DB_SQLITE2_VERSION 9
#define DB_SQLITE2_VERSION 10
/* Forwards */

View File

@ -67,7 +67,7 @@ static char **db_sqlite3_row = NULL;
static char db_sqlite3_path[PATH_MAX + 1];
#define DB_SQLITE3_VERSION 9
#define DB_SQLITE3_VERSION 10
/* Forwards */

View File

@ -1,184 +1,10 @@
/*
* $Id: $
* Update a database from an old version to a new version.
* Commands to update database to new version
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "conf.h"
#include "daapd.h"
#include "db-generic.h"
#include "db-sql.h"
#include "err.h"
//int (*db_sql_exec_fn)(char **pe, int loglevel, char *fmt, ...) = NULL;
CONFIG config;
int db_error_exec(char **pe, int loglevel, char *fmt, ...);
int db_error_close(void);
int db_error_open(char **pe, char *dsn);
extern int db_sql_fetch_int(char **pe, int *result, char *fmt, ...);
#ifdef HAVE_LIBSQLITE
# include "db-sql-sqlite2.h"
# define SQLITE_EXEC db_sqlite2_exec
# define SQLITE_OPEN db_sql_open_sqlite2
# define SQLITE_CLOSE db_sqlite2_close
# define SQLITE_UPDATES db_sqlite_updates
#else
# define SQLITE_EXEC db_error_exec
# define SQLITE_OPEN db_error_open
# define SQLITE_CLOSE db_error_close
# define SQLITE_UPDATES db_error_updates
#endif
#ifdef HAVE_LIBSQLITE3
# include "db-sql-sqlite3.h"
# define SQLITE3_EXEC db_sqlite3_exec
# define SQLITE3_OPEN db_sql_open_sqlite3
# define SQLITE3_CLOSE db_sqlite3_close
# define SQLITE3_UPDATES db_sqlite_updates
#else
# define SQLITE3_EXEC db_error_exec
# define SQLITE3_OPEN db_error_open
# define SQLITE3_CLOSE db_error_close
# define SQLITE3_UPDATES db_error_updates
#endif
extern char *db_sqlite_updates[];
char *db_error_updates[] = {
NULL
};
int (*dbu_sql_open_fn)(char **pe, char *dsn) = NULL;
int (*dbu_sql_close_fn)(void) = NULL;
int (*dbu_sql_exec_fn)(char **pe, int loglevel, char *fmt, ...) = NULL;
char **dbu_updates;
int db_error_exec(char **pe, int loglevel, char *fmt, ...) {
char *err="Unsupported DB type";
db_get_error(pe,DB_E_SQL_ERROR,err);
return DB_E_SQL_ERROR;
}
int db_error_close(void) {
return DB_E_SQL_ERROR;
}
int db_error_open(char **pe, char *dsn) {
char *err="Unsupported DB type";
db_get_error(pe,DB_E_SQL_ERROR,err);
return DB_E_SQL_ERROR;
}
int main(int argc, char *argv[]) {
char *configfile = CONFFILE;
char *pe;
int result;
int option;
char *db_type;
char *db_parms;
int version;
int max_version;
err_setlevel(1);
while((option=getopt(argc,argv,"d:c:")) != -1) {
switch(option) {
case 'd':
err_setlevel(atoi(optarg));
break;
case 'c':
configfile = optarg;
break;
default:
break;
}
}
printf("Loading config file: %s\n",configfile);
if(conf_read(configfile) != CONF_E_SUCCESS) {
fprintf(stderr,"Error loading config file!\n");
exit(EXIT_FAILURE);
}
db_type = conf_alloc_string("general","db_type",NULL);
db_parms = conf_alloc_string("general","db_parms",NULL);
if((!db_type) || (!db_parms)) {
fprintf(stderr,"Bad config: missing db_type or db_parms\n");
exit(EXIT_FAILURE);
}
printf("Opening database (type: %s, parms: %s)\n",db_type,db_parms);
if(strcasecmp(db_type,"sqlite") == 0) {
dbu_sql_open_fn = SQLITE_OPEN;
dbu_sql_close_fn = SQLITE_CLOSE;
dbu_sql_exec_fn = SQLITE_EXEC;
dbu_updates = SQLITE_UPDATES;
} else if(strcasecmp(db_type,"sqlite3") == 0) {
dbu_sql_open_fn = SQLITE3_OPEN;
dbu_sql_close_fn = SQLITE3_CLOSE;
dbu_sql_exec_fn = SQLITE3_EXEC;
dbu_updates = SQLITE3_UPDATES;
} else {
fprintf(stderr,"Error: unknown database type: %s\n",db_type);
exit(EXIT_FAILURE);
}
pe=NULL;
result = dbu_sql_open_fn(&pe,db_parms);
if((result != DB_E_SUCCESS)&&(result != DB_E_WRONGVERSION)) {
fprintf(stderr,"Error: %s\n",pe);
exit(EXIT_FAILURE);
}
if(result == DB_E_SUCCESS) {
printf("Database is already up-to-date\n");
} else {
/* update the db */
result = db_sql_fetch_int(&pe,&version,"select value from config "
"where term='version'");
if(result != DB_E_SUCCESS) {
fprintf(stderr,"Error: %s\n",pe);
exit(EXIT_FAILURE);
}
max_version=0;
while(dbu_updates[max_version] != NULL) {
max_version++;
}
printf("Current database version: %d\n",version);
printf("Target version: %d\n",max_version);
while(version < max_version) {
printf("Upgrading db: %d --> %d\n",version,version+1);
result = dbu_sql_exec_fn(&pe,E_LOG,"%s",dbu_updates[version]);
if(result != DB_E_SUCCESS) {
fprintf(stderr,"Could not upgrade db. Aborting.\n");
dbu_sql_close_fn();
exit(EXIT_FAILURE);
}
version++;
}
}
dbu_sql_close_fn();
printf("Success!\n");
return EXIT_SUCCESS;
}
char *db_sqlite_updates[] = {
/* version 0 -> version 1 -- initial update */
@ -474,5 +300,59 @@ char *db_sqlite_updates[] = {
"create index idx_path on songs(path);\n"
"drop table tempsongs;\n"
"update config set value=9 where term='version';\n",
/* version 9 -> version 10 */
"drop index idx_path;\n"
"create temp table tempsongs as select * from songs;\n"
"drop table songs;\n"
"CREATE TABLE songs (\n"
" id INTEGER PRIMARY KEY NOT NULL,\n"
" path VARCHAR(4096) NOT NULL,\n"
" fname VARCHAR(255) NOT NULL,\n"
" title VARCHAR(1024) DEFAULT NULL,\n"
" artist VARCHAR(1024) DEFAULT NULL,\n"
" album VARCHAR(1024) DEFAULT NULL,\n"
" genre VARCHAR(255) DEFAULT NULL,\n"
" comment VARCHAR(4096) DEFAULT NULL,\n"
" type VARCHAR(255) DEFAULT NULL,\n"
" composer VARCHAR(1024) DEFAULT NULL,\n"
" orchestra VARCHAR(1024) DEFAULT NULL,\n"
" conductor VARCHAR(1024) DEFAULT NULL,\n"
" grouping VARCHAR(1024) DEFAULT NULL,\n"
" url VARCHAR(1024) DEFAULT NULL,\n"
" bitrate INTEGER DEFAULT 0,\n"
" samplerate INTEGER DEFAULT 0,\n"
" song_length INTEGER DEFAULT 0,\n"
" file_size INTEGER DEFAULT 0,\n"
" year INTEGER DEFAULT 0,\n"
" track INTEGER DEFAULT 0,\n"
" total_tracks INTEGER DEFAULT 0,\n"
" disc INTEGER DEFAULT 0,\n"
" total_discs INTEGER DEFAULT 0,\n"
" bpm INTEGER DEFAULT 0,\n"
" compilation INTEGER DEFAULT 0,\n"
" rating INTEGER DEFAULT 0,\n"
" play_count INTEGER DEFAULT 0,\n"
" data_kind INTEGER DEFAULT 0,\n"
" item_kind INTEGER DEFAULT 0,\n"
" description INTEGER DEFAULT 0,\n"
" time_added INTEGER DEFAULT 0,\n"
" time_modified INTEGER DEFAULT 0,\n"
" time_played INTEGER DEFAULT 0,\n"
" db_timestamp INTEGER DEFAULT 0,\n"
" disabled INTEGER DEFAULT 0,\n"
" sample_count INTEGER DEFAULT 0,\n"
" force_update INTEGER DEFAULT 0,\n"
" codectype VARCHAR(5) DEFAULT NULL,\n"
" idx INTEGER NOT NULL,\n"
" has_video INTEGER DEFAULT 0,\n"
" contentrating INTEGER DEFAULT 0\n"
");\n"
"begin transaction;\n"
"insert into songs select * from tempsongs;\n"
"commit transaction;\n"
"create index idx_path on songs(path,idx);\n"
"drop table tempsongs;\n"
"update config set value=10 where term='version';\n",
NULL /* No more versions! */
};

View File

@ -55,6 +55,8 @@ static int db_sql_in_playlist_scan=0;
static int db_sql_in_scan=0;
static int db_sql_need_dispose=0;
extern char *db_sqlite_updates[];
/* Forwards */
int db_sql_get_size(DBQUERYINFO *pinfo, char **valarray);
int db_sql_build_dmap(DBQUERYINFO *pinfo, char **valarray, unsigned char *presult, int len);
@ -297,8 +299,44 @@ int db_sql_parse_smart(char **pe, char **clause, char *phrase) {
*/
int db_sql_open(char **pe, char *parameters) {
int result;
int current_version;
int max_version;
char **db_updates;
result = db_sql_open_fn(pe,parameters);
if(result == DB_E_WRONGVERSION) {
/* need to update the version */
if(pe) free(*pe);
db_updates = db_sqlite_updates;
result = db_sql_fetch_int(pe,&current_version,"select value from "
"config where term='version'");
if(result != DB_E_SUCCESS)
return result;
max_version = 0;
while(db_updates[max_version])
max_version++;
DPRINTF(E_DBG,L_DB,"Current db version: %d\n",current_version);
DPRINTF(E_DBG,L_DB,"Target db version: %d\n",max_version);
while(current_version < max_version) {
DPRINTF(E_LOG,L_DB,"Upgrading db: %d --> %d\n",current_version,
current_version + 1);
result = db_sql_exec_fn(pe,E_LOG,"%s",db_updates[current_version]);
if(result != DB_E_SUCCESS) {
DPRINTF(E_LOG,L_DB,"Error upgrading db: %s\n", pe ? *pe :
"Unknown");
return result;
}
current_version++;
}
}
return result;
}