feat: implement create_repository for sqlite

This commit is contained in:
Aravinth Manivannan 2022-05-04 12:10:37 +05:30
parent cb8500da3f
commit 21c93a5e4e
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 125 additions and 7 deletions

1
Cargo.lock generated
View file

@ -390,6 +390,7 @@ dependencies = [
"async-trait",
"db-core",
"sqlx",
"url",
]
[[package]]

View file

@ -20,3 +20,4 @@ async-trait = "0.1.51"
actix-rt = "2"
sqlx = { version = "0.5.11", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] }
db-core = {path = "../db-core", features = ["test"]}
url = { version = "2.2.2", features = ["serde"] }

View file

@ -40,7 +40,8 @@ CREATE TABLE IF NOT EXISTS starchart_repositories (
hostname_id INTEGER NOT NULL REFERENCES starchart_forges(ID) ON DELETE CASCADE,
owner_id INTEGER NOT NULL REFERENCES starchart_users(ID) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL,
description TEXT DEFAULT NULL,
website TEXT DEFAULT NULL,
html_url TEXT NOT NULL UNIQUE,
created INTEGER NOT NULL,
last_crawl INTEGER NOT NULL

View file

@ -54,6 +54,16 @@
},
"query": "SELECT ID FROM starchart_forge_type WHERE name = $1"
},
"6f5ca3d71a541eb6f33e37a5889c048536ab6ad7e81a6236d73aa71433c13717": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 1
}
},
"query": "INSERT OR IGNORE INTO starchart_project_topics ( name ) VALUES ( $1 );"
},
"71079442588dfaece04582acdb14d2c8928c695d4eab5332d09b82cefc880d54": {
"describe": {
"columns": [
@ -110,6 +120,26 @@
},
"query": "INSERT INTO \n starchart_users (\n hostname_id, username, html_url,\n profile_photo_html_url, added_on, last_crawl_on\n ) \n VALUES (\n (SELECT ID FROM starchart_forges WHERE hostname = $1), $2, $3, $4, $5, $6)"
},
"e00c8a8b0dbeb4a89a673864055c137365c2ae7bc5daf677bdacb20f21d0fcb2": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 8
}
},
"query": "INSERT INTO \n starchart_repositories (\n hostname_id, owner_id, name, description, html_url, website, created, last_crawl\n )\n VALUES (\n (SELECT ID FROM starchart_forges WHERE hostname = $1),\n (SELECT ID FROM starchart_users WHERE username = $2),\n $3, $4, $5, $6, $7, $8\n );"
},
"e30ccfaa6aeda8cf30a2b3e9134abd0c0420441c5ed05189c3be605b1405c8e9": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Right": 2
}
},
"query": "\n INSERT INTO starchart_repository_topic_mapping ( topic_id, repository_id )\n VALUES (\n (SELECT ID FROM starchart_project_topics WHERE name = $1),\n (SELECT ID FROM starchart_repositories WHERE html_url = $2)\n );"
},
"f52cde89ec10d5ca2151c9df6ae273ee0d52af9f79bb776765cfa716aad6af53": {
"describe": {
"columns": [],

View file

@ -241,6 +241,62 @@ impl SCDatabase for Database {
Err(e) => Err(DBError::DBError(Box::new(e).into())),
}
}
/// add new repository to database.
async fn create_repository(&self, r: &AddRepository) -> DBResult<()> {
// unimplemented!()
let now = now_unix_time_stamp();
sqlx::query!(
"INSERT INTO
starchart_repositories (
hostname_id, owner_id, name, description, html_url, website, created, last_crawl
)
VALUES (
(SELECT ID FROM starchart_forges WHERE hostname = $1),
(SELECT ID FROM starchart_users WHERE username = $2),
$3, $4, $5, $6, $7, $8
);",
r.hostname,
r.owner,
r.name,
r.description,
r.html_link,
r.website,
now,
now
)
.execute(&self.pool)
.await
.map_err(map_register_err)?;
if let Some(topics) = &r.tags {
for topic in topics.iter() {
sqlx::query!(
"INSERT OR IGNORE INTO starchart_project_topics ( name ) VALUES ( $1 );",
topic,
)
.execute(&self.pool)
.await
.map_err(map_register_err)?;
sqlx::query!(
"
INSERT INTO starchart_repository_topic_mapping ( topic_id, repository_id )
VALUES (
(SELECT ID FROM starchart_project_topics WHERE name = $1),
(SELECT ID FROM starchart_repositories WHERE html_url = $2)
);",
topic,
r.html_link,
)
.execute(&self.pool)
.await
.map_err(map_register_err)?;
}
}
Ok(())
}
}
fn now_unix_time_stamp() -> i64 {

View file

@ -14,8 +14,11 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use sqlx::sqlite::SqlitePoolOptions;
use std::env;
use std::rc::Rc;
use sqlx::sqlite::SqlitePoolOptions;
use url::Url;
use crate::*;
@ -23,35 +26,61 @@ use db_core::tests::*;
#[actix_rt::test]
async fn everything_works() {
const HOSTNAME: &str = "test-gitea.example.com";
const HOSTNAME: &str = "https://test-gitea.example.com";
const HTML_PROFILE_URL: &str = "https://test-gitea.example.com/user1";
const HTML_PROFILE_PHOTO_URL_2: &str = "https://test-gitea.example.com/profile-photo/user2";
const USERNAME: &str = "user1";
const USERNAME2: &str = "user2";
const REPO_NAME: &str = "starchart";
const HTML_REPO_URL: &str = "https://test-gitea.example.com/user1/starchart";
const TAGS: [&str; 3] = ["test", "starchart", "spider"];
let hostname = Url::parse(HOSTNAME).unwrap();
let hostname = get_hostname(&hostname);
let create_forge_msg = CreateForge {
hostname: HOSTNAME,
hostname: &hostname,
forge_type: ForgeImplementation::Gitea,
};
let add_user_msg = AddUser {
hostname: HOSTNAME,
hostname: &hostname,
html_link: HTML_PROFILE_URL,
profile_photo: None,
username: USERNAME,
};
let add_user_msg_2 = AddUser {
hostname: HOSTNAME,
hostname: &hostname,
html_link: HTML_PROFILE_PHOTO_URL_2,
profile_photo: Some(HTML_PROFILE_PHOTO_URL_2),
username: USERNAME2,
};
let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var");
let pool_options = SqlitePoolOptions::new().max_connections(2);
let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url });
let db = connection_options.connect().await.unwrap();
adding_forge_works(&db, create_forge_msg, add_user_msg, add_user_msg_2).await;
let add_repo_msg = AddRepository {
html_link: HTML_REPO_URL,
name: REPO_NAME,
tags: Some(TAGS.into()),
owner: USERNAME,
website: None,
description: None,
hostname: &hostname,
};
adding_forge_works(
&db,
create_forge_msg,
add_user_msg,
add_user_msg_2,
add_repo_msg,
)
.await;
}
#[actix_rt::test]