Skip to content

Commit ab1b401

Browse files
committed
Merge migrations
1 parent 68d9181 commit ab1b401

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

crates/database/src/migrations/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
use rusqlite::OptionalExtension;
2-
use v000_migration::InitialMigration;
3-
use v001_task::TaskMigration;
42

53
use crate::{Database, Result};
64

7-
mod v000_migration;
8-
mod v001_task;
5+
mod v000_initial;
96

10-
const MIGRATIONS: &[&dyn Migration] = &[&InitialMigration, &TaskMigration];
7+
const MIGRATIONS: &[&dyn Migration] = &[&v000_initial::InitialMigration];
118

129
trait Migration {
1310
fn sql(&self) -> &str;
1411

1512
fn apply(&self, db: &mut Database) -> Result<()> {
16-
let sql = self.sql();
17-
let sql = format!("BEGIN;\n\n{sql}\n\nCOMMIT;\n");
18-
db.connection.execute_batch(&sql)?;
13+
db.connection.execute_batch(self.sql())?;
1914
Ok(())
2015
}
2116

crates/database/src/migrations/v001_task.rs renamed to crates/database/src/migrations/v000_initial.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
use super::Migration;
22

3-
/// The ID column of most of tables has to be globally unique,
4-
/// because the application is intented to be used in a distributed way,
5-
/// where the clients don't know about each other.
63
const SQL: &str = r"
4+
BEGIN TRANSACTION;
5+
6+
CREATE TABLE migration_table (
7+
`id` INTEGER PRIMARY KEY,
8+
`sql` TEXT NOT NULL
9+
) STRICT;
10+
11+
712
CREATE TABLE IF NOT EXISTS task_table (
813
`id` BLOB PRIMARY KEY, -- UUID
914
`title` TEXT,
@@ -86,11 +91,15 @@ CREATE TABLE IF NOT EXISTS backend_config_table (
8691
8792
CREATE INDEX IF NOT EXISTS task_tag_table_task_id_idx ON task_tag_table (task_id);
8893
CREATE INDEX IF NOT EXISTS task_tag_table_tag_id_idx ON task_tag_table (tag_id);
94+
95+
ANALYZE;
96+
97+
COMMIT;
8998
";
9099

91-
pub(super) struct TaskMigration;
100+
pub(super) struct InitialMigration;
92101

93-
impl Migration for TaskMigration {
102+
impl Migration for InitialMigration {
94103
fn sql(&self) -> &str {
95104
SQL
96105
}

crates/database/src/migrations/v000_migration.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)