{"id":231635,"date":"2022-03-15T09:51:20","date_gmt":"2022-03-15T08:51:20","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=datagrip&#038;p=231635"},"modified":"2022-09-12T13:37:31","modified_gmt":"2022-09-12T12:37:31","slug":"working-with-source-code-in-datagrip","status":"publish","type":"datagrip","link":"https:\/\/blog.jetbrains.com\/en\/datagrip\/2022\/03\/15\/working-with-source-code-in-datagrip","title":{"rendered":"Working with source codes in DataGrip"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2560\" height=\"1440\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/03\/DSGN-13084-Illustration-for-Working-with-source-codes-in-DataGrip_blog.png\" alt=\"\" class=\"wp-image-233856\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"basics\">Basics<\/h2>\n\n\n\n<p>The source code of the object is the <strong>core<\/strong> part of the DDL script which is needed to create this object. In other words, the <em>CREATE<\/em> statement is not part of the object&#8217;s source code. The source code is stored inside the database. The most common objects found with the source code are views, functions, and stored procedures.<br><br>In many other tools, to update the source code, you would need to generate the <em>CREATE OR REPLACE<\/em> script, make the required changes and run it. In DataGrip it works a little differently. You just need to make the changes you need to make and the proper script will be generated for you.<\/p>\n\n\n\n<p>People coming from other tools all make the same mistake: they open the DDL, copy it, modify it, adjust the <em>CREATE<\/em> statement (usually add the <em>OR REPLACE<\/em> part) and run the new script. <strong>This is not the way you are supposed to update the source code.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loading-sources\">Loading sources<\/h2>\n\n\n\n<p>For any data source, DataGrip performs the process called &#8216;introspection&#8217; \u2013 the IDE retrieves all the metadata for objects in advance. This means that all source code is loaded when the introspection is performed.<\/p>\n\n\n\n<p>You can manage this process by choosing the required value in the <em>Load Sources for<\/em> options in the data source properties.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"1560\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/SettingsLoadSources.png\" alt=\"\" class=\"wp-image-231636\"\/><\/figure>\n\n\n\n<p>In Oracle there is the possibility not to load sources by choosing a lower introspection level:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"1560\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/SettingsIntrospectionLevel.png\" alt=\"\" class=\"wp-image-231647\"\/><\/figure>\n\n\n\n<p>You might be asking: if the source code is loaded when introspection is performed, doesn\u2019t it mean that they are out-dated?<\/p>\n\n\n\n<p>Yes, it does. Handling this situation will be covered later in our tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-flow\">The flow<\/h2>\n\n\n\n<p>Double-clicking on the object in the database explorer or pressing <em>Cmd\/Ctrl+B<\/em> on it in the SQL script opens the DDL editor. In case of the view, double-clicking on it in the database explorer opens the data: to open the DDL explorer click on the DDL button in the toolbar.<\/p>\n\n\n\n<p>The DDL editor will open. Here you can find the <em>CREATE<\/em> script for the object.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"918\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/OpenDDLBasics.gif\" alt=\"\" class=\"wp-image-231658\"\/><\/figure>\n\n\n\n<p><strong>Important! <\/strong>The object references in the generated script are not qualified, in other words, <em>%view_name%<\/em> is used rather than the <em>%schema_name%.%view_name%<\/em>. The reason for this is the ability to copy the script for applying it in another context. If you want a script with qualified references, use the <a href=\"https:\/\/www.jetbrains.com\/help\/datagrip\/export-data.html#generate-ddl-definitions-for-dat\" target=\"_blank\" rel=\"noopener\">SQL generator<\/a>.<\/p>\n\n\n\n<p>In the DDL editor, you can change the source code. When you edit the source code of any object, DataGrip tracks changes and highlights them in the gutter.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"821\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/ChangesOnGutter.png\" alt=\"\" class=\"wp-image-231669\"\/><\/figure>\n\n\n\n<p>For example, add a comment line to a procedure or a function. The line you added is highlighted. If you click the highlighted line in the gutter, a small toolbar is displayed with the <em>Show Diff <\/em>button. You can click the <em>Show Diff<\/em> button to see the difference between the code that you added and the source code.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"560\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/DiffSmall.png\" alt=\"\" class=\"wp-image-231817\"\/><\/figure>\n\n\n\n<p>After you made the required changes, click the <em>Submit<\/em> button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"821\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/SubmitButton.png\" alt=\"\" class=\"wp-image-231680\"\/><\/figure>\n\n\n\n<p>DataGrip will generate the modification script and show you a preview. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"1233\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/MigrationDialog.png\" alt=\"\" class=\"wp-image-231691\"\/><\/figure>\n\n\n\n<p>If you are okay with the result, click on OK and the script will be executed in the database. As a result, the required source code will be changed.&nbsp;<\/p>\n\n\n\n<p>DataGrip not only adds <em>OR REPLACE<\/em> to the creation script, but it can also handle more difficult situations, i.e. changing the object&#8217;s signature or object renaming. When it&#8217;s needed, <em>DROP<\/em> and <em>CREATE<\/em> scripts will be created.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"1371\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/DropCreateScript-1.png\" alt=\"\" class=\"wp-image-231806\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"changing-several-objects-at-once\">Changing several objects at once<\/h2>\n\n\n\n<p>When the changes are made in the DDL editor, but not submitted yet, DataGrip stores them until you submit them. For example, if you changed several objects, several changed DDL are cached waiting for being applied. In the tool window called <em>Database Changes, <\/em>you can observe all pending source code changes and submit them all together at once.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2336\" height=\"1380\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/DataBaseChanges.png\" alt=\"\" class=\"wp-image-231828\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"outdated-cached-objects\">Outdated cached objects\ufeff<\/h2>\n\n\n\n<p>As we mentioned, DataGrip caches the source code which was loaded at the introspection. If an object that you opened was updated from a third-party location, you will see a notification that the cached object differs from the source code of the same object in the database.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"872\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/ChachedObjectDif.png\" alt=\"\" class=\"wp-image-231724\"\/><\/figure>\n\n\n\n<p>If you see this warning in the IDE, you can choose one of the following actions to take:<\/p>\n\n\n\n<ul><li><em>Synchronize<\/em>: fetch changes from the database and update the cached local object.<\/li><li><em>Disable check<\/em>: disable this notification.<\/li><\/ul>\n\n\n\n<p>Also, there might be a conflict between your version of the object source code and that in the database. For example, when you have modified the same source code as someone else and clicked <strong>Submit<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"953\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/PossibleLossOfchanges.png\" alt=\"\" class=\"wp-image-231735\"\/><\/figure>\n\n\n\n<p>You can forcefully replace the source code of the object in the database using <em>Force Refactoring<\/em> or you can synchronize the object state and then proceed with changes by clicking <em>Abort Refactoring and Synchronize<\/em>.<\/p>\n\n\n\n<p>If you&#8217;ve selected <em>Abort Refactoring and Synchronize<\/em>, DataGrip aborts the submit operation and fetches the changes from the database just like if you had clicked <em>Synchronize<\/em>. If the conflict still exists, you will see the following notification.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"953\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/ObjectChanged-1.png\" alt=\"\" class=\"wp-image-231746\"\/><\/figure>\n\n\n\n<p>In this notification, you can choose between the following options:<\/p>\n\n\n\n<ul><li><em>Revert local changes<\/em>: roll back all your changes and replace them with the version from the database.<\/li><li><em>Keep local changes<\/em>: use your changes and overwrite changes in the database.<\/li><li><em>Merge<\/em>: display the diff dialog to merge the versions of the object source code.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"local-history\">Local history<\/h2>\n\n\n\n<p>All the changes you make to the source code are stored locally. If you want to go back in time and check the source code of the function before the latest updates you can do it with the help of <em>Local History.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2000\" height=\"1321\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/ShowLocalHistory.png\" alt=\"\" class=\"wp-image-231757\"\/><\/figure>\n\n\n\n<p>The revisions in <em>Local History <\/em>contain all your local changes and also include the versions retrieved from the database during introspections. So, the versions of the object that were introduced from third-party sources and not &#8216;loaded&#8217; during the introspection may be missing here.&nbsp;You can compare any historical revision with the current version of the source code.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-editorskit-shadow\"><img decoding=\"async\" loading=\"lazy\" width=\"2340\" height=\"1108\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/02\/LocalHistory.png\" alt=\"\" class=\"wp-image-231768\"\/><\/figure>\n\n\n\n<p>If you are the only user who works with a particular database, and you change sources only from DataGrip, <em>Local History<\/em> has a complete history of the objects&#8217; changes.&nbsp;<\/p>\n\n\n\n<p>That&#8217;s it! We know that this flow can feel unfamiliar, especially if you came from the other tool, but following this flow will make your life easier by removing repetitive operations.<\/p>\n\n\n\n<p>The DataGrip team.<\/p>\n\n\n\n<p>P.S. The color scheme used for the screenshots is <em>Monocai<\/em>.<\/p>\n","protected":false},"author":395,"featured_media":278339,"comment_status":"closed","ping_status":"closed","template":"","categories":[89,2347],"tags":[91,792],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/datagrip\/231635"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/datagrip"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/types\/datagrip"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/comments?post=231635"}],"version-history":[{"count":8,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/datagrip\/231635\/revisions"}],"predecessor-version":[{"id":278338,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/datagrip\/231635\/revisions\/278338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/media\/278339"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/media?parent=231635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/categories?post=231635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/tags?post=231635"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/cross-post-tag?post=231635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}