sa28-00

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revisionBoth sides next revision
sa28-00 [2019/10/18 10:49] – [Moving Blobs Back Into The Database] Justin Willeysa28-00 [2021/11/26 12:44] – ValidateExternalBlobStoreFilesExist updated Michael Scott
Line 72: Line 72:
 ==== Validating the External Storage ==== ==== Validating the External Storage ====
  
-Once the blobs are stored outside the database, the normal database validation cannot check that everything is present and correct. The stored procedure below (which will only work with SQL Anywhere v12 and later) checks that each external file referred to in the BlobStore table exists and can be accessed by the database server. It does not identify "orphan blobs" ie files that exist in the file structure but which are not listed in the BlobStore. +Once the blobs are stored outside the database, the normal database validation cannot check that everything is present and correct. The stored procedure ValidateExternalBlobStoreFilesExist checks that each external file referred to in the BlobStore table exists and can be accessed by the database server. It does not identify "orphan blobs" ie files that exist in the file structure but which are not listed in the BlobStore. It is run by SELECTing from the stored procedure:
- +
-<sxh SQL> +
-CREATE PROCEDURE "pears"."ValidateExternalBlobStoreFilesExist"(IN "@AlternativeLocation" long varchar DEFAULT NULL) +
-  RESULT ("Class" char(1),"ID" char(20),"ExternalFilePath" long varchar, "Issue" SmallInt, "Description" char(250)) +
-BEGIN +
-  DECLARE "@Folder" long varchar; +
-  DECLARE "@StandardLocation" long varchar; +
-  -- create temp table to hold errors +
-  DECLARE LOCAL TEMPORARY TABLE BlobStoreCheckIssue("Class" char(1),"ID" char(20), +
-       "ExternalFilePath" long varchar,"Issue" char(10)) NOT TRANSACTIONAL; +
-  -- regularise Standard and Alternative locations +
-  SET "@StandardLocation" = (SELECT "BlobExternalRootFolder" from params); +
-  IF "@AlternativeLocation" is NULL THEN SET "@AlternativeLocation" = "@StandardLocation" END IF;  +
-  -- @AlternativeLocation now holds the actual location of the files we want to check +
-  IF right("@StandardLocation",1) != '\' THEN SET "@StandardLocation" = string("@StandardLocation",'\') END IF; +
-  IF right("@AlternativeLocation",1) != '\' THEN SET "@AlternativeLocation" = string("@AlternativeLocation",'\') END IF; +
-  -- loop through external blobs +
-    FOR BlobLoop as BlobCursor NO SCROLL CURSOR  +
-      FOR select "Class" as BClass, "ID" as BID, replace("ExternalFilePath","@StandardLocation","@AlternativeLocation") as BExtPath  +
-         from BlobStore where "ExternalFilePath" is not null order by "Class", "ID" +
-          FOR READ ONLY +
-      DO  +
-        if (select byte_substr(xp_read_file(BExtPath,1),0,1)) is NULL // ie error reading file +
-            then insert into BlobStoreCheckIssue("Class","ID","ExternalFilePath","Issue") values (BClass, BID, BExtPath, 1) end if; +
-    END FOR; +
-  -- recheck errors in case files were in use +
-    FOR BlobLoop2 as BlobCursor2 NO SCROLL CURSOR  +
-      FOR select "Class" as BClass, "ID" as BID, "ExternalFilePath" as BExtPath from BlobStoreCheckIssue where Issue = 1 order by "Class", "ID" +
-          FOR READ ONLY +
-      DO  +
-        if (select byte_substr(xp_read_file(BExtPath,1),0,1)) is NULL // ie error reading file +
-            then update BlobStoreCheckIssue set "Issue" = 2 where "Class" = "BClass" and "ID" = BID // still a problem +
-            else update BlobStoreCheckIssue set "Issue" = 0 where "Class" = "BClass" and "ID" = BID // now OK +
-        end if; +
-    END FOR; +
-  -- check existence of remaining issues   +
-    FOR BlobLoop3 as BlobCursor3 NO SCROLL CURSOR  +
-      FOR select "Class" as BClass, "ID" as BID, "ExternalFilePath" as BExtPath from BlobStoreCheckIssue where Issue = 2 order by "Class", "ID" +
-          FOR READ ONLY +
-      DO  +
-        set "@Folder" = null; +
-        set "@Folder" = left(BExtPath,locate(BExtPath,'\',-1)-1); +
-        if exists (select * from sp_list_directory("@Folder",1) where file_path=BExtPath and file_type ='F'  +
-           then update BlobStoreCheckIssue set "Issue" = 3  +
-                  where "Class" = "BClass" and "ID" = BID // file present in folder - must be locked or insufficient rights to read +
-           else update BlobStoreCheckIssue set "Issue" = 4  +
-                  where "Class" = "BClass" and "ID" = BID; // file NOT present        +
-        end if; +
-    END FOR; +
-select "Class", "ID", "ExternalFilePath", "Issue", case  +
-        when "Issue" = 3 then 'File present but not accessible'  +
-        when "Issue" = 4 then 'File missing' end  +
-  as "Description" from BlobStoreCheckIssue; +
-END; +
-</sxh> +
- +
-The validation is run by SELECTing from the stored procedure:+
 <code>select * from pears.ValidateExternalBlobStoreFilesExist();</code> <code>select * from pears.ValidateExternalBlobStoreFilesExist();</code>
  
Line 136: Line 79:
 |O|TI01FQSS161120130006|i:\iqxdocs\2013\11-16\OTI01FQSS161120130006.dat|4|File missing| |O|TI01FQSS161120130006|i:\iqxdocs\2013\11-16\OTI01FQSS161120130006.dat|4|File missing|
  
-Orphaned Blobs in the file system can be identified with this stored procedure:+Orphaned Blobs in the file system can be identified with this stored procedure (Note this may need updating for recent versions of SQLanywhere):
  
 <sxh SQL> <sxh SQL>
  • sa28-00.txt
  • Last modified: 2021/11/26 12:45
  • by Michael Scott