Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect up migrations in SQL migration files #610

Open
kvch opened this issue Jan 20, 2025 · 0 comments
Open

Detect up migrations in SQL migration files #610

kvch opened this issue Jan 20, 2025 · 0 comments
Labels
enhancement New feature or request sql2pgroll Issues relating to the sql2pgroll package

Comments

@kvch
Copy link
Contributor

kvch commented Jan 20, 2025

During testing pgroll with ORMs, I read SQL statements from migrations files. sql2pgroll package can transform a single SQL operation into a pgroll migration (if implemented). So each SQL statement in a migration file was translated individually. However, there are migration file where multiple statements can be translated into a single pgroll migrations.

The example below adds a new column to an existing table, and sets a backfill value. At the moment pgroll translates this into an add_column operation, and expects the user to edit the up attribute of the migration. Then the UPDATE statement is kept as a raw SQL migration.

-- AlterTable
ALTER TABLE "table1" ADD COLUMN "new_column" TEXT;

-- Update all rows
UPDATE "table1" SET "new_column" = gen_random_uuid() WHERE "new_column" IS NULL;

At the moment this is translated into the following migrations:

[
   {
     "add_column": {
       "column": {
         "name": "new_column",
         "nullable": true,
         "type": "text"
       },
       "table": "table1",
       "up": "TODO: Implement SQL data migration"
     }
   }
 ]

and

[
  {
     "sql": {
       "up": "UPDATE \"table1\" SET \"new_column\" = gen_random_uuid() WHERE \"new_column\" IS NULL"
     }
   }
]

This behaviour can be improved. pgroll could read the whole migration file, and find the appropriate up migration in the file. This could reduce the friction of converting ORM migrations to pgroll migrations.

Improved output where up migration is included in the operation:

[
   {
     "add_column": {
       "column": {
         "name": "new_column",
         "nullable": true,
         "type": "text"
       },
       "table": "table1",
       "up": "gen_random_uuid()"
     }
   }
 ]
@kvch kvch added enhancement New feature or request sql2pgroll Issues relating to the sql2pgroll package labels Jan 20, 2025
@kvch kvch changed the title Detect up migrations in SQL migration files Detect up migrations in SQL migration files Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sql2pgroll Issues relating to the sql2pgroll package
Projects
None yet
Development

No branches or pull requests

1 participant