Skip to content

Commit

Permalink
refactor build.rs for creeating TESTUSER and deleting it after build
Browse files Browse the repository at this point in the history
  • Loading branch information
eltorio committed Sep 25, 2024
1 parent b2167f3 commit f40cb80
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ jobs:
EOF
- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}
path: target/${{ matrix.target }}/release/
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ jobs:
export TAG_NAME=${{ github.event.release.tag_name }}
export TAG_NAME=${TAG_NAME:-"nightly"}
echo $TAG_NAME
mv /tmp/windows_${{ matrix.job.arch }}_${{ github.event.release.tag_name }}.zip /tmp/windows_${{ matrix.job.arch }}_${TAG_NAME}.zip
ls -l /tmp
mv /tmp/windows_*.zip /tmp/windows_${{ matrix.job.arch }}_${TAG_NAME}.zip
gh release create $TAG_NAME -t "$TAG_NAME" -n "$TAG_NAME" || true
gh release upload $TAG_NAME /tmp/windows_${{ matrix.job.arch }}_${TAG_NAME}.zip --clobber
19 changes: 7 additions & 12 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,7 @@ impl PackageJson {
#[tokio::main]
async fn main() {
let db_path = env::var("DATABASE_URL").unwrap_or("sqlite://db_v2.sqlite3".to_string());
let mut conn = SqliteConnection::connect(&format!("{}", db_path))
.await
.expect("Failed to open database");
conn.execute(
r#"
INSERT OR IGNORE INTO peer (guid, id, uuid, pk, created_at, "user", status, note, region, strategy, info, last_online) VALUES
(x'95CC7775BA37481DAD7214A4F6CE5A94', 'TESTUSER', randomblob(16), randomblob(16), '1901-01-01 12:00:00', randomblob(16), 0, '', NULL, randomblob(16), '{}', '1901-01-01 12:00:00');
"#
)
.await
.expect("Failed to insert test data");
println!("cargo:rerun-if-changed=build.rs");

println!("cargo:rerun-if-changed=webconsole");

let data = fs::read_to_string("./webconsole/package.json").unwrap();
Expand Down Expand Up @@ -118,9 +107,15 @@ async fn main() {
str::from_utf8(&output.stdout).unwrap_or(""),
str::from_utf8(&output.stderr).unwrap_or("")
);

// Delete test data if it exists
let mut conn = SqliteConnection::connect(&format!("{}", db_path))
.await
.expect("Failed to open database");
conn.execute(
r#"
DELETE FROM peer WHERE guid = x'95CC7775BA37481DAD7214A4F6CE5A94';
PRAGMA journal_mode=DELETE;
"#
)
.await
Expand Down
Binary file modified db_v2.sqlite3
Binary file not shown.
4 changes: 4 additions & 0 deletions libs/state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ tracing-subscriber = "0.3"
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio-rustls", "macros", "chrono", "json"] }
bcrypt = "0.15"
uuid = { version = "1.0", features = ["v4"] }

[build-dependencies]
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio-rustls", "macros", "chrono", "json"] }
35 changes: 35 additions & 0 deletions libs/state/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2024 Ronan LE MEILLAT for SCTG Development
//
// This file is part of the SCTGDesk project.
//
// SCTGDesk is free software: you can redistribute it and/or modify
// it under the terms of the Affero General Public License version 3 as
// published by the Free Software Foundation.
//
// SCTGDesk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Affero General Public License for more details.
//
// You should have received a copy of the Affero General Public License
// along with SCTGDesk. If not, see <https://www.gnu.org/licenses/agpl-3.0.html>.
use sqlx::{Connection, Executor, SqliteConnection};
use std::env;


#[tokio::main]
async fn main() {
let db_path = env::var("DATABASE_URL").unwrap_or("sqlite://db_v2.sqlite3".to_string());
let mut conn = SqliteConnection::connect(&format!("{}", db_path))
.await
.expect("Failed to open database");
conn.execute(
r#"
INSERT OR IGNORE INTO peer (guid, id, uuid, pk, created_at, "user", status, note, region, strategy, info, last_online) VALUES
(x'95CC7775BA37481DAD7214A4F6CE5A94', 'TESTUSER', randomblob(16), randomblob(16), '1901-01-01 12:00:00', randomblob(16), 0, '', NULL, randomblob(16), '{}', '1901-01-01 12:00:00');
"#
)
.await
.expect("Failed to insert test data");

}
5 changes: 4 additions & 1 deletion libs/state/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,9 +1560,12 @@ impl Database {

pub async fn get_cpus_count(&self) -> Vec<CpuCount> {
let mut conn = self.pool.acquire().await.unwrap();
// for avoiding compiltion error you can use the following query
// for avoiding compiltion error you can use the following query it is included in the build.rs
// INSERT OR IGNORE INTO peer (guid, id, uuid, pk, created_at, "user", status, note, region, strategy, info, last_online) VALUES
// (x'95CC7775BA37481DAD7214A4F6CE5A94', 'TESTUSER', randomblob(16), randomblob(16), '1901-01-01 12:00:00', randomblob(16), 0, '', NULL, randomblob(16), '{}', '1901-01-01 12:00:00');
// Note: that the build.rs will not remove the peer from the database
// for removing the peer from the database you can use the following query
// DELETE FROM peer WHERE guid = x'95CC7775BA37481DAD7214A4F6CE5A94';
let res = sqlx::query!(
r#"
SELECT
Expand Down

0 comments on commit f40cb80

Please sign in to comment.