Skip to content

Commit

Permalink
Merge pull request #129 from javad-zobeidi/dev
Browse files Browse the repository at this point in the history
fix id auto-increment for PostgreSQL compatibility (#127)
  • Loading branch information
javad-zobeidi authored Sep 22, 2024
2 parents 82d28e6 + 30be917 commit 2395028
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/src/database/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Migration {
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);
}
Expand Down Expand Up @@ -1245,9 +1246,13 @@ class Migration {

/// Mapper for mysql to postgresql query
String _mysqlToPosgresqlMapper(String queryStr) {
if (queryStr.contains("AUTO_INCREMENT")) {
queryStr = queryStr.replaceAll("AUTO_INCREMENT", "PRIMARY KEY");
}

queryStr = queryStr.replaceAllMapped(
RegExp(
r'`(\w+)`\s+BIGINT\(\d+\)\s+UNSIGNED\s+NOT\s+NULL\s+AUTO_INCREMENT',
caseSensitive: false),
(match) => '"${match[1]}" SERIAL NOT NULL PRIMARY KEY');


if (RegExp(r"PRIMARY KEY \(`.*?`\) USING BTREE").hasMatch(queryStr)) {
queryStr = queryStr.replaceAll(
Expand Down

0 comments on commit 2395028

Please sign in to comment.