postgresql_embedded 0.1.0

Embed PostgreSQL in Rust applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use crate::command::traits::CommandBuilder;
use std::convert::AsRef;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;

/// pg_restore restores a PostgreSQL database from an archive created by pg_dump.
#[derive(Clone, Debug, Default)]
pub struct PgRestoreBuilder {
    program_dir: Option<PathBuf>,
    dbname: Option<OsString>,
    file: Option<OsString>,
    format: Option<OsString>,
    list: bool,
    verbose: bool,
    version: bool,
    help: bool,
    data_only: bool,
    clean: bool,
    create: bool,
    exit_on_error: bool,
    index: Option<OsString>,
    jobs: Option<OsString>,
    use_list: Option<OsString>,
    schema: Option<OsString>,
    exclude_schema: Option<OsString>,
    no_owner: bool,
    function: Option<OsString>,
    schema_only: bool,
    superuser: Option<OsString>,
    table: Option<OsString>,
    trigger: Option<OsString>,
    no_privileges: bool,
    single_transaction: bool,
    disable_triggers: bool,
    enable_row_security: bool,
    if_exists: bool,
    no_comments: bool,
    no_data_for_failed_tables: bool,
    no_publications: bool,
    no_security_labels: bool,
    no_subscriptions: bool,
    no_table_access_method: bool,
    no_tablespaces: bool,
    section: Option<OsString>,
    strict_names: bool,
    use_set_session_authorization: bool,
    host: Option<OsString>,
    port: Option<u16>,
    username: Option<OsString>,
    no_password: bool,
    password: bool,
    role: Option<OsString>,
}

impl PgRestoreBuilder {
    /// Create a new [`PgRestoreBuilder`]
    pub fn new() -> Self {
        Self::default()
    }

