|
1 | | -use std::path::{Path, PathBuf}; |
| 1 | +use std::path::Path; |
2 | 2 | use std::time::Instant; |
3 | 3 |
|
4 | 4 | use anyhow::{Context, Result, anyhow}; |
@@ -800,7 +800,7 @@ fn build_restore_database_sql( |
800 | 800 | moves.push(format!( |
801 | 801 | "MOVE {} TO {}", |
802 | 802 | sql_fragments::quote_string(&file.logical_name), |
803 | | - sql_fragments::quote_string(&sql_fragments::path_for_sql(&path)) |
| 803 | + sql_fragments::quote_string(&path) |
804 | 804 | )); |
805 | 805 | } |
806 | 806 |
|
@@ -822,13 +822,32 @@ WITH {options};", |
822 | 822 | )) |
823 | 823 | } |
824 | 824 |
|
| 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 | + |
825 | 844 | fn restore_file_path( |
826 | 845 | database: &str, |
827 | 846 | data_dir: &Path, |
828 | 847 | file: &RestoreFile, |
829 | 848 | data_seen: &mut usize, |
830 | 849 | log_seen: &mut usize, |
831 | | -) -> PathBuf { |
| 850 | +) -> String { |
832 | 851 | let safe_database = sanitize_file_component(database); |
833 | 852 | let is_log = file.file_type.eq_ignore_ascii_case("L"); |
834 | 853 | let filename = if is_log { |
@@ -848,7 +867,7 @@ fn restore_file_path( |
848 | 867 | *data_seen += 1; |
849 | 868 | format!("{}{}.mdf", safe_database, suffix) |
850 | 869 | }; |
851 | | - data_dir.join(filename) |
| 870 | + join_sql_server_path(data_dir, &filename) |
852 | 871 | } |
853 | 872 |
|
854 | 873 | fn sanitize_file_component(value: &str) -> String { |
@@ -888,7 +907,7 @@ mod tests { |
888 | 907 |
|
889 | 908 | use super::{ |
890 | 909 | AdminExecution, BatchOutcome, RestoreFile, VerifyOutcome, build_restore_database_sql, |
891 | | - execution_to_json, |
| 910 | + execution_to_json, join_sql_server_path, |
892 | 911 | }; |
893 | 912 |
|
894 | 913 | fn batch(index: usize, error: Option<&str>) -> BatchOutcome { |
@@ -1014,6 +1033,24 @@ mod tests { |
1014 | 1033 | assert_eq!(payload["verify"]["passed"], false); |
1015 | 1034 | } |
1016 | 1035 |
|
| 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 | + |
1017 | 1054 | #[test] |
1018 | 1055 | fn restore_database_sql_moves_logical_files_into_data_dir() { |
1019 | 1056 | let files = vec![ |
|