mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
Add liquibase to perform database migrations
This commit is contained in:
parent
35c5b00223
commit
71403e93af
9 changed files with 100 additions and 3 deletions
|
@ -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'
|
||||
|
|
|
@ -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) {
|
||||
|
|
51
src/main/java/me/kavin/piped/utils/LiquibaseHelper.java
Normal file
51
src/main/java/me/kavin/piped/utils/LiquibaseHelper.java
Normal 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();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
9
src/main/resources/changelog/db.changelog-master.xml
Normal file
9
src/main/resources/changelog/db.changelog-master.xml
Normal 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>
|
3
src/main/resources/changelog/version/0-1-init-crdb.sql
Normal file
3
src/main/resources/changelog/version/0-1-init-crdb.sql
Normal 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;
|
3
src/main/resources/changelog/version/0-1-init-pg.sql
Normal file
3
src/main/resources/changelog/version/0-1-init-pg.sql
Normal 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;
|
11
src/main/resources/changelog/version/0-1-init.sql
Normal file
11
src/main/resources/changelog/version/0-1-init.sql
Normal 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;
|
13
src/main/resources/changelog/version/0-init.xml
Normal file
13
src/main/resources/changelog/version/0-init.xml
Normal 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>
|
|
@ -4,7 +4,7 @@
|
|||
"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>
|
||||
|
|
Loading…
Reference in a new issue