# HG changeset patch # User Mahlon E. Smith # Date 1317225856 25200 # Node ID e4f1a551d45cc228915f3dcdf0bd0d55cfef2f63 # Parent 85718bc3ff47309f07b00075fde2da942b1b1db0 Fine tuning for database initialization/upgrades. diff -r 85718bc3ff47 -r e4f1a551d45c db.c --- a/db.c Thu Sep 22 21:43:50 2011 -0700 +++ b/db.c Wed Sep 28 09:04:16 2011 -0700 @@ -45,7 +45,7 @@ db_attach( void ) { if ( v.db != NULL ) return( SQLITE_OK ); /* already attached */ - if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* name not set */ + if ( strlen(v.dbname) == 0 ) return( SQLITE_ERROR ); /* db filename not set? */ debug( 2, LOC, "Attaching to database '%s'\n", v.dbname ); if ( sqlite3_open( v.dbname, &v.db ) != SQLITE_OK ) { @@ -55,9 +55,9 @@ /* check DB version */ unsigned short int version = db_version(); + debug( 2, LOC, "Database version: %d\n", version ); if ( version != DB_VERSION ) { - debug( 1, LOC, "Database version mismatch: expected %hu, got %d.\n", - DB_VERSION, version ); + debug( 2, LOC, "Database version mismatch: expected %hu\n", DB_VERSION ); /* We're in need of a DB initialization, or just behind. * Attempt to "stair step" upgrade to the current version. */ @@ -90,10 +90,10 @@ for ( i = current_version + 1; i <= DB_VERSION; i++ ) { if ( i == 1 ) { - debug( 1, LOC, "Initializing new database.\n" ); + debug( 2, LOC, "Initializing new database.\n" ); } else { - debug( 1, LOC, "Upgrading database version from %hu to %hu\n", current_version, i ); + debug( 2, LOC, "Upgrading database version from %hu to %hu\n", current_version, i ); } sprintf( sql_file, "sql/%d.sql", i ); @@ -102,7 +102,8 @@ /* If there is SQL to execute, do so and then reset for more */ if ( sqlite3_exec( v.db, upgrade_sql, NULL, NULL, NULL ) != SQLITE_OK ) { - debug( 1, LOC, "Error upgrading database: %s\n", sqlite3_errmsg(v.db) ); + debug( 2, LOC, "Error %s database: %s\n", + (i == 1 ? "initalizing" : "upgrading"), sqlite3_errmsg(v.db) ); return( sqlite3_errcode(v.db) ); } free( upgrade_sql ); @@ -112,7 +113,7 @@ current_version = i; sprintf( user_pragma, "PRAGMA user_version = %hu;", current_version ); if ( sqlite3_exec( v.db, user_pragma, NULL, NULL, NULL ) != SQLITE_OK ) { - debug( 1, LOC, "Error upgrading database: %s\n", sqlite3_errmsg(v.db) ); + debug( 2, LOC, "Error setting version: %s\n", sqlite3_errmsg(v.db) ); return( sqlite3_errcode(v.db) ); } } @@ -133,7 +134,7 @@ int version = -1; if ( sqlite3_prepare_v2( v.db, "PRAGMA user_version", -1, &stmt, NULL ) != SQLITE_OK ) { - debug( 1, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) ); + debug( 2, LOC, "Error finding DB version: %s\n", sqlite3_errmsg(v.db) ); return( -1 ); } @@ -141,6 +142,6 @@ version = sqlite3_column_int( stmt, 0 ); sqlite3_finalize( stmt ); - return version; + return( version ); } diff -r 85718bc3ff47 -r e4f1a551d45c db.h --- a/db.h Thu Sep 22 21:43:50 2011 -0700 +++ b/db.h Wed Sep 28 09:04:16 2011 -0700 @@ -36,11 +36,9 @@ extern const unsigned short int DB_VERSION; /* - * * Function prototypes * */ - int db_attach( void ); int db_upgrade( unsigned short int current_version ); short int db_version( void ); diff -r 85718bc3ff47 -r e4f1a551d45c main.c --- a/main.c Thu Sep 22 21:43:50 2011 -0700 +++ b/main.c Wed Sep 28 09:04:16 2011 -0700 @@ -28,14 +28,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* - * TODO - * - * inet_pton( AF_INET, *char src, dest ) - * an option to run the DB out of memory? - * - */ - #include "volta.h" #include "db.h" diff -r 85718bc3ff47 -r e4f1a551d45c sql/1.sql --- a/sql/1.sql Thu Sep 22 21:43:50 2011 -0700 +++ b/sql/1.sql Wed Sep 28 09:04:16 2011 -0700 @@ -1,6 +1,8 @@ BEGIN; -DROP TABLE IF EXISTS init; -CREATE TABLE init ( bar INT ); +DROP TABLE IF EXISTS rules; +CREATE TABLE rules ( + redir TINYINT NOT NULL DEFAULT 0 CHECK( redir IN (0,1,2) ) +); -COMMIT; +COMMIT;