mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 02:11:53 +00:00
retry migration if incomplete (#2522)
This commit is contained in:
@@ -96,44 +96,64 @@ pub async fn init_postgres(datadir: impl AsRef<Path>) -> Result<(), Error> {
|
|||||||
|
|
||||||
let pg_version_string = pg_version.to_string();
|
let pg_version_string = pg_version.to_string();
|
||||||
let pg_version_path = db_dir.join(&pg_version_string);
|
let pg_version_path = db_dir.join(&pg_version_string);
|
||||||
if tokio::fs::metadata(&pg_version_path).await.is_err() {
|
if exists
|
||||||
let conf_dir = Path::new("/etc/postgresql").join(pg_version.to_string());
|
// maybe migrate
|
||||||
let conf_dir_tmp = {
|
{
|
||||||
let mut tmp = conf_dir.clone();
|
let incomplete_path = db_dir.join(format!("{pg_version}.migration.incomplete"));
|
||||||
tmp.set_extension("tmp");
|
if tokio::fs::metadata(&incomplete_path).await.is_ok() // previous migration was incomplete
|
||||||
tmp
|
&& tokio::fs::metadata(&pg_version_path).await.is_ok()
|
||||||
};
|
|
||||||
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
|
||||||
Command::new("mv")
|
|
||||||
.arg(&conf_dir)
|
|
||||||
.arg(&conf_dir_tmp)
|
|
||||||
.invoke(ErrorKind::Filesystem)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
let mut old_version = pg_version;
|
|
||||||
while old_version > 13
|
|
||||||
/* oldest pg version included in startos */
|
|
||||||
{
|
{
|
||||||
old_version -= 1;
|
tokio::fs::remove_dir_all(&pg_version_path).await?;
|
||||||
let old_datadir = db_dir.join(old_version.to_string());
|
|
||||||
if tokio::fs::metadata(&old_datadir).await.is_ok() {
|
|
||||||
Command::new("pg_upgradecluster")
|
|
||||||
.arg(old_version.to_string())
|
|
||||||
.arg("main")
|
|
||||||
.invoke(crate::ErrorKind::Database)
|
|
||||||
.await?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
if tokio::fs::metadata(&pg_version_path).await.is_err()
|
||||||
|
// need to migrate
|
||||||
|
{
|
||||||
|
let conf_dir = Path::new("/etc/postgresql").join(pg_version.to_string());
|
||||||
|
let conf_dir_tmp = {
|
||||||
|
let mut tmp = conf_dir.clone();
|
||||||
|
tmp.set_extension("tmp");
|
||||||
|
tmp
|
||||||
|
};
|
||||||
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
||||||
tokio::fs::remove_dir_all(&conf_dir).await?;
|
Command::new("mv")
|
||||||
|
.arg(&conf_dir)
|
||||||
|
.arg(&conf_dir_tmp)
|
||||||
|
.invoke(ErrorKind::Filesystem)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
Command::new("mv")
|
let mut old_version = pg_version;
|
||||||
.arg(&conf_dir_tmp)
|
while old_version > 13
|
||||||
.arg(&conf_dir)
|
/* oldest pg version included in startos */
|
||||||
.invoke(ErrorKind::Filesystem)
|
{
|
||||||
.await?;
|
old_version -= 1;
|
||||||
|
let old_datadir = db_dir.join(old_version.to_string());
|
||||||
|
if tokio::fs::metadata(&old_datadir).await.is_ok() {
|
||||||
|
tokio::fs::File::create(&incomplete_path)
|
||||||
|
.await?
|
||||||
|
.sync_all()
|
||||||
|
.await?;
|
||||||
|
Command::new("pg_upgradecluster")
|
||||||
|
.arg(old_version.to_string())
|
||||||
|
.arg("main")
|
||||||
|
.invoke(crate::ErrorKind::Database)
|
||||||
|
.await?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
||||||
|
if tokio::fs::metadata(&conf_dir).await.is_ok() {
|
||||||
|
tokio::fs::remove_dir_all(&conf_dir).await?;
|
||||||
|
}
|
||||||
|
Command::new("mv")
|
||||||
|
.arg(&conf_dir_tmp)
|
||||||
|
.arg(&conf_dir)
|
||||||
|
.invoke(ErrorKind::Filesystem)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
tokio::fs::remove_file(&incomplete_path).await?;
|
||||||
|
}
|
||||||
|
if tokio::fs::metadata(&incomplete_path).await.is_ok() {
|
||||||
|
unreachable!() // paranoia
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user