adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.08k stars 195 forks source link

Using await in migration cause unexpected behavior #1062

Closed JAWS-tm closed 2 weeks ago

JAWS-tm commented 2 weeks ago

Package version

20.6.0

Describe the bug

Including await in a migration file when creating or altering a table schema results in unexpected errors. Specifically, even if the table or column does not exist, running the migration fails with a message indicating that the column already exists.

Example for Reproduction:

The following code should create a new table, test_table, with a single column column_name. However, using await in the up method causes an error.

export default class extends BaseSchema {
    protected tableName = "test_table"

    async up() {
        await this.schema.createTable(tableName, (table) => {
               table.string("column_name")
        } 
    }

    ...

}

When running this migration, the following error occurs:

column 'column_name' of relation 'test_table' already exists"

If await is removed, the migration works correctly, creating the table and column without errors.

Expected Behavior:

With await in the migration, the table and column should be created, and no errors about existing columns should be thrown.

Actual Behavior:

The migration fails with an error stating that the column already exists, even when it does not.

Environment:

Database: PostgreSQL Node.js version: v20.11.0 Adonis Lucid version:: 20.6.0

Reproduction repo

No response

thetutlage commented 2 weeks ago

That's the expected behavior. You never await the statements within the migration class, since that is done by the Migration runner behind the scenes.

By not awaiting right away we are able to support the dry run behavior using the --dry-run flag. In dry run, the queries are not executed and instead their corresponding SQL is displayed in the output