This repository was archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Add mysql support #41
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
866878a
updated Postgres drop_table_if_exists to produce correct syntax
1805fcc
added the missing doube-quote back to the Pg::drop_table_if_exists fu…
25e37ca
Merge remote-tracking branch 'upstream/master'
172796f
Merge remote-tracking branch 'upstream/master'
fb27f7d
changed pg DOUBLE to be DOUBLE PRECISION per the documents and update…
f4db507
rolling back the version
5f0f3d6
initial attempt at adding mysql support
97388fa
updated the create and drop table if exist functions to create the pr…
a66028f
more mysql syntax changes and a features flag change from sqlite to t…
ef74acb
fixing drop table syntax for mysql
27542d2
removing quotes from around table name on delete
c6605a1
successfullly created the baseballdatabank tables
9f2dfb3
Merge remote-tracking branch 'upstream/master'
8aaf500
Merge branch 'master' into add-mysql-support
e7ba5f8
added tests for mysql that are the same as the other two supported da…
a2c4f36
updated the tests/mod.rs file so the mysql tests actually run, now ha…
90a32b5
added UUID support to MySQL, I chose to go with char(36) since that i…
39c42b3
adding the newline
rippinrobr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//! MySQL implementation of a generator | ||
//! | ||
//! This module generates strings that are specific to MySQL | ||
//! databases. They should be thoroughly tested via unit testing | ||
|
||
use super::SqlGenerator; | ||
use types::{BaseType, Type}; | ||
|
||
/// MySQL generator backend | ||
pub struct MySql; | ||
impl SqlGenerator for MySql { | ||
fn create_table(name: &str) -> String { | ||
format!("CREATE TABLE {}", name) | ||
} | ||
|
||
fn create_table_if_not_exists(name: &str) -> String { | ||
format!("CREATE TABLE {} IF NOT EXISTS", name) | ||
} | ||
|
||
fn drop_table(name: &str) -> String { | ||
format!("DROP TABLE {}", name) | ||
} | ||
|
||
fn drop_table_if_exists(name: &str) -> String { | ||
format!("DROP TABLE {} IF EXISTS", name) | ||
} | ||
|
||
fn rename_table(old: &str, new: &str) -> String { | ||
format!("RENAME TABLE \"{}\" TO \"{}\"", old, new) | ||
} | ||
|
||
fn alter_table(name: &str) -> String { | ||
format!("ALTER TABLE \"{}\"", name) | ||
} | ||
|
||
fn add_column(ex: bool, name: &str, tt: &Type) -> String { | ||
let bt: BaseType = tt.get_inner(); | ||
use self::BaseType::*; | ||
|
||
#[cfg_attr(rustfmt, rustfmt_skip)] /* This shouldn't be formatted. It's too long */ | ||
format!( | ||
"{}{}{}", | ||
match bt { | ||
Text => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Varchar(_) => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Primary => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Integer => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Float => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Double => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
UUID => unimplemented!(), | ||
Json => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Boolean => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Date => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Binary => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Foreign(_) => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Custom(_) => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(bt)), | ||
Array(it) => format!("{}{} {}", MySql::prefix(ex), name, MySql::print_type(Array(Box::new(*it)))) | ||
}, | ||
match (&tt.default).as_ref() { | ||
Some(ref m) => format!(" DEFAULT '{}'", m), | ||
_ => format!(""), | ||
}, | ||
match tt.nullable { | ||
true => "", | ||
false => " NOT NULL", | ||
} | ||
) | ||
} | ||
|
||
fn drop_column(name: &str) -> String { | ||
format!("DROP COLUMN \"{}\"", name) | ||
} | ||
|
||
fn rename_column(old: &str, new: &str) -> String { | ||
format!("CHANGE COLUMN \"{}\" \"{}\"", old, new) | ||
} | ||
} | ||
|
||
impl MySql { | ||
fn prefix(ex: bool) -> String { | ||
match ex { | ||
true => format!("ADD COLUMN "), | ||
false => format!(""), | ||
} | ||
} | ||
|
||
fn print_type(t: BaseType) -> String { | ||
use self::BaseType::*; | ||
match t { | ||
Text => format!("TEXT"), | ||
Varchar(l) => match l { | ||
0 => format!("VARCHAR"), // For "0" remove the limit | ||
_ => format!("VARCHAR({})", l), | ||
}, | ||
/* "NOT NULL" is added here because normally primary keys are implicitly not-null */ | ||
Primary => format!("INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY"), | ||
Integer => format!("INTEGER"), | ||
Float => format!("FLOAT"), | ||
Double => format!("DOUBLE"), | ||
UUID => format!("CHAR(36)"), | ||
Boolean => format!("BOOLEAN"), | ||
Date => format!("DATE"), | ||
Json => format!("JSON"), | ||
Binary => format!("BYTEA"), | ||
Foreign(t) => format!("INTEGER REFERENCES {}", t), | ||
Custom(t) => format!("{}", t), | ||
Array(meh) => format!("{}[]", MySql::print_type(*meh)), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! All add_column combinations for pgsql | ||
#![allow(unused_imports)] | ||
|
||
use backend::{MySql, SqlGenerator}; | ||
use types; | ||
|
||
#[test] | ||
fn text() { | ||
let sql = MySql::add_column(true, "Text", &types::text()); | ||
assert_eq!(String::from("ADD COLUMN \"Text\" TEXT NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn varchar() { | ||
let sql = MySql::add_column(true, "Varchar", &types::varchar(255)); | ||
assert_eq!( | ||
String::from("ADD COLUMN \"Varchar\" VARCHAR(255) NOT NULL"), | ||
sql | ||
); | ||
} | ||
|
||
#[test] | ||
fn integer() { | ||
let sql = MySql::add_column(true, "Integer", &types::integer()); | ||
assert_eq!(String::from("ADD COLUMN \"Integer\" INTEGER NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn float() { | ||
let sql = MySql::add_column(true, "Float", &types::float()); | ||
assert_eq!(String::from("ADD COLUMN \"Float\" FLOAT NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn double() { | ||
let sql = MySql::add_column(true, "Double", &types::double()); | ||
assert_eq!(String::from("ADD COLUMN \"Double\" DOUBLE PRECISION NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn boolean() { | ||
let sql = MySql::add_column(true, "Boolean", &types::boolean()); | ||
assert_eq!(String::from("ADD COLUMN \"Boolean\" BOOLEAN NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn binary() { | ||
let sql = MySql::add_column(true, "Binary", &types::binary()); | ||
assert_eq!(String::from("ADD COLUMN \"Binary\" BYTEA NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn date() { | ||
let sql = MySql::add_column(true, "Date", &types::date()); | ||
assert_eq!(String::from("ADD COLUMN \"Date\" DATE NOT NULL"), sql); | ||
} | ||
|
||
#[test] | ||
fn foreign() { | ||
let sql = MySql::add_column(true, "Foreign", &types::foreign("posts")); | ||
assert_eq!( | ||
String::from("ADD COLUMN \"Foreign\" INTEGER REFERENCES posts NOT NULL"), | ||
sql | ||
); | ||
} | ||
|
||
#[test] | ||
fn uuid() { | ||
let sql = MySql::add_column(true, "MyUUID", &types::UUID); | ||
assert_eq!( | ||
String::from("ADD COLUMN Foreign CHAR(36)"), | ||
sql | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//! Some unit tests that create create tables | ||
#![allow(unused_imports)] | ||
|
||
use backend::{SqlGenerator, MySql}; | ||
use {types, Migration, Table}; | ||
|
||
#[test] | ||
fn create_multiple_tables() { | ||
let mut m = Migration::new(); | ||
m.create_table("artist", |t| { | ||
t.add_column("name", types::text().nullable(true)); | ||
t.add_column("description", types::text().nullable(true)); | ||
t.add_column("pic", types::text().nullable(true)); | ||
t.add_column("mbid", types::text().nullable(true)); | ||
}); | ||
m.create_table("album", |t| { | ||
t.add_column("name", types::text().nullable(true)); | ||
t.add_column("pic", types::text().nullable(true)); | ||
t.add_column("mbid", types::text().nullable(true)); | ||
}); | ||
assert_eq!(m.make::<MySql>(), String::from("CREATE TABLE artist (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, description TEXT, pic TEXT, mbid TEXT);CREATE TABLE album (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, pic TEXT, mbid TEXT);")); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! A few simple tests for the sqlite3 string backend | ||
|
||
mod create_table; | ||
mod simple; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Other simple table/ column migrations | ||
|
||
#![allow(unused_imports)] | ||
|
||
use backend::{SqlGenerator, MySql}; | ||
|
||
#[test] | ||
fn create_table() { | ||
let sql = MySql::create_table("table_to_create"); | ||
assert_eq!(String::from("CREATE TABLE table_to_create"), sql); | ||
} | ||
|
||
#[test] | ||
fn create_table_if_not_exists() { | ||
let sql = MySql::create_table_if_not_exists("table_to_create"); | ||
assert_eq!( | ||
String::from("CREATE TABLE table_to_create IF NOT EXISTS"), | ||
sql | ||
); | ||
} | ||
|
||
#[test] | ||
fn drop_table() { | ||
let sql = MySql::drop_table("table_to_drop"); | ||
assert_eq!(String::from("DROP TABLE table_to_drop"), sql); | ||
} | ||
|
||
#[test] | ||
fn drop_table_if_exists() { | ||
let sql = MySql::drop_table_if_exists("table_to_drop"); | ||
assert_eq!(String::from("DROP TABLE table_to_drop IF EXISTS"), sql); | ||
} | ||
|
||
#[test] | ||
fn rename_table() { | ||
let sql = MySql::rename_table("old_table", "new_table"); | ||
assert_eq!( | ||
String::from("RENAME TABLE \"old_table\" TO \"new_table\""), | ||
sql | ||
); | ||
} | ||
|
||
#[test] | ||
fn alter_table() { | ||
let sql = MySql::alter_table("table_to_alter"); | ||
assert_eq!(String::from("ALTER TABLE \"table_to_alter\""), sql); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the MySQL 8.0 docs there are supported UUID fields. This is definitely something that
barrel
should reflect.Unrelated: this also makes me wonder about how to document the vast amounts of implementation specific features across multiple versions as well 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add support for UUID for sure. As far as documentation goes, that is a great question, to which, I don't have a great answer. I can look around to see if there are others who've done it well and if not I'm willing to help in that cause.