/* ================================================================= */ static void create_taxtable_tables( GncSqlBackend* be ) { gint version; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TT_TABLE_NAME ); if ( version == 0 ) { gnc_sql_create_table( be, TT_TABLE_NAME, TT_TABLE_VERSION, tt_col_table ); } else if ( version == 1 ) { /* Upgrade 64 bit int handling */ gnc_sql_upgrade_table( be, TT_TABLE_NAME, tt_col_table ); gnc_sql_set_table_version( be, TT_TABLE_NAME, TT_TABLE_VERSION ); } version = gnc_sql_get_table_version( be, TTENTRIES_TABLE_NAME ); if ( version == 0 ) { gnc_sql_create_table( be, TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION, ttentries_col_table ); } else if ( version == 1 ) { /* Upgrade 64 bit int handling */ gnc_sql_upgrade_table( be, TTENTRIES_TABLE_NAME, ttentries_col_table ); gnc_sql_set_table_version( be, TTENTRIES_TABLE_NAME, TTENTRIES_TABLE_VERSION ); } }
static void create_recurrence_tables( GncSqlBackend* be ) { gint version; gboolean ok; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); } else if ( version < TABLE_VERSION ) { /* Upgrade: 1->2: Add recurrence_weekend_adjust field (mandatory, non-null field) */ if ( version == 1 ) { upgrade_recurrence_table_1_2 (be); } (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); PINFO("Recurrence table upgraded from version %d to version %d\n", version, TABLE_VERSION); } }
/* ================================================================= */ static void create_lots_tables( GncSqlBackend* be ) { gint version; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { /* The table doesn't exist, so create it */ (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); } else if ( version == 1 ) { /* Version 1 -> 2 removes the 'NOT NULL' constraint on the account_guid field. Create a temporary table, copy the data from the old table, delete the old table, then rename the new one. */ gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); PINFO("Lots table upgraded from version 1 to version %d\n", TABLE_VERSION); } }
/* ================================================================= */ static void create_slots_tables( GncSqlBackend* be ) { gint version; gboolean ok; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } } else if ( version < TABLE_VERSION ) { /* Upgrade: 1->2: 64-bit int values to proper definition, add index 2->3: Add gdate field */ if ( version == 1 ) { gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); ok = gnc_sql_create_index( be, "slots_guid_index", TABLE_NAME, obj_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } } else if ( version == 2 ) { ok = gnc_sql_add_columns_to_table( be, TABLE_NAME, gdate_col_table ); if ( !ok ) { PERR( "Unable to add gdate column\n" ); } } (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); PINFO("Slots table upgraded from version %d to version %d\n", version, TABLE_VERSION); } }
/* ================================================================= */ static void create_prices_tables( GncSqlBackend* be ) { gint version; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { (void)gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); } else if ( version == 1 ) { /* Upgrade 64 bit int handling */ gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); (void)gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); } }
/* ================================================================= */ static void create_customer_tables( GncSqlBackend* be ) { gint version; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); } else if ( version == 1 ) { /* Upgrade 64 bit int handling */ gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); PINFO("Customers table upgraded from version 1 to version %d\n", TABLE_VERSION); } }
/* ================================================================= */ static void create_invoice_tables( GncSqlBackend* be ) { gint version; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TABLE_NAME ); if ( version == 0 ) { gnc_sql_create_table( be, TABLE_NAME, TABLE_VERSION, col_table ); } else if ( version < TABLE_VERSION ) { /* Upgrade: 1->2: 64 bit int handling 2->3: invoice open date can be NULL */ gnc_sql_upgrade_table( be, TABLE_NAME, col_table ); gnc_sql_set_table_version( be, TABLE_NAME, TABLE_VERSION ); PINFO("Invoices table upgraded from version %d to version %d\n", version, TABLE_VERSION); } }
/* Test the gnc_dbi_load logic that forces a newer database to be * opened read-only and an older one to be safe-saved. Again, it would * be better to do this starting from a fresh file, but instead we're * being lazy and using an existing one. */ void test_dbi_version_control( const gchar* driver, const gchar* url ) { QofSession *sess; QofBook *book; QofBackend *qbe; QofBackendError err; gint ourversion = gnc_get_long_version(); g_test_message ( "Testing safe save %s\n", driver ); // Load the session data sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); if (sess && qof_session_get_error(sess) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(sess), qof_session_get_error_message(sess)); do_test( FALSE, "DB Session Creation Failed"); goto cleanup; } qof_session_load( sess, NULL ); qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash", GNUCASH_RESAVE_VERSION - 1 ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); qof_session_load( sess, NULL ); err = qof_session_pop_error( sess ); do_test( err == ERR_SQL_DB_TOO_OLD, "DB Failed to flag too old" ); qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash", ourversion ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash-Resave", ourversion + 1 ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); qof_session_load( sess, NULL ); qof_session_ensure_all_data_loaded( sess ); err = qof_session_pop_error( sess ); do_test( err == ERR_SQL_DB_TOO_NEW, "DB Failed to flag too new" ); cleanup: qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash-Resave", GNUCASH_RESAVE_VERSION ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); }
/** * Creates the transaction and split tables. * * @param be SQL backend */ static void create_transaction_tables( GncSqlBackend* be ) { gint version; gboolean ok; g_return_if_fail( be != NULL ); version = gnc_sql_get_table_version( be, TRANSACTION_TABLE ); if ( version == 0 ) { (void)gnc_sql_create_table( be, TRANSACTION_TABLE, TX_TABLE_VERSION, tx_col_table ); ok = gnc_sql_create_index( be, "tx_post_date_index", TRANSACTION_TABLE, post_date_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } } else if ( version < TX_TABLE_VERSION ) { /* Upgrade: 1->2: 64 bit int handling 2->3: allow dates to be NULL */ gnc_sql_upgrade_table( be, TRANSACTION_TABLE, tx_col_table ); (void)gnc_sql_set_table_version( be, TRANSACTION_TABLE, TX_TABLE_VERSION ); PINFO("Transactions table upgraded from version %d to version %d\n", version, TX_TABLE_VERSION); } version = gnc_sql_get_table_version( be, SPLIT_TABLE ); if ( version == 0 ) { (void)gnc_sql_create_table( be, SPLIT_TABLE, SPLIT_TABLE_VERSION, split_col_table ); ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } } else if ( version < SPLIT_TABLE_VERSION ) { /* Upgrade: 1->2: 64 bit int handling 3->4: Split reconcile date can be NULL */ gnc_sql_upgrade_table( be, SPLIT_TABLE, split_col_table ); ok = gnc_sql_create_index( be, "splits_tx_guid_index", SPLIT_TABLE, tx_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } ok = gnc_sql_create_index( be, "splits_account_guid_index", SPLIT_TABLE, account_guid_col_table ); if ( !ok ) { PERR( "Unable to create index\n" ); } (void)gnc_sql_set_table_version( be, SPLIT_TABLE, SPLIT_TABLE_VERSION ); PINFO("Splits table upgraded from version %d to version %d\n", version, SPLIT_TABLE_VERSION); } }