diff --git a/CHANGELOG.md b/CHANGELOG.md index 3433a7d05..3fd9329dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 0.0.6 (latest) +* fixed bug when new column was'nt added properly in sqlite #157 + +# 0.0.6 (latest) + * added `JSONB` support for Postgres in #126 (thanks @CreepGin) * fixed in in sqlite query runner in #141 (thanks @marcinwadon) * added shortcut exports for table schema classes in #135 (thanks @eduardoweiland) diff --git a/package.json b/package.json index c510ef046..a2751a477 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "typeorm", "private": true, - "version": "0.0.6", + "version": "0.0.7.alpha.1", "description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases.", "license": "MIT", "readmeFilename": "README.md", diff --git a/src/driver/sqlite/SqliteQueryRunner.ts b/src/driver/sqlite/SqliteQueryRunner.ts index 449a68ba2..5a6f7dc60 100644 --- a/src/driver/sqlite/SqliteQueryRunner.ts +++ b/src/driver/sqlite/SqliteQueryRunner.ts @@ -429,7 +429,7 @@ export class SqliteQueryRunner implements QueryRunner { const tableSchema = await this.getTableSchema(tableSchemaOrName); const newTableSchema = tableSchema.clone(); newTableSchema.addColumns([column]); - await this.recreateTable(tableSchema); + await this.recreateTable(newTableSchema, tableSchema); } /** @@ -452,7 +452,7 @@ export class SqliteQueryRunner implements QueryRunner { const tableSchema = await this.getTableSchema(tableSchemaOrName); const newTableSchema = tableSchema.clone(); newTableSchema.addColumns(columns); - await this.recreateTable(tableSchema); + await this.recreateTable(newTableSchema, tableSchema); } /** @@ -818,7 +818,7 @@ export class SqliteQueryRunner implements QueryRunner { return c; } - protected async recreateTable(tableSchema: TableSchema): Promise { + protected async recreateTable(tableSchema: TableSchema, oldTableSchema?: TableSchema): Promise { // const withoutForeignKeyColumns = columns.filter(column => column.foreignKeys.length === 0); // const createForeignKeys = options && options.createForeignKeys; const columnDefinitions = tableSchema.columns.map(dbColumn => this.buildCreateColumnSql(dbColumn)).join(", "); @@ -844,8 +844,11 @@ export class SqliteQueryRunner implements QueryRunner { // recreate a table with a temporary name await this.query(sql1); + // we need only select data from old columns + const oldColumnNames = oldTableSchema ? oldTableSchema.columns.map(column => `"${column.name}"`).join(", ") : columnNames; + // migrate all data from the table into temporary table - const sql2 = `INSERT INTO "temporary_${tableSchema.name}" SELECT ${columnNames} FROM "${tableSchema.name}"`; + const sql2 = `INSERT INTO "temporary_${tableSchema.name}"(${oldColumnNames}) SELECT ${oldColumnNames} FROM "${tableSchema.name}"`; await this.query(sql2); // drop old table diff --git a/src/driver/websql/WebsqlQueryRunner.ts b/src/driver/websql/WebsqlQueryRunner.ts index ba449ee38..f3cfed5b4 100644 --- a/src/driver/websql/WebsqlQueryRunner.ts +++ b/src/driver/websql/WebsqlQueryRunner.ts @@ -438,7 +438,7 @@ export class WebsqlQueryRunner implements QueryRunner { const tableSchema = await this.getTableSchema(tableSchemaOrName); const newTableSchema = tableSchema.clone(); newTableSchema.addColumns([column]); - await this.recreateTable(tableSchema); + await this.recreateTable(newTableSchema, tableSchema); } /** @@ -461,7 +461,7 @@ export class WebsqlQueryRunner implements QueryRunner { const tableSchema = await this.getTableSchema(tableSchemaOrName); const newTableSchema = tableSchema.clone(); newTableSchema.addColumns(columns); - await this.recreateTable(tableSchema); + await this.recreateTable(newTableSchema, tableSchema); } /** @@ -827,7 +827,7 @@ export class WebsqlQueryRunner implements QueryRunner { return c; } - protected async recreateTable(tableSchema: TableSchema): Promise { + protected async recreateTable(tableSchema: TableSchema, oldTableSchema?: TableSchema): Promise { // const withoutForeignKeyColumns = columns.filter(column => column.foreignKeys.length === 0); // const createForeignKeys = options && options.createForeignKeys; const columnDefinitions = tableSchema.columns.map(dbColumn => this.buildCreateColumnSql(dbColumn)).join(", "); @@ -853,8 +853,11 @@ export class WebsqlQueryRunner implements QueryRunner { // recreate a table with a temporary name await this.query(sql1); + // we need only select data from old columns + const oldColumnNames = oldTableSchema ? oldTableSchema.columns.map(column => `"${column.name}"`).join(", ") : columnNames; + // migrate all data from the table into temporary table - const sql2 = `INSERT INTO "temporary_${tableSchema.name}" SELECT ${columnNames} FROM "${tableSchema.name}"`; + const sql2 = `INSERT INTO "temporary_${tableSchema.name}"${oldColumnNames} SELECT ${oldColumnNames} FROM "${tableSchema.name}"`; await this.query(sql2); // drop old table