createTable method

Future<void> createTable(
  1. String name,
  2. Function callback
)

Implementation

Future<void> createTable(String name, Function callback) async {
  try {
    Stopwatch stopwatch = Stopwatch()..start();
    final query = StringBuffer();
    _tableName = name;
    callback();
    String index = _indexes.isNotEmpty ? ',${_indexes.join(',')}' : '';
    String foreig = _foreignKey.isNotEmpty ? ',${_foreignKey.join(',')}' : '';
    String primary = _primaryField.isNotEmpty
        ? ',PRIMARY KEY (`$_primaryField`) USING $_primaryAlgorithm'
        : '';
    query.write(
        '''DROP TABLE IF EXISTS `$name`; CREATE TABLE `$name` (${_queries.join(',')}$primary$index$foreig)''');

    String sqlQuery = query.toString();
    if (MigrationConnection().database?.driver == 'Postgresql') {
      sqlQuery = _mysqlToPosgresqlMapper(sqlQuery);
    }

    await MigrationConnection()
        .dbConnection
        ?.execute(sqlQuery.replaceAll(RegExp(r',\s?\)'), ')'));

    stopwatch.stop();
    print(
        ' Create $name table....................................\x1B[32m ${stopwatch.elapsedMilliseconds}ms DONE\x1B[0m');
  } catch (e) {
    print(e);
    exit(0);
  }
}