From ea94d33b8730e6d89eb16eef642c3bd65a112fb8 Mon Sep 17 00:00:00 2001 From: Garth Date: Sun, 28 Jul 2024 10:13:31 +0200 Subject: [PATCH] add methods to add and drop indexes with alter table --- .../orm/angel_migration/lib/src/table.dart | 4 +++ .../lib/src/mariadb/table.dart | 36 ++++++++++++++++++- .../lib/src/mysql/table.dart | 36 ++++++++++++++++++- .../lib/src/postgres/table.dart | 36 ++++++++++++++++++- 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/packages/orm/angel_migration/lib/src/table.dart b/packages/orm/angel_migration/lib/src/table.dart index c7968848..8a213b6f 100755 --- a/packages/orm/angel_migration/lib/src/table.dart +++ b/packages/orm/angel_migration/lib/src/table.dart @@ -1,4 +1,5 @@ import 'package:angel3_orm/angel3_orm.dart'; + import 'column.dart'; abstract class Table { @@ -49,4 +50,7 @@ abstract class MutableTable extends Table { void changeColumnType(String name, ColumnType type); void dropNotNull(String name); void setNotNull(String name); + void addIndex(String name, List columns, IndexType type); + void dropIndex(String name); + void dropPrimaryIndex(); } diff --git a/packages/orm/angel_migration_runner/lib/src/mariadb/table.dart b/packages/orm/angel_migration_runner/lib/src/mariadb/table.dart index 5532d81a..a0f5f347 100644 --- a/packages/orm/angel_migration_runner/lib/src/mariadb/table.dart +++ b/packages/orm/angel_migration_runner/lib/src/mariadb/table.dart @@ -1,6 +1,7 @@ import 'dart:collection'; -import 'package:angel3_orm/angel3_orm.dart'; + import 'package:angel3_migration/angel3_migration.dart'; +import 'package:angel3_orm/angel3_orm.dart'; import 'package:charcode/ascii.dart'; abstract class MariaDbGenerator { @@ -170,4 +171,37 @@ class MariaDbAlterTable extends Table implements MutableTable { void rename(String newName) { _stack.add('RENAME TO $newName'); } + + @override + void addIndex(String name, List columns, IndexType type) { + String indexType = ''; + + switch (type) { + case IndexType.primaryKey: + indexType = 'PRIMARY KEY'; + break; + case IndexType.unique: + indexType = 'UNIQUE INDEX IF NOT EXISTS `$name`'; + break; + case IndexType.standardIndex: + case IndexType.none: + indexType = 'INDEX IF NOT EXISTS `$name`'; + break; + } + + // mask the column names, is more safety + columns.map((column) => '`$column`'); + + _stack.add('ADD $indexType (${columns.join(',')})'); + } + + @override + void dropIndex(String name) { + _stack.add('DROP INDEX IF EXISTS `$name`'); + } + + @override + void dropPrimaryIndex() { + _stack.add('DROP PRIMARY KEY'); + } } diff --git a/packages/orm/angel_migration_runner/lib/src/mysql/table.dart b/packages/orm/angel_migration_runner/lib/src/mysql/table.dart index 34302b06..9f418a66 100644 --- a/packages/orm/angel_migration_runner/lib/src/mysql/table.dart +++ b/packages/orm/angel_migration_runner/lib/src/mysql/table.dart @@ -1,6 +1,7 @@ import 'dart:collection'; -import 'package:angel3_orm/angel3_orm.dart'; + import 'package:angel3_migration/angel3_migration.dart'; +import 'package:angel3_orm/angel3_orm.dart'; import 'package:charcode/ascii.dart'; abstract class MySqlGenerator { @@ -169,4 +170,37 @@ class MysqlAlterTable extends Table implements MutableTable { void rename(String newName) { _stack.add('RENAME TO $newName'); } + + @override + void addIndex(String name, List columns, IndexType type) { + String indexType = ''; + + switch (type) { + case IndexType.primaryKey: + indexType = 'PRIMARY KEY'; + break; + case IndexType.unique: + indexType = 'UNIQUE INDEX `$name`'; + break; + case IndexType.standardIndex: + case IndexType.none: + indexType = 'INDEX `$name`'; + break; + } + + // mask the column names, is more safety + columns.map((column) => '`$column`'); + + _stack.add('ADD $indexType (${columns.join(',')})'); + } + + @override + void dropIndex(String name) { + _stack.add('DROP INDEX `$name`'); + } + + @override + void dropPrimaryIndex() { + _stack.add('DROP PRIMARY KEY'); + } } diff --git a/packages/orm/angel_migration_runner/lib/src/postgres/table.dart b/packages/orm/angel_migration_runner/lib/src/postgres/table.dart index 4ca12528..393f3a43 100755 --- a/packages/orm/angel_migration_runner/lib/src/postgres/table.dart +++ b/packages/orm/angel_migration_runner/lib/src/postgres/table.dart @@ -1,6 +1,7 @@ import 'dart:collection'; -import 'package:angel3_orm/angel3_orm.dart'; + import 'package:angel3_migration/angel3_migration.dart'; +import 'package:angel3_orm/angel3_orm.dart'; import 'package:charcode/ascii.dart'; abstract class PostgresGenerator { @@ -165,4 +166,37 @@ class PostgresAlterTable extends Table implements MutableTable { void rename(String newName) { _stack.add('RENAME TO "$newName"'); } + + @override + void addIndex(String name, List columns, IndexType type) { + String indexType = ''; + + switch (type) { + case IndexType.primaryKey: + indexType = 'PRIMARY KEY'; + break; + case IndexType.unique: + indexType = 'CONSTRAINT "$name" UNIQUE'; + break; + case IndexType.standardIndex: + case IndexType.none: + // not working with postgres + return; + } + + // mask the column names, is more safety + columns.map((column) => '"$column"'); + + _stack.add('ADD $indexType (${columns.join(',')})'); + } + + @override + void dropIndex(String name) { + _stack.add('DROP CONSTRAINT "$name"'); + } + + @override + void dropPrimaryIndex() { + _stack.add('DROP CONSTRAINT "${tableName}_pkey"'); + } }