Redgate Flyway

For PostgreSQL users - where are your DBs hosted?

Tutorial - Find invalid objects before baselining

 Flyway Enterprise

For SQL Server and Oracle

When you baseline your project in Flyway Desktop using a script, if we detect any objects that might be invalid, we will show you a list and you can decide which objects to filter out of the project.  These objects will be excluded from the baseline script so it won't fail when you try to generate migrations or build/spin up a new database from the scripts in the repository.  This should only be done for objects that truly aren't needed for development and deployment since they won't be included in the schema-model or when you generate additional migrations.  

Hint: To select multiple objects, check the first row and then hold Shift+Click to check the last row.  All the rows in between will be checked. 

Scripts for Invalid Object detection

Here are the scripts we use to detect invalid objects in SQL Server and Oracle databases:.

SQL Server - Find Invalid Objects Toggle source code

  1. WITH linked_servers AS (
  2. SELECT name, 1 AS is_linked_server
  3. FROM sys.servers
  4. WHERE is_linked = 1
  5. ),
  6. invalid_db_objects AS (
  7. SELECT
  8. cte.referencing_id AS invalid_object_id,
  9. SCHEMA_NAME(all_object.[schema_id]) AS invalid_obj_schema,
  10. all_object.name AS invalid_obj_name,
  11. all_object.[type] AS invalid_obj_type,
  12. 'Invalid object name ''' + cte.obj_name + '''' AS custom_error_message
  13. FROM (
  14. SELECT
  15. sed.referencing_id,
  16. COALESCE(sed.referenced_schema_name + '.', '') + sed.referenced_entity_name AS obj_name
  17. FROM sys.sql_expression_dependencies sed
  18. WHERE sed.is_ambiguous = 0
  19. AND sed.referenced_id IS NULL
  20. AND sed.referenced_database_name IS NULL
  21. ) cte
  22. JOIN sys.objects all_object ON cte.referencing_id = all_object.[object_id]
  23. ),
  24. ambiguous_db_objects AS (
  25. SELECT
  26. sed.referencing_id AS invalid_object_id,
  27. SCHEMA_NAME(o.[schema_id]) AS invalid_obj_schema,
  28. o.name AS invalid_obj_name,
  29. o.[type] AS invalid_obj_type,
  30. 'Unable to verify ''' + COALESCE(sed.referenced_database_name + '.', '') + COALESCE(sed.referenced_schema_name + '.', '') + sed.referenced_entity_name + '''' AS custom_error_message,
  31. ISNULL(ls.is_linked_server, 0) AS is_linked_server
  32. FROM sys.sql_expression_dependencies sed
  33. JOIN sys.objects o ON sed.referencing_id = o.[object_id]
  34. LEFT JOIN linked_servers ls ON ls.name = sed.referenced_server_name
  35. WHERE sed.is_ambiguous = 0
  36. AND sed.referenced_id IS NULL
  37. AND sed.referenced_database_name IS NOT NULL
  38. )
  39. SELECT
  40. invalid_obj_schema AS [Invalid Object Schema],
  41. invalid_obj_name AS [Invalid Object Name],
  42. invalid_obj_type AS [Object Type],
  43. custom_error_message AS [Error Message],
  44. 'Local' AS [Query Type]
  45. FROM invalid_db_objects
  46. UNION ALL
  47. SELECT
  48. invalid_obj_schema AS [Invalid Object Schema],
  49. invalid_obj_name AS [Invalid Object Name],
  50. invalid_obj_type AS [Object Type],
  51. custom_error_message AS [Error Message],
  52. CASE WHEN is_linked_server = 1 THEN 'Linked Server' ELSE 'Cross DB' END AS [Query Type]
  53. FROM ambiguous_db_objects;

Oracle - Find Invalid Objects Toggle source code

  1. SELECT
  2. OWNER AS "SCHEMA_NAME",
  3. OBJECT_NAME AS "OBJECT_NAME",
  4. OBJECT_TYPE AS "OBJECT_TYPE"
  5. FROM
  6. ALL_OBJECTS
  7. WHERE
  8. STATUS = 'INVALID';



If you have any feedback on these scripts or a script to find invalid objects in other databases, please get in touch to help us document this.





Didn't find what you were looking for?