Add liquibase to perform database migrations

This commit is contained in:
Kavin 2023-08-13 23:20:30 +01:00
parent 35c5b00223
commit 71403e93af
No known key found for this signature in database
GPG Key ID: 6E4598CA5C92C41F
9 changed files with 100 additions and 3 deletions

View File

@ -33,6 +33,7 @@ dependencies {
implementation 'org.postgresql:postgresql:42.6.0'
implementation 'org.hibernate:hibernate-core:6.2.7.Final'
implementation 'org.hibernate:hibernate-hikaricp:6.2.7.Final'
implementation 'org.liquibase:liquibase-core:4.23.1'
implementation 'com.zaxxer:HikariCP:5.0.1'
implementation 'org.springframework.security:spring-security-crypto:6.1.2'
implementation 'commons-logging:commons-logging:1.2'

View File

@ -20,7 +20,6 @@ import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.services.youtube.YoutubeThrottlingDecrypter;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
import rocks.kavin.reqwest4j.ReqwestUtils;
import java.util.*;
import java.util.concurrent.CompletableFuture;
@ -70,6 +69,13 @@ public class Main {
}
}).start();
try {
LiquibaseHelper.init();
} catch (Exception e) {
ExceptionHandler.handle(e);
System.exit(1);
}
try (Session ignored = DatabaseSessionFactory.createSession()) {
System.out.println("Database connection is ready!");
} catch (Throwable t) {

View File

@ -0,0 +1,51 @@
package me.kavin.piped.utils;
import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.command.CommandScope;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.ChangeExecListenerCommandStep;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.command.core.helpers.DbUrlConnectionCommandStep;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import me.kavin.piped.consts.Constants;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class LiquibaseHelper {
public static void init() throws Exception {
String url = Constants.hibernateProperties.get("hibernate.connection.url");
String username = Constants.hibernateProperties.get("hibernate.connection.username");
String password = Constants.hibernateProperties.get("hibernate.connection.password");
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(DriverManager.getConnection(url, username, password)));
try (Liquibase liquibase = new Liquibase("changelog/db.changelog-master.xml", new ClassLoaderResourceAccessor(), database)) {
Map<String, Object> scopeObjects = new HashMap<>();
scopeObjects.put(Scope.Attr.database.name(), liquibase.getDatabase());
scopeObjects.put(Scope.Attr.resourceAccessor.name(), liquibase.getResourceAccessor());
Scope.child(scopeObjects, () -> {
CommandScope updateCommand = new CommandScope(UpdateCommandStep.COMMAND_NAME);
updateCommand.addArgumentValue(DbUrlConnectionCommandStep.DATABASE_ARG, liquibase.getDatabase());
updateCommand.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, liquibase.getChangeLogFile());
updateCommand.execute();
});
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<include file="version/0-init.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

View File

@ -0,0 +1,3 @@
CREATE INDEX IF NOT EXISTS users_session_id_idx ON users (session_id ASC) STORING (password, username);
--rollback DROP INDEX IF EXISTS users_session_id_idx;

View File

@ -0,0 +1,3 @@
CREATE INDEX IF NOT EXISTS users_session_id_idx ON users (session_id ASC);
--rollback DROP INDEX IF EXISTS users_session_id_idx;

View File

@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS users (
id SERIAL NOT NULL,
password STRING NULL,
session_id STRING(36) NULL,
username STRING(24) NULL UNIQUE,
CONSTRAINT users_pkey PRIMARY KEY (id ASC),
INDEX users_id_idx (id ASC),
INDEX username_idx (username ASC)
);
--rollback DROP TABLE IF EXISTS users;

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet id="0-1" author="kavin" runInTransaction="false">
<sqlFile path="0-1-init.sql" relativeToChangelogFile="true"/>
<sqlFile path="0-1-init-crdb.sql" dbms="cockroachdb" relativeToChangelogFile="true"/>
<sqlFile path="0-1-init-pg.sql" dbms="postgresql" relativeToChangelogFile="true"/>
</changeSet>
</databaseChangeLog>

View File

@ -4,11 +4,11 @@
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<!-- Optional: Show SQL output for debugging -->
<property name="hibernate.show_sql">false</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</property>
<property name="hibernate.connection.handling_mode">DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT</property>
<property name="hibernate.jdbc.batch_size">50</property>