oatpp / oatpp-sqlite

SQLite adapter for oatpp ORM.
https://oatpp.io/
Apache License 2.0
23 stars 18 forks source link

[oatpp::sqlite::Executor::getConnection()]: Error. Can't connect #14

Open ajinkyabawaskar opened 2 years ago

ajinkyabawaskar commented 2 years ago

I am building a project with the given CMakeLists.txt with conan:


cmake_minimum_required(VERSION 3.1)
set(PROJECT_NAME my-project)
project(${PROJECT_NAME})

set(CMAKE_CXX_STANDARD 14)

############################### CONAN SETUP BEGIN ##########################################
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
    message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
    file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.9/conan.cmake"
            "${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt BASIC_SETUP)
############################### CONAN SETUP END ############################################

add_library(${PROJECT_NAME}-lib
        # controllers
        src/controller/StaticController.hpp
        src/controller/DeviceController.hpp

        # db
        src/db/DeviceDb.hpp

        # dto
        src/dto/PageDto.hpp
        src/dto/StatusDto.hpp
        src/dto/DeviceDto.hpp

        # services
        src/service/DeviceService.cpp
        src/service/DeviceService.hpp

        # base
        src/AppComponent.hpp
        src/DatabaseComponent.hpp
        src/ErrorHandler.cpp
        src/ErrorHandler.hpp)

## include directories
target_include_directories(${PROJECT_NAME}-lib PUBLIC src)

add_definitions(
        ## SQLite database file
        -DDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/db.sqlite"

        ## SQLite database test file
        -DTESTDATABASE_FILE="${CMAKE_CURRENT_SOURCE_DIR}/sql/test-db.sqlite"

        ## Path to database migration scripts
        -DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"

        ##  Server
        -DHOST="0.0.0.0"
        -DPORT=8080
)

if(CMAKE_SYSTEM_NAME MATCHES Linux)
    find_package(Threads REQUIRED)
    target_link_libraries(${PROJECT_NAME}-lib INTERFACE Threads::Threads ${CMAKE_DL_LIBS})
endif()

## add executables
add_executable(${PROJECT_NAME}-exe src/App.cpp)
target_link_libraries(${PROJECT_NAME}-exe ${PROJECT_NAME}-lib ${CONAN_LIBS})

add_executable(${PROJECT_NAME}-test
        test/tests.cpp
        test/app/TestClient.hpp
        test/app/TestDatabaseComponent.hpp
        test/app/TestComponent.hpp
        test/DeviceControllerTest.hpp
        test/DeviceControllerTest.cpp)

target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}-lib ${CONAN_LIBS})

enable_testing()
add_test(e2e-tests ${PROJECT_NAME}-test)

Using this yields me a working executable on my machine running ubuntu 20.04. But when I move this executable to a LXD container running ubuntu 20.04, I get the following error:

ubuntu@container:~$ ./my-project-exe 
terminate called after throwing an instance of 'std::runtime_error'
  what():  [oatpp::sqlite::Executor::getConnection()]: Error. Can't connect.
Aborted (core dumped)

ubuntu@container:~$ sudo ./my-project-exe 
terminate called after throwing an instance of 'std::runtime_error'
  what():  [oatpp::sqlite::Executor::getConnection()]: Error. Can't connect.
Aborted (core dumped)

Looks like the database file db.sqlite fails to create. How can I eliminate this error?

lizemin5235 commented 2 years ago

<migration.migrate(); // <-- run migrations. This guy will throw on error.> how to deal this error

lganzzzo commented 2 years ago

Hey @ajinkyabawaskar ,

Looks like the database file db.sqlite fails to create. How can I eliminate this error?

Please make sure you have permissions to create file.

lganzzzo commented 2 years ago

<migration.migrate(); // <-- run migrations. This guy will throw on error.> how to deal this error

@lizemin5235 , you can use try - catch