add standard index handling for mariadb and mysql

This commit is contained in:
Martin Zander 2024-07-08 11:54:56 +02:00
parent d9e9fe1c38
commit 3803fd0ace
2 changed files with 36 additions and 0 deletions

View file

@ -57,6 +57,14 @@ abstract class MariaDbGenerator {
return buf.toString();
}
static String? compileIndex(String name, MigrationColumn column) {
if (column.indexType == IndexType.standardIndex) {
return ' INDEX(`$name`)';
}
return null;
}
static String compileReference(MigrationColumnReference ref) {
var buf = StringBuffer('REFERENCES ${ref.foreignTable}(${ref.foreignKey})');
if (ref.behavior != null) buf.write(' ${ref.behavior!}');
@ -79,6 +87,7 @@ class MariaDbTable extends Table {
void compile(StringBuffer buf, int indent) {
var i = 0;
List indexBuf = [];
_columns.forEach((name, column) {
var col = MariaDbGenerator.compileColumn(column);
@ -89,7 +98,16 @@ class MariaDbTable extends Table {
}
buf.write('$name $col');
var index = MariaDbGenerator.compileIndex(name, column);
if (index != null) indexBuf.add(index);
});
if (indexBuf.isNotEmpty) {
for (var i = 0; i < indexBuf.length; i++) {
buf.write(',\n${indexBuf[$1]}');
}
}
}
}

View file

@ -56,6 +56,14 @@ abstract class MySqlGenerator {
return buf.toString();
}
static String? compileIndex(String name, MigrationColumn column) {
if (column.indexType == IndexType.standardIndex) {
return ' INDEX(`$name`)';
}
return null;
}
static String compileReference(MigrationColumnReference ref) {
var buf = StringBuffer('REFERENCES ${ref.foreignTable}(${ref.foreignKey})');
if (ref.behavior != null) buf.write(' ${ref.behavior!}');
@ -78,6 +86,7 @@ class MysqlTable extends Table {
void compile(StringBuffer buf, int indent) {
var i = 0;
List indexBuf = [];
_columns.forEach((name, column) {
var col = MySqlGenerator.compileColumn(column);
@ -88,7 +97,16 @@ class MysqlTable extends Table {
}
buf.write('$name $col');
var index = MySqlGenerator.compileIndex(name, column);
if (index != null) indexBuf.add(index);
});
if (indexBuf.isNotEmpty) {
for (var i = 0; i < indexBuf.length; i++) {
buf.write(',\n${indexBuf[$1]}');
}
}
}
}