Fossil SCM
Simplifications to the checkout-db schema auto-update mechanism.
Commit
21f8161e8e5f733e567daecf99ded88ed64b7a45
Parent
9d8bdc90f9f15e4…
1 file changed
+30
-30
M
src/db.c
+30
-30
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -819,68 +819,68 @@ | ||
| 819 | 819 | free(zDbName); |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | 822 | |
| 823 | 823 | /* |
| 824 | - * * Returns TRUE if zTable exists in the local database. | |
| 825 | - */ | |
| 826 | -static int db_local_table_exists(const char *zTable){ | |
| 827 | - return db_exists("SELECT 1 FROM %s.sqlite_master" | |
| 824 | +** Returns TRUE if zTable exists in the local database but lacks column | |
| 825 | +** zColumn | |
| 826 | +*/ | |
| 827 | +static int db_local_table_exists_but_lacks_column( | |
| 828 | + const char *zTable, | |
| 829 | + const char *zColumn | |
| 830 | +){ | |
| 831 | + char *zDef = db_text(0, "SELECT sql FROM %s.sqlite_master" | |
| 828 | 832 | " WHERE name=='%s' /*scan*/", |
| 829 | 833 | db_name("localdb"), zTable); |
| 830 | -} | |
| 831 | - | |
| 832 | -/* | |
| 833 | -** Returns TRUE if zColumn exists in zTable in the local database. | |
| 834 | -*/ | |
| 835 | -static int db_local_column_exists(const char *zTable, const char *zColumn){ | |
| 836 | - return db_exists("SELECT 1 FROM %s.sqlite_master" | |
| 837 | - " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/", | |
| 838 | - db_name("localdb"), zTable, zColumn); | |
| 834 | + int rc = 0; | |
| 835 | + if( zDef ){ | |
| 836 | + char *zPattern = mprintf("* %s *", zColumn); | |
| 837 | + rc = strglob(zPattern, zDef)==0; | |
| 838 | + fossil_free(zPattern); | |
| 839 | + fossil_free(zDef); | |
| 840 | + } | |
| 841 | + return rc; | |
| 839 | 842 | } |
| 840 | 843 | |
| 841 | 844 | /* |
| 842 | 845 | ** If zDbName is a valid local database file, open it and return |
| 843 | 846 | ** true. If it is not a valid local database file, return 0. |
| 844 | 847 | */ |
| 845 | 848 | static int isValidLocalDb(const char *zDbName){ |
| 846 | 849 | i64 lsize; |
| 850 | + char *zVFileDef; | |
| 847 | 851 | |
| 848 | 852 | if( file_access(zDbName, F_OK) ) return 0; |
| 849 | 853 | lsize = file_size(zDbName); |
| 850 | 854 | if( lsize%1024!=0 || lsize<4096 ) return 0; |
| 851 | 855 | db_open_or_attach(zDbName, "localdb"); |
| 856 | + zVFileDef = db_text(0, "SELECT sql FROM %s.sqlite_master" | |
| 857 | + " WHERE name=='vfile'", db_name("localdb")); | |
| 852 | 858 | |
| 853 | 859 | /* If the "isexe" column is missing from the vfile table, then |
| 854 | 860 | ** add it now. This code added on 2010-03-06. After all users have |
| 855 | 861 | ** upgraded, this code can be safely deleted. |
| 856 | 862 | */ |
| 857 | - if( !db_local_column_exists("vfile", "isexe") ){ | |
| 863 | + if( !strglob("* isexe *", zVFileDef) ){ | |
| 858 | 864 | db_multi_exec("ALTER TABLE vfile ADD COLUMN isexe BOOLEAN DEFAULT 0"); |
| 859 | 865 | } |
| 860 | 866 | |
| 861 | 867 | /* If "islink"/"isLink" columns are missing from tables, then |
| 862 | 868 | ** add them now. This code added on 2011-01-17 and 2011-08-27. |
| 863 | 869 | ** After all users have upgraded, this code can be safely deleted. |
| 864 | 870 | */ |
| 865 | - if( !db_local_column_exists("vfile", "islink") ){ | |
| 871 | + if( !strglob("* islink *", zVFileDef) ){ | |
| 866 | 872 | db_multi_exec("ALTER TABLE vfile ADD COLUMN islink BOOLEAN DEFAULT 0"); |
| 867 | - } | |
| 868 | - | |
| 869 | - if( !db_local_column_exists("stashfile", "isLink") && | |
| 870 | - db_local_table_exists("stashfile") ){ | |
| 871 | - db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOLEAN DEFAULT 0"); | |
| 872 | - } | |
| 873 | - | |
| 874 | - if( !db_local_column_exists("undo", "isLink") && | |
| 875 | - db_local_table_exists("undo") ){ | |
| 876 | - db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0"); | |
| 877 | - } | |
| 878 | - | |
| 879 | - if( !db_local_column_exists("undo_vfile", "islink") && | |
| 880 | - db_local_table_exists("undo_vfile") ){ | |
| 881 | - db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOLEAN DEFAULT 0"); | |
| 873 | + if( db_local_table_exists_but_lacks_column("stashfile", "isLink") ){ | |
| 874 | + db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOL DEFAULT 0"); | |
| 875 | + } | |
| 876 | + if( db_local_table_exists_but_lacks_column("undo", "isLink") ){ | |
| 877 | + db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0"); | |
| 878 | + } | |
| 879 | + if( db_local_table_exists_but_lacks_column("undo_vfile", "islink") ){ | |
| 880 | + db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOL DEFAULT 0"); | |
| 881 | + } | |
| 882 | 882 | } |
| 883 | 883 | return 1; |
| 884 | 884 | } |
| 885 | 885 | |
| 886 | 886 | /* |
| 887 | 887 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -819,68 +819,68 @@ | |
| 819 | free(zDbName); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /* |
| 824 | * * Returns TRUE if zTable exists in the local database. |
| 825 | */ |
| 826 | static int db_local_table_exists(const char *zTable){ |
| 827 | return db_exists("SELECT 1 FROM %s.sqlite_master" |
| 828 | " WHERE name=='%s' /*scan*/", |
| 829 | db_name("localdb"), zTable); |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | ** Returns TRUE if zColumn exists in zTable in the local database. |
| 834 | */ |
| 835 | static int db_local_column_exists(const char *zTable, const char *zColumn){ |
| 836 | return db_exists("SELECT 1 FROM %s.sqlite_master" |
| 837 | " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/", |
| 838 | db_name("localdb"), zTable, zColumn); |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | ** If zDbName is a valid local database file, open it and return |
| 843 | ** true. If it is not a valid local database file, return 0. |
| 844 | */ |
| 845 | static int isValidLocalDb(const char *zDbName){ |
| 846 | i64 lsize; |
| 847 | |
| 848 | if( file_access(zDbName, F_OK) ) return 0; |
| 849 | lsize = file_size(zDbName); |
| 850 | if( lsize%1024!=0 || lsize<4096 ) return 0; |
| 851 | db_open_or_attach(zDbName, "localdb"); |
| 852 | |
| 853 | /* If the "isexe" column is missing from the vfile table, then |
| 854 | ** add it now. This code added on 2010-03-06. After all users have |
| 855 | ** upgraded, this code can be safely deleted. |
| 856 | */ |
| 857 | if( !db_local_column_exists("vfile", "isexe") ){ |
| 858 | db_multi_exec("ALTER TABLE vfile ADD COLUMN isexe BOOLEAN DEFAULT 0"); |
| 859 | } |
| 860 | |
| 861 | /* If "islink"/"isLink" columns are missing from tables, then |
| 862 | ** add them now. This code added on 2011-01-17 and 2011-08-27. |
| 863 | ** After all users have upgraded, this code can be safely deleted. |
| 864 | */ |
| 865 | if( !db_local_column_exists("vfile", "islink") ){ |
| 866 | db_multi_exec("ALTER TABLE vfile ADD COLUMN islink BOOLEAN DEFAULT 0"); |
| 867 | } |
| 868 | |
| 869 | if( !db_local_column_exists("stashfile", "isLink") && |
| 870 | db_local_table_exists("stashfile") ){ |
| 871 | db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOLEAN DEFAULT 0"); |
| 872 | } |
| 873 | |
| 874 | if( !db_local_column_exists("undo", "isLink") && |
| 875 | db_local_table_exists("undo") ){ |
| 876 | db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0"); |
| 877 | } |
| 878 | |
| 879 | if( !db_local_column_exists("undo_vfile", "islink") && |
| 880 | db_local_table_exists("undo_vfile") ){ |
| 881 | db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOLEAN DEFAULT 0"); |
| 882 | } |
| 883 | return 1; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -819,68 +819,68 @@ | |
| 819 | free(zDbName); |
| 820 | } |
| 821 | |
| 822 | |
| 823 | /* |
| 824 | ** Returns TRUE if zTable exists in the local database but lacks column |
| 825 | ** zColumn |
| 826 | */ |
| 827 | static int db_local_table_exists_but_lacks_column( |
| 828 | const char *zTable, |
| 829 | const char *zColumn |
| 830 | ){ |
| 831 | char *zDef = db_text(0, "SELECT sql FROM %s.sqlite_master" |
| 832 | " WHERE name=='%s' /*scan*/", |
| 833 | db_name("localdb"), zTable); |
| 834 | int rc = 0; |
| 835 | if( zDef ){ |
| 836 | char *zPattern = mprintf("* %s *", zColumn); |
| 837 | rc = strglob(zPattern, zDef)==0; |
| 838 | fossil_free(zPattern); |
| 839 | fossil_free(zDef); |
| 840 | } |
| 841 | return rc; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | ** If zDbName is a valid local database file, open it and return |
| 846 | ** true. If it is not a valid local database file, return 0. |
| 847 | */ |
| 848 | static int isValidLocalDb(const char *zDbName){ |
| 849 | i64 lsize; |
| 850 | char *zVFileDef; |
| 851 | |
| 852 | if( file_access(zDbName, F_OK) ) return 0; |
| 853 | lsize = file_size(zDbName); |
| 854 | if( lsize%1024!=0 || lsize<4096 ) return 0; |
| 855 | db_open_or_attach(zDbName, "localdb"); |
| 856 | zVFileDef = db_text(0, "SELECT sql FROM %s.sqlite_master" |
| 857 | " WHERE name=='vfile'", db_name("localdb")); |
| 858 | |
| 859 | /* If the "isexe" column is missing from the vfile table, then |
| 860 | ** add it now. This code added on 2010-03-06. After all users have |
| 861 | ** upgraded, this code can be safely deleted. |
| 862 | */ |
| 863 | if( !strglob("* isexe *", zVFileDef) ){ |
| 864 | db_multi_exec("ALTER TABLE vfile ADD COLUMN isexe BOOLEAN DEFAULT 0"); |
| 865 | } |
| 866 | |
| 867 | /* If "islink"/"isLink" columns are missing from tables, then |
| 868 | ** add them now. This code added on 2011-01-17 and 2011-08-27. |
| 869 | ** After all users have upgraded, this code can be safely deleted. |
| 870 | */ |
| 871 | if( !strglob("* islink *", zVFileDef) ){ |
| 872 | db_multi_exec("ALTER TABLE vfile ADD COLUMN islink BOOLEAN DEFAULT 0"); |
| 873 | if( db_local_table_exists_but_lacks_column("stashfile", "isLink") ){ |
| 874 | db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOL DEFAULT 0"); |
| 875 | } |
| 876 | if( db_local_table_exists_but_lacks_column("undo", "isLink") ){ |
| 877 | db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0"); |
| 878 | } |
| 879 | if( db_local_table_exists_but_lacks_column("undo_vfile", "islink") ){ |
| 880 | db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOL DEFAULT 0"); |
| 881 | } |
| 882 | } |
| 883 | return 1; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 |