DataGrip | Source Code Handling Tutorial


Basic knowledge


An object's source code is the core part of the DDL script required to create the object. That is, the CREATE statement is not part of the object's source code. Source code is stored in a database. The most common objects in source code are views, functions, and stored procedures.


In many other tools, before updating the source code, the CREATE OR REPLACE script needs to be generated, made the required changes, and then run. In DataGrip, it works a little differently. Once the changes are made, the correct script is generated.


Consumers from other tools often make the mistake of opening the DDL, copying, modifying, adjusting the action statement (usually adding an OR REPLACE section), and then running the new script.  This is not the correct way to update source code.


load source


Regardless of the data source, DataGrip performs an "introspection" process - the IDE retrieves all metadata for the object ahead of time. This means that all source code is loaded when introspection is performed.


You can manage this process by selecting the desired value in the  Load Sources for option of the data source properties .


In Oracle, a lower level of introspection can be selected to not load the source:


You might ask: if source code is loaded when introspection is performed, does that mean they are out of date?


yes. A later part of the tutorial will explain how to handle this situation.


flow


The DDL editor can be opened by double-clicking an object in the Database Explorer or by pressing Cmd/Ctrl+B in an SQL script. Double-clicking a view in the Database Explorer opens the data: To open the DDL Explorer, click the DDL button in the toolbar.


这将打开 DDL 编辑器。 在这里,您可以找到对象的 CREATE 脚本。


重要提示 !生成的脚本中的对象引用是非限定的,换句话说,使用的是 %view_name% 而不是 %schema_name%.%view_name%。 这是为了能够复制脚本以在另一个上下文中应用。 如果您想要一个带有限定引用的脚本,请使用 SQL 生成器

  • SQL 生成器

    https://www.jetbrains.com.cn/help/datagrip/export-data.html#generate-ddl-definitions-for-dat


在 DDL 编辑器中,您可以更改源代码。 编辑对象的源代码时,DataGrip 会跟踪变更并在间距中高亮显示。


例如,向过程或函数添加注释行。 添加的行会高亮显示。 点击间距中高亮显示的行,会显示带有 Show Diff (显示差异)按钮的小工具栏。 点击 Show Diff(显示差异)按钮即可查看添加的代码与源代码之间的差异。


完成变更后,点击 Submit(提交)按钮。


DataGrip 将生成修改脚本并显示预览。


如果您对结果满意,请点击 OK(确定),脚本随后会在数据库中执行。 因此,所需的源代码将被更改。


DataGrip 不仅在创建脚本中添加 OR REPLACE,还可以处理更困难的情况,包括更改对象的签名或对象重命名。 需要时,将创建 DROP 和 CREATE 脚本。


一次更改多个对象


在 DDL 编辑器中完成变更但尚未提交时,DataGrip 会将其存储,直到提交。 例如,如果您更改了多个对象,则会缓存多个更改的 DDL,等待应用。 在 Database Changes(数据库变更)工具窗口中,您可以观察所有待处理源代码变更并一次全部提交。


过时的缓存对象


如前所述,DataGrip 会缓存在内省时加载的源代码。 如果打开的对象更新自第三方位置,通知将显示缓存对象与数据库中同一对象的源代码不同。


如果您在 IDE 中看到此警告,您可以选择以下操作:

  • Synchronize(同步):从数据库获取变更并更新缓存的本地对象。

  • Disable check(禁用检查):禁用此通知。


此外,对象源代码版本与数据库中的版本之间可能存在冲突。 例如,与其他人修改了相同的源代码并点击 Submit(提交)的情况。


您可以使用 Force Refactoring(强制重构)强制替换数据库中对象的源代码,也可以同步对象状态,然后点击 Abort Refactoring and Synchronize(中止重构并同步)保留变更并继续。


选择 Abort Refactoring and Synchronize(中止重构并同步)后,DataGrip 将中止提交操作并从数据库获取变更,就像点击 Synchronize(同步)一样。 如果冲突仍然存在,您将看到以下通知。


在此通知中,您可以选择以下选项:

  • Revert local changes(还原本地变更):回滚所有变更并将其替换为数据库中的版本。

  • Keep local changes(保留本地变更):使用您的变更并覆盖数据库中的变更。

  • Merge(合并):显示差异对话框,合并对象源代码的版本。


本地历史记录


您对源代码所做的所有变更都存储在本地。 如果您想在最新更新之前及时返回并检查函数的源代码,您可以使用 Local History(本地历史记录)。


Local History(本地历史记录)中的修订包含所有本地变更,还包括内省期间从数据库检索的版本。 因此,从第三方源引入且在内省期间未“加载”的对象版本可能在此处丢失。您可以将任意历史版本与源代码的当前版本进行比较。


如果您是特定数据库的唯一用户,并且您仅从 DataGrip 更改源,Local History(本地历史记录)会提供对象变更的完整历史记录。 


文章就到这里了。 这个流程对于有些人来说可能比较陌生,特别是来自其他工具的用户,但遵循这一流程将免除重复操作,让工作更加轻松。


P.S. 截图使用的配色方案是 Monocai。

  • 扫码开始30天免费试用

    DataGrip »»»

  • 点击观看

    DataGrip 入门介绍 »»»


本文英文原作者:Maksim Sobolevskiy

⏬ 戳「阅读原文」了解 DataGrip 并开启免费试用

本文分享自微信公众号 - JetBrains(JetBrainsChina)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/5494143/blog/5509325