Skip to content

Commit db6ab43

Browse files
committed
updated the alter table with the correct 12-step generalized alter table procedure (TBF)
1 parent 4e7940a commit db6ab43

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/manager.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class SQLiteManager {
6262
query += ');'
6363
} else {
6464
if (typeof op != 'undefined' && column) {
65-
let rand: string
65+
let oldname: string
6666
switch (op) {
6767
case AT.RENAME_TABLE:
6868
this.query += 'ALTER TABLE "' + this.table.name + '" RENAME TO "' + column.name + '";\n'
@@ -77,14 +77,27 @@ export class SQLiteManager {
7777
if (newColumn) this.query += 'ALTER TABLE "' + this.table.name + '" RENAME COLUMN "' + column.name + '" TO "' + newColumn + '";\n'
7878
break
7979
default:
80-
rand = Math.floor(Math.random() * 1000000000000).toString()
8180
this.query += '\n\nPRAGMA foreign_keys = OFF;\n'
8281
this.query += 'BEGIN TRANSACTION;\n'
83-
this.query += 'ALTER TABLE "' + this.table.name + '" RENAME TO sqlitemanager_temp_table_' + rand + ';\n'
82+
this.query += 'SELECT type, sql FROM sqlite_schema WHERE tbl_name=' + this.table.name + ';\n'
8483
this.create = true
84+
oldname = this.table.name
85+
this.table.name = 'new_' + this.table.name
8586
this.query += this.queryBuilder()
8687
this.create = false
87-
this.query += '\nDROP TABLE sqlitemanager_temp_table_' + rand + ';\n'
88+
this.query += '\nINSERT INTO "' + this.table.name + '" SELECT * FROM "' + oldname + '";\n'
89+
this.query += 'DROP TABLE "' + oldname + '";\n'
90+
this.query += 'ALTER TABLE "' + this.table.name + '" RENAME TO "' + oldname + '";\n'
91+
this.table.name = oldname
92+
/*
93+
TODO
94+
Use CREATE INDEX, CREATE TRIGGER, and CREATE VIEW to reconstruct indexes, triggers, and views associated with table X. Perhaps use the old format of the triggers, indexes, and views saved from step 3 above as a guide, making changes as appropriate for the alteration.
95+
96+
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.
97+
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+
100+
*/
88101
this.query += 'COMMIT;\n'
89102
this.query += 'PRAGMA foreign_keys = ON;\n'
90103

test/manager.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ describe('Create a table', () => {
5959

6060
expect(risCh).toContain('PRAGMA foreign_keys = OFF;')
6161
expect(risCh).toContain('BEGIN TRANSACTION;')
62-
expect(risCh).toContain('ALTER TABLE "' + testTable.name + '" RENAME TO sqlitemanager_temp_table_')
63-
expect(risCh).toContain('CREATE TABLE "' + testTable.name + '" ("')
62+
expect(risCh).toContain('ALTER TABLE "new_' + testTable.name + '" RENAME TO "' + testTable.name + '";')
63+
expect(risCh).toContain('CREATE TABLE "new_' + testTable.name + '" ("')
6464
expect(risCh).toContain('"' + testTable.columns[2].name + '" TEXT')
65-
expect(risCh).toContain('DROP TABLE sqlitemanager_temp_table_')
65+
expect(risCh).toContain('DROP TABLE "' + testTable.name)
6666
expect(risCh).toContain('COMMIT;')
6767
expect(risCh).toContain('PRAGMA foreign_keys = ON;')
6868

0 commit comments

Comments
 (0)