未使用架构创建表

问题描述 投票:0回答:1

我正在开发一个项目,我必须创建 2 个连接,一个到 h2,一个到 s3 来上传文件。 s3 已启动并正在运行,模式正在创建得很好,尽管当尝试对 h2 执行相同操作时,表并未被创建,因此当 cron 检查“PENDING”中是否有任何文件时,我收到错误状态将上传到数据库。

对于 s3,我使用的是 postgres,并且该语言中的模式工作得很好。问题出在 h2 上。

我正在运行应用程序 -> InteliJ 右侧 3op 上的 3 个点 -> 编辑 -> 活动配置文件 -> 输入:local

 Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabela "FILE_IMPORT" does not exist.
Table "FILE_IMPORT" not found; SQL statement:
SELECT id FROM file_import WHERE status = ? ORDER BY created_at ASC LIMIT 1 [42102-214] 

应用程序本地.yaml

spring:
  datasource:
    url: jdbc:h2:mem:test
    username: sa
    password:
    driverClassName: org.h2.Driver
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update  # or your preferred value
    open-in-view: false
  h2:
    console:
      enabled: true  # This line enables the H2 console for development
  servlet:
    multipart:
      max-file-size: 5000MB
      max-request-size: 5000MB
  upload:
    dir: "src/main/resources/files-to-import"
  integration:
    jdbc:
      initialize-schema: always
server:
  port: 8080
file-storage:
  solution: local

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG

架构-h2.sql

CREATE TABLE IF NOT EXISTS public.file_import (
                                                  id BIGINT AUTO_INCREMENT PRIMARY KEY,
                                                  created_at TIMESTAMP(6),
    file_path VARCHAR(255),
    finished_at TIMESTAMP(6),
    last_processed_row BIGINT,
    started_at TIMESTAMP(6),
    status VARCHAR(255),
    CONSTRAINT file_import_status_check CHECK (status IN ('PENDING', 'SUCCESS', 'FAILED'))
    );

本地文件存储


package com.example.personmanagement.file;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;


@Component
@ConditionalOnProperty(prefix = "file-storage", name = "solution", havingValue = "local")
public class LocalFileStorage implements FileStorage {

    @Value("${spring.upload.dir}")
    private String UPLOAD_DIR;

    @Override
    public String save(InputStream inputsStream, String originalFilename, long byteSize) throws IOException {
        String uniqueFilename = System.currentTimeMillis() + "_" + originalFilename;
        Path filePath = Path.of(UPLOAD_DIR, uniqueFilename);
        Files.copy(inputsStream, filePath, StandardCopyOption.REPLACE_EXISTING);
        return uniqueFilename;
    }

    @Override
    public BufferedReader load(String fileName) throws FileNotFoundException {
        Path filePath = Path.of(UPLOAD_DIR, fileName);
        return new BufferedReader(new InputStreamReader(new FileInputStream(String.valueOf(filePath))));
    }

}

使用 cron 运行代码的方法并且发现没有表


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@Slf4j
public class FileImportRepository {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public FileImportRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insert(FileImport fileImport) throws DataAccessException {
        String sql = "INSERT INTO file_import (file_path, last_processed_row, status, created_at) VALUES (?, ?, ?, ?)";
        jdbcTemplate.update(sql, fileImport.getFilePath(), fileImport.getLastProcessedRow(),
                fileImport.getStatus().toString(), fileImport.getCreatedAt());
    }

    public void update(FileImport fileImport) throws DataAccessException {
        String sql = """
                UPDATE file_import
                SET
                    file_path = ?,
                    last_processed_row = ?,
                    status = ?,
                    finished_at = ?,
                    started_at = ?
                WHERE id = ?
                """;
        jdbcTemplate.update(sql, fileImport.getFilePath(), fileImport.getLastProcessedRow(),
                fileImport.getStatus().toString(), fileImport.getFinishedAt(), fileImport.getStartedAt(), fileImport.getId());
    }

    public Optional<Long> findFirstByStatusOrderByCreatedAtAsc() throws EmptyResultDataAccessException {
        String sql = "SELECT id FROM file_import WHERE status = ? ORDER BY created_at ASC LIMIT 1";
        Long fileId = jdbcTemplate.queryForObject(sql, new Object[]{FileStatus.PENDING.toString()}, Long.class);
        return Optional.ofNullable(fileId);
    }

    public Optional<FileImport> findById(Long id) throws DataAccessException {
        String sql = "SELECT * FROM file_import WHERE id = ?";
        FileImport fileImport = jdbcTemplate.queryForObject(sql, new Object[]{id}, new FileImportRowMapper());
        return Optional.ofNullable(fileImport);
    }

    public void deleteAll() {
        String sql = "TRUNCATE TABLE file_import";
        jdbcTemplate.update(sql);
    }
}

您能帮我创建表格吗?我无法连接到 h2 控制台,因为登录部分来自 Spring boot 而不是 h2。

更改架构 更改个人资料

sql spring schema boot
1个回答
0
投票

你可以尝试这两种方法吗:

  1. 将 schema-h2.sql 重命名为 schema.sql(立即检查)
  2. 使用 schema.sql 文件名并从 DDL 中删除“public”。
    From 
        CREATE TABLE IF NOT EXISTS public.file_import
    To 
        CREATE TABLE IF NOT EXISTS file_import
© www.soinside.com 2019 - 2024. All rights reserved.