Skip to content

Commit

Permalink
Adding command for exporting database dump to JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
isontheline committed May 26, 2024
1 parent 610b8b0 commit dcc5fa7
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation 'dev.langchain4j:langchain4j-open-ai:0.30.0'
implementation platform('org.dizitart:nitrite-bom:4.2.1')
implementation 'org.dizitart:nitrite'
implementation 'org.dizitart:nitrite-support'
implementation 'org.dizitart:nitrite-mvstore-adapter'
implementation 'org.dizitart:nitrite-jackson-mapper'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
Expand Down
35 changes: 35 additions & 0 deletions backend/src/main/java/ai/dragon/command/DatabaseExportCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ai.dragon.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import ai.dragon.service.DatabaseService;

@Component
public class DatabaseExportCommand implements ApplicationRunner {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private DatabaseService databaseService;

@Override
public void run(ApplicationArguments args) throws Exception {
if (args.containsOption("command") && args.containsOption("database-export")) {
logger.info("Database export command received");

if(!args.containsOption("output")) {
logger.error("Database export path not provided : --output=/tmp/dump.json");
System.exit(1);
}

databaseService.exportDatabase(args.getOptionValues("output").get(0));
logger.info("Database export completed");

System.exit(0);
}
}
}
10 changes: 10 additions & 0 deletions backend/src/main/java/ai/dragon/service/DatabaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.dizitart.no2.NitriteBuilder;
import org.dizitart.no2.common.mapper.JacksonMapperModule;
import org.dizitart.no2.mvstore.MVStoreModule;
import org.dizitart.no2.support.exchange.ExportOptions;
import org.dizitart.no2.support.exchange.Exporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -62,6 +64,14 @@ public void openDatabase() {
db = dbBuilder.openOrCreate();
}

public void exportDatabase(String fileOutput) {
ExportOptions exportOptions = new ExportOptions();
exportOptions.setNitriteFactory(() -> getNitriteDB());

Exporter exporter = Exporter.withOptions(exportOptions);
exporter.exportTo(fileOutput);
}

private String getDatabaseFilename() {
return Optional.ofNullable(dataProperties.getDb()).orElse("dragon.db");
}
Expand Down
1 change: 1 addition & 0 deletions docs/docs/api/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label: 'API'
7 changes: 7 additions & 0 deletions docs/docs/api/using-api.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import WIPS from '../../src/components/Admonitions/_wip_section.mdx';

# Using API
<WIPS />

## Swagger UI
The API documentation is available at [http://localhost:1985/api/swagger-ui/index.html](http://localhost:1985/api/swagger-ui/index.html).
1 change: 1 addition & 0 deletions docs/docs/commands/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label: 'Commands'
23 changes: 23 additions & 0 deletions docs/docs/commands/database-export.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import WIPS from '../../src/components/Admonitions/_wip_section.mdx';

# Database Export
This command is used to export the database to a JSON file. It can be used to backup the database or to move the database to another instance.

## Offline Export
:::info

This command should only be used when the backend server is offline. Running this command while the backend server is running may not work as the database could be locked. To export the database while the backend server is running, use the API endpoint below.

:::

### Using Gradle
gradle bootRun --args="--command --database-export --output=/path/to/dump.json"

### Using Java
java -jar backend.jar --command --database-export --output=/path/to/dump.json

### Using Docker
docker run -v /path/to/output:/output dragon --command --database-export --output=/output/dump.json

## Online Export
<WIPS />
1 change: 1 addition & 0 deletions docs/docs/contributing/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
label: 'Contributing'
33 changes: 33 additions & 0 deletions docs/docs/contributing/launch-dragon-dev.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
sidebar_position: 1
---
# dRAGon Development Mode
:::warning[Visual Studio Code Dev Containers]

In order to be as close as possible to the development environment, we **strongly** recommend using Visual Studio Code Dev Containers. You can find the configuration file in the `.devcontainer` folder.

If it's not possible please make sure you have the following tools installed with the **correct** versions:
* Java 17
* Gradle 8.7
* Node.js 20
* NPM 10
* "Unix like" System (Linux, MacOS)

However we do **NOT** accept any issues related to troubles encountered by **NOT** using the Dev Containers.

:::

## Clone Repository
```shell
git clone https://github.com/dRAGon-Okinawa/dRAGon
```

## Prepare Dependencies
```shell
gradle npmInstall build
```

## Launching dRAGon
```shell
gradle bootRun
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
sidebar_position: 1
---
import WIPS from '../src/components/Admonitions/_wip_section.mdx';

# Getting Started

Let's discover **dRAGon in less than 5 minutes**.

## Launching dRAGon
<WIPS />

Soon
### Gradle
```shell
gradle bootRun
```
4 changes: 4 additions & 0 deletions docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ const config: Config = {
{
docs: {
sidebarPath: './sidebars.ts',
showLastUpdateTime: true,
},
theme: {
customCss: './src/css/custom.css',
},
pages: {
showLastUpdateTime: true,
},
} satisfies Preset.Options,
],
],
Expand Down
5 changes: 5 additions & 0 deletions docs/src/components/Admonitions/_wip_section.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Admonition from '@theme/Admonition';

<Admonition type="note" icon="🚧" title={props.title ? props.title : 'Work In Progress'}>
This section lacks useful information. It is a work in progress and will be updated soon.
</Admonition>
7 changes: 0 additions & 7 deletions docs/src/pages/markdown-page.md

This file was deleted.

0 comments on commit dcc5fa7

Please sign in to comment.