dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Copyright (c) 2007 D. Richard Hipp dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is free software; you can redistribute it and/or dbda8d6ce9 2007-07-21 drh: ** modify it under the terms of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License version 2 as published by the Free Software Foundation. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is distributed in the hope that it will be useful, dbda8d6ce9 2007-07-21 drh: ** but WITHOUT ANY WARRANTY; without even the implied warranty of dbda8d6ce9 2007-07-21 drh: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU dbda8d6ce9 2007-07-21 drh: ** General Public License for more details. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** You should have received a copy of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License along with this library; if not, write to the dbda8d6ce9 2007-07-21 drh: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, dbda8d6ce9 2007-07-21 drh: ** Boston, MA 02111-1307, USA. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** Author contact information: dbda8d6ce9 2007-07-21 drh: ** drh@hwaci.com dbda8d6ce9 2007-07-21 drh: ** http://www.hwaci.com/drh/ dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ******************************************************************************* dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This file contains code used to rebuild the database. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: #include "config.h" dbda8d6ce9 2007-07-21 drh: #include "rebuild.h" dbda8d6ce9 2007-07-21 drh: #include <assert.h> dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** COMMAND: rebuild dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** Reconstruct the entire repository database from the core dbda8d6ce9 2007-07-21 drh: ** records. Run this command after updating the fossil dbda8d6ce9 2007-07-21 drh: ** executable in a way that changes the database schema. dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: void rebuild_database(void){ dbda8d6ce9 2007-07-21 drh: Stmt s; dbda8d6ce9 2007-07-21 drh: int errCnt; dbda8d6ce9 2007-07-21 drh: int forceFlag; dbda8d6ce9 2007-07-21 drh: char *zTable; dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: forceFlag = find_option("force","f",0)!=0; dbda8d6ce9 2007-07-21 drh: if( g.argc!=3 ){ dbda8d6ce9 2007-07-21 drh: usage("REPOSITORY-FILENAME"); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: errCnt = 0; dbda8d6ce9 2007-07-21 drh: db_open_repository(g.argv[2]); dbda8d6ce9 2007-07-21 drh: db_begin_transaction(); eea381f416 2007-08-09 drh: db_multi_exec( eea381f416 2007-08-09 drh: "CREATE INDEX IF NOT EXISTS delta_i1 ON delta(srcid);" eea381f416 2007-08-09 drh: ); dbda8d6ce9 2007-07-21 drh: for(;;){ dbda8d6ce9 2007-07-21 drh: zTable = db_text(0, dbda8d6ce9 2007-07-21 drh: "SELECT name FROM sqlite_master" dbda8d6ce9 2007-07-21 drh: " WHERE type='table'" dbda8d6ce9 2007-07-21 drh: " AND name NOT IN ('blob','delta','rcvfrom','user','config')"); dbda8d6ce9 2007-07-21 drh: if( zTable==0 ) break; dbda8d6ce9 2007-07-21 drh: db_multi_exec("DROP TABLE %Q", zTable); dbda8d6ce9 2007-07-21 drh: free(zTable); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: db_multi_exec(zRepositorySchema2); dbda8d6ce9 2007-07-21 drh: 73bddaebb9 2007-08-09 drh: db_prepare(&s, "SELECT rid, size FROM blob"); dbda8d6ce9 2007-07-21 drh: while( db_step(&s)==SQLITE_ROW ){ dbda8d6ce9 2007-07-21 drh: int rid = db_column_int(&s, 0); 73bddaebb9 2007-08-09 drh: int size = db_column_int(&s, 1); 73bddaebb9 2007-08-09 drh: if( size>=0 ){ 73bddaebb9 2007-08-09 drh: Blob content; 73bddaebb9 2007-08-09 drh: content_get(rid, &content); 73bddaebb9 2007-08-09 drh: manifest_crosslink(rid, &content); 73bddaebb9 2007-08-09 drh: blob_reset(&content); 73bddaebb9 2007-08-09 drh: }else{ 73bddaebb9 2007-08-09 drh: db_multi_exec("INSERT INTO phantom VALUES(%d)", rid); 73bddaebb9 2007-08-09 drh: } dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: if( errCnt && !forceFlag ){ dbda8d6ce9 2007-07-21 drh: printf("%d errors. Rolling back changes. Use --force to force a commit.\n", dbda8d6ce9 2007-07-21 drh: errCnt); dbda8d6ce9 2007-07-21 drh: db_end_transaction(1); dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: db_end_transaction(0); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: }