Skip to content

Commit

Permalink
Merge pull request #148 from Garthi/feature/migration_standalone_index
Browse files Browse the repository at this point in the history
add a way to change indexes with migrations without alter table
  • Loading branch information
dukefirehawk authored Aug 4, 2024
2 parents 6bf4ad4 + c816775 commit 9bb742d
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/orm/angel_migration/lib/src/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ abstract class Schema {
void createIfNotExists(String tableName, void Function(Table table) callback);

void alter(String tableName, void Function(MutableTable table) callback);

void indexes(
String tableName,
void Function(MutableIndexes indexes) callback,
);
}
12 changes: 11 additions & 1 deletion packages/orm/angel_migration/lib/src/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,24 @@ abstract class Table {
}
}

abstract class MutableTable extends Table {
abstract class MutableTable extends Table implements MutableIndexes {
void rename(String newName);

void dropColumn(String name);

void renameColumn(String name, String newName);

void changeColumnType(String name, ColumnType type);

void dropNotNull(String name);

void setNotNull(String name);
}

abstract class MutableIndexes {
void addIndex(String name, List<String> columns, IndexType type);

void dropIndex(String name);

void dropPrimaryIndex();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:angel3_migration/angel3_migration.dart';
import 'package:logging/logging.dart';
import 'package:mysql1/mysql1.dart';
Expand Down Expand Up @@ -74,4 +75,12 @@ class MariaDbSchema extends Schema {
void createIfNotExists(
String tableName, void Function(Table table) callback) =>
_create(tableName, callback, true);

@override
void indexes(String tableName, void Function(MutableIndexes index) callback) {
var tbl = MariaDbIndexes(tableName);
callback(tbl);

tbl.compile(_buf);
}
}
50 changes: 50 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 @@ -229,3 +229,53 @@ class MariaDbAlterTable extends Table implements MutableTable {
_stack.add('DROP PRIMARY KEY');
}
}

class MariaDbIndexes implements MutableIndexes {
final String tableName;
final Queue<String> _stack = Queue<String>();

MariaDbIndexes(this.tableName);

void compile(StringBuffer buf) {
while (_stack.isNotEmpty) {
var str = _stack.removeFirst();
buf.writeln(str);
}
}

@override
void addIndex(String name, List<String> columns, IndexType type) {
// mask the column names, is more safety
columns = columns.map((column) => '`$column`').toList();

switch (type) {
case IndexType.primaryKey:
_stack.add(
'ALTER TABLE `$tableName` ADD PRIMARY KEY (${columns.join(',')});',
);
break;
case IndexType.unique:
_stack.add(
'CREATE UNIQUE INDEX IF NOT EXISTS `$name` '
'ON `$tableName` (${columns.join(',')});',
);
break;
case IndexType.standardIndex:
case IndexType.none:
_stack.add(
'CREATE INDEX `$name` ON `$tableName` (${columns.join(',')});',
);
break;
}
}

@override
void dropIndex(String name) {
_stack.add('DROP INDEX IF EXISTS `$name` ON `$tableName`;');
}

@override
void dropPrimaryIndex() {
_stack.add('DROP INDEX `PRIMARY` ON `$tableName`;');
}
}
9 changes: 9 additions & 0 deletions packages/orm/angel_migration_runner/lib/src/mysql/schema.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:angel3_migration/angel3_migration.dart';
import 'package:logging/logging.dart';
import 'package:mysql_client/mysql_client.dart';
Expand Down Expand Up @@ -79,4 +80,12 @@ class MySqlSchema extends Schema {
void createIfNotExists(
String tableName, void Function(Table table) callback) =>
_create(tableName, callback, true);

@override
void indexes(String tableName, void Function(MutableIndexes table) callback) {
var tbl = MysqlIndexes(tableName);
callback(tbl);

tbl.compile(_buf);
}
}
49 changes: 49 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 @@ -235,3 +235,52 @@ class MysqlAlterTable extends Table implements MutableTable {
_stack.add('DROP PRIMARY KEY');
}
}

class MysqlIndexes implements MutableIndexes {
final String tableName;
final Queue<String> _stack = Queue<String>();

MysqlIndexes(this.tableName);

void compile(StringBuffer buf) {
while (_stack.isNotEmpty) {
buf.writeln(_stack.removeFirst());
}
}

@override
void addIndex(String name, List<String> columns, IndexType type) {
// mask the column names, is more safety
columns.map((column) => '`$column`');

switch (type) {
case IndexType.primaryKey:
_stack.add(
'ALTER TABLE `$tableName` ADD PRIMARY KEY (${columns.join(',')});',
);
break;
case IndexType.unique:
_stack.add(
'CREATE UNIQUE INDEX `$name` ON `$tableName` (${columns.join(',')});',
);
break;
case IndexType.standardIndex:
case IndexType.none:
default:
_stack.add(
'CREATE INDEX `$name` ON `$tableName` (${columns.join(',')});',
);
break;
}
}

@override
void dropIndex(String name) {
_stack.add('DROP INDEX `$name` ON `$tableName`;');
}

@override
void dropPrimaryIndex() {
_stack.add('DROP INDEX `PRIMARY` ON `$tableName`;');
}
}
12 changes: 11 additions & 1 deletion packages/orm/angel_migration_runner/lib/src/postgres/schema.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';

import 'package:angel3_migration/angel3_migration.dart';
import 'package:postgres/postgres.dart';
import 'package:logging/logging.dart';
import 'package:postgres/postgres.dart';

import 'table.dart';

/// A PostgreSQL database schema generator
Expand Down Expand Up @@ -73,4 +75,12 @@ class PostgresSchema extends Schema {
void createIfNotExists(
String tableName, void Function(Table table) callback) =>
_create(tableName, callback, true);

@override
void indexes(String tableName, void Function(MutableIndexes table) callback) {
var tbl = PostgresIndexes(tableName);
callback(tbl);

tbl.compile(_buf);
}
}
51 changes: 51 additions & 0 deletions packages/orm/angel_migration_runner/lib/src/postgres/table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,54 @@ class PostgresAlterTable extends Table implements MutableTable {
_stack.add('DROP CONSTRAINT "${tableName}_pkey"');
}
}

class PostgresIndexes implements MutableIndexes {
final String tableName;
final Queue<String> _stack = Queue<String>();

PostgresIndexes(this.tableName);

void compile(StringBuffer buf) {
while (_stack.isNotEmpty) {
buf.writeln(_stack.removeFirst());
}
}

@override
void addIndex(String name, List<String> columns, IndexType type) {
// mask the column names, is more safety
columns.map((column) => '"$column"');

switch (type) {
case IndexType.primaryKey:
_stack.add(
'ALTER TABLE "$tableName" ADD PRIMARY KEY (${columns.join(',')});',
);
break;
case IndexType.unique:
_stack.add(
'CREATE UNIQUE INDEX IF NOT EXISTS "$name" '
'ON "$tableName" (${columns.join(',')});',
);
break;
case IndexType.standardIndex:
case IndexType.none:
default:
_stack.add(
'CREATE INDEX IF NOT EXISTS "$name" '
'ON "$tableName" (${columns.join(',')});',
);
break;
}
}

@override
void dropIndex(String name) {
_stack.add('DROP INDEX "$name";');
}

@override
void dropPrimaryIndex() {
_stack.add('ALTER TABLE "$tableName" DROP CONSTRAINT "${tableName}_pkey"');
}
}

0 comments on commit 9bb742d

Please sign in to comment.