Skip to content

Commit 7a20518

Browse files
committed
fix: integrate SQL migrations into binary
1 parent 922ed63 commit 7a20518

File tree

8 files changed

+49
-8
lines changed

8 files changed

+49
-8
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: |
3333
cargo install sqlx-cli
3434
cargo sqlx database create
35-
cargo sqlx migrate run
35+
cargo sqlx migrate run --source src/migrations
3636
cargo sqlx prepare
3737
3838
- name: Cache dependencies
@@ -176,13 +176,15 @@ jobs:
176176
bin: InvitationBot
177177
toolchain: beta
178178
cross: false
179+
skip-tests: true
179180

180181
- os-name: Linux-x86_64
181182
runs-on: ubuntu-22.04
182183
target: x86_64-unknown-linux-musl
183184
bin: InvitationBot
184185
toolchain: nightly
185186
cross: false
187+
skip-tests: true
186188

187189
steps:
188190
- uses: actions/checkout@v4
@@ -226,7 +228,7 @@ jobs:
226228
run: |
227229
cargo install sqlx-cli
228230
cargo sqlx database create
229-
cargo sqlx migrate run
231+
cargo sqlx migrate run --source src/migrations
230232
cargo sqlx prepare
231233
232234
- name: Cache dependencies
@@ -306,7 +308,7 @@ jobs:
306308
307309
[ -f "${dir}/${CRATE_NAME}" ] && mv "${dir}/${CRATE_NAME}" "release/${dir_name}"
308310
[ -f "${dir}/${CRATE_NAME}.exe" ] && mv "${dir}/${CRATE_NAME}.exe" "release/${dir_name}.exe"
309-
311+
310312
done
311313
312314
ls -la release

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ RUN apt-get update && apt-get install -y \
1111
ENV DATABASE_URL=sqlite:/tmp/bot.db
1212
RUN cargo install sqlx-cli && \
1313
sqlx database create && \
14-
sqlx migrate run
14+
sqlx migrate run --source src/migrations
1515

1616
RUN cargo build --release
1717

src/http_server.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ mod tests {
173173
async fn setup_test_app() -> (Router, SqlitePool) {
174174
let db_url = format!("sqlite:file:{}?mode=memory", Uuid::new_v4());
175175
let pool = crate::utils::db::create_pool(&db_url).await.unwrap();
176-
sqlx::migrate!().run(&pool).await.unwrap();
177176

178177
let config = Arc::new(Config {
179178
bot: BotConfig {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::collections::HashMap;
66
mod handlers;
77
mod http_server;
88
mod i18n;
9+
mod migrations;
910
mod slash_commands;
1011
mod utils;
1112

File renamed without changes.

src/migrations/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use rust_embed::RustEmbed;
2+
3+
#[derive(RustEmbed)]
4+
#[folder = "src/migrations"]
5+
pub struct Migrations;
6+
7+
pub fn get_migration(name: &str) -> Option<String> {
8+
Migrations::get(name).map(|f| String::from_utf8_lossy(f.data.as_ref()).into_owned())
9+
}
10+
11+
pub fn get_migrations() -> Vec<String> {
12+
Migrations::iter().map(|f| f.to_string()).collect()
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
#[test]
20+
fn test_get_migration() {
21+
// Test existing migration
22+
let migration =
23+
get_migration("20240101000000_create_tables.sql").expect("Failed to load migration");
24+
assert!(migration.contains("CREATE TABLE IF NOT EXISTS invites"));
25+
26+
// Test non-existing migration
27+
assert!(get_migration("invalid.sql").is_none());
28+
}
29+
30+
#[test]
31+
fn test_get_migrations() {
32+
let migrations = get_migrations();
33+
assert!(!migrations.is_empty());
34+
assert!(migrations.contains(&"20240101000000_create_tables.sql".to_string()));
35+
}
36+
}

src/utils/db.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ use sqlx::types::time::OffsetDateTime;
22
type Pool = sqlx::Pool<sqlx::Sqlite>;
33

44
pub async fn setup_database(pool: &Pool) -> Result<(), sqlx::Error> {
5-
sqlx::migrate!().run(pool).await?;
5+
let migrations = crate::migrations::get_migrations();
6+
for migration in migrations {
7+
if let Some(sql) = crate::migrations::get_migration(migration.as_str()) {
8+
sqlx::query(&sql).execute(pool).await?;
9+
}
10+
}
611
Ok(())
712
}
813

@@ -195,7 +200,6 @@ mod tests {
195200
async fn setup_test_db() -> Pool {
196201
let db_url = format!("sqlite:file:{}?mode=memory", Uuid::new_v4());
197202
let pool = create_pool(&db_url).await.unwrap();
198-
sqlx::migrate!().run(&pool).await.unwrap();
199203
pool
200204
}
201205

src/utils/test_helpers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ impl TestContext {
1010
pub async fn new() -> Self {
1111
let db_url = format!("sqlite:file:{}?mode=memory", Uuid::new_v4());
1212
let pool = crate::utils::db::create_pool(&db_url).await.unwrap();
13-
sqlx::migrate!().run(&pool).await.unwrap();
1413

1514
let config = crate::utils::config::Config {
1615
bot: crate::utils::config::BotConfig {

0 commit comments

Comments
 (0)