pg-ephemeral 0.5.0

Ephemeral PostgreSQL instances for testing
Documentation
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
pub mod cache;
pub mod container;
pub mod host;
pub mod instance;
pub mod meta;
pub mod platform;
pub mod session;
pub mod transparent;

use crate::config::Config;
use crate::seed::SeedName;
use crate::{InstanceMap, InstanceName};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    Command(#[from] cmd_proc::CommandError),
    #[error(transparent)]
    Config(#[from] crate::config::Error),
    #[error(transparent)]
    Container(#[from] crate::container::Error),
    #[error(transparent)]
    AttachSession(#[from] crate::container::AttachSessionError),
    #[error(transparent)]
    EnvVariableValue(#[from] cmd_proc::EnvVariableValueError),
    #[error("Unknown instance: {0}")]
    UnknownInstance(InstanceName),
    #[error("Instance {instance} has no seeds; cache credentials requires a cacheable seed")]
    NoSeedsDefined { instance: InstanceName },
    #[error("Instance {instance} has no seed named {seed}")]
    UnknownSeed {
        instance: InstanceName,
        seed: SeedName,
    },
    #[error(
        "Seed {seed} on instance {instance} is uncacheable; cache credentials requires a cacheable seed"
    )]
    SeedUncacheable {
        instance: InstanceName,
        seed: SeedName,
    },
    #[error(
        "Seed {seed} on instance {instance} is not yet cached; run `pg-ephemeral cache populate` first"
    )]
    SeedNotCached {
        instance: InstanceName,
        seed: SeedName,
    },
    #[error("Failed to resolve container backend")]
    BackendResolve(#[source] ociman::backend::resolve::Error),
    #[error(transparent)]
    Session(crate::session::ListError),
    #[error(transparent)]
    SessionFind(crate::session::FindError),
    #[error(transparent)]
    SessionStop(crate::session::StopError),
    #[error(transparent)]
    SessionMetadata(#[from] crate::session::MetadataError),
    #[error("Unknown session: {name}")]
    UnknownSession { name: crate::session::Name },
    #[error("Failed to read current working directory")]
    CurrentDir(#[source] std::io::Error),
    #[error(transparent)]
    TransparentWorkdir(#[from] crate::definition::TransparentWorkdirError),
}

#[derive(Clone, Debug, Default)]
pub enum ConfigFileSource {
    #[default]
    Implicit,
    Explicit(std::path::PathBuf),
    None,
}

impl ConfigFileSource {
    fn from_arguments(config_file: Option<std::path::PathBuf>, no_config_file: bool) -> Self {
        match (config_file, no_config_file) {
            (Some(path), false) => Self::Explicit(path),
            (None, true) => Self::None,
            (None, false) => Self::Implicit,
            (Some(_), true) => unreachable!("clap conflicts_with prevents this"),
        }
    }
}

#[derive(Clone, Debug, clap::Parser)]
#[command(after_help = "EXECUTION CONTEXT:
    Bare commands (psql, run-env, schema-dump, shell) run in transparent mode:
    the current working directory is bind-mounted into the container at the
    same path, the command executes inside the container as the host user,
    and PG* / DATABASE_URL point at the in-container unix socket.

    Use `host <sub>` for an explicit host-side process (TCP to published port),
    or `container <sub>` for an explicit in-container exec without the cwd
    bind mount.

INSTANCE SELECTION:
    All commands target the \"main\" instance by default.
    Use --instance <NAME> to target a different instance.")]
#[command(version = crate::VERSION_STR)]
pub struct App {
    /// Config file to use, defaults to attempt to load database.toml
    ///
    /// If absent on default location a single "main" database is assumed on
    /// autodetected backend with latest postgres and no other configuration.
    #[arg(long, conflicts_with = "no_config_file")]
    config_file: Option<std::path::PathBuf>,
    /// Do not load any config file, use default instance map
    #[arg(long, conflicts_with = "config_file")]
    no_config_file: bool,
    /// Overwrite backend
    ///
    /// If not specified on the CLI and not in the config file will be autodetected:
    /// first based on env variable OCIMAN_BACKEND, then on installed tools.
    /// If the autodetection fails exits with an error.
    #[arg(long)]
    backend: Option<ociman::backend::Selection>,
    /// Overwrite image
    #[arg(long)]
    image: Option<crate::image::Image>,
    /// Enable SSL with the specified hostname
    #[arg(long)]
    ssl_hostname: Option<pg_client::config::HostName>,
    #[clap(subcommand)]
    command: Option<Command>,
}

impl App {
    pub async fn run(&self) -> Result<(), Error> {
        let overwrites = crate::config::InstanceDefinition {
            image: self.image.clone(),
            parameters: pg_client::parameter::Map::new(),
            seeds: indexmap::IndexMap::new(),
            ssl_config: self
                .ssl_hostname
                .clone()
                .map(|hostname| crate::config::SslConfigDefinition { hostname }),
            wait_available_timeout: None,
        };

        let config_file_source =
            ConfigFileSource::from_arguments(self.config_file.clone(), self.no_config_file);

        let resolved = match config_file_source {
            ConfigFileSource::Explicit(config_file) => {
                Config::load_toml_file(&config_file, self.backend, &overwrites)?
            }
            ConfigFileSource::None => {
                log::debug!("--no-config-file specified, using default instance map");
                crate::Config::default().resolve(self.backend, &overwrites)?
            }
            ConfigFileSource::Implicit => {
                log::debug!("No config file specified, trying to load from default location");

                match Config::load_toml_file("database.toml", self.backend, &overwrites) {
                    Ok(value) => value,
                    Err(crate::config::Error::IO(crate::config::IoError(
                        std::io::ErrorKind::NotFound,
                    ))) => {
                        log::debug!(
                            "Config file does not exist in default location, using default instance map"
                        );
                        crate::Config::default().resolve(self.backend, &overwrites)?
                    }
                    Err(error) => return Err(error.into()),
                }
            }
        };

        self.command
            .clone()
            .unwrap_or_default()
            .run(resolved.backend_selection, &resolved.instances)
            .await?;

        Ok(())
    }
}

async fn resolve_backend(selection: ociman::backend::Selection) -> Result<ociman::Backend, Error> {
    selection.resolve().await.map_err(Error::BackendResolve)
}

#[derive(Clone, Debug, clap::Parser)]
pub enum Command {
    /// Cache related commands
    Cache {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
        #[clap(subcommand)]
        command: cache::Command,
    },
    /// Operations executed inside the running container
    ///
    /// Each subcommand `podman exec`s the target inside the booted
    /// PostgreSQL container: the executable resolves against the image's
    /// $PATH, sees the container filesystem, and connects to PostgreSQL
    /// via the in-container unix socket (`/var/run/postgresql`). Use these
    /// when you need container-side semantics — version-matched `pg_dump`,
    /// scripts that depend on container-installed extensions, etc.
    Container {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
        #[clap(subcommand)]
        command: instance::Command,
    },
    /// Operations executed on the host against the running container
    ///
    /// Each subcommand runs as a host process with stdio inherited and
    /// PG* / DATABASE_URL pointing at the container's published TCP port.
    /// Use these when the tool must read or write host filesystem, or
    /// must stream binary data through pipes without PTY corruption.
    Host {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
        #[clap(subcommand)]
        command: instance::Command,
    },
    /// Run integration server
    ///
    /// Intent to be used for automation with other languages wrapping pg-ephemeral.
    ///
    /// After successful boot connects to the inherited pipe file descriptors,
    /// writes a single JSON line with connection details to --result-fd,
    /// then waits for EOF on --control-fd before shutting down.
    #[command(name = "integration-server")]
    IntegrationServer {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
        /// File descriptor for writing the result JSON
        #[arg(long)]
        result_fd: std::os::fd::RawFd,
        /// File descriptor for reading the control signal (EOF = shutdown)
        #[arg(long)]
        control_fd: std::os::fd::RawFd,
    },
    /// List defined instances
    List,
    /// Backend introspection (kind, version, rootless status)
    Meta {
        #[clap(subcommand)]
        command: meta::Command,
    },
    /// Platform related commands
    #[command(name = "platform")]
    Platform {
        #[clap(subcommand)]
        command: platform::Command,
    },
    /// Run interactive psql
    Psql {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
    },
    /// Run a command with PostgreSQL connection environment
    #[command(name = "run-env")]
    RunEnv {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
        /// The command to run
        command: String,
        /// Arguments to pass to the command
        arguments: Vec<String>,
    },
    /// Named-session management
    Session {
        #[clap(subcommand)]
        command: session::Command,
    },
    /// Dump schema to stdout
    #[command(name = "schema-dump")]
    SchemaDump {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
    },
    /// Run interactive shell
    Shell {
        /// Target instance name
        #[arg(long = "instance", default_value_t)]
        instance_name: InstanceName,
    },
}

impl Default for Command {
    fn default() -> Self {
        Self::Psql {
            instance_name: InstanceName::default(),
        }
    }
}

impl Command {
    pub async fn run(
        &self,
        backend_selection: ociman::backend::Selection,
        instance_map: &InstanceMap,
    ) -> Result<(), Error> {
        match self {
            Self::Cache {
                instance_name,
                command,
            } => {
                let backend = resolve_backend(backend_selection).await?;
                command.run(&backend, instance_map, instance_name).await?
            }
            Self::Container {
                instance_name,
                command,
            } => {
                let backend = resolve_backend(backend_selection).await?;
                let definition =
                    get_instance(instance_map, instance_name)?.definition(backend, instance_name);
                container::Command(command).run(&definition).await?
            }
            Self::Host {
                instance_name,
                command,
            } => {
                let backend = resolve_backend(backend_selection).await?;
                let definition =
                    get_instance(instance_map, instance_name)?.definition(backend, instance_name);
                host::Command(command).run(&definition).await?
            }
            Self::IntegrationServer {
                instance_name,
                result_fd,
                control_fd,
            } => {
                let backend = resolve_backend(backend_selection).await?;
                let definition =
                    get_instance(instance_map, instance_name)?.definition(backend, instance_name);
                definition
                    .run_integration_server(*result_fd, *control_fd)
                    .await?
            }
            Self::List => {
                for instance_name in instance_map.keys() {
                    println!("{instance_name}")
                }
            }
            Self::Meta { command } => {
                let backend = resolve_backend(backend_selection).await?;
                command.run(&backend).await?
            }
            Self::Platform { command } => command.run(),
            Self::Psql { instance_name } => {
                let backend = resolve_backend(backend_selection).await?;
                run_transparent(
                    backend,
                    instance_map,
                    instance_name,
                    &instance::Command::Psql,
                )
                .await?
            }
            Self::RunEnv {
                instance_name,
                command,
                arguments,
            } => {
                let backend = resolve_backend(backend_selection).await?;
                run_transparent(
                    backend,
                    instance_map,
                    instance_name,
                    &instance::Command::RunEnv {
                        command: command.clone(),
                        arguments: arguments.clone(),
                    },
                )
                .await?
            }
            Self::Session { command } => {
                let backend = resolve_backend(backend_selection).await?;
                command.run(&backend, instance_map).await?
            }
            Self::SchemaDump { instance_name } => {
                let backend = resolve_backend(backend_selection).await?;
                run_transparent(
                    backend,
                    instance_map,
                    instance_name,
                    &instance::Command::SchemaDump,
                )
                .await?
            }
            Self::Shell { instance_name } => {
                let backend = resolve_backend(backend_selection).await?;
                run_transparent(
                    backend,
                    instance_map,
                    instance_name,
                    &instance::Command::Shell,
                )
                .await?
            }
        }

        Ok(())
    }
}

async fn run_transparent(
    backend: ociman::Backend,
    instance_map: &InstanceMap,
    instance_name: &InstanceName,
    command: &instance::Command,
) -> Result<(), Error> {
    let cwd = std::env::current_dir().map_err(Error::CurrentDir)?;
    let workdir = crate::definition::TransparentWorkdir::try_from(cwd)?;
    let definition = get_instance(instance_map, instance_name)?
        .definition(backend, instance_name)
        .transparent_workdir(workdir.clone());
    transparent::Command {
        command,
        workdir: &workdir,
    }
    .run(&definition)
    .await
}

#[allow(
    clippy::result_large_err,
    reason = "cli::Error aggregates container/seed errors that intentionally carry diagnostic context; the 128-byte threshold targets async-server hot paths that don't apply here"
)]
pub(super) fn get_instance<'a>(
    instance_map: &'a InstanceMap,
    instance_name: &InstanceName,
) -> Result<&'a crate::config::Instance, Error> {
    instance_map
        .get(instance_name)
        .ok_or_else(|| Error::UnknownInstance(instance_name.clone()))
}