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

Do not use sys.tables to find if a table exists #2328

Merged
merged 6 commits into from Feb 1, 2022

Conversation

thomasbiddle
Copy link
Contributor

@thomasbiddle thomasbiddle commented Nov 14, 2017

On AWS RDS (SQL Server Web) access to sys.tables is not available; this query will provide
the same result without relying on the system table.

Tests pass and I'm able to migrate again in my case; but would love for someone else to have a pair of eyes on this; especially due to removing the formattedTable

@elhigu
Copy link
Member

elhigu commented Nov 14, 2017

@Grimace1975 Could you check if this is ok?

@smorey2
Copy link
Collaborator

smorey2 commented Nov 14, 2017

@thomasbiddle,

I am not sure how any test was passed, unless there was no test exercising this method.

````WHERE TABLE_NAME = ${tableName}`;```

should not function, would need the $(tableName) wrapped as a string.

previously this did such wrapping:
const formattedTable = this.formatter.parameter(this.formatter.wrap(tableName));

Please make sure table is passed accordingly, and that a test is there to check its use.

@thomasbiddle
Copy link
Contributor Author

@Grimace1975

I'm not sure I understand; when I was testing tableName was passed as a string just fine; and formattedString was always returning a ? in my case.

@smorey2
Copy link
Collaborator

smorey2 commented Nov 14, 2017 via email

On AWS RDS access to sys.tables is not available; this query will provide
the same result without relying on the system table.
@thomasbiddle
Copy link
Contributor Author

@Grimace1975

Added in the quotes; must've missed that. Branch reflects this now.

Aware of the ? being a variable parameter; however it looks like the bindings being set are always wrapped in []

Example from some debug output using this PR:

[ { sql: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'migrate\'',
    output: [Function: output],
    bindings: [ '[migrate]' ] } ]

[ { sql: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'migrate_lock\'',
    output: [Function: output],
    bindings: [ '[migrate_lock]' ] } ]

And with substituting formattedTable

[ { sql: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'?\'',
    output: [Function: output],
    bindings: [ '[migrate]' ] } ]

[ { sql: 'if object_id(\'[migrate]\', \'U\') is null CREATE TABLE [migrate] ([id] int identity(1,1) not null primary key, [name] nvarchar(255), [batch] int, [migration_time] datetime)',
    bindings: [] } ]
[ { sql: 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'?\'',
    output: [Function: output],
    bindings: [ '[migrate_lock]' ] } ]

When using formattedTable it ends up never finding the table (I've been printing out resp.length to check this.

@smorey2
Copy link
Collaborator

smorey2 commented Nov 15, 2017 via email

@kibertoad
Copy link
Collaborator

@thomasbiddle Would you like any help with this?

@thomasbiddle
Copy link
Contributor Author

@kibertoad - That'd be great; I'm not putting any further time into this, just wanted to contribute what I had to get it working for our situation and don't have time to add anything more.

@OlivierCavadenti OlivierCavadenti merged commit c4a3abc into knex:master Feb 1, 2022
@kibertoad
Copy link
Collaborator

Released in 1.0.2

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

Successfully merging this pull request may close these issues.

None yet

5 participants