IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 15, 2009, 11:37:32 AM (17 years ago)
Author:
bills
Message:

Add a new table to the database dbversion where we will store the
schema version number. In -exportrun save the version in the output files.
Then in -importrun insure that the input file version matches the schema.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/pxtools.c

    r25840 r25851  
    177177    return true;
    178178}
     179
     180bool pxLookupVersion(pxConfig *config, psArray **pArray)
     181{
     182    const char *query = "SELECT * FROM dbversion";
     183
     184    if (!p_psDBRunQuery(config->dbh, query)) {
     185        psError(PS_ERR_UNKNOWN, false, "database error");
     186        return false;
     187    }
     188
     189    psArray *output = p_psDBFetchResult(config->dbh);
     190    if (!output) {
     191        psError(PS_ERR_UNKNOWN, false, "database error");
     192        return false;
     193    }
     194    if (!psArrayLength(output)) {
     195        psFree(output);
     196        psError(PS_ERR_UNKNOWN, true, "no rows in dbversion");
     197        return false;
     198    }
     199    if (psArrayLength(output) > 1) {
     200        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found in dbversion: %" PRId64,
     201                psArrayLength(output));
     202        return false;
     203    }
     204    *pArray = output;
     205
     206    return true;
     207}
     208
     209psString pxGetDBVersion(pxConfig *config)
     210{
     211    PS_ASSERT_PTR_NON_NULL(config, NULL);
     212
     213    psArray *array = NULL;
     214    if (!pxLookupVersion(config, &array)) {
     215        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
     216        return NULL;
     217    }
     218    psMetadata *md = array->data[0];
     219    if (!md) {
     220        psError(PS_ERR_UNKNOWN, true, "output of pxLookupVersion is null");
     221        return NULL;
     222    }
     223   
     224    psString version = psMetadataLookupStr(NULL, md, "schema_version");
     225
     226    return version;
     227}
     228
     229bool pxExportVersion(pxConfig *config, FILE *file)
     230{
     231    PS_ASSERT_PTR_NON_NULL(config, NULL);
     232    PS_ASSERT_PTR_NON_NULL(file, NULL);
     233
     234    psArray *array = NULL;
     235    if (!pxLookupVersion(config, &array) || !array) {
     236        psError(PS_ERR_UNKNOWN, false, "pxLookupVersion failed");
     237        return false;
     238    }
     239    if (!ippdbPrintMetadatas(file, array, "dbversion", true)) {
     240        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     241        psFree(array);
     242        return false;
     243    }
     244    return true;
     245}
     246
     247bool pxCheckImportVersion(pxConfig *config, psMetadata *input)
     248{
     249    PS_ASSERT_PTR_NON_NULL(config, NULL);
     250    PS_ASSERT_PTR_NON_NULL(input, NULL);
     251
     252    // This code was adapted from the way camtool parses the structures.
     253    // Is this really the way to do it?
     254    psMetadataItem *multi_item =  psMetadataLookup(input, "dbversion");
     255    if (!multi_item || (multi_item->type != PS_DATA_METADATA_MULTI)) {
     256        psError(PS_ERR_UNKNOWN, true, "dbversion multi not found in input");
     257        return false;
     258    }
     259   
     260    psMetadataItem *dbversion = psListGet(multi_item->data.list, 0);
     261    if (!dbversion) {
     262        psError(PS_ERR_UNKNOWN, true, "dbversion not found in input");
     263        return false;
     264    }
     265
     266    if (!strcmp(dbversion->name, "dbversion")) {
     267        // horray
     268        psMetadata *md = dbversion->data.md;
     269        psString schema_version = pxGetDBVersion(config);
     270        if (!schema_version) {
     271            psError(PS_ERR_UNKNOWN, false, "pxGetDBVersion failed");
     272            return false;
     273        }
     274       
     275        psString import_version = psMetadataLookupStr(NULL, md, "schema_version");
     276        if (import_version && strcmp(import_version, schema_version)) {
     277            psError(PS_ERR_UNKNOWN, true, "input file schema_version: %s does not match data base: %s",
     278                import_version, schema_version);
     279            return false;
     280        } else if (!import_version) {
     281            psError(PS_ERR_UNKNOWN, true, "input file schema_version is NULL");
     282            return false;
     283        } else {
     284            // YIPPEE this file is the same version
     285        }
     286    } else {
     287        psError(PS_ERR_UNKNOWN, true, "Unexpected config dump format");
     288        return false;
     289    }
     290
     291    return true;
     292}
Note: See TracChangeset for help on using the changeset viewer.