{"id":173,"date":"2019-10-14T21:44:36","date_gmt":"2019-10-14T21:44:36","guid":{"rendered":"https:\/\/snowflake.pavlik.us\/?p=173"},"modified":"2019-10-14T21:44:42","modified_gmt":"2019-10-14T21:44:42","slug":"object-dependency-checking-in-snowflake","status":"publish","type":"post","link":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/","title":{"rendered":"Object Dependency Checking in Snowflake"},"content":{"rendered":"\n<p>Snowflake allows dropping of an object that has a view dependent on it. For example, create tables A and B, and view C that joins A and B together. Snowflake will allow you to drop table A or B, and when you attempt to use view C you will get an error. This behavior is fairly common for DBMSes.<\/p>\n\n\n\n<p>It&#8217;s important to document and maintain dependencies using a combination of object (source) control, a rigorous testing regimen, and promotion protocols. That said, here&#8217;s a stored procedure to check object dependencies, specifically to see if all views are working. The stored procedure inventories all views in the Snowflake account and attempts to select a single row. If the row selection succeeds, it reports success for that view. If it fails, it reports the reason for the failure. <\/p>\n\n\n\n<p>Note that running this stored procedure could take a very long time since it needs to select one row from every view in the account. This is a prototype procedure, and a future version will include the ability to limit the database and\/or schema to test, probably using RegEx to enable pattern matching. This will allow testing of all views in the PROD database matching something like a prefix, since whatever change may be only likely to affect views in that database starting with a prefix.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nuse warehouse TEST;  -- An Extra Small warehouse is best for this test.\nuse role SYSADMIN;   -- Could be any role except for ACCOUNTADMIN. The ACCOUNTADMIN will own the SP to test views and grant usage to this role.\nuse database TEST;   -- Could be any database; just put one in context.\n\nuse role ACCOUNTADMIN; -- We need to create the stored procedure as ACCOUNTADMIN to ensure we have read access to all views.\n\n\/********************************************************************************************************************\n\nProcedure:   TestAllViews\nDescription: Executes as ACCOUNTADMIN and attempts to select a single row from every view in the account. For each\n             view in the account, it returns &quot;Success&quot; if it can read a single row, or &quot;Failure&quot; and the error \n             message if it fails trying to read a single row. This enables a quick sanity check of the views after\n             changing objects, such as dropping a table, view, or column.\nParameters:  None.\nReturn:      A string with each line the test result of each view.\nNotes:       This is a prototype SP. Future versions will have parameters to limit databases and schemas to check.\n\n*********************************************************************************************************************\/\ncreate or replace procedure TEST.PUBLIC.TestAllViews()\nreturns string\nlanguage JavaScript\nexecute as OWNER\nas\n  $$  \n   \n    var sql_command = \n    `select DATABASE_NAME from INFORMATION_SCHEMA.DATABASES;`;\n    \n    var databaseArray = new Array();\n\n    try {\n            var stmt = snowflake.createStatement( {sqlText: sql_command} );\n            var resultSet = stmt.execute();\n            \n            while (resultSet.next())  {\n                databaseArray.push(resultSet.getColumnValue(&#039;DATABASE_NAME&#039;));\n            }\n        }\n    catch (err)  {\n        return &quot;Failed: &quot; + err;   \/\/ Return a success\/error indicator.\n        }\n    \n    var outString = &#039;&#039;;\n    var index = 0;\n    \n    var viewArray = new Array();\n    \n    while (index &lt; databaseArray.length) { \n        \n        sql_command = &quot;select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME from &quot; + databaseArray&#x5B;index] + \n                      &quot;.INFORMATION_SCHEMA.VIEWS where TABLE_SCHEMA &lt;&gt; &#039;INFORMATION_SCHEMA&#039;;&quot;;\n        \n        try {\n            var stmt = snowflake.createStatement( {sqlText: sql_command} );\n            var resultSet = stmt.execute();\n            \n            while (resultSet.next())  {\n                viewArray.push(resultSet.getColumnValue(&#039;TABLE_CATALOG&#039;) + &quot;.&quot; + \n                               resultSet.getColumnValue(&#039;TABLE_SCHEMA&#039;) + &quot;.&quot; + \n                               resultSet.getColumnValue(&#039;TABLE_NAME&#039;));\n            }\n        }\n    catch (err)  {\n        return &quot;Failed: &quot; + err;   \/\/ Return a success\/error indicator.\n        }\n      \n      index++; \n    }\n    \n    index = 0;\n    \n    \/\/ We now have a complete list of views... Test them one at a time...\n    \n    var outArray = new Array();\n\n    index = 0;\n\n    while (index &lt; viewArray.length) { \n      sql_command = &quot;select * from &quot; + viewArray&#x5B;index] + &quot; limit 1;&quot;;\n\n      try {\n        var stmt = snowflake.createStatement( {sqlText: sql_command} );\n        var resultSet = stmt.execute();\n        \n        outArray.push(&quot;Success...&quot; + viewArray&#x5B;index]);\n      }\n      catch (err) {\n        outArray.push(&quot;Failure...&quot; + viewArray&#x5B;index] + &quot; : &quot; + err.message.replace(\/(\\r\\n|\\n|\\r)\/gm, &quot; - &quot;));\n       }\n      index++; \n    }\n\n    index = 0;    \n\n    while (index &lt; outArray.length) { \n      outString += outArray&#x5B;index] + &quot;\\n&quot;; \n      index++; \n    }   \n\n   return outString;\n\n  $$;\n\ngrant usage on procedure TEST.PUBLIC.TestAllViews() to role SYSADMIN;\n\nuse role SYSADMIN;\n\ncall TEST.PUBLIC.TestAllViews();\n-- Note: Completing this SP could take a long time.\n\n\/*\n\nThe output should look something like this (note dropped table &quot;TEST.VIEWTEST.A&quot; to break view &quot;TEST.VIEWTEST.C&quot;)\n\nSuccess...CITIBIKE_BIG_V2.CLUSTERED.TRIPS_VW\nSuccess...CITIBIKE_BIG_V2.CLUSTERED.TRIPS_WEATHER_VW\nSuccess...CITIBIKE_BIG_V2.UNCLUSTERED.TRIPS_VW\nSuccess...CITIBIKE_BIG_V2.UNCLUSTERED.TRIPS_WEATHER_VW\nSuccess...SALES.PUBLIC.REVENUE\nSuccess...SALES.PUBLIC.SALES_REVENUE\nSuccess...SALES_05_31_19.PUBLIC.REVENUE\nSuccess...SALES_05_31_19.PUBLIC.SALES_REVENUE\nSuccess...TEST.AUDIT.NEW_VIEW\nFailure...TEST.VIEWTEST.C : SQL compilation error: - Failure during expansion of view &#039;C&#039;: SQL compilation error: - Object &#039;TEST.VIEWTEST.A&#039; does not exist.\nSuccess...TEST.PUBLIC.ORDERS_SECURE_VIEW\nSuccess...TEST.TPCH_SF1.ORDERS_SECURE_VIEW\nSuccess...TEST_CHAR.PUBLIC.V_TEST_CHAR\n\n*\/\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Snowflake allows dropping of an object that has a view dependent on it. For example, create tables A and B, and view C that joins A and B together. Snowflake will allow you to drop table A or B, and when you attempt to use view C you will get an error. This behavior is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,2,13,12],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-object-dependencies","category-sql","category-stored-procedures-sql","category-stored-procedures"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>Object Dependency Checking in Snowflake - Snowflake in the Carolinas<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Object Dependency Checking in Snowflake - Snowflake in the Carolinas\" \/>\r\n<meta property=\"og:description\" content=\"Snowflake allows dropping of an object that has a view dependent on it. For example, create tables A and B, and view C that joins A and B together. Snowflake will allow you to drop table A or B, and when you attempt to use view C you will get an error. This behavior is [&hellip;]\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/\" \/>\r\n<meta property=\"og:site_name\" content=\"Snowflake in the Carolinas\" \/>\r\n<meta property=\"article:published_time\" content=\"2019-10-14T21:44:36+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2019-10-14T21:44:42+00:00\" \/>\r\n<meta name=\"author\" content=\"Greg Pavlik\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Greg Pavlik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/\"},\"author\":{\"name\":\"Greg Pavlik\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\"},\"headline\":\"Object Dependency Checking in Snowflake\",\"datePublished\":\"2019-10-14T21:44:36+00:00\",\"dateModified\":\"2019-10-14T21:44:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/\"},\"wordCount\":230,\"commentCount\":1,\"articleSection\":[\"Object Dependencies\",\"SnowSQL\",\"Stored Procedures\",\"Stored Procedures\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/\",\"url\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/\",\"name\":\"Object Dependency Checking in Snowflake - Snowflake in the Carolinas\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#website\"},\"datePublished\":\"2019-10-14T21:44:36+00:00\",\"dateModified\":\"2019-10-14T21:44:42+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/index.php\\\/2019\\\/10\\\/14\\\/object-dependency-checking-in-snowflake\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/snowflake.pavlik.us\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Object Dependency Checking in Snowflake\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#website\",\"url\":\"https:\\\/\\\/snowflake.pavlik.us\\\/\",\"name\":\"Snowflake in the Carolinas\",\"description\":\"Random thoughts on all things Snowflake in the Carolinas\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/snowflake.pavlik.us\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/snowflake.pavlik.us\\\/#\\\/schema\\\/person\\\/019455f4675665b6cf5edea31ec44d7b\",\"name\":\"Greg Pavlik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g\",\"caption\":\"Greg Pavlik\"},\"description\":\"Greg is a Senior Sales Engineer at Snowflake Computing, in the Raleigh-Durham area. He's been in data management and security for the twenty years.\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Object Dependency Checking in Snowflake - Snowflake in the Carolinas","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/","og_locale":"en_US","og_type":"article","og_title":"Object Dependency Checking in Snowflake - Snowflake in the Carolinas","og_description":"Snowflake allows dropping of an object that has a view dependent on it. For example, create tables A and B, and view C that joins A and B together. Snowflake will allow you to drop table A or B, and when you attempt to use view C you will get an error. This behavior is [&hellip;]","og_url":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/","og_site_name":"Snowflake in the Carolinas","article_published_time":"2019-10-14T21:44:36+00:00","article_modified_time":"2019-10-14T21:44:42+00:00","author":"Greg Pavlik","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Greg Pavlik","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/#article","isPartOf":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/"},"author":{"name":"Greg Pavlik","@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b"},"headline":"Object Dependency Checking in Snowflake","datePublished":"2019-10-14T21:44:36+00:00","dateModified":"2019-10-14T21:44:42+00:00","mainEntityOfPage":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/"},"wordCount":230,"commentCount":1,"articleSection":["Object Dependencies","SnowSQL","Stored Procedures","Stored Procedures"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/","url":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/","name":"Object Dependency Checking in Snowflake - Snowflake in the Carolinas","isPartOf":{"@id":"https:\/\/snowflake.pavlik.us\/#website"},"datePublished":"2019-10-14T21:44:36+00:00","dateModified":"2019-10-14T21:44:42+00:00","author":{"@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b"},"breadcrumb":{"@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/snowflake.pavlik.us\/index.php\/2019\/10\/14\/object-dependency-checking-in-snowflake\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/snowflake.pavlik.us\/"},{"@type":"ListItem","position":2,"name":"Object Dependency Checking in Snowflake"}]},{"@type":"WebSite","@id":"https:\/\/snowflake.pavlik.us\/#website","url":"https:\/\/snowflake.pavlik.us\/","name":"Snowflake in the Carolinas","description":"Random thoughts on all things Snowflake in the Carolinas","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/snowflake.pavlik.us\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/snowflake.pavlik.us\/#\/schema\/person\/019455f4675665b6cf5edea31ec44d7b","name":"Greg Pavlik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d81df729eebf37a042922b17d4a4c834b1e0ccfa9fea1c2c78cb8e95c7e91701?s=96&d=mm&r=g","caption":"Greg Pavlik"},"description":"Greg is a Senior Sales Engineer at Snowflake Computing, in the Raleigh-Durham area. He's been in data management and security for the twenty years."}]}},"_links":{"self":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":1,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":174,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/posts\/173\/revisions\/174"}],"wp:attachment":[{"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/snowflake.pavlik.us\/index.php\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}