-
Notifications
You must be signed in to change notification settings - Fork 282
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
add addIndex
& dropIndex
@ AlterTableBuilder
#522
Closed
Gaspero
wants to merge
11
commits into
kysely-org:master
from
Gaspero:add-and-remove-index-in-alter-table
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a6df428
add AddIndexNode
Gaspero 3c90d4a
add AddIndexNode & DropIndexNode @ AlterTableNode
Gaspero d45e797
add AddIndexNode @ OperationNodeTransformer
Gaspero 6855239
add AddIndexNode @ OperationNodeVisitor
Gaspero 7d8e8c9
add AddIndexNode @ OperationNodeKind
Gaspero 02d334e
add visitAddIndex @ DefaultQueryCompiler
Gaspero 59dde05
add AddIndexBuilder
Gaspero ba5e352
add AlterTableAddIndexBuilder
Gaspero 1b64a69
add addIndex & dropIndex @ AlterTableBuilder
Gaspero 03cebf2
Add tests for add/drop index in alter table expr
Gaspero c03513a
remove doc duplicates @ AddIndexBuilder
Gaspero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { freeze } from '../util/object-utils.js' | ||
import { IdentifierNode } from './identifier-node.js' | ||
import { OperationNode } from './operation-node.js' | ||
import { RawNode } from './raw-node.js' | ||
|
||
export type AddIndexNodeProps = Omit<AddIndexNode, 'kind' | 'name'> | ||
|
||
export interface AddIndexNode extends OperationNode { | ||
readonly kind: 'AddIndexNode' | ||
readonly name: IdentifierNode | ||
readonly columns?: OperationNode[] | ||
readonly unique?: boolean | ||
readonly using?: RawNode | ||
readonly ifNotExists?: boolean | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const AddIndexNode = freeze({ | ||
is(node: OperationNode): node is AddIndexNode { | ||
return node.kind === 'AddIndexNode' | ||
}, | ||
|
||
create(index: Omit<AddIndexNode, 'kind'>): AddIndexNode { | ||
return freeze({ | ||
kind: 'AddIndexNode', | ||
...index, | ||
}) | ||
}, | ||
|
||
cloneWith(node: AddIndexNode, props: AddIndexNodeProps): AddIndexNode { | ||
return freeze({ | ||
...node, | ||
...props, | ||
}) | ||
}, | ||
|
||
cloneWithColumns(node: AddIndexNode, columns: OperationNode[]): AddIndexNode { | ||
return freeze({ | ||
...node, | ||
columns: [...(node.columns || []), ...columns], | ||
}) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Expression } from '../expression/expression.js' | ||
import { AddIndexNode } from '../operation-node/add-index-node.js' | ||
import { IndexType } from '../operation-node/create-index-node.js' | ||
import { OperationNodeSource } from '../operation-node/operation-node-source.js' | ||
import { RawNode } from '../operation-node/raw-node.js' | ||
import { | ||
ExtractColumnNameFromOrderedColumnName, | ||
OrderedColumnName, | ||
parseOrderedColumnName, | ||
} from '../parser/reference-parser.js' | ||
import { QueryExecutor } from '../query-executor/query-executor.js' | ||
import { freeze } from '../util/object-utils.js' | ||
import { preventAwait } from '../util/prevent-await.js' | ||
import { QueryId } from '../util/query-id.js' | ||
|
||
export interface AddIndexBuilderInterface<R> { | ||
unique(): R | ||
columns<CL extends string>(columns: OrderedColumnName<CL>[]): R | ||
expression(expression: Expression<any>): R | ||
using(indexType: IndexType): R | ||
using(indexType: string): R | ||
} | ||
|
||
export class AddIndexBuilder<C = never> | ||
implements AddIndexBuilderInterface<AddIndexBuilder>, OperationNodeSource | ||
{ | ||
readonly #props: AddIndexBuilderProps | ||
|
||
constructor(props: AddIndexBuilderProps) { | ||
this.#props = freeze(props) | ||
} | ||
|
||
unique(): AddIndexBuilder<C> { | ||
return new AddIndexBuilder({ | ||
...this.#props, | ||
node: AddIndexNode.cloneWith(this.#props.node, { | ||
unique: true, | ||
}), | ||
}) | ||
} | ||
|
||
column<CL extends string>( | ||
column: OrderedColumnName<CL> | ||
): AddIndexBuilder<C | ExtractColumnNameFromOrderedColumnName<CL>> { | ||
return new AddIndexBuilder({ | ||
...this.#props, | ||
node: AddIndexNode.cloneWithColumns(this.#props.node, [ | ||
parseOrderedColumnName(column), | ||
]), | ||
}) | ||
} | ||
|
||
columns<CL extends string>( | ||
columns: OrderedColumnName<CL>[] | ||
): AddIndexBuilder<C | ExtractColumnNameFromOrderedColumnName<CL>> { | ||
return new AddIndexBuilder({ | ||
...this.#props, | ||
node: AddIndexNode.cloneWithColumns( | ||
this.#props.node, | ||
columns.map(parseOrderedColumnName) | ||
), | ||
}) | ||
} | ||
|
||
expression(expression: Expression<any>): AddIndexBuilder<C> { | ||
return new AddIndexBuilder({ | ||
...this.#props, | ||
node: AddIndexNode.cloneWithColumns(this.#props.node, [ | ||
expression.toOperationNode(), | ||
]), | ||
}) | ||
} | ||
|
||
using(indexType: IndexType): AddIndexBuilder<C> | ||
using(indexType: string): AddIndexBuilder<C> | ||
using(indexType: string): AddIndexBuilder<C> { | ||
return new AddIndexBuilder({ | ||
...this.#props, | ||
node: AddIndexNode.cloneWith(this.#props.node, { | ||
using: RawNode.createWithSql(indexType), | ||
}), | ||
}) | ||
} | ||
|
||
/** | ||
* Simply calls the provided function passing `this` as the only argument. `$call` returns | ||
* what the provided function returns. | ||
*/ | ||
$call<T>(func: (qb: this) => T): T { | ||
return func(this) | ||
} | ||
|
||
toOperationNode(): AddIndexNode { | ||
return this.#props.node | ||
} | ||
} | ||
|
||
export interface AddIndexBuilderProps { | ||
readonly queryId: QueryId | ||
readonly executor: QueryExecutor | ||
readonly node: AddIndexNode | ||
} | ||
|
||
preventAwait(AddIndexBuilder, "don't await AddIndexBuilder instances directly.") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there ever a situation when it's created with anything but a name at first?
If not, accept a single argument
name
here.