Skip to content

SQLite, sqljs migration issues with scale & precision #6636

@michaelbromley

Description

@michaelbromley

Issue type:

[ x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ x ] sqlite
[ x ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Given the following entity:

@Entity()
export class Test {

    @PrimaryColumn()
    id: number;

    @Column({ nullable: true, precision: 6 })
    startedAt?: Date;

    @Column({ type: 'decimal', precision: 5, scale: 2 })
    value: number;

}

Running a migration on a newly-synced SQLite/sql.js DB results in the following uneccessary migration commands:

[
     {
        query: 'CREATE TABLE "temporary_test" ("id" integer PRIMARY KEY NOT NULL, "startedAt" datetime(6), "value" decimal(5,2) NOT NULL)',
        parameters: undefined
    },
    {
        query: 'INSERT INTO "temporary_test"("id", "startedAt", "value") SELECT "id", "startedAt", "value" FROM "test"',
        parameters: undefined
    },
    {query: 'DROP TABLE "test"', parameters: undefined},
    {
        query: 'ALTER TABLE "temporary_test" RENAME TO "test"',
        parameters: undefined
    }
]

This is caused by a false positive in the findChangedColumns() method:

|| tableColumn.precision !== columnMetadata.precision
|| tableColumn.scale !== columnMetadata.scale

because the precision and scale arguments are not correctly inferred when reading the table. Then length property, however, is correctly inferred:

let pos = tableColumn.type.indexOf("(");
if (pos !== -1) {
let dataType = tableColumn.type.substr(0, pos);
if (!!this.driver.withLengthColumnTypes.find(col => col === dataType)) {
let len = parseInt(tableColumn.type.substring(pos + 1, tableColumn.type.length - 1));
if (len) {
tableColumn.length = len.toString();
tableColumn.type = dataType; // remove the length part from the datatype
}
}
}

All that is missing is to do the same for precision & scale.

I intend to submit a PR shortly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions