126.96.36.199. Execution of Database Scripts by Gradle Tasks
This mechanism is generally used by application developers for updating their own database instance. The execution of scripts essentially comes down to running a special Gradle task from build.gradle build script. This can be done from the command line or via the Studio interface.
To run scripts to create the database, the
createDb task is used. In Studio, it corresponds to the CUBA > Create database command in the main menu. When this task is started, the following occurs:
Scripts of the application components and
db/**/*.sqlscripts of the core module of the current project are built in the
modules/core/build/dbdirectory. Sets of scripts for application components are located in subdirectories with numeric prefixes. The prefixes are used to provide the alphabetical order of the execution of scripts according to the dependencies between components.
If the database exists, it is completely erased. A new empty database is created.
All creation scripts from
modules/core/build/db/init/**/*create-db.sqlsubdirectory are executed sequentially in the alphabetical order, and their names along with the path relative to the db directory are registered in the SYS_DB_CHANGELOG table.
Similarly, in the SYS_DB_CHANGELOG table, all currently available
modules/core/build/db/update/**/*.sqlupdate scripts are registered. This is required for applying the future incremental updates to the database.
To run scripts to update the database, the
updateDb task is used. In Studio, it corresponds to the CUBA > Update database command in the main menu. When this task is started, the following occurs:
The scripts are built in the same way as for the
createDbcommand described above.
The execution mechanism checks, whether all creation scripts of application components have been run (by checking the
SYS_DB_CHANGELOGtable). If not, the application component creation scripts are executed and registered in the
A search is performed in
modules/core/build/db/update/**directories, for update scripts which are not registered in the
SYS_DB_CHANGELOGtable, i.e., not previously executed.
All scripts found in the previous step are executed sequentially in the alphabetical order, and their names along with the path relative to the
dbdirectory are registered in the