DataGrip 2023.3 Help

Modify source code of database objects

DataGrip tracks changes that you make to objects that store their source code in the database. These objects might be triggers, procedures, functions, views, or other objects. Every modification that you make to these objects in the editor is saved as a local version of the object source code.

Updating source code

DataGrip retrieves the information about a database during the introspection process. This information is used to show the objects in the Database Explorer, display their DDL, suggest them during completion, and in other features for coding assistance.

You can update the source code of database objects directly by editing their DDL and submitting your changes. The IDE will generate a migration script and execute it in the database.

The Database Changes tool window displays a summary of all your changes.

Load source code for a data source

DataGrip retrieves the source code for a data source during the introspection. You can manage this process in the data source properties.

  1. Open data source properties. You can open data source properties by using one of the following options:

    • Navigate to File | Data Sources....

    • Press Control+Alt+Shift+S.

    • In the Database Explorer Command 1 ( View | Tool Windows | Database Explorer), click the Data Source Properties icon (The Data Source Properties icon).

    Add new data source
  2. Select one or more data sources for which you want to download source code.

  3. Right-click the selection and navigate to Load Sources. You can select between the following options:

    • None: do not download source code.

    • Except System Schemas: download source code for all the objects except for system schemas.

    • All Schemas: download all the available source code.

    Load source code for a data source

Edit source code of an object

You can update the source code of database objects by directly editing their DDL CREATE scripts and submitting your changes in the editor. The IDE will generate a migration script with your changes, prompt you to verify it, and then execute it in the database.

  1. Right-click an object and select Navigation | Go to DDL. Alternatively, press Control+B.

  2. Make changes to source code.

  3. (Optional) In the Database Changes tool window (View | Tool Windows | Database Changes), double-click the modified object to open a difference viewer and verify your changes.

  4. Click the Submit button (the Submit button).

    When you click the Submit button (the Submit button) in the Database Changes tool window, you see the Migration dialog. The Migration dialog displays a migration script for the object.

  5. In the Object Migration dialog, verify that the migration script is correct and click OK.

    A migration script is code that changes the entire database or a part of it. You can use migration scripts to add or remove a column, upgrade the database version, or change column properties.

    DataGrip can automatically generate a migration script, but you must check it before running.

See all the changes in source code

  • Select View | Tool Windows | Database Changes from the main menu.

    the Database Changes tool window

See the difference between modified and stored versions

  • When you edit source code of any object, DataGrip tracks changes and highlights them in the gutter. For example, add a comment line to a routine or a trigger function. The added line becomes highlighted. If you click the highlighted line in the gutter, a small toolbar is displayed with the Show Diff for Lines button. You can click the Show Diff for Lines button (the Show Diff icon) to see the difference between the code that you added and the one from source code.

    IDE highlights changes in source code
  • In the Database Changes tool window (View | Tool Windows | Database Changes), double-click the modified object to open a difference viewer and verify your changes.

    the Database Changes tool window

Outdated cached objects

DataGrip caches the source code of all the objects from the database to provide fast coding assistance and navigation. 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.

Outdated cached objects

If you see this warning in the IDE, you can select between the following actions:

  • Synchronize: fetch changes from the database and update the cached local object.

  • Disable check: disable this notification.

Also, there might be a conflict between your version of the object source code and the one in the database. For example, when you have modified the same source code as someone else and pressed Submit (Control+K).

Outdated cached objects

You can forcefully replace source code of the object in the database (Force Refactoring) or synchronize the object state and then proceed with changes (Abort Refactoring and Synchronize).

If you selected Abort Refactoring and Synchronize, DataGrip aborts the submit operation and fetches changes from the database (just like if you pressed Synchronize Control+F5). If the conflict still exists, you will see the following notification.

Outdated cached objects

In this notification, you can select between the following options:

  • Revert local changes: roll back all your changes and replace them with the version from the database.

  • Keep local changes: use your changes and overwrite changes in the database.

  • Merge: display the diff dialog to merge two versions of the object source code.

    Outdated cached objects

Colors of the object state

By default, all the objects are displayed in black. It means that the current state of an object source code does not differ from source code of the same object on a server.

If source code of the object has been changed but there are no conflicts with the object source code on a server, the object is displayed in blue.

If there are conflicts in source code of a local object and the same object on a server, the object is displayed in red. If you open such object in the editor, you will see a notification about conflicts.

For example, in the following screenshot, the get_customer_balance function is modified but contains no conflicts, the film_in_stock function was modified and conflicts with the source of the same function on a server, other objects are not modified.

Colors of database objects

Controls of the Database Changes tool window

Icon

Action and shortcut

Description

the Submit button

Submit

Control+K

Submit local changes to the database server. For more information about submitting and reverting changes, refer to Submit changes to a database.

the Roll back icon

Roll back

Control+Alt+Z

(For the Manual transaction mode.) Roll back changes.

the Show Diff icon

Show Diff

Control+D

Compare and see the difference between the code that you edited and the one from source code.

the Group by icon

Group by

Group objects according to their type. For example, routines, rules, or views.

the Expand All icon

Expand All

Control+NumPad-+

Expand all nodes in all trees.

the Collapse All icon

Collapse All

Control+NumPad--

Collapse all nodes in all trees.

Last modified: 26 October 2023