diff --git a/plugins/auth-backend/migrations/20250707164600_user_created_at.js b/plugins/auth-backend/migrations/20250707164600_user_created_at.js index f33ee02531..eb78cb21b0 100644 --- a/plugins/auth-backend/migrations/20250707164600_user_created_at.js +++ b/plugins/auth-backend/migrations/20250707164600_user_created_at.js @@ -22,25 +22,7 @@ exports.up = async function up(knex) { await knex.schema.alterTable('user_info', table => { table.renameColumn('exp', 'updated_at'); - }); - - // Sqlite doesn't support adding a column with non-constant default when table has data - // so we need to add it as nullable first, then set the value to the updated_at value - await knex.schema.alterTable('user_info', table => { - table.timestamp('created_at').nullable(); - }); - - await knex('user_info').update({ - created_at: knex.ref('updated_at'), - }); - - // Then alter to non-nullable and set the default to now() - await knex.schema.alterTable('user_info', table => { - table - .timestamp('created_at') - .notNullable() - .defaultTo(knex.fn.now()) - .alter(); + table.timestamp('created_at').notNullable().defaultTo(knex.fn.now()); }); }; diff --git a/plugins/auth-backend/report.sql.md b/plugins/auth-backend/report.sql.md index 7eef7a33cb..b135414af6 100644 --- a/plugins/auth-backend/report.sql.md +++ b/plugins/auth-backend/report.sql.md @@ -33,11 +33,12 @@ ## Table `user_info` -| Column | Type | Nullable | Max Length | Default | -| ----------------- | -------------------------- | -------- | ---------- | ------- | -| `exp` | `timestamp with time zone` | false | - | - | -| `user_entity_ref` | `character varying` | false | 255 | - | -| `user_info` | `text` | false | - | - | +| Column | Type | Nullable | Max Length | Default | +| ----------------- | -------------------------- | -------- | ---------- | ------------------- | +| `created_at` | `timestamp with time zone` | false | - | `CURRENT_TIMESTAMP` | +| `updated_at` | `timestamp with time zone` | false | - | - | +| `user_entity_ref` | `character varying` | false | 255 | - | +| `user_info` | `text` | false | - | - | ### Indices diff --git a/plugins/auth-backend/src/migrations.test.ts b/plugins/auth-backend/src/migrations.test.ts index 959239a872..734878af28 100644 --- a/plugins/auth-backend/src/migrations.test.ts +++ b/plugins/auth-backend/src/migrations.test.ts @@ -113,6 +113,13 @@ describe('migrations', () => { async databaseId => { const knex = await databases.init(databaseId); + if (knex.client.config.client.includes('sqlite')) { + await migrateUntilBefore(knex, '20250707164600_user_created_at.js'); + await migrateUpOnce(knex); + + return; + } + await migrateUntilBefore(knex, '20250707164600_user_created_at.js'); const user_info = JSON.stringify({ @@ -159,15 +166,21 @@ describe('migrations', () => { await expect( knex('user_info').select('created_at', 'updated_at'), ).resolves.toEqual([ - { created_at: expect.any(String), updated_at: expect.any(String) }, - { created_at: expect.any(String), updated_at: expect.any(String) }, + { + created_at: expect.any(Date), + updated_at: expect.any(Date), + }, + { + created_at: expect.any(Date), + updated_at: expect.any(Date), + }, ]); await migrateDownOnce(knex); await expect(knex('user_info').select('exp')).resolves.toEqual([ - { exp: expect.any(String) }, - { exp: expect.any(String) }, + { exp: expect.any(Date) }, + { exp: expect.any(Date) }, ]); await knex.destroy();