Skip to content

Commit cc8f22e

Browse files
committed
Inlcude schema dump in database dump.
1 parent d8b45d9 commit cc8f22e

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

src/tasks/dump_db.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn dump_db(
1616
target_name: String,
1717
) -> Result<(), PerformError> {
1818
let directory = DumpDirectory::create()?;
19-
directory.dump_db(&database_url)?;
19+
directory.populate(&database_url)?;
2020
let tarball = DumpTarball::create(&directory.export_dir)?;
2121
tarball.upload(&target_name, &env.uploader)?;
2222
println!("Database dump uploaded to {}.", &target_name);
@@ -45,14 +45,11 @@ impl DumpDirectory {
4545
})
4646
}
4747

48-
pub fn dump_db(&self, database_url: &str) -> Result<(), PerformError> {
48+
pub fn populate(&self, database_url: &str) -> Result<(), PerformError> {
4949
self.add_readme()?;
5050
self.add_metadata()?;
51-
let export_script = self.export_dir.join("export.sql");
52-
let import_script = self.export_dir.join("import.sql");
53-
gen_scripts::gen_scripts(&export_script, &import_script)?;
54-
std::fs::create_dir(self.export_dir.join("data"))?;
55-
run_psql(&export_script, database_url)
51+
self.dump_schema(database_url)?;
52+
self.dump_db(database_url)
5653
}
5754

5855
fn add_readme(&self) -> Result<(), PerformError> {
@@ -80,6 +77,30 @@ impl DumpDirectory {
8077
serde_json::to_writer_pretty(file, &metadata)?;
8178
Ok(())
8279
}
80+
81+
pub fn dump_schema(&self, database_url: &str) -> Result<(), PerformError> {
82+
let schema_sql = File::create(self.export_dir.join("schema.sql"))?;
83+
let status = std::process::Command::new("pg_dump")
84+
.arg("--schema-only")
85+
.arg("--no-owner")
86+
.arg("--no-acl")
87+
.arg(database_url)
88+
.stdout(schema_sql)
89+
.spawn()?
90+
.wait()?;
91+
if !status.success() {
92+
Err("pg_dump did not finish successfully.")?;
93+
}
94+
Ok(())
95+
}
96+
97+
pub fn dump_db(&self, database_url: &str) -> Result<(), PerformError> {
98+
let export_script = self.export_dir.join("export.sql");
99+
let import_script = self.export_dir.join("import.sql");
100+
gen_scripts::gen_scripts(&export_script, &import_script)?;
101+
std::fs::create_dir(self.export_dir.join("data"))?;
102+
run_psql(&export_script, database_url)
103+
}
83104
}
84105

85106
impl Drop for DumpDirectory {

src/tasks/dump_db/readme_for_tarball.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ This is a dump of the public information in the crates.io database.
1717

1818
## Restoring to a Local crates.io Database
1919

20-
WARNING: This will destroy the current database contents.
21-
22-
1. Create a new database and run the Diesel migrations.
20+
1. Create a new database.
2321

2422
createdb DATABASE_NAME
25-
diesel migration run --database-url DATABASE_URL
2623

27-
2. Run this script.
24+
2. Restore the database schema.
25+
26+
psql DATABASE_NAME < schema.sql
27+
28+
3. Run the import script.
2829

29-
cd DUMP_DIRECTORY
3030
psql DATABASE_URL < import.sql

src/tests/dump_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn dump_db_and_reimport_dump() {
1111
// TODO prefill database with some data
1212

1313
let directory = dump_db::DumpDirectory::create().unwrap();
14-
directory.dump_db(&database_url).unwrap();
14+
directory.populate(&database_url).unwrap();
1515

1616
let schema = TemporarySchema::create(database_url, "test_db_dump");
1717
schema.run_migrations();

0 commit comments

Comments
 (0)