Repository: nervosnetwork/ckb (language: rust)

Issue:
Ensure `ckb run` Automatically Creates `default.db-options` if Missing in `data_dir`
## Feature Request

#### Is your feature request related to a problem? Please describe.
When running `ckb run`, it would be ideal if the absence of the `default.db-options` file in the `data_dir` doesn't prevent the launch. It might be best for `ckb run` to check if the `default.db-options` file exists in `data_dir` during the initialization process, and if not, create it automatically.

#### Describe the solution you'd like
create `default.db-options` file automatically if it doesn't exists.


Discussion / hints:

Fix patch:
diff --git a/ckb-bin/src/subcommand/run.rs b/ckb-bin/src/subcommand/run.rs
index 41e9413947..f65f8d1f49 100644
--- a/ckb-bin/src/subcommand/run.rs
+++ b/ckb-bin/src/subcommand/run.rs
@@ -6,12 +6,15 @@ use ckb_async_runtime::{new_global_runtime, Handle};
 use ckb_build_info::Version;
 use ckb_launcher::Launcher;
 use ckb_logger::info;
+use ckb_logger::warn;
+use ckb_resource::{Resource, TemplateContext};
 
 use ckb_stop_handler::{broadcast_exit_signals, wait_all_ckb_services_exit};
 
 use ckb_types::core::cell::setup_system_cell_cache;
 
 pub fn run(args: RunArgs, version: Version, async_handle: Handle) -> Result<(), ExitCode> {
+    check_default_db_options_exists(&args)?;
     deadlock_detection();
 
     let rpc_threads_num = calc_rpc_threads_num(&args);
@@ -79,3 +82,24 @@ fn calc_rpc_threads_num(args: &RunArgs) -> usize {
     let default_num = usize::max(system_parallelism, 1);
     args.config.rpc.threads.unwrap_or(default_num)
 }
+
+fn check_default_db_options_exists(args: &RunArgs) -> Result<(), ExitCode> {
+    // check is there a default.db-options file exist in args.config.root_dir, if not, create one.
+    let db_options_path = args.config.root_dir.join("default.db-options");
+
+    // Check if the default.db-options file exists, if not, create one.
+    if !db_options_path.exists() {
+        warn!(
+            "default.db-options file does not exist in {}, creating one.",
+            args.config.root_dir.display()
+        );
+        // context_for_db_options is used to generate a default default.db-options file.
+        let context_for_db_options = TemplateContext::new("", vec![]);
+
+        // Attempt to export the bundled DB options to the specified path.
+        Resource::bundled_db_options()
+            .export(&context_for_db_options, &args.config.root_dir)
+            .map_err(|_| ExitCode::Config)?;
+    }
+    Ok(())
+}


Test patch: