Search in sources :

Example 1 with DatabaseOperate

use of com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate in project nacos by alibaba.

the class ConfigOpsController method importDerby.

/**
 * // TODO the front page should appear operable The external data source is imported into derby.
 *
 * <p>mysqldump --defaults-file="XXX" --host=0.0.0.0 --protocol=tcp --user=XXX --extended-insert=FALSE \
 * --complete-insert=TRUE \ --skip-triggers --no-create-info --skip-column-statistics "{SCHEMA}" "{TABLE_NAME}"
 *
 * @param multipartFile {@link MultipartFile}
 * @return {@link DeferredResult}
 */
@PostMapping(value = "/data/removal")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin")
public DeferredResult<RestResult<String>> importDerby(@RequestParam(value = "file") MultipartFile multipartFile) {
    DeferredResult<RestResult<String>> response = new DeferredResult<>();
    if (!PropertyUtil.isEmbeddedStorage()) {
        response.setResult(RestResultUtils.failed("Limited to embedded storage mode"));
        return response;
    }
    DatabaseOperate databaseOperate = ApplicationUtils.getBean(DatabaseOperate.class);
    WebUtils.onFileUpload(multipartFile, file -> {
        NotifyCenter.publishEvent(new DerbyImportEvent(false));
        databaseOperate.dataImport(file).whenComplete((result, ex) -> {
            NotifyCenter.publishEvent(new DerbyImportEvent(true));
            if (Objects.nonNull(ex)) {
                response.setResult(RestResultUtils.failed(ex.getMessage()));
                return;
            }
            response.setResult(result);
        });
    }, response);
    return response;
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) DerbyImportEvent(com.alibaba.nacos.config.server.model.event.DerbyImportEvent) DatabaseOperate(com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate) DeferredResult(org.springframework.web.context.request.async.DeferredResult) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 2 with DatabaseOperate

use of com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate in project nacos by alibaba.

the class ConfigDerbyImport_CITCase method testDerbyImport.

@Test()
public void testDerbyImport() throws Throwable {
    DatabaseOperate operate = context.getBean(DatabaseOperate.class);
    File file = DiskUtils.createTmpFile("derby_import" + System.currentTimeMillis(), ".tmp");
    DiskUtils.writeFile(file, ByteUtils.toBytes(SQL_SCRIPT_CONTEXT), false);
    try {
        List<Integer> ids = operate.queryMany("SELECT id FROM config_info", new Object[] {}, Integer.class);
        for (Integer each : ids) {
            System.out.println("current id in table config_info contain: " + each);
        }
        CompletableFuture<RestResult<String>> future = operate.dataImport(file);
        RestResult<String> result = future.join();
        System.out.println(result);
        Assert.assertTrue(result.ok());
        final String queryDataId = "people";
        final String queryGroup = "DEFAULT_GROUP";
        final String expectContent = "people.enable=true";
        PersistService persistService = context.getBean(PersistService.class);
        ConfigInfo configInfo = persistService.findConfigInfo(queryDataId, queryGroup, "");
        System.out.println(configInfo);
        Assert.assertNotNull(configInfo);
        Assert.assertEquals(queryDataId, configInfo.getDataId());
        Assert.assertEquals(queryGroup, configInfo.getGroup());
        Assert.assertEquals("", configInfo.getTenant());
        Assert.assertEquals(expectContent, configInfo.getContent());
    } finally {
        DiskUtils.deleteQuietly(file);
    }
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) DatabaseOperate(com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate) File(java.io.File) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RestResult (com.alibaba.nacos.common.model.RestResult)2 DatabaseOperate (com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate)2 Secured (com.alibaba.nacos.auth.annotation.Secured)1 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)1 DerbyImportEvent (com.alibaba.nacos.config.server.model.event.DerbyImportEvent)1 PersistService (com.alibaba.nacos.config.server.service.repository.PersistService)1 File (java.io.File)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1