From 208d91dea0b66284098e12542ef9a7bfb6faefb5 Mon Sep 17 00:00:00 2001 From: Aries-ckt <916701291@qq.com> Date: Tue, 30 Jan 2024 12:09:26 +0800 Subject: [PATCH] fix: MySQL Database not support DDL init and upgrade. (#1133) Co-authored-by: csunny --- dbgpt/app/base.py | 30 ++++++++++--- dbgpt/storage/vector_store/milvus_store.py | 4 +- docs/docs/installation/sourcecode.md | 43 +++++++++++++++++++ .../simple_dbschema_retriever_example.py | 0 .../simple_rag_embedding_example.py | 0 .../simple_rag_retriever_example.py | 0 6 files changed, 68 insertions(+), 9 deletions(-) rename examples/{awel => rag}/simple_dbschema_retriever_example.py (100%) rename examples/{awel => rag}/simple_rag_embedding_example.py (100%) rename examples/{awel => rag}/simple_rag_retriever_example.py (100%) diff --git a/dbgpt/app/base.py b/dbgpt/app/base.py index 622440666..830fa75ea 100644 --- a/dbgpt/app/base.py +++ b/dbgpt/app/base.py @@ -105,12 +105,28 @@ def _migration_db_storage(param: "WebServerParameters"): from dbgpt.storage.metadata.db_manager import db from dbgpt.util._db_migration_utils import _ddl_init_and_upgrade - # try to create all tables - try: - db.create_all() - except Exception as e: - logger.warning(f"Create all tables stored in this metadata error: {str(e)}") - _ddl_init_and_upgrade(default_meta_data_path, param.disable_alembic_upgrade) + # Try to create all tables, when the dbtype is sqlite, it will auto create and upgrade system schema, + # Otherwise, you need to execute initialization scripts to create schemas. + CFG = Config() + if CFG.LOCAL_DB_TYPE == "sqlite": + try: + db.create_all() + except Exception as e: + logger.warning( + f"Create all tables stored in this metadata error: {str(e)}" + ) + + _ddl_init_and_upgrade( + default_meta_data_path, param.disable_alembic_upgrade + ) + else: + warn_msg = """For safety considerations, MySQL Database not support DDL init and upgrade. " + "1.If you are use DB-GPT firstly, please manually execute the following command to initialize, + `mysql -h127.0.0.1 -uroot -p{your_password} < ./assets/schema/dbgpt.sql` " + "2.If there are any changes to the table columns in the DB-GPT database, + it is necessary to compare with the DB-GPT/assets/schema/dbgpt.sql file + and manually make the columns changes in the MySQL database instance.""" + logger.warning(warn_msg) def _initialize_db( @@ -118,7 +134,7 @@ def _initialize_db( ) -> str: """Initialize the database - Now just support sqlite and mysql. If db type is sqlite, the db path is `pilot/meta_data/{db_name}.db`. + Now just support sqlite and MySQL. If db type is sqlite, the db path is `pilot/meta_data/{db_name}.db`. """ from urllib.parse import quote from urllib.parse import quote_plus as urlquote diff --git a/dbgpt/storage/vector_store/milvus_store.py b/dbgpt/storage/vector_store/milvus_store.py index 2daedf08a..a29e137d7 100644 --- a/dbgpt/storage/vector_store/milvus_store.py +++ b/dbgpt/storage/vector_store/milvus_store.py @@ -79,7 +79,7 @@ def __init__(self, vector_store_config: MilvusVectorConfig) -> None: self.port = milvus_vector_config.get("post") or os.getenv( "MILVUS_PORT", "19530" ) - self.username = milvus_vector_config.get("user") or os.getenv("MILVUS_USER") + self.username = milvus_vector_config.get("user") or os.getenv("MILVUS_USERNAME") self.password = milvus_vector_config.get("password") or os.getenv( "MILVUS_PASSWORD" ) @@ -133,7 +133,7 @@ def __init__(self, vector_store_config: MilvusVectorConfig) -> None: connections.connect( host=self.uri or "127.0.0.1", port=self.port or "19530", - username=self.username, + user=self.username, password=self.password, alias="default", ) diff --git a/docs/docs/installation/sourcecode.md b/docs/docs/installation/sourcecode.md index bab7399b4..785c87417 100644 --- a/docs/docs/installation/sourcecode.md +++ b/docs/docs/installation/sourcecode.md @@ -354,7 +354,50 @@ Modify the `.env` file to use llama.cpp, and then you can start the service by r | `llama_cpp_cache_capacity` | None | Maximum model cache size. For example: 2000MiB, 2GiB | | `llama_cpp_prefer_cpu` | False | If a GPU is available, the GPU will be used first by default unless prefer_cpu=False is configured. | +## Install DB-GPT Application Database + + + +:::tip NOTE + +You do not need to separately create the database tables related to the DB-GPT application in SQLite; +they will be created automatically for you by default. + +::: + + + + + +:::warning NOTE + +After version 0.4.7, we removed the automatic generation of MySQL database Schema for safety. +::: + +1. Frist, execute MySQL script to create database and tables. + +```python +$ mysql -h127.0.0.1 -uroot -p{your_password} < ./assets/schema/dbgpt.sql +``` + +2. Second, set DB-GPT MySQL database settings in `.env` file. + +```python +LOCAL_DB_TYPE=mysql +LOCAL_DB_USER= {your username} +LOCAL_DB_PASSWORD={your_password} +LOCAL_DB_HOST=127.0.0.1 +LOCAL_DB_PORT=3306 +``` + + + ## Test data (optional) diff --git a/examples/awel/simple_dbschema_retriever_example.py b/examples/rag/simple_dbschema_retriever_example.py similarity index 100% rename from examples/awel/simple_dbschema_retriever_example.py rename to examples/rag/simple_dbschema_retriever_example.py diff --git a/examples/awel/simple_rag_embedding_example.py b/examples/rag/simple_rag_embedding_example.py similarity index 100% rename from examples/awel/simple_rag_embedding_example.py rename to examples/rag/simple_rag_embedding_example.py diff --git a/examples/awel/simple_rag_retriever_example.py b/examples/rag/simple_rag_retriever_example.py similarity index 100% rename from examples/awel/simple_rag_retriever_example.py rename to examples/rag/simple_rag_retriever_example.py