GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(admin): build SQL Server restore paths without the host separator · jwcraig/sql-server-cli@531e772

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/commands
    • admin.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/commands/admin.rs‎

Copy file name to clipboardExpand all lines: src/commands/admin.rs
+42-5Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::{Path, PathBuf};
1+
use std::path::Path;
22
use std::time::Instant;
33

44
use anyhow::{Context, Result, anyhow};
@@ -800,7 +800,7 @@ fn build_restore_database_sql(
800800
moves.push(format!(
801801
"MOVE {} TO {}",
802802
sql_fragments::quote_string(&file.logical_name),
803-
sql_fragments::quote_string(&sql_fragments::path_for_sql(&path))
803+
sql_fragments::quote_string(&path)
804804
));
805805
}
806806

@@ -822,13 +822,32 @@ WITH {options};",
822822
))
823823
}
824824

825+
/// Join a SQL Server directory and file name without importing the host's path
826+
/// separator.
827+
///
828+
/// The result is interpreted by SQL Server, which routinely runs on a different
829+
/// OS than sscli. `PathBuf::join` uses the separator of the machine sscli runs
830+
/// on, so building the POSIX path `/var/opt/mssql/data` on Windows would yield
831+
/// `/var/opt/mssql/data\WDM.mdf` and produce an unusable RESTORE statement.
832+
fn join_sql_server_path(data_dir: &Path, filename: &str) -> String {
833+
let dir = sql_fragments::path_for_sql(data_dir);
834+
let trimmed = dir.trim_end_matches(['/', '\\']);
835+
// Only a path that uses backslashes and no forward slashes is a Windows path.
836+
let separator = if trimmed.contains('\\') && !trimmed.contains('/') {
837+
'\\'
838+
} else {
839+
'/'
840+
};
841+
format!("{}{}{}", trimmed, separator, filename)
842+
}
843+
825844
fn restore_file_path(
826845
database: &str,
827846
data_dir: &Path,
828847
file: &RestoreFile,
829848
data_seen: &mut usize,
830849
log_seen: &mut usize,
831-
) -> PathBuf {
850+
) -> String {
832851
let safe_database = sanitize_file_component(database);
833852
let is_log = file.file_type.eq_ignore_ascii_case("L");
834853
let filename = if is_log {
@@ -848,7 +867,7 @@ fn restore_file_path(
848867
*data_seen += 1;
849868
format!("{}{}.mdf", safe_database, suffix)
850869
};
851-
data_dir.join(filename)
870+
join_sql_server_path(data_dir, &filename)
852871
}
853872

854873
fn sanitize_file_component(value: &str) -> String {
@@ -888,7 +907,7 @@ mod tests {
888907

889908
use super::{
890909
AdminExecution, BatchOutcome, RestoreFile, VerifyOutcome, build_restore_database_sql,
891-
execution_to_json,
910+
execution_to_json, join_sql_server_path,
892911
};
893912

894913
fn batch(index: usize, error: Option<&str>) -> BatchOutcome {
@@ -1014,6 +1033,24 @@ mod tests {
10141033
assert_eq!(payload["verify"]["passed"], false);
10151034
}
10161035

1036+
/// SQL Server interprets these paths, so they must not pick up the host's
1037+
/// separator. This failed on Windows runners before `join_sql_server_path`.
1038+
#[test]
1039+
fn sql_server_paths_keep_their_own_separator_regardless_of_host() {
1040+
assert_eq!(
1041+
join_sql_server_path(Path::new("/var/opt/mssql/data"), "WDM.mdf"),
1042+
"/var/opt/mssql/data/WDM.mdf"
1043+
);
1044+
assert_eq!(
1045+
join_sql_server_path(Path::new("/var/opt/mssql/data/"), "WDM.mdf"),
1046+
"/var/opt/mssql/data/WDM.mdf"
1047+
);
1048+
assert_eq!(
1049+
join_sql_server_path(Path::new(r"C:\SQLData"), "WDM.mdf"),
1050+
r"C:\SQLData\WDM.mdf"
1051+
);
1052+
}
1053+
10171054
#[test]
10181055
fn restore_database_sql_moves_logical_files_into_data_dir() {
10191056
let files = vec![

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.