    /// Location of the program binary
    pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.program_dir = Some(path.into());
        self
    }

    /// connect to database name
    pub fn dbname<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.dbname = Some(name.as_ref().to_os_string());
        self
    }

    /// output file name (- for stdout)
    pub fn file<S: AsRef<OsStr>>(mut self, filename: S) -> Self {
        self.file = Some(filename.as_ref().to_os_string());
        self
    }

    /// backup file format (should be automatic)
    pub fn format<S: AsRef<OsStr>>(mut self, format: S) -> Self {
        self.format = Some(format.as_ref().to_os_string());
        self
    }

    /// print summarized TOC of the archive
    pub fn list(mut self) -> Self {
        self.list = true;
        self
    }

    /// verbose mode
    pub fn verbose(mut self) -> Self {
        self.verbose = true;
        self
    }

    /// output version information, then exit
    pub fn version(mut self) -> Self {
        self.version = true;
        self
    }

    /// show help, then exit
    pub fn help(mut self) -> Self {
        self.help = true;
        self
    }

    /// restore only the data, no schema
    pub fn data_only(mut self) -> Self {
        self.data_only = true;
        self
    }

    /// clean (drop) database objects before recreating
    pub fn clean(mut self) -> Self {
        self.clean = true;
        self
    }

    /// create the target database
    pub fn create(mut self) -> Self {
        self.create = true;
        self
    }

    /// exit on error, default is to continue
    pub fn exit_on_error(mut self) -> Self {
        self.exit_on_error = true;
        self
    }

    /// restore named index
    pub fn index<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.index = Some(name.as_ref().to_os_string());
        self
    }

    /// use this many parallel jobs to restore
    pub fn jobs<S: AsRef<OsStr>>(mut self, num: S) -> Self {
        self.jobs = Some(num.as_ref().to_os_string());
        self
    }

    /// use table of contents from this file for selecting/ordering output
    pub fn use_list<S: AsRef<OsStr>>(mut self, filename: S) -> Self {
        self.use_list = Some(filename.as_ref().to_os_string());
        self
    }

    /// restore only objects in this schema
    pub fn schema<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.schema = Some(name.as_ref().to_os_string());
        self
    }

    /// do not restore objects in this schema
    pub fn exclude_schema<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.exclude_schema = Some(name.as_ref().to_os_string());
        self
    }

    /// skip restoration of object ownership
    pub fn no_owner(mut self) -> Self {
        self.no_owner = true;
        self
    }

    /// restore named function
    pub fn function<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.function = Some(name.as_ref().to_os_string());
        self
    }

    /// restore only the schema, no data
    pub fn schema_only(mut self) -> Self {
        self.schema_only = true;
        self
    }

    /// superuser user name to use for disabling triggers
    pub fn superuser<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.superuser = Some(name.as_ref().to_os_string());
        self
    }

    /// restore named relation (table, view, etc.)
    pub fn table<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.table = Some(name.as_ref().to_os_string());
        self
    }

    /// restore named trigger
    pub fn trigger<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.trigger = Some(name.as_ref().to_os_string());
        self
    }

    /// skip restoration of access privileges (grant/revoke)
    pub fn no_privileges(mut self) -> Self {
        self.no_privileges = true;
        self
    }

    /// restore as a single transaction
    pub fn single_transaction(mut self) -> Self {
        self.single_transaction = true;
        self
    }

    /// disable triggers during data-only restore
    pub fn disable_triggers(mut self) -> Self {
        self.disable_triggers = true;
        self
    }

    /// enable row security
    pub fn enable_row_security(mut self) -> Self {
        self.enable_row_security = true;
        self
    }

    /// use IF EXISTS when dropping objects
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }

    /// do not restore comments
    pub fn no_comments(mut self) -> Self {
        self.no_comments = true;
        self
    }

    /// do not restore data of tables that could not be created
    pub fn no_data_for_failed_tables(mut self) -> Self {
        self.no_data_for_failed_tables = true;
        self
    }

    /// do not restore publications
    pub fn no_publications(mut self) -> Self {
        self.no_publications = true;
        self
    }

    /// do not restore security labels
    pub fn no_security_labels(mut self) -> Self {
        self.no_security_labels = true;
        self
    }

    /// do not restore subscriptions
    pub fn no_subscriptions(mut self) -> Self {
        self.no_subscriptions = true;
        self
    }

    /// do not restore table access methods
    pub fn no_table_access_method(mut self) -> Self {
        self.no_table_access_method = true;
        self
    }

    /// do not restore tablespace assignments
    pub fn no_tablespaces(mut self) -> Self {
        self.no_tablespaces = true;
        self
    }

    /// restore named section (pre-data, data, or post-data)
    pub fn section<S: AsRef<OsStr>>(mut self, section: S) -> Self {
        self.section = Some(section.as_ref().to_os_string());
        self
    }

    /// require table and/or schema include patterns to match at least one entity each
    pub fn strict_names(mut self) -> Self {
        self.strict_names = true;
        self
    }

    /// use SET SESSION AUTHORIZATION commands instead of ALTER OWNER commands to set ownership
    pub fn use_set_session_authorization(mut self) -> Self {
        self.use_set_session_authorization = true;
        self
    }

    /// database server host or socket directory
    pub fn host<S: AsRef<OsStr>>(mut self, hostname: S) -> Self {
        self.host = Some(hostname.as_ref().to_os_string());
        self
    }

    /// database server port number
    pub fn port(mut self, port: u16) -> Self {
        self.port = Some(port);
        self
    }

    /// connect as specified database user
    pub fn username<S: AsRef<OsStr>>(mut self, name: S) -> Self {
        self.username = Some(name.as_ref().to_os_string());
        self
    }

    /// never prompt for password
    pub fn no_password(mut self) -> Self {
        self.no_password = true;
        self
    }

    /// force password prompt (should happen automatically)
    pub fn password(mut self) -> Self {
        self.password = true;
        self
    }

    /// do SET ROLE before restore
    pub fn role<S: AsRef<OsStr>>(mut self, rolename: S) -> Self {
        self.role = Some(rolename.as_ref().to_os_string());
        self
    }
}

