Skip to content

Commit 0b3d5ae

Browse files
committed
0.3.0
1 parent 6698318 commit 0b3d5ae

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ export declare function testPostgresUrl(url: string): Promise<PgResponse>
1717
export declare function createDatabase(url: string, database: string): Promise<PgResponse>
1818
export declare function testRedisParameters(host: string, username?: string | undefined | null, password?: string | undefined | null): Promise<string>
1919
export declare function fileExists(filePath: string): boolean
20+
export declare function renameDatabase(url: string, database: string, newDatabaseName: string): Promise<PgResponse>
21+
export declare function findNonexistentFiles(paths: Array<string>): Array<string>
22+
export declare function copyFile(source: string, destination: string, createDestIfNotExists?: boolean | undefined | null): void

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,13 @@ if (!nativeBinding) {
310310
throw new Error(`Failed to load native binding`)
311311
}
312312

313-
const { runNpmScript, testPostgresUrl, createDatabase, testRedisParameters, fileExists } = nativeBinding
313+
const { runNpmScript, testPostgresUrl, createDatabase, testRedisParameters, fileExists, renameDatabase, findNonexistentFiles, copyFile } = nativeBinding
314314

315315
module.exports.runNpmScript = runNpmScript
316316
module.exports.testPostgresUrl = testPostgresUrl
317317
module.exports.createDatabase = createDatabase
318318
module.exports.testRedisParameters = testRedisParameters
319319
module.exports.fileExists = fileExists
320+
module.exports.renameDatabase = renameDatabase
321+
module.exports.findNonexistentFiles = findNonexistentFiles
322+
module.exports.copyFile = copyFile

src/lib.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use napi_derive::napi;
22
use std::env;
3-
use std::fs;
3+
use std::fs::{self, File};
4+
use std::io;
45
use std::path::Path;
56
use std::process::Command;
67
use serde::{Serialize, Deserialize};
@@ -69,6 +70,22 @@ async fn handle_postgres_connection(
6970
}
7071
}
7172

73+
fn read_file(path: &str) -> napi::Result<File> {
74+
fs::File::open(path)
75+
.map_err(|error| napi::Error::new(
76+
napi::Status::GenericFailure,
77+
format!("Failed to open {}: {}", path, error)
78+
))
79+
}
80+
81+
fn create_file(path: &str) -> napi::Result<File> {
82+
fs::File::create(path)
83+
.map_err(|error| napi::Error::new(
84+
napi::Status::GenericFailure,
85+
format!("Failed to create {}: {}", path, error)
86+
))
87+
}
88+
7289
#[napi]
7390
pub async fn run_npm_script(script: String) -> napi::Result<ProcessOutput> {
7491
let home_dir = env::var("HOME").or_else(|_| env::var("USERPROFILE")).unwrap_or_default();
@@ -194,3 +211,54 @@ pub fn file_exists(file_path: String) -> bool {
194211
fs::metadata(filepath).map(|metadata| metadata.is_file()).unwrap_or(false)
195212
}
196213

214+
#[napi]
215+
pub async fn rename_database(url: String, database: String, new_database_name: String) -> napi::Result<PgResponse> {
216+
let (client, connection) = match create_postgres_connection(&url).await {
217+
Ok((client, connection)) => (client, connection),
218+
Err(error) => {
219+
return create_postgres_error_response(&error);
220+
}
221+
};
222+
223+
tokio::spawn(handle_postgres_connection(connection));
224+
225+
let query = format!("ALTER DATABASE \"{}\" RENAME TO \"{}\";", database, new_database_name);
226+
match client.batch_execute(&query).await {
227+
Ok(_) => Ok(PgResponse {
228+
code: "00000".to_string(),
229+
message: format!("Database {} renamed to {}", database, new_database_name)
230+
}),
231+
Err(error) => create_postgres_error_response(&error)
232+
}
233+
}
234+
235+
#[napi]
236+
pub fn find_nonexistent_files(paths: Vec<String>) -> Vec<String> {
237+
paths
238+
.into_iter()
239+
.filter(|path| !fs::metadata(path).is_ok())
240+
.collect()
241+
}
242+
243+
#[napi]
244+
pub fn copy_file(
245+
source: String,
246+
destination: String,
247+
create_dest_if_not_exists: Option<bool>
248+
) -> napi::Result<()> {
249+
let mut source_file = read_file(&source)?;
250+
251+
let mut destination_file = if create_dest_if_not_exists.unwrap_or(false) {
252+
create_file(&destination)?
253+
} else {
254+
read_file(&destination)?
255+
};
256+
257+
io::copy(&mut source_file, &mut destination_file)
258+
.map_err(|error| napi::Error::new(
259+
napi::Status::GenericFailure,
260+
format!("Failed to write to {}: {}", &destination, error)
261+
))?;
262+
263+
Ok(())
264+
}

0 commit comments

Comments
 (0)