tern_core/executor/sqlx_backend/
mysql.rs1use sqlx::MySql;
6
7use super::pool::SqlxExecutor;
8use crate::migration::{AppliedMigration, Query, QueryRepository};
9
10pub type SqlxMySqlExecutor = SqlxExecutor<MySql, SqlxMySqlQueryRepo>;
12
13#[derive(Debug, Clone)]
15pub struct SqlxMySqlQueryRepo;
16
17impl QueryRepository for SqlxMySqlQueryRepo {
18 fn create_history_if_not_exists_query(history_table: &str) -> Query {
19 let sql = format!(
20 "
21CREATE TABLE IF NOT EXISTS {history_table}(
22 version bigint PRIMARY KEY,
23 description text NOT NULL,
24 content text NOT NULL,
25 duration_ms bigint NOT NULL,
26 applied_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
27);
28"
29 );
30
31 Query::new(sql)
32 }
33
34 fn drop_history_query(history_table: &str) -> Query {
35 let sql = format!("DROP TABLE IF EXISTS {history_table};");
36
37 Query::new(sql)
38 }
39
40 fn insert_into_history_query(
41 history_table: &str,
42 _: &AppliedMigration,
43 ) -> Query {
44 let sql = format!(
47 "
48INSERT INTO {history_table}(version, description, content, duration_ms, applied_at)
49 VALUES (?, ?, ?, ?, ?);
50"
51 );
52
53 Query::new(sql)
54 }
55
56 fn select_star_from_history_query(history_table: &str) -> Query {
57 let sql = format!(
58 "
59SELECT
60 version,
61 description,
62 content,
63 duration_ms,
64 applied_at
65FROM
66 {history_table}
67ORDER BY
68 version;
69"
70 );
71
72 Query::new(sql)
73 }
74
75 fn upsert_history_query(
76 history_table: &str,
77 _: &AppliedMigration,
78 ) -> Query {
79 let sql = format!(
80 "
81INSERT INTO {history_table}(version, description, content, duration_ms, applied_at)
82 VALUES (?, ?, ?, ?, ?)
83 ON DUPLICATE_KEY
84 UPDATE
85 description = VALUES(description),
86 content = VALUES(content),
87 duration_ms = VALUES(duration_ms),
88 applied_at = VALUES(applied_at)
89"
90 );
91
92 Query::new(sql)
93 }
94}