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

Using await in migration cause unexpected behavior #1062

Closed
JAWS-tm opened this issue Nov 6, 2024 · 1 comment
Closed

Using await in migration cause unexpected behavior #1062

JAWS-tm opened this issue Nov 6, 2024 · 1 comment
Assignees
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?

Comments

@JAWS-tm
Copy link

JAWS-tm commented Nov 6, 2024

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
Copy link
Member

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

@thetutlage thetutlage self-assigned this Nov 7, 2024
@thetutlage thetutlage added the Type: Invalid Doesn't really belong here. Maybe use discussion threads? label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?
Projects
None yet
Development

No branches or pull requests

2 participants