Merge pull request #141 from Garthi/feature/fix_table_index_setting

add/fix table index setting
This commit is contained in:
Thomas 2024-07-08 21:06:21 +08:00 committed by GitHub
commit 6c71ee34a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 1 deletions

View file

@ -21,7 +21,7 @@ class MigrationColumn extends Column {
MigrationColumn(ColumnType type,
{bool isNullable = true,
super.length,
IndexType indexType = IndexType.standardIndex,
IndexType indexType = IndexType.none,
dynamic defaultValue})
: super(type: type, isNullable: isNullable, defaultValue: defaultValue) {
_nullable = isNullable;

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]}');
}
}
}
}