Skip to content

Commit

Permalink
fix: sqlite temporary tables now honor withoutRowid (#8431)
Browse files Browse the repository at this point in the history
* fix: sqlite temporary tables now honor withoutRowid

Closes: #8430

* merge master

* fixed `withoutRowid` on table recreation;
added test;

Co-authored-by: Thrasher <thrasher@grayshift.com>
Co-authored-by: Alex Messer <dmzt08@gmail.com>
  • Loading branch information
3 people committed Aug 25, 2022
1 parent c4f4650 commit b8d04dc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,10 +1332,12 @@ export abstract class AbstractSqliteQueryRunner
dbTable["database"],
)}.${dbTable["name"]}`
: dbTable["name"]
const table = new Table({ name: tablePath })

const sql = dbTable["sql"]

const withoutRowid = sql.includes("WITHOUT ROWID")
const table = new Table({ name: tablePath, withoutRowid })

// load columns and indices
const [dbColumns, dbIndices, dbForeignKeys]: ObjectLiteral[][] =
await Promise.all([
Expand Down Expand Up @@ -1828,11 +1830,7 @@ export abstract class AbstractSqliteQueryRunner

sql += `)`

const tableMetadata = this.connection.entityMetadatas.find(
(metadata) =>
this.getTablePath(table) === this.getTablePath(metadata),
)
if (tableMetadata && tableMetadata.withoutRowid) {
if (table.withoutRowid) {
sql += " WITHOUT ROWID"
}

Expand Down
5 changes: 5 additions & 0 deletions src/schema-builder/options/TableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export interface TableOptions {
*/
justCreated?: boolean

/**
* Enables Sqlite "WITHOUT ROWID" modifier for the "CREATE TABLE" statement
*/
withoutRowid?: boolean

/**
* Table engine.
*/
Expand Down
11 changes: 9 additions & 2 deletions src/schema-builder/table/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export class Table {
*/
justCreated: boolean = false

/**
* Enables Sqlite "WITHOUT ROWID" modifier for the "CREATE TABLE" statement
*/
withoutRowid?: boolean = false

/**
* Table engine.
*/
Expand All @@ -85,9 +90,7 @@ export class Table {
constructor(options?: TableOptions) {
if (options) {
this.database = options.database

this.schema = options.schema

this.name = options.name

if (options.columns)
Expand Down Expand Up @@ -131,6 +134,8 @@ export class Table {
if (options.justCreated !== undefined)
this.justCreated = options.justCreated

if (options.withoutRowid) this.withoutRowid = options.withoutRowid

this.engine = options.engine
}
}
Expand Down Expand Up @@ -164,6 +169,7 @@ export class Table {
checks: this.checks.map((constraint) => constraint.clone()),
exclusions: this.exclusions.map((constraint) => constraint.clone()),
justCreated: this.justCreated,
withoutRowid: this.withoutRowid,
engine: this.engine,
})
}
Expand Down Expand Up @@ -386,6 +392,7 @@ export class Table {
schema,
database,
),
withoutRowid: entityMetadata.withoutRowid,
engine: entityMetadata.engine,
columns: entityMetadata.columns
.filter((column) => column)
Expand Down
10 changes: 10 additions & 0 deletions test/github-issues/8430/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryColumn } from "../../../../src"

@Entity({ withoutRowid: true })
export class Post {
@PrimaryColumn()
id: number

@Column()
name: string
}
50 changes: 50 additions & 0 deletions test/github-issues/8430/issue-8430.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import "reflect-metadata"
import { DataSource } from "../../../src"
import {
reloadTestingDatabases,
createTestingConnections,
closeTestingConnections,
} from "../../utils/test-utils"
import { expect } from "chai"

describe("github issues > #8430 sqlite temporary tables do not honor withoutRowid", () => {
let connections: DataSource[] = []
before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["sqlite", "better-sqlite3"],
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

// -------------------------------------------------------------------------
// Specifications
// -------------------------------------------------------------------------

it("should keep 'withoutRowid' after table recreation", () =>
Promise.all(
connections.map(async (connection) => {
const queryRunner = connection.createQueryRunner()
const table = await queryRunner.getTable("post")

expect(table!.withoutRowid).to.be.true

let nameColumn = table!.findColumnByName("name")!
const changedColumn = nameColumn.clone()
changedColumn.name = "changedName"

await queryRunner.changeColumn(
table!,
nameColumn,
changedColumn,
)

const changedTable = await queryRunner.getTable("post")
await queryRunner.release()

expect(changedTable!.withoutRowid).to.be.true
}),
))
})

0 comments on commit b8d04dc

Please sign in to comment.