Skip to content

Commit

Permalink
feat(db-sqlite): add autoIncrement option (#9427)
Browse files Browse the repository at this point in the history
### What?
Exposes ability to enable
[AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for Primary Keys
which ensures that the same ID cannot be reused from previously deleted
rows.

```ts
sqliteAdapter({
  autoIncrement: true
})
```

### Why?
This may be essential for some systems. Enabled `autoIncrement: true`
also for the SQLite Adapter in our tests, which can be useful when
testing whether the doc was deleted or not when you also have other
create operations.

### How?
Uses Drizzle's `autoIncrement` option.

WARNING:
This cannot be enabled in an existing project without a custom
migration, as it completely changes how primary keys are stored in the
database.
  • Loading branch information
r1tsuu authored Dec 20, 2024
1 parent 99481cb commit 4e95353
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 31 deletions.
1 change: 1 addition & 0 deletions docs/database/sqlite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default buildConfig({
| `beforeSchemaInit` | Drizzle schema hook. Runs before the schema is built. [More Details](#beforeschemainit) |
| `afterSchemaInit` | Drizzle schema hook. Runs after the schema is built. [More Details](#afterschemainit) |
| `generateSchemaOutputFile` | Override generated schema from `payload generate:db-schema` file path. Defaults to `{CWD}/src/payload-generated.schema.ts` |
| `autoIncrement` | Pass `true` to enable SQLite [AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for primary keys to ensure the same ID cannot be reused from deleted rows |

## Access to Drizzle

Expand Down
8 changes: 7 additions & 1 deletion packages/db-sqlite/src/columnToCodeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ export const columnToCodeConverter: ColumnToCodeConverter = ({
}

if (column.primaryKey) {
code = `${code}.primaryKey()`
let arg = ''

if (column.type === 'integer' && column.autoIncrement) {
arg = `{ autoIncrement: true }`
}

code = `${code}.primaryKey(${arg})`
}

if (defaultStatement) {
Expand Down
1 change: 1 addition & 0 deletions packages/db-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
return createDatabaseAdapter<SQLiteAdapter>({
name: 'sqlite',
afterSchemaInit: args.afterSchemaInit ?? [],
autoIncrement: args.autoIncrement ?? false,
beforeSchemaInit: args.beforeSchemaInit ?? [],
client: undefined,
clientConfig: args.client,
Expand Down
8 changes: 7 additions & 1 deletion packages/db-sqlite/src/schema/buildDrizzleTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ export const buildDrizzleTable: BuildDrizzleTable = ({ adapter, locales, rawTabl
}

if (column.primaryKey) {
columns[key].primaryKey()
let args: Record<string, unknown> | undefined = undefined

if (column.type === 'integer' && column.autoIncrement) {
args = { autoIncrement: true }
}

columns[key].primaryKey(args)
}

if (column.notNull) {
Expand Down
3 changes: 3 additions & 0 deletions packages/db-sqlite/src/schema/setColumnID.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SetColumnID } from '@payloadcms/drizzle/types'

import type { SQLiteAdapter } from '../types.js'

export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
const idField = fields.find((field) => field.name === 'id')
if (idField) {
Expand Down Expand Up @@ -36,6 +38,7 @@ export const setColumnID: SetColumnID = ({ adapter, columns, fields }) => {
columns.id = {
name: 'id',
type: 'integer',
autoIncrement: (adapter as unknown as SQLiteAdapter).autoIncrement,
primaryKey: true,
}

Expand Down
6 changes: 6 additions & 0 deletions packages/db-sqlite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export type Args = {
* Examples may include: composite indices, generated columns, vectors
*/
afterSchemaInit?: SQLiteSchemaHook[]
/**
* Enable [AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for Primary Keys.
* This ensures that the same ID cannot be reused from previously deleted rows.
*/
autoIncrement?: boolean
/**
* Transform the schema before it's built.
* You can use it to preserve an existing database schema and if there are any collissions Payload will override them.
Expand Down Expand Up @@ -124,6 +129,7 @@ type Drizzle = { $client: Client } & LibSQLDatabase<ResolveSchemaType<GeneratedD

export type SQLiteAdapter = {
afterSchemaInit: SQLiteSchemaHook[]
autoIncrement: boolean
beforeSchemaInit: SQLiteSchemaHook[]
client: Client
clientConfig: Args['client']
Expand Down
12 changes: 11 additions & 1 deletion packages/drizzle/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,21 @@ export type EnumRawColumn = (
) &
BaseRawColumn

export type IntegerRawColumn = {
/**
* SQLite only.
* Enable [AUTOINCREMENT](https://www.sqlite.org/autoinc.html) for primary key to ensure that the same ID cannot be reused from previously deleted rows.
*/
autoIncrement?: boolean
type: 'integer'
} & BaseRawColumn

export type RawColumn =
| ({
type: 'boolean' | 'geometry' | 'integer' | 'jsonb' | 'numeric' | 'serial' | 'text' | 'varchar'
type: 'boolean' | 'geometry' | 'jsonb' | 'numeric' | 'serial' | 'text' | 'varchar'
} & BaseRawColumn)
| EnumRawColumn
| IntegerRawColumn
| TimestampRawColumn
| UUIDRawColumn

Expand Down
7 changes: 7 additions & 0 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,4 +1174,11 @@ describe('database', () => {
// Should stay the same ID
expect(postShouldCreated.id).toBe(postShouldUpdated.id)
})

it('should enforce unique ids on db level even after delete', async () => {
const { id } = await payload.create({ collection: 'posts', data: { title: 'ASD' } })
await payload.delete({ id, collection: 'posts' })
const { id: id_2 } = await payload.create({ collection: 'posts', data: { title: 'ASD' } })
expect(id_2).not.toBe(id)
})
})
56 changes: 28 additions & 28 deletions test/database/payload-generated-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { sql, relations } from '@payloadcms/db-sqlite/drizzle'
export const posts = sqliteTable(
'posts',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
title: text('title').notNull(),
hasTransaction: integer('has_transaction', { mode: 'boolean' }),
throwAfterChange: integer('throw_after_change', { mode: 'boolean' }).default(false),
Expand Down Expand Up @@ -59,12 +59,12 @@ export const default_values_array = sqliteTable(
export const default_values = sqliteTable(
'default_values',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
title: text('title'),
defaultValue: text('default_value').default('default value from database'),
group_defaultValue: text('group_default_value').default('default value from database'),
select: text('select', { enum: ['option0', 'option1', 'default'] }).default('default'),
point: text('point', { mode: 'json' }).default('"SRID=4326;POINT(10 20)"'),
point: text('point', { mode: 'json' }).default('[10,20]'),
updatedAt: text('updated_at')
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
Expand All @@ -81,7 +81,7 @@ export const default_values = sqliteTable(
export const relation_a = sqliteTable(
'relation_a',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
title: text('title'),
richText: text('rich_text', { mode: 'json' }),
updatedAt: text('updated_at')
Expand All @@ -100,7 +100,7 @@ export const relation_a = sqliteTable(
export const relation_b = sqliteTable(
'relation_b',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
title: text('title'),
relationship: integer('relationship_id').references(() => relation_a.id, {
onDelete: 'set null',
Expand Down Expand Up @@ -243,7 +243,7 @@ export const pg_migrations_blocks_my_block_locales = sqliteTable(
export const pg_migrations = sqliteTable(
'pg_migrations',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
relation1: integer('relation1_id').references(() => relation_a.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -292,7 +292,7 @@ export const _pg_migrations_v_version_my_array_my_sub_array = sqliteTable(
{
_order: integer('_order').notNull(),
_parentID: integer('_parent_id').notNull(),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
_uuid: text('_uuid'),
},
(columns) => ({
Expand Down Expand Up @@ -338,7 +338,7 @@ export const _pg_migrations_v_version_my_array = sqliteTable(
{
_order: integer('_order').notNull(),
_parentID: integer('_parent_id').notNull(),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
relation2: integer('relation2_id').references(() => relation_b.id, {
onDelete: 'set null',
}),
Expand All @@ -364,7 +364,7 @@ export const _pg_migrations_v_blocks_my_block = sqliteTable(
_order: integer('_order').notNull(),
_parentID: integer('_parent_id').notNull(),
_path: text('_path').notNull(),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
relation5: integer('relation5_id').references(() => relation_a.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -414,7 +414,7 @@ export const _pg_migrations_v_blocks_my_block_locales = sqliteTable(
export const _pg_migrations_v = sqliteTable(
'_pg_migrations_v',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
parent: integer('parent_id').references(() => pg_migrations.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -485,7 +485,7 @@ export const customs_customSelect = sqliteTable(
order: integer('order').notNull(),
parent: integer('parent_id').notNull(),
value: text('value', { enum: ['a', 'b', 'c'] }),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
},
(columns) => ({
orderIdx: index('customs_customSelect_order_idx').on(columns.order),
Expand Down Expand Up @@ -584,7 +584,7 @@ export const customBlocks_locales = sqliteTable(
export const customs = sqliteTable(
'customs',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
text: text('text'),
radio: text('radio', { enum: ['a', 'b', 'c'] }),
updatedAt: text('updated_at')
Expand Down Expand Up @@ -658,7 +658,7 @@ export const __customs_v_version_customSelect_v = sqliteTable(
order: integer('order').notNull(),
parent: integer('parent_id').notNull(),
value: text('value', { enum: ['a', 'b', 'c'] }),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
},
(columns) => ({
orderIdx: index('__customs_v_version_customSelect_v_order_idx').on(columns.order),
Expand All @@ -676,7 +676,7 @@ export const _customArrays_v = sqliteTable(
{
_order: integer('_order').notNull(),
_parentID: integer('_parent_id').notNull(),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
text: text('text'),
_uuid: text('_uuid'),
},
Expand Down Expand Up @@ -718,7 +718,7 @@ export const _customBlocks_v = sqliteTable(
_order: integer('_order').notNull(),
_parentID: integer('_parent_id').notNull(),
_path: text('_path').notNull(),
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
text: text('text'),
_uuid: text('_uuid'),
blockName: text('block_name'),
Expand Down Expand Up @@ -759,7 +759,7 @@ export const _customBlocks_v_locales = sqliteTable(
export const _customs_v = sqliteTable(
'_customs_v',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
parent: integer('parent_id').references(() => customs.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -856,7 +856,7 @@ export const _customs_v_rels = sqliteTable(
export const places = sqliteTable(
'places',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
country: text('country'),
city: text('city'),
updatedAt: text('updated_at')
Expand All @@ -875,7 +875,7 @@ export const places = sqliteTable(
export const fields_persistance = sqliteTable(
'fields_persistance',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
updatedAt: text('updated_at')
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
Expand Down Expand Up @@ -916,7 +916,7 @@ export const custom_ids = sqliteTable(
export const _custom_ids_v = sqliteTable(
'_custom_ids_v',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
parent: text('parent_id').references(() => custom_ids.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -962,7 +962,7 @@ export const _custom_ids_v = sqliteTable(
export const fake_custom_ids = sqliteTable(
'fake_custom_ids',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
title: text('title'),
updatedAt: text('updated_at')
.notNull()
Expand All @@ -980,7 +980,7 @@ export const fake_custom_ids = sqliteTable(
export const relationships_migration = sqliteTable(
'relationships_migration',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
relationship: integer('relationship_id').references(() => default_values.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -1036,7 +1036,7 @@ export const relationships_migration_rels = sqliteTable(
export const _relationships_migration_v = sqliteTable(
'_relationships_migration_v',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
parent: integer('parent_id').references(() => relationships_migration.id, {
onDelete: 'set null',
}),
Expand Down Expand Up @@ -1110,7 +1110,7 @@ export const _relationships_migration_v_rels = sqliteTable(
export const users = sqliteTable(
'users',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
updatedAt: text('updated_at')
.notNull()
.default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
Expand All @@ -1137,7 +1137,7 @@ export const users = sqliteTable(
export const payload_locked_documents = sqliteTable(
'payload_locked_documents',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
globalSlug: text('global_slug'),
updatedAt: text('updated_at')
.notNull()
Expand Down Expand Up @@ -1290,7 +1290,7 @@ export const payload_locked_documents_rels = sqliteTable(
export const payload_preferences = sqliteTable(
'payload_preferences',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
key: text('key'),
value: text('value', { mode: 'json' }),
updatedAt: text('updated_at')
Expand Down Expand Up @@ -1343,7 +1343,7 @@ export const payload_preferences_rels = sqliteTable(
export const payload_migrations = sqliteTable(
'payload_migrations',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name'),
batch: numeric('batch'),
updatedAt: text('updated_at')
Expand All @@ -1364,7 +1364,7 @@ export const payload_migrations = sqliteTable(
)

export const customGlobal = sqliteTable('customGlobal', {
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
text: text('text'),
updatedAt: text('updated_at').default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
createdAt: text('created_at').default(sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`),
Expand All @@ -1373,7 +1373,7 @@ export const customGlobal = sqliteTable('customGlobal', {
export const _customGlobal_v = sqliteTable(
'_customGlobal_v',
{
id: integer('id').primaryKey(),
id: integer('id').primaryKey({ autoIncrement: true }),
version_text: text('version_text'),
version_updatedAt: text('version_updated_at').default(
sql`(strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`,
Expand Down
1 change: 1 addition & 0 deletions test/generateDatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const allDatabaseAdapters = {
client: {
url: process.env.SQLITE_URL || 'file:./payloadtests.db',
},
autoIncrement: true
})`,
'sqlite-uuid': `
import { sqliteAdapter } from '@payloadcms/db-sqlite'
Expand Down

0 comments on commit 4e95353

Please sign in to comment.