Skip to content

Commit

Permalink
🐛 Fix mysql and minor code changes
Browse files Browse the repository at this point in the history
This commit fixes the mysql create table query and
adds minor code beautification.

Signed-off-by: Edward Fitz Abucay <ffimnsr@gmail.com>
  • Loading branch information
ffimnsr committed Sep 18, 2024
1 parent 6159223 commit cff331e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
8 changes: 2 additions & 6 deletions src/commander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,10 @@ impl<T: SequelDriver + 'static + ?Sized> Migrator<T> {

debug!("Creating new env file: {:?}", filepath);
let mut f = File::create(filepath)?;
let contents = formatdoc!(
"
let contents = formatdoc! {"
DSN={}
MIGRATIONS_ROOT={}
",
dsn,
source
);
", dsn, source};
f.write_all(contents.as_bytes())?;
f.sync_all()?;

Expand Down
17 changes: 10 additions & 7 deletions src/sequel/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use indoc::formatdoc;
use indoc::{formatdoc, indoc};
use log::trace;
use mysql::{params, prelude::Queryable, Pool, PooledConn};

Expand All @@ -24,7 +24,13 @@ impl SequelDriver for Mysql {
}

fn ensure_migration_table_exists(&mut self) -> Result<(), Error> {
let payload = "CREATE TABLE IF NOT EXISTS __schema_migrations (id SERIAL PRIMARY KEY, migration BIGINT)";
let payload = indoc! {"
CREATE TABLE IF NOT EXISTS __schema_migrations (
id INT NOT NULL AUTO_INCREMENT,
migration BIGINT,
PRIMARY KEY (id)
) AUTO_INCREMENT = 100;
"};
self.conn.query_drop(payload)?;
Ok(())
}
Expand All @@ -36,13 +42,10 @@ impl SequelDriver for Mysql {
}

fn drop_database(&mut self, db_name: &str) -> Result<(), Error> {
let payload = formatdoc!(
"
let payload = formatdoc! {"
DROP DATABASE IF EXISTS `{db_name}`;
CREATE DATABASE `{db_name}`;
",
db_name = db_name
);
", db_name = db_name};
self.conn.exec_drop(payload, ())?;
Ok(())
}
Expand Down
7 changes: 2 additions & 5 deletions src/sequel/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ impl SequelDriver for Postgres {
}

fn drop_database(&mut self, db_name: &str) -> Result<(), Error> {
let payload = formatdoc!(
"
let payload = formatdoc! {"
DROP DATABASE IF EXISTS `{db_name}`;
CREATE DATABASE `{db_name}`;
",
db_name = db_name
);
", db_name = db_name };
self.client.execute(&payload, &[])?;
Ok(())
}
Expand Down
8 changes: 7 additions & 1 deletion src/sequel/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indoc::indoc;
use log::trace;
use rusqlite::{Connection, Result};

Expand All @@ -22,7 +23,12 @@ impl SequelDriver for Sqlite {
}

fn ensure_migration_table_exists(&mut self) -> Result<(), Error> {
let payload = "CREATE TABLE IF NOT EXISTS __schema_migrations (id INTEGER PRIMARY KEY AUTOINCREMENT, migration BIGINT)";
let payload = indoc! {"
CREATE TABLE IF NOT EXISTS __schema_migrations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
migration BIGINT
);
"};
self.conn.execute(payload, ())?;
Ok(())
}
Expand Down

0 comments on commit cff331e

Please sign in to comment.