forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 11
Use native operations for more schema related operations in the sqlite driver #17
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
Open
netroy
wants to merge
4
commits into
master
Choose a base branch
from
sqlite-use-native-operations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
---|---|---|
|
@@ -443,90 +443,25 @@ export abstract class AbstractSqliteQueryRunner | |
* Renames the given table. | ||
*/ | ||
async renameTable( | ||
oldTableOrName: Table | string, | ||
tableOrName: Table | string, | ||
newTableName: string, | ||
): Promise<void> { | ||
const oldTable = InstanceChecker.isTable(oldTableOrName) | ||
? oldTableOrName | ||
: await this.getCachedTable(oldTableOrName) | ||
const newTable = oldTable.clone() | ||
|
||
newTable.name = newTableName | ||
|
||
// rename table | ||
const up = new Query( | ||
`ALTER TABLE ${this.escapePath( | ||
oldTable.name, | ||
)} RENAME TO ${this.escapePath(newTableName)}`, | ||
) | ||
const down = new Query( | ||
`ALTER TABLE ${this.escapePath( | ||
newTableName, | ||
)} RENAME TO ${this.escapePath(oldTable.name)}`, | ||
) | ||
await this.executeQueries(up, down) | ||
|
||
// rename unique constraints | ||
newTable.uniques.forEach((unique) => { | ||
const oldUniqueName = | ||
this.connection.namingStrategy.uniqueConstraintName( | ||
oldTable, | ||
unique.columnNames, | ||
) | ||
|
||
// Skip renaming if Unique has user defined constraint name | ||
if (unique.name !== oldUniqueName) return | ||
|
||
unique.name = this.connection.namingStrategy.uniqueConstraintName( | ||
newTable, | ||
unique.columnNames, | ||
) | ||
}) | ||
|
||
// rename foreign key constraints | ||
newTable.foreignKeys.forEach((foreignKey) => { | ||
const oldForeignKeyName = | ||
this.connection.namingStrategy.foreignKeyName( | ||
oldTable, | ||
foreignKey.columnNames, | ||
this.getTablePath(foreignKey), | ||
foreignKey.referencedColumnNames, | ||
) | ||
|
||
// Skip renaming if foreign key has user defined constraint name | ||
if (foreignKey.name !== oldForeignKeyName) return | ||
|
||
foreignKey.name = this.connection.namingStrategy.foreignKeyName( | ||
newTable, | ||
foreignKey.columnNames, | ||
this.getTablePath(foreignKey), | ||
foreignKey.referencedColumnNames, | ||
) | ||
}) | ||
|
||
// rename indices | ||
newTable.indices.forEach((index) => { | ||
const oldIndexName = this.connection.namingStrategy.indexName( | ||
oldTable, | ||
index.columnNames, | ||
index.where, | ||
) | ||
const table = InstanceChecker.isTable(tableOrName) | ||
? tableOrName | ||
: await this.getCachedTable(tableOrName) | ||
const oldName = this.escapePath(table.name) | ||
const newName = this.escapePath(newTableName) | ||
|
||
// Skip renaming if Index has user defined constraint name | ||
if (index.name !== oldIndexName) return | ||
// Generate rename table queries | ||
const up = new Query(`ALTER TABLE ${oldName} RENAME TO ${newName}`) | ||
const down = new Query(`ALTER TABLE ${newName} RENAME TO ${oldName}`) | ||
|
||
index.name = this.connection.namingStrategy.indexName( | ||
newTable, | ||
index.columnNames, | ||
index.where, | ||
) | ||
}) | ||
|
||
// rename old table; | ||
oldTable.name = newTable.name | ||
// Execute the rename | ||
await this.executeQueries(up, down) | ||
|
||
// recreate table with new constraint names | ||
await this.recreateTable(newTable, oldTable) | ||
// Clear the table cache and update the in-memory table name | ||
this.clearCachedTable(table.name) | ||
table.name = newTableName | ||
} | ||
|
||
/** | ||
|
@@ -539,7 +474,7 @@ export abstract class AbstractSqliteQueryRunner | |
const table = InstanceChecker.isTable(tableOrName) | ||
? tableOrName | ||
: await this.getCachedTable(tableOrName) | ||
return this.addColumns(table!, [column]) | ||
return this.addColumns(table, [column]) | ||
} | ||
|
||
/** | ||
|
@@ -552,9 +487,44 @@ export abstract class AbstractSqliteQueryRunner | |
const table = InstanceChecker.isTable(tableOrName) | ||
? tableOrName | ||
: await this.getCachedTable(tableOrName) | ||
const changedTable = table.clone() | ||
columns.forEach((column) => changedTable.addColumn(column)) | ||
await this.recreateTable(changedTable, table) | ||
|
||
// Determine if we can use the native ADD COLUMN syntax or need to recreate the table | ||
const shouldRecreateTable = columns.some( | ||
(column) => | ||
column.isPrimary || | ||
(column.generatedType && column.asExpression), | ||
) | ||
if (shouldRecreateTable) { | ||
const changedTable = table.clone() | ||
columns.forEach((column) => changedTable.addColumn(column)) | ||
await this.recreateTable(changedTable, table) | ||
return | ||
} | ||
|
||
const tableName = this.escapePath(table.name) | ||
const upQueries: Query[] = [] | ||
const downQueries: Query[] = [] | ||
for (const column of columns) { | ||
upQueries.push( | ||
new Query( | ||
`ALTER TABLE ${tableName} ADD ${this.buildCreateColumnSql( | ||
column, | ||
)}`, | ||
), | ||
) | ||
|
||
downQueries.push( | ||
new Query( | ||
`ALTER TABLE ${tableName} DROP COLUMN "${column.name}"`, | ||
), | ||
) | ||
|
||
table.addColumn(column) | ||
} | ||
|
||
if (upQueries.length > 0) { | ||
await this.executeQueries(upQueries, downQueries) | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -576,15 +546,37 @@ export abstract class AbstractSqliteQueryRunner | |
`Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`, | ||
) | ||
|
||
let newColumn: TableColumn | undefined = undefined | ||
let newColumn: TableColumn | ||
if (InstanceChecker.isTableColumn(newTableColumnOrName)) { | ||
newColumn = newTableColumnOrName | ||
} else { | ||
newColumn = oldColumn.clone() | ||
newColumn.name = newTableColumnOrName | ||
} | ||
|
||
return this.changeColumn(table, oldColumn, newColumn) | ||
if (oldColumn.name === newColumn.name) return | ||
|
||
const tableName = this.escapePath(table.name) | ||
const upQueries: Query[] = [ | ||
new Query( | ||
`ALTER TABLE ${tableName} RENAME COLUMN "${oldColumn.name}" TO "${newColumn.name}"`, | ||
), | ||
] | ||
const downQueries: Query[] = [ | ||
new Query( | ||
`ALTER TABLE ${tableName} RENAME COLUMN "${newColumn.name}" TO "${oldColumn.name}"`, | ||
), | ||
] | ||
|
||
await this.executeQueries(upQueries, downQueries) | ||
|
||
// Update column name in the in-memory table object | ||
const tableColumn = table.columns.find( | ||
(column) => column.name === oldColumn.name, | ||
) | ||
if (tableColumn) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we throw if not found instead? |
||
tableColumn.name = newColumn.name | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -759,32 +751,100 @@ export abstract class AbstractSqliteQueryRunner | |
? tableOrName | ||
: await this.getCachedTable(tableOrName) | ||
|
||
// clone original table and remove column and its constraints from cloned table | ||
const changedTable = table.clone() | ||
columns.forEach((column: TableColumn | string) => { | ||
const columnInstance = InstanceChecker.isTableColumn(column) | ||
? column | ||
: table.findColumnByName(column) | ||
if (!columnInstance) | ||
const tableColumns = columns.map((columnOrName) => { | ||
const columnName = InstanceChecker.isTableColumn(columnOrName) | ||
? columnOrName.name | ||
: columnOrName | ||
const column = table.findColumnByName(columnName) | ||
if (!column) { | ||
throw new Error( | ||
`Column "${column}" was not found in table "${table.name}"`, | ||
`Column "${columnName}" was not found in table "${table.name}"`, | ||
) | ||
} | ||
return column | ||
}) | ||
|
||
changedTable.removeColumn(columnInstance) | ||
changedTable | ||
.findColumnUniques(columnInstance) | ||
.forEach((unique) => | ||
changedTable.removeUniqueConstraint(unique), | ||
) | ||
changedTable | ||
.findColumnIndices(columnInstance) | ||
.forEach((index) => changedTable.removeIndex(index)) | ||
changedTable | ||
.findColumnForeignKeys(columnInstance) | ||
.forEach((fk) => changedTable.removeForeignKey(fk)) | ||
// Determine if we can use the native DROP COLUMN syntax or need to recreate the table | ||
const shouldRecreateTable = tableColumns.some((column) => { | ||
// If column is part of a primary key, we need to recreate the table | ||
if (column.isPrimary) { | ||
return true | ||
} | ||
|
||
// If column is referenced by foreign keys, we need to recreate the table | ||
const referencingForeignKeys = table.foreignKeys.filter( | ||
(foreignKey) => foreignKey.columnNames.includes(column.name), | ||
) | ||
if (referencingForeignKeys.length > 0) { | ||
return true | ||
} | ||
|
||
// If column is part of a unique constraint, we need to recreate the table | ||
const uniqueConstraints = table.uniques.filter((unique) => | ||
unique.columnNames.includes(column.name), | ||
) | ||
if (uniqueConstraints.length > 0) { | ||
return true | ||
} | ||
|
||
// Check if it's a generated column | ||
if (column.generatedType && column.asExpression) { | ||
return true | ||
} | ||
|
||
return false | ||
}) | ||
|
||
await this.recreateTable(changedTable, table) | ||
if (shouldRecreateTable) { | ||
// Fall back to recreating the table if we need to handle constraints | ||
const changedTable = table.clone() | ||
tableColumns.forEach((column) => { | ||
changedTable | ||
.findColumnUniques(column) | ||
.forEach((unique) => | ||
changedTable.removeUniqueConstraint(unique), | ||
) | ||
changedTable | ||
.findColumnIndices(column) | ||
.forEach((index) => changedTable.removeIndex(index)) | ||
changedTable | ||
.findColumnForeignKeys(column) | ||
.forEach((fk) => changedTable.removeForeignKey(fk)) | ||
changedTable.removeColumn(column) | ||
}) | ||
await this.recreateTable(changedTable, table) | ||
} else { | ||
// Use native DROP COLUMN if it's a simple column drop | ||
const upQueries: Query[] = [] | ||
const downQueries: Query[] = [] | ||
const tableName = this.escapePath(table.name) | ||
|
||
for (const column of tableColumns) { | ||
// Drop column | ||
upQueries.push( | ||
new Query( | ||
`ALTER TABLE ${tableName} DROP COLUMN "${column.name}"`, | ||
), | ||
) | ||
downQueries.push( | ||
new Query( | ||
`ALTER TABLE ${tableName} ADD ${this.buildCreateColumnSql( | ||
column, | ||
)}`, | ||
), | ||
) | ||
|
||
// Remove column from the in-memory table | ||
const columnIndex = table.columns.findIndex( | ||
(tableColumn) => tableColumn.name === column.name, | ||
) | ||
if (columnIndex !== -1) { | ||
table.columns.splice(columnIndex, 1) | ||
} | ||
} | ||
|
||
await this.executeQueries(upQueries, downQueries) | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains hidden or 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 hidden or 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
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.
Should we log a warning here? Same for other cases that require recreating.