Skip to content

Commit

Permalink
Merge pull request dart-backend#141 from Garthi/feature/fix_table_ind…
Browse files Browse the repository at this point in the history
…ex_setting

add/fix table index setting
  • Loading branch information
dukefirehawk authored Jul 8, 2024
2 parents b71f350 + 3803fd0 commit 6c71ee3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/orm/angel_migration/lib/src/column.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions packages/orm/angel_migration_runner/lib/src/mariadb/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!}');
Expand All @@ -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);
Expand All @@ -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]}');
}
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/orm/angel_migration_runner/lib/src/mysql/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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!}');
Expand All @@ -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);
Expand All @@ -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]}');
}
}
}
}

Expand Down

0 comments on commit 6c71ee3

Please sign in to comment.