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

knex.dropTable() errors on Oracle when .withSchema() #4595

Closed
code-ape opened this issue Jul 29, 2021 · 2 comments · Fixed by #4596
Closed

knex.dropTable() errors on Oracle when .withSchema() #4595

code-ape opened this issue Jul 29, 2021 · 2 comments · Fixed by #4596

Comments

@code-ape
Copy link
Collaborator

Environment

Knex version: 0.95.8
Database + version: Oracle 18c (@atiertant)
OS: Ubuntu Linux

Bug

  1. Explain what kind of behaviour you are getting and how you think it should do

When attempting to delete a table for a given schema (called a "user" in Oracle) then the migration fails with an error.

  1. Error message
-- indent formatted by issue filer for easier reading
drop table "my_table";
begin
  execute immediate 'drop sequence "my_table"';
  exception when others then if sqlcode != -2289 then raise;
  end if;
end;

-- Error: drop table "my_table" - ORA-00942: table or view does not exist
  1. Reduced test code, for example in https://npm.runkit.com/knex or if it needs real
    database connection to MySQL or PostgreSQL, then single file example which initializes
    needed data and demonstrates the problem.
// Create schema / user
const sanitizedRaw = knex.raw(`CREATE USER ?? IDENTIFIED BY ??`, [schemaName, schemaName]).toQuery()
await knex.schema.raw(sanitizedRaw)
// Create table
await knex.schema.withSchema('my_schema').createTable('my_table', (table) => {
  table.increments('id').primary()
})
// Drop table
knex.schema.withSchema('my_schema').dropTable('my_table')
@code-ape
Copy link
Collaborator Author

It appears this can be resolved by fixing the following functions in knex/lib/dialects/oracle/schema/oracle-compiler.js:

  1. dropTable needs to use schema name if it exists (this.schema).
  2. dropTableIfExists needs to use schema name if it exists (this.schema).
  3. dropSequenceIfExists needs to use schema name if it exists (this.schema).

Example code:

  dropSequenceIfExists(sequenceName) {
    const prefix = this.schema ? `"${this.schema}".` : "";
    this.pushQuery(
      utils.wrapSqlWithCatch(
        `drop sequence ${prefix}${this.formatter.wrap(sequenceName)}`,
        -2289
      )
    );
  }

@kibertoad
Copy link
Collaborator

Released in 0.95.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants