Skip to main content

perforce_cli/cmd/
dbverify.rs

1use std::path::{Path, PathBuf};
2use std::process::{Child, Command, Stdio};
3
4use super::SubCommand;
5
6use crate::global::GlobalOpts;
7use crate::spawn::ParameterizedSpawn;
8
9#[cfg_attr(
10    feature = "lt2015_1",
11    doc = "`p4 [g-opts] dbverify [ -t db.tablename ] [-U]`"
12)]
13#[cfg_attr(
14    all(feature = "lt2016_1", not(feature = "lt2015_1")),
15    doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U]`"
16)]
17#[cfg_attr(
18    not(feature = "lt2016_1"),
19    doc = "`p4 [g-opts] dbverify [-t db.tablename] [-U][-v]`"
20)]
21///
22/// Perform low-level verification of the database tables.
23///
24/// By default, all current tables are verified, which can be computationally
25/// expensive and might require scheduled user downtime on large systems;
26/// [`Self::table`] restricts verification to a single table.
27/// [`Self::less_detailed`] performs the faster `-U` validation, which only
28/// looks for tables with non-zero unlock counts and has minimal performance
29/// impact.
30#[cfg_attr(
31    not(feature = "lt2016_1"),
32    doc = "",
33    doc = "Use [`Self::verbose`] to request verbose information on the verification."
34)]
35#[derive(Debug, Clone, Default)]
36pub struct DBVerify {
37    bin: PathBuf,
38
39    global_opts: GlobalOpts,
40
41    table: Option<String>,
42
43    less_detailed: bool,
44
45    #[cfg(not(feature = "lt2016_1"))]
46    verbose: bool,
47}
48
49impl DBVerify {
50    /// Creates a new `p4 dbverify` command.
51    ///
52    /// `bin` is the path to the Perforce command-line executable.
53    pub fn new(bin: impl Into<PathBuf>, global_opts: GlobalOpts) -> Self {
54        Self {
55            bin: bin.into(),
56            global_opts,
57            table: None,
58            less_detailed: false,
59            #[cfg(not(feature = "lt2016_1"))]
60            verbose: false,
61        }
62    }
63
64    /// Get the Perforce command path.
65    pub fn get_bin(&self) -> &Path {
66        &self.bin
67    }
68
69    /// Set the Perforce command path.
70    pub fn set_bin(&mut self, bin: impl Into<PathBuf>) -> &mut Self {
71        self.bin = bin.into();
72        self
73    }
74
75    /// Set the Perforce command path.
76    pub fn bin(mut self, bin: impl Into<PathBuf>) -> Self {
77        self.set_bin(bin);
78        self
79    }
80
81    /// # Description
82    ///
83    /// g-opts
84    ///
85    #[cfg_attr(
86        feature = "lt2014_2",
87        doc = "See the [Global Options](GlobalOpts) section."
88    )]
89    #[cfg_attr(
90        all(feature = "lt2015_1", not(feature = "lt2014_2")),
91        doc = "See the [“Global Options”](GlobalOpts) section."
92    )]
93    #[cfg_attr(
94        all(feature = "lt2017_1", not(feature = "lt2015_1")),
95        doc = "See [“Global Options”](GlobalOpts)."
96    )]
97    #[cfg_attr(
98        all(feature = "lt2018_2", not(feature = "lt2017_1")),
99        doc = "See [Global Options](GlobalOpts)."
100    )]
101    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
102    pub fn get_global_opts(&self) -> &GlobalOpts {
103        &self.global_opts
104    }
105
106    /// # Description
107    ///
108    /// g-opts
109    ///
110    #[cfg_attr(
111        feature = "lt2014_2",
112        doc = "See the [Global Options](GlobalOpts) section."
113    )]
114    #[cfg_attr(
115        all(feature = "lt2015_1", not(feature = "lt2014_2")),
116        doc = "See the [“Global Options”](GlobalOpts) section."
117    )]
118    #[cfg_attr(
119        all(feature = "lt2017_1", not(feature = "lt2015_1")),
120        doc = "See [“Global Options”](GlobalOpts)."
121    )]
122    #[cfg_attr(
123        all(feature = "lt2018_2", not(feature = "lt2017_1")),
124        doc = "See [Global Options](GlobalOpts)."
125    )]
126    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
127    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
128        self.global_opts = v;
129        self
130    }
131
132    /// # Description
133    ///
134    /// g-opts
135    ///
136    #[cfg_attr(
137        feature = "lt2014_2",
138        doc = "See the [Global Options](GlobalOpts) section."
139    )]
140    #[cfg_attr(
141        all(feature = "lt2015_1", not(feature = "lt2014_2")),
142        doc = "See the [“Global Options”](GlobalOpts) section."
143    )]
144    #[cfg_attr(
145        all(feature = "lt2017_1", not(feature = "lt2015_1")),
146        doc = "See [“Global Options”](GlobalOpts)."
147    )]
148    #[cfg_attr(
149        all(feature = "lt2018_2", not(feature = "lt2017_1")),
150        doc = "See [Global Options](GlobalOpts)."
151    )]
152    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
153    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
154        self.global_opts = v;
155        self
156    }
157
158    /// # Description
159    ///
160    #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
161    #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
162    ///
163    /// Restrict verification to the specified table name.
164    pub fn get_table(&self) -> Option<&str> {
165        self.table.as_deref()
166    }
167
168    /// # Description
169    ///
170    #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
171    #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
172    ///
173    /// Restrict verification to the specified table name.
174    pub fn set_table(&mut self, v: impl Into<String>) -> &mut Self {
175        self.table = Some(v.into());
176        self
177    }
178
179    /// # Description
180    ///
181    #[cfg_attr(feature = "lt2015_1", doc = "-t tablename")]
182    #[cfg_attr(not(feature = "lt2015_1"), doc = "-t db.tablename")]
183    ///
184    /// Restrict verification to the specified table name.
185    pub fn table(mut self, v: impl Into<String>) -> Self {
186        self.set_table(v);
187        self
188    }
189
190    /// # Description
191    ///
192    /// -U
193    ///
194    /// Perform a less-detailed validation.
195    ///
196    /// Looks for tables with non-zero unlock counts; the presence of a
197    /// non-zero unlock count does not positively indicate corruption (and a
198    /// zero unlock count does not guarantee data integrity), but this check
199    /// has minimal performance impact.
200    pub fn get_less_detailed(&self) -> bool {
201        self.less_detailed
202    }
203
204    /// # Description
205    ///
206    /// -U
207    ///
208    /// Perform a less-detailed validation.
209    ///
210    /// Looks for tables with non-zero unlock counts; the presence of a
211    /// non-zero unlock count does not positively indicate corruption (and a
212    /// zero unlock count does not guarantee data integrity), but this check
213    /// has minimal performance impact.
214    pub fn set_less_detailed(&mut self, v: bool) -> &mut Self {
215        self.less_detailed = v;
216        self
217    }
218
219    /// # Description
220    ///
221    /// -U
222    ///
223    /// Perform a less-detailed validation.
224    ///
225    /// Looks for tables with non-zero unlock counts; the presence of a
226    /// non-zero unlock count does not positively indicate corruption (and a
227    /// zero unlock count does not guarantee data integrity), but this check
228    /// has minimal performance impact.
229    pub fn less_detailed(mut self, v: bool) -> Self {
230        self.set_less_detailed(v);
231        self
232    }
233
234    /// # Description
235    ///
236    /// -v
237    ///
238    /// Provide verbose information on the verification.
239    #[cfg(not(feature = "lt2016_1"))]
240    pub fn get_verbose(&self) -> bool {
241        self.verbose
242    }
243
244    /// # Description
245    ///
246    /// -v
247    ///
248    /// Provide verbose information on the verification.
249    #[cfg(not(feature = "lt2016_1"))]
250    pub fn set_verbose(&mut self, v: bool) -> &mut Self {
251        self.verbose = v;
252        self
253    }
254
255    /// # Description
256    ///
257    /// -v
258    ///
259    /// Provide verbose information on the verification.
260    #[cfg(not(feature = "lt2016_1"))]
261    pub fn verbose(mut self, v: bool) -> Self {
262        self.set_verbose(v);
263        self
264    }
265}
266
267impl SubCommand for DBVerify {
268    fn name(&self) -> &str {
269        "dbverify"
270    }
271
272    fn inject_local_args(&self, command: &mut Command) {
273        if let Some(table) = &self.table {
274            command.arg("-t").arg(table);
275        }
276
277        if self.less_detailed {
278            command.arg("-U");
279        }
280
281        #[cfg(not(feature = "lt2016_1"))]
282        if self.verbose {
283            command.arg("-v");
284        }
285    }
286
287    fn global_opts(&self) -> Option<&GlobalOpts> {
288        Some(&self.global_opts)
289    }
290}
291
292impl ParameterizedSpawn<()> for DBVerify {
293    type Output = Child;
294    type Error = std::io::Error;
295
296    /// Spawns `p4 dbverify` as a child process with piped standard output and
297    /// error streams; use the returned [`Child`] handle to wait for it or
298    /// interact with it.
299    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
300        self.setup_command(&self.bin)
301            .stdout(Stdio::piped())
302            .stderr(Stdio::piped())
303            .spawn()
304    }
305}
306
307#[cfg(test)]
308mod tests {
309    use super::*;
310    use crate::cmd::args_of;
311
312    #[test]
313    fn bin_accessors_update_executable_path() {
314        let mut command = DBVerify::new("p4", GlobalOpts::new());
315        assert_eq!(command.get_bin(), Path::new("p4"));
316
317        command.set_bin("/usr/local/bin/p4");
318        assert_eq!(command.get_bin(), Path::new("/usr/local/bin/p4"));
319
320        let command = command.bin("p4.exe");
321        assert_eq!(command.get_bin(), Path::new("p4.exe"));
322    }
323
324    /// Dry-run checks of the assembled `p4 dbverify` command line; no process
325    /// is spawned.
326    #[test]
327    fn without_options() {
328        let dbverify = DBVerify::new("p4", GlobalOpts::new());
329
330        assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify"]);
331    }
332
333    #[test]
334    fn with_table() {
335        let dbverify = DBVerify::new("p4", GlobalOpts::new()).table("db.have");
336
337        assert_eq!(dbverify.get_table(), Some("db.have"));
338        assert_eq!(
339            args_of(&dbverify.setup_command("p4")),
340            ["dbverify", "-t", "db.have"]
341        );
342    }
343
344    #[test]
345    fn less_detailed_flag() {
346        let dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
347
348        assert!(dbverify.get_less_detailed());
349        assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-U"]);
350    }
351
352    #[test]
353    fn table_and_less_detailed_in_syntax_order() {
354        let mut dbverify = DBVerify::new("p4", GlobalOpts::new()).less_detailed(true);
355        dbverify.set_table("db.have");
356
357        assert_eq!(
358            args_of(&dbverify.setup_command("p4")),
359            ["dbverify", "-t", "db.have", "-U"]
360        );
361    }
362
363    #[cfg(not(feature = "lt2016_1"))]
364    #[test]
365    fn verbose_flag() {
366        let dbverify = DBVerify::new("p4", GlobalOpts::new()).verbose(true);
367
368        assert!(dbverify.get_verbose());
369        assert_eq!(args_of(&dbverify.setup_command("p4")), ["dbverify", "-v"]);
370    }
371
372    #[cfg(not(feature = "lt2016_1"))]
373    #[test]
374    fn all_options_in_syntax_order() {
375        let dbverify = DBVerify::new("p4", GlobalOpts::new())
376            .table("db.have")
377            .less_detailed(true)
378            .verbose(true);
379
380        assert_eq!(
381            args_of(&dbverify.setup_command("p4")),
382            ["dbverify", "-t", "db.have", "-U", "-v"]
383        );
384    }
385
386    #[test]
387    fn global_opts_are_injected_once() {
388        let dbverify = DBVerify::new(
389            "p4",
390            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
391        )
392        .table("db.have");
393
394        assert_eq!(
395            args_of(&dbverify.setup_command("p4")),
396            ["-p", "localhost:1666", "-q", "dbverify", "-t", "db.have"]
397        );
398    }
399}