Specialize MySQL CREATE TABLE
@@ -11,6 +11,7 @@ use regex::Regex;
use sql_schema_describer::walkers::ColumnWalker;
use sql_schema_describer::*;
use std::borrow::Cow;
+use walkers::TableWalker;
const VARCHAR_LENGTH_PREFIX: &str = "(191)";
@@ -103,6 +104,51 @@ impl SqlRenderer for MysqlFlavour {
Vec::new() // enums are defined on each column that uses them on MySQL
}
+ fn render_create_table(&self, table: &TableWalker<'_>, schema_name: &str) -> anyhow::Result<String> {
+ let columns: String = table
+ .columns()
+ .map(|column| self.render_column(&schema_name, column, false))
+ .join(",\n");
+
+ let primary_columns = table.table.primary_key_columns();
+
+ let primary_key = if !primary_columns.is_empty() {
+ let column_names = primary_columns.iter().map(|col| self.quote(&col)).join(",");
+ format!(",\nPRIMARY KEY ({})", column_names)
+ } else {
+ String::new()
+ };
+
+ let indexes = if !table.table.indices.is_empty() {
+ let indices: String = table
+ .table
+ .indices
+ .iter()
+ .map(|index| {
+ let tpe = if index.is_unique() { "UNIQUE " } else { "" };
+ format!(
+ "{}Index {}({})",
+ tpe,
+ self.quote(&index.name),
+ index.columns.iter().map(|col| self.quote(&col)).join(",\n")
+ )
+ })
+ .join(",\n");
+
+ format!(",\n{}", indices)
+ } else {
+ String::new()
+ };
+
+ Ok(format!(
+ "CREATE TABLE {} (\n{columns}{indexes}{primary_key}\n) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci",
+ table_name = self.quote_with_schema(&schema_name, table.name()),
+ columns = columns,
+ primary_key = primary_key,
+ indexes = indexes
+ ))
+ }
+
fn render_drop_enum(&self, _drop_enum: &crate::DropEnum) -> Vec<String> {
Vec::new()
}
