Class AbstractDatabase.InitScripts
java.lang.Object
is.codion.common.db.database.AbstractDatabase.InitScripts
- Enclosing class:
AbstractDatabase
The init scripts of a database, each run once in the database's life, as
Database.INIT_SCRIPTS describes,
for a dialect running them to provide the record of the scripts run, table(), and how a script runs.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidCreates the table recording the scripts run, seetable(), in case it does not exist.protected abstract booleanexists(Connection connection) protected abstract InputStreamOpens a script for reading, its path as listed.final voidrun(Connection connection, List<String> scriptPaths, boolean existing) Runs the scripts not yet run, in the order listed, each recorded with a checksum of its content once run, after checking that each one run is unchanged and still listed, and that none not yet run is listed before one that has, so that a database brought up to date one list at a time ends up as one created from the last.protected abstract voidRuns a script, within the transaction recording it.Sanitizes script paths, a dialect possibly running a script by its path in a statement of its own, for a dialect to refuse them before opening the database,run(Connection, List, boolean)andvalidate(Connection, List)sanitizing them as well.final Stringtable()final voidvalidate(Connection connection, List<String> scriptPaths) Validates a database against the init scripts it would be opened with, refusing it asrun(Connection, List, boolean)would, changing nothing, so the connection may be read only.
-
Constructor Details
-
InitScripts
- Parameters:
table- the table recording the scripts run, with the columnspath, a varchar primary key,checksum, 32 bytes, andrun_at, a timestamp defaulting to the current one
-
-
Method Details
-
table
- Returns:
- the table recording the scripts run
-
run
public final void run(Connection connection, List<String> scriptPaths, boolean existing) throws SQLException Runs the scripts not yet run, in the order listed, each recorded with a checksum of its content once run, after checking that each one run is unchanged and still listed, and that none not yet run is listed before one that has, so that a database brought up to date one list at a time ends up as one created from the last. A database refused is left as it was, a moved script's record included.- Parameters:
connection- the connection, its auto-commit disabled by this methodscriptPaths- the scripts, in the order to runexisting- true in case the database existed before, false in case it is being created- Throws:
DatabaseException- in case the database is refused, a script can not be read or failsSQLException- in case of an exception
-
validate
Validates a database against the init scripts it would be opened with, refusing it asrun(Connection, List, boolean)would, changing nothing, so the connection may be read only. A database without the record of its scripts is never refused, nor one opened with none listed.- Parameters:
connection- a connection to the databasescriptPaths- the init scripts it would be opened with- Throws:
DatabaseException- in case the database would be refused, or a script listed can not be readSQLException- in case the record of the scripts run can not be read
-
create
Creates the table recording the scripts run, seetable(), in case it does not exist.- Parameters:
statement- the statement to use- Throws:
SQLException- in case of an exception
-
exists
- Parameters:
connection- the connection- Returns:
- true if the table recording the scripts run exists
- Throws:
SQLException- in case of an exception
-
open
Opens a script for reading, its path as listed.- Parameters:
scriptPath- the script path- Returns:
- a stream reading the script
- Throws:
IOException- in case the script can not be read
-
run
protected abstract void run(Statement statement, String scriptPath, String script) throws SQLException Runs a script, within the transaction recording it.- Parameters:
statement- the statement to usescriptPath- the script path, as listedscript- the script, asopen(String)reads it- Throws:
SQLException- in case the script fails
-
sanitize
Sanitizes script paths, a dialect possibly running a script by its path in a statement of its own, for a dialect to refuse them before opening the database,run(Connection, List, boolean)andvalidate(Connection, List)sanitizing them as well.- Parameters:
scriptPaths- the script paths- Returns:
- the paths, trimmed, their separators forward slashes
- Throws:
SecurityException- if a script path is empty or contains potential injection attempts
-