Skip to content

Commit bab2bb2

Browse files
committed
added duplicate column name check and foreign key check before committing other kinds of table schema changes
1 parent db6ab43 commit bab2bb2

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

src/manager.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ export class SQLiteManager {
8282
this.query += 'SELECT type, sql FROM sqlite_schema WHERE tbl_name=' + this.table.name + ';\n'
8383
this.create = true
8484
oldname = this.table.name
85-
this.table.name = 'new_' + this.table.name
85+
if (typeof this.findColumn('new_' + this.table.name) == 'undefined') {
86+
this.table.name = 'new_' + this.table.name
87+
} else {
88+
throw new Error('Column new_' + this.table.name + 'already exists')
89+
}
8690
this.query += this.queryBuilder()
8791
this.create = false
8892
this.query += '\nINSERT INTO "' + this.table.name + '" SELECT * FROM "' + oldname + '";\n'
@@ -95,9 +99,8 @@ Use CREATE INDEX, CREATE TRIGGER, and CREATE VIEW to reconstruct indexes, trigge
9599
96100
If any views refer to table X in a way that is affected by the schema change, then drop those views using DROP VIEW and recreate them with whatever changes are necessary to accommodate the schema change using CREATE VIEW.
97101
98-
If foreign key constraints were originally enabled then run PRAGMA foreign_key_check to verify that the schema change did not break any foreign key constraints.
99-
100102
*/
103+
this.query += 'PRAGMA foreign_key_check("' + this.table.name + '");\n'
101104
this.query += 'COMMIT;\n'
102105
this.query += 'PRAGMA foreign_keys = ON;\n'
103106

@@ -175,7 +178,11 @@ If foreign key constraints were originally enabled then run PRAGMA foreign_key_c
175178

176179
addColumn(column: SQLiteManagerColumn): string {
177180
if (this.table.columns) {
178-
this.table.columns.push(column)
181+
if (typeof this.findColumn(column.name) == 'undefined') {
182+
this.table.columns.push(column)
183+
} else {
184+
throw new Error('Column already exists')
185+
}
179186
} else {
180187
this.table.columns = [column]
181188
}
@@ -197,7 +204,11 @@ If foreign key constraints were originally enabled then run PRAGMA foreign_key_c
197204
const i = this.findColumn(oldColumnName)
198205

199206
if (typeof i != 'undefined' && this.table.columns) {
200-
this.table.columns[i].name = newColumnName
207+
if (typeof this.findColumn(newColumnName) == 'undefined') {
208+
this.table.columns[i].name = newColumnName
209+
} else {
210+
throw new Error('Column already exists')
211+
}
201212
}
202213

203214
return this.queryBuilder(AT.RENAME_COLUMN, { name: oldColumnName } as SQLiteManagerColumn, newColumnName)

test/assets/manager-test-tables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const testTable2 = {
5757
},
5858
{
5959
name: 'test2',
60-
type: types.SQLiteManagerType.INTEGER,
60+
type: types.SQLiteManagerType.BLOB,
6161
constraints: {
6262
NOT_NULL: true,
6363
UNIQUE: true
@@ -73,7 +73,7 @@ export const testTable2 = {
7373
},
7474
{
7575
name: 'test4',
76-
type: types.SQLiteManagerType.BLOB,
76+
type: types.SQLiteManagerType.INTEGER,
7777
constraints: {
7878
NOT_NULL: true,
7979
UNIQUE: true,

0 commit comments

Comments
 (0)