Skip to content

Commit

Permalink
fix(sqlite): respect autoincrement: false in schema diffing
Browse files Browse the repository at this point in the history
Closes #2800
  • Loading branch information
B4nan committed Feb 20, 2022
1 parent 6cfb526 commit b39b6ad
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/better-sqlite/src/BetterSqliteSchemaHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class BetterSqliteSchemaHelper extends SchemaHelper {

async getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<any[]> {
const columns = await connection.execute<any[]>(`pragma table_info('${tableName}')`);
const sql = `select sql from sqlite_master where type = ? and name = ?`;
const tableDefinition = await connection.execute<{ sql: string }>(sql, ['table', tableName], 'get');
const composite = columns.reduce((count, col) => count + (col.pk ? 1 : 0), 0) > 1;
// there can be only one, so naive check like this should be enough
const hasAutoincrement = tableDefinition.sql.toLowerCase().includes('autoincrement');

return columns.map(col => {
const mappedType = connection.getPlatform().getMappedType(col.type);
Expand All @@ -35,7 +39,7 @@ export class BetterSqliteSchemaHelper extends SchemaHelper {
primary: !!col.pk,
mappedType,
unsigned: false,
autoincrement: !composite && col.pk && this.platform.isNumericColumn(mappedType),
autoincrement: !composite && col.pk && this.platform.isNumericColumn(mappedType) && hasAutoincrement,
};
});
}
Expand Down
6 changes: 5 additions & 1 deletion packages/sqlite/src/SqliteSchemaHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class SqliteSchemaHelper extends SchemaHelper {

async getColumns(connection: AbstractSqlConnection, tableName: string, schemaName?: string): Promise<any[]> {
const columns = await connection.execute<any[]>(`pragma table_info('${tableName}')`);
const sql = `select sql from sqlite_master where type = ? and name = ?`;
const tableDefinition = await connection.execute<{ sql: string }>(sql, ['table', tableName], 'get');
const composite = columns.reduce((count, col) => count + (col.pk ? 1 : 0), 0) > 1;
// there can be only one, so naive check like this should be enough
const hasAutoincrement = tableDefinition.sql.toLowerCase().includes('autoincrement');

return columns.map(col => {
const mappedType = connection.getPlatform().getMappedType(col.type);
Expand All @@ -35,7 +39,7 @@ export class SqliteSchemaHelper extends SchemaHelper {
primary: !!col.pk,
mappedType,
unsigned: false,
autoincrement: !composite && col.pk && this.platform.isNumericColumn(mappedType),
autoincrement: !composite && col.pk && this.platform.isNumericColumn(mappedType) && hasAutoincrement,
};
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`dropping tables with FKs in postgres schema generator removes stale FKs on target table dropping 1 1`] = `
"alter table \`sequence\` add column \`another_property\` text null;
"
`;
101 changes: 101 additions & 0 deletions tests/features/schema-generator/fk-diffing.sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Entity, ManyToOne, MikroORM, PrimaryKey, Property } from '@mikro-orm/core';

@Entity()
export class Door {

@PrimaryKey({ autoincrement: false })
id!: number;

@Property()
label!: string;

@Property({ default: true })
isEnabled!: boolean;

@Property()
state!: 'open' | 'closed';

}

@Entity({ tableName: 'sequence' })
export class Sequence0 {

@PrimaryKey()
id!: number;

@Property()
index!: number;

@Property()
action!: 'on' | 'off' | 'low' | 'high';

@Property()
target!:
| 'relay1'
| 'relay2'
| 'relay3'
| 'digitalOutput1'
| 'digitalOutput2'
| 'digitalOutput3';

@Property()
duration!: number;

@ManyToOne(() => Door)
door!: Door;

}

@Entity({ tableName: 'sequence' })
export class Sequence1 {

@PrimaryKey()
id!: number;

@Property()
index!: number;

@Property()
action!: 'on' | 'off' | 'low' | 'high';

@Property({ nullable: true })
anotherProperty?: string;

@Property()
target!:
| 'relay1'
| 'relay2'
| 'relay3'
| 'digitalOutput1'
| 'digitalOutput2'
| 'digitalOutput3';

@Property()
duration!: number;

@ManyToOne(() => Door)
door!: Door;

}

describe('dropping tables with FKs in postgres', () => {

test('schema generator removes stale FKs on target table dropping 1', async () => {
const orm = await MikroORM.init({
entities: [Sequence0, Door],
dbName: `:memory:`,
type: 'better-sqlite',
cache: { enabled: false },
});
await orm.getSchemaGenerator().refreshDatabase();

orm.getMetadata().reset('Sequence0');
await orm.discoverEntity([Sequence1]);
const diff1 = await orm.getSchemaGenerator().getUpdateSchemaSQL({ wrap: false });
expect(diff1).toMatchSnapshot();
await orm.getSchemaGenerator().execute(diff1);

await orm.close(true);
});

});

0 comments on commit b39b6ad

Please sign in to comment.