db_t create_database(char* database_name, dbs_versioned_schema_def_t *versioned_schema, InitDataCallback cb) { db_t hdb; /* Create a new file storage database with default parameters. */ hdb = db_create_file_storage(database_name, NULL); if (hdb == NULL) { return NULL; } if (dbs_create_schema(hdb, &versioned_schema->schema) < 0) { db_shutdown(hdb, DB_SOFT_SHUTDOWN, NULL); /* Remove incomplete database. */ remove(database_name); return NULL; } if( cb && 0 != (*cb)( hdb, versioned_schema->version ) ) { print_error_message( "Couldn't initialize table data in created database", NULL); return NULL; } set_schema_version( hdb, versioned_schema->version ); db_commit_tx( hdb, DB_DEFAULT_COMPLETION ); return hdb; }
db_t open_database(char* database_name, dbs_versioned_schema_def_t *versioned_schema, UpgradeCallback cb) { db_t hdb; int version; int upgrade_rc = 0; /* Open an existing file storage database with default parameters. */ hdb = db_open_file_storage(database_name, NULL); if (hdb == NULL) { return NULL; } db_begin_tx( hdb, DB_DEFAULT_ISOLATION | DB_LOCK_DEFAULT ); version = get_schema_version( hdb ); db_commit_tx( hdb, DB_DEFAULT_COMPLETION ); if( version != versioned_schema->version && ( !cb || 0 != ( upgrade_rc = (*cb)( hdb, version, versioned_schema->version ) ) ) ) { if( 1 == upgrade_rc ) { fprintf( stderr, "ITTIA evaluation mode can't be used to perform full upgrade actions set,\n" " in this conditions we just suppose upgrade job is successfull\n" ); } else { print_error_message( "Opened DB has wrong version and it couldn't be upgraded to version requiested", NULL ); return NULL; } } if ( 0 == upgrade_rc && !dbs_check_schema(hdb, &versioned_schema->schema)) { fprintf(stderr, "WARNING: schema conflict in %s. Version: %d\n", database_name, versioned_schema->version ); return NULL; } db_begin_tx( hdb, DB_DEFAULT_ISOLATION | DB_LOCK_DEFAULT ); set_schema_version( hdb, versioned_schema->version ); db_commit_tx( hdb, DB_DEFAULT_COMPLETION ); //open_sequences(hdb); return hdb; }
int proxy_db_open_with_version(pool *p, const char *table_path, const char *schema_name, unsigned int schema_version, int flags) { pool *tmp_pool; int res, xerrno = 0; unsigned int current_version = 0; res = proxy_db_open(p, table_path, schema_name); if (res < 0) { return -1; } tmp_pool = make_sub_pool(p); res = get_schema_version(tmp_pool, schema_name, ¤t_version); if (res < 0) { xerrno = errno; destroy_pool(tmp_pool); errno = xerrno; return -1; } if (current_version >= schema_version) { pr_trace_msg(trace_channel, 11, "schema version %u >= desired version %u for schema '%s'", current_version, schema_version, schema_name); return 0; } if (flags & PROXY_DB_OPEN_FL_ERROR_ON_SCHEMA_VERSION_SKEW) { pr_trace_msg(trace_channel, 5, "schema version %u < desired version %u for schema '%s', failing", current_version, schema_version, schema_name); destroy_pool(tmp_pool); errno = EPERM; return -1; } proxy_db_close(p, schema_name); if (unlink(table_path) < 0) { pr_log_pri(PR_LOG_NOTICE, MOD_PROXY_VERSION ": error deleting '%s': %s", table_path, strerror(errno)); } res = proxy_db_open(p, table_path, schema_name); if (res < 0) { xerrno = errno; destroy_pool(tmp_pool); errno = xerrno; return -1; } res = set_schema_version(tmp_pool, schema_name, schema_version); xerrno = errno; destroy_pool(tmp_pool); if (res < 0) { errno = xerrno; return -1; } return 0; }