impl CommandBuilder for PgRestoreBuilder {
    /// Get the program name
    fn get_program(&self) -> &'static OsStr {
        "pg_restore".as_ref()
    }

    /// Location of the program binary
    fn get_program_dir(&self) -> &Option<PathBuf> {
        &self.program_dir
    }

    /// Get the arguments for the command
    fn get_args(&self) -> Vec<OsString> {
        let mut args: Vec<OsString> = Vec::new();

        if let Some(name) = &self.dbname {
            args.push("--dbname".into());
            args.push(name.into());
        }

        if let Some(filename) = &self.file {
            args.push("--file".into());
            args.push(filename.into());
        }

        if let Some(format) = &self.format {
            args.push("--format".into());
            args.push(format.into());
        }

        if self.list {
            args.push("--list".into());
        }

        if self.verbose {
            args.push("--verbose".into());
        }

        if self.version {
            args.push("--version".into());
        }

        if self.help {
            args.push("--help".into());
        }

        if self.data_only {
            args.push("--data-only".into());
        }

        if self.clean {
            args.push("--clean".into());
        }

        if self.create {
            args.push("--create".into());
        }

        if self.exit_on_error {
            args.push("--exit-on-error".into());
        }

        if let Some(name) = &self.index {
            args.push("--index".into());
            args.push(name.into());
        }

        if let Some(num) = &self.jobs {
            args.push("--jobs".into());
            args.push(num.into());
        }

        if let Some(filename) = &self.use_list {
            args.push("--use-list".into());
            args.push(filename.into());
        }

        if let Some(name) = &self.schema {
            args.push("--schema".into());
            args.push(name.into());
        }

        if let Some(name) = &self.exclude_schema {
            args.push("--exclude-schema".into());
            args.push(name.into());
        }

        if self.no_owner {
            args.push("--no-owner".into());
        }

        if let Some(name) = &self.function {
            args.push("--function".into());
            args.push(name.into());
        }

        if self.schema_only {
            args.push("--schema-only".into());
        }

        if let Some(name) = &self.superuser {
            args.push("--superuser".into());
            args.push(name.into());
        }

        if let Some(name) = &self.table {
            args.push("--table".into());
            args.push(name.into());
        }

        if let Some(name) = &self.trigger {
            args.push("--trigger".into());
            args.push(name.into());
        }

        if self.no_privileges {
            args.push("--no-privileges".into());
        }

        if self.single_transaction {
            args.push("--single-transaction".into());
        }

        if self.disable_triggers {
            args.push("--disable-triggers".into());
        }

        if self.enable_row_security {
            args.push("--enable-row-security".into());
        }

        if self.if_exists {
            args.push("--if-exists".into());
        }

        if self.no_comments {
            args.push("--no-comments".into());
        }

        if self.no_data_for_failed_tables {
            args.push("--no-data-for-failed-tables".into());
        }

        if self.no_publications {
            args.push("--no-publications".into());
        }

        if self.no_security_labels {
            args.push("--no-security-labels".into());
        }

        if self.no_subscriptions {
            args.push("--no-subscriptions".into());
        }

        if self.no_table_access_method {
            args.push("--no-table-access-method".into());
        }

        if self.no_tablespaces {
            args.push("--no-tablespaces".into());
        }

        if let Some(section) = &self.section {
            args.push("--section".into());
            args.push(section.into());
        }

        if self.strict_names {
            args.push("--strict-names".into());
        }

        if self.use_set_session_authorization {
            args.push("--use-set-session-authorization".into());
        }

        if let Some(hostname) = &self.host {
            args.push("--host".into());
            args.push(hostname.into());
        }

        if let Some(port) = &self.port {
            args.push("--port".into());
            args.push(port.to_string().into());
        }

        if let Some(name) = &self.username {
            args.push("--username".into());
            args.push(name.into());
        }

        if self.no_password {
            args.push("--no-password".into());
        }

        if self.password {
            args.push("--password".into());
        }

        if let Some(role) = &self.role {
            args.push("--role".into());
            args.push(role.into());
        }

        args
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::command::traits::CommandToString;
    use test_log::test;

    #[test]
    fn test_builder_new() {
        let command = PgRestoreBuilder::new().program_dir(".").build();

        assert_eq!(
            PathBuf::from(".").join("pg_restore"),
            PathBuf::from(command.to_command_string().replace("\"", ""))
        );
    }

    #[test]
    fn test_builder() {
        let command = PgRestoreBuilder::new()
            .dbname("dbname")
            .file("file")
            .format("format")
            .list()
            .verbose()
            .version()
            .help()
            .data_only()
            .clean()
            .create()
            .exit_on_error()
            .index("index")
            .jobs("jobs")
            .use_list("use_list")
            .schema("schema")
            .exclude_schema("exclude_schema")
            .no_owner()
            .function("function")
            .schema_only()
            .superuser("superuser")
            .table("table")
            .trigger("trigger")
            .no_privileges()
            .single_transaction()
            .disable_triggers()
            .enable_row_security()
            .if_exists()
            .no_comments()
            .no_data_for_failed_tables()
            .no_publications()
            .no_security_labels()
            .no_subscriptions()
            .no_table_access_method()
            .no_tablespaces()
            .section("section")
            .strict_names()
            .use_set_session_authorization()
            .host("localhost")
            .port(5432)
            .username("username")
            .no_password()
            .password()
            .role("role")
            .build();

        assert_eq!(
            r#""pg_restore" "--dbname" "dbname" "--file" "file" "--format" "format" "--list" "--verbose" "--version" "--help" "--data-only" "--clean" "--create" "--exit-on-error" "--index" "index" "--jobs" "jobs" "--use-list" "use_list" "--schema" "schema" "--exclude-schema" "exclude_schema" "--no-owner" "--function" "function" "--schema-only" "--superuser" "superuser" "--table" "table" "--trigger" "trigger" "--no-privileges" "--single-transaction" "--disable-triggers" "--enable-row-security" "--if-exists" "--no-comments" "--no-data-for-failed-tables" "--no-publications" "--no-security-labels" "--no-subscriptions" "--no-table-access-method" "--no-tablespaces" "--section" "section" "--strict-names" "--use-set-session-authorization" "--host" "localhost" "--port" "5432" "--username" "username" "--no-password" "--password" "--role" "role""#,
            command.to_command_string()
        );
    }
}