Skip to content

Commit

Permalink
Add basic schema versioning support
Browse files Browse the repository at this point in the history
  • Loading branch information
jsonn authored and abhinav-upadhyay committed Jan 30, 2012
1 parent c21098e commit 75b408d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
72 changes: 52 additions & 20 deletions apropos-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,23 @@ static int
create_db(sqlite3 *db)
{
const char *sqlstr = NULL;
char *schemasql;
char *errmsg = NULL;

/*------------------------ Create the tables------------------------------*/

schemasql = sqlite3_mprintf("PRAGMA user_version = %d",
APROPOS_SCHEMA_VERSION);
sqlite3_exec(db, schemasql, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
sqlite3_close(db);
sqlite3_shutdown();
return -1;
}
sqlite3_free(schemasql);

sqlstr = "CREATE VIRTUAL TABLE mandb USING fts4(section, name, "
"name_desc, desc, lib, return_vals, env, files, "
"exit_status, diagnostics, errors, md5_hash UNIQUE, machine, "
Expand Down Expand Up @@ -263,20 +276,22 @@ sqlite3 *
init_db(int db_flag)
{
sqlite3 *db = NULL;
sqlite3_stmt *stmt;
struct stat sb;
int rc;
int create_db_flag = 0;

/* Check if the databse exists or not */
/* Check if the database exists or not */
if (!(stat(DBPATH, &sb) == 0 && S_ISREG(sb.st_mode))) {
/* Database does not exist, check if DB_CREATE was specified, and set
* flag to create the database schema
*/
if (db_flag == (MANDB_CREATE))
create_db_flag = 1;
else
/* db does not exist and DB_CREATE was also not specified, return NULL */
if (db_flag != (MANDB_CREATE)) {
warnx("Missing apropos database. "
"Please run makemandb to create it.");
return NULL;
}
create_db_flag = 1;
}

/* Now initialize the database connection */
Expand All @@ -288,32 +303,50 @@ init_db(int db_flag)
sqlite3_shutdown();
return NULL;
}


if (create_db_flag && create_db(db) < 0) {
warnx("%s", "Unable to create database schema");
goto error;
}

rc = sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
warnx("Unable to query schema version");
goto error;
}
if (sqlite3_step(stmt) != SQLITE_ROW) {
sqlite3_finalize(stmt);
warnx("Unable to query schema version");
goto error;
}
if (sqlite3_column_int(stmt, 0) != APROPOS_SCHEMA_VERSION) {
sqlite3_finalize(stmt);
warnx("Incorrect schema version found. "
"Please run makemandb -f.");
goto error;
}
sqlite3_finalize(stmt);

sqlite3_extended_result_codes(db, 1);

/* Register the zip and unzip functions for FTS compression */
rc = sqlite3_create_function(db, "zip", 1, SQLITE_ANY, NULL, zip, NULL, NULL);
if (rc != SQLITE_OK) {
sqlite3_close(db);
sqlite3_shutdown();
errx(EXIT_FAILURE, "Unable to register function: compress");
warnx("Unable to register function: compress");
goto error;
}

rc = sqlite3_create_function(db, "unzip", 1, SQLITE_ANY, NULL,
unzip, NULL, NULL);
if (rc != SQLITE_OK) {
sqlite3_close(db);
sqlite3_shutdown();
errx(EXIT_FAILURE, "Unable to register function: uncompress");
}

if (create_db_flag && create_db(db) < 0) {
warnx("%s", "Unable to create database schema");
sqlite3_close(db);
sqlite3_shutdown();
return NULL;
warnx("Unable to register function: uncompress");
goto error;
}
return db;
error:
sqlite3_close(db);
sqlite3_shutdown();
return NULL;
}

/*
Expand Down Expand Up @@ -719,4 +752,3 @@ int run_query_pager(sqlite3 *db, query_args *args)
args->callback_data = (void *) &orig_data;
return run_query(db, snippet_args, args);
}

2 changes: 2 additions & 0 deletions apropos-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#define MANDB_WRITE SQLITE_OPEN_READWRITE
#define MANDB_CREATE SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE

#define APROPOS_SCHEMA_VERSION 20120130

/*
* Used to identify the section of a man(7) page.
* This is similar to the enum mdoc_sec defined in mdoc.h from mdocml project.
Expand Down
4 changes: 1 addition & 3 deletions apropos.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ main(int argc, char *argv[])
}

if ((db = init_db(MANDB_READONLY)) == NULL)
errx(EXIT_FAILURE, "The database does not exist. Please run makemandb "
"first and then try again");
exit(EXIT_FAILURE);


/* If user wants to page the output, then set some settings */
if (aflags.pager) {
/* Open a pipe to the pager */
Expand Down
8 changes: 4 additions & 4 deletions makemandb.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef struct secbuff {
typedef struct makemandb_flags {
int optimize;
int limit; // limit the indexing to only NAME section
int f; // force removal of old database
int recreate; // Database was created from scratch
} makemandb_flags;

typedef struct mandb_rec {
Expand Down Expand Up @@ -299,7 +299,7 @@ main(int argc, char *argv[])
switch (ch) {
case 'f':
remove(DBPATH);
mflags.f = 1;
mflags.recreate = 1;
break;
case 'l':
mflags.limit = 1;
Expand All @@ -320,7 +320,7 @@ main(int argc, char *argv[])
mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);

if ((db = init_db(MANDB_CREATE)) == NULL)
errx(EXIT_FAILURE, "Could not initialize the database");
exit(EXIT_FAILURE);

sqlite3_exec(db, "PRAGMA synchronous = 0", NULL, NULL, &errmsg);
if (errmsg != NULL) {
Expand Down Expand Up @@ -657,7 +657,7 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
" due to errors = %d\n",
total_count, new_count, link_count, err_count);

if (!mflags.f) {
if (!mflags.recreate) {
printf("Deleting stale index entries\n");
sqlstr = "DELETE FROM mandb WHERE rowid IN"
" (SELECT id FROM mandb_meta "
Expand Down

0 comments on commit 75b408d

Please sign in to comment.