Conversation
…e-instead-of-typeorm-2
…e-instead-of-typeorm-2
…ite-instead-of-typeorm-folders
…de-sqlite-instead-of-typeorm-folders-2
…node-sqlite-instead-typeorm-checkpoint
…ize-sqlite-tables
…ize-sqlite-tables
| CREATE TABLE | ||
| drive_folder_new ( | ||
| id INTEGER NOT NULL, | ||
| uuid VARCHAR(36) PRIMARY KEY NOT NULL, |
There was a problem hiding this comment.
VARCHAR2 doesn't exist in sqlite, but sonarcloud is still showing the warning.
| @@ -0,0 +1,73 @@ | |||
| /** | |||
There was a problem hiding this comment.
I've tested this with 3 different sqlites from users that had the issue with the UNIQUE field, so they have old databases and it's workign perfectly. I've added the 3 databases to the ticket acceptance criterias so that @ValentynaBlahodyrQA can do the check too.
|
| expect(nonSqlFiles).toStrictEqual(['run-migrations.test.ts', 'run-migrations.ts']); | ||
| sqlFiles.forEach((file, index) => { | ||
| const expectedPrefix = String(index + 1).padStart(3, '0'); | ||
| expect(file).toMatch(new RegExp(`^${expectedPrefix}_.*\\.sql$`)); |
There was a problem hiding this comment.
I would match the order the function itself applies the migrations by matching the calls to the db.exec and asserting that every nth call has received the file name in the order you would expect (sorted)
There was a problem hiding this comment.
Hey, I think I can't match against db.exec since it receives the content of the file, not the filename. However, I'm technically matching the order of applied migrations here:
it('should run all migrations successfully', () => {
// When
runMigrations();
// Then
expect({ ...db.prepare('SELECT * FROM migrations').all() }).toMatchObject({
'0': { id: 1, filename: '001_create_schema.sql', run_at: expect.any(String) },
'1': { id: 2, filename: '002_normalize_file_table.sql', run_at: expect.any(String) },
'2': { id: 3, filename: '003_normalize_folder_table.sql', run_at: expect.any(String) },
});
});

What
(Last PR related to
node:sqlite)Previously this project was using
typeormto manage the sqlite database, but we had some issues:typeormoption withsynchronize: true, which runs migrations automatically. However not all migrations were run automatically. If we remove theUNIQUEproperty of a field (like we did withfileId) it's not removed automatically from the database.synchronize: truejust runs some migrations like adding a column. This is an issue since we needed to know the internals oftypeormwhich is not usual knowledge.syncrhonize: trueoption and usingtypeormmigrations was not the best. The migrations were inside a typescript file being just a single line string, so it wasn't easy to spot issues or apply more customize migrations.Basically
typeormis high level orm that was removing "complexity" but we didn't have control about what was happening. So we decided to try different alternatives. First, the requirements. We needed something that was able to run migrations from code (since it's a desktop app and we cannot run them from cli) and also thas was able to run different sqlite drivers. For example,drizzleOrmonly allowsbetter_sqlite3, which is the fastest sqlite driver, but it's native so we needed to rebuild it everytime we wanted to run the app or running tests, which was throwing the development experience away.We found that no ORM was meeting our requirements so we decided to move to raw sql using
node:sqlite. Why?node:sqliteis nearly as fast asbetter_sqlite3but doesn't need to be compiled against Electron.The downsides are that
node:sqliteis still experimental (but enough stable for our use cases) and that we lose type inference (we are going to rely on tests so we don't break anything). Also, to have a better view of our current sqlite state and compare against the type definitions in this file, we have added the command:npm run schemawhich generates the
schema.sqlfile close to this one so we can compare that everything is right. Everytime we add a migration we will update theschema.sqlto see the real state of the database and not miss anything.