Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

[WIP] Find a fix for PostgreSQL sequences after PUT forcing record id #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cliquet/storage/postgresql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,19 @@ def update(self, resource, user_id, record_id, record):
# Create or update ?
query = "SELECT id FROM records WHERE id = %s;"
cursor.execute(query, (record_id,))
query = query_update if cursor.rowcount > 0 else query_create
if cursor.rowcount > 0:
cursor.execute(query_update, placeholders)
result = cursor.fetchone()
else:
cursor.execute(query_create, placeholders)
result = cursor.fetchone()

cursor.execute(query, placeholders)
result = cursor.fetchone()
cursor.execute('SELECT MAX(id) AS max FROM records;')
max_id = cursor.fetchone()['max']
bump_serial = """
ALTER SEQUENCE records_id_seq RESTART WITH %s;"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to use UUID here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this was late yesterday night, I hate this and this does not work at all :)

"""
cursor.execute(bump_serial, (max_id,))

record = record.copy()
record[resource.id_field] = record_id
Expand Down
3 changes: 2 additions & 1 deletion cliquet/storage/postgresql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ CREATE CAST (TIMESTAMP AS BIGINT)
--
-- Actual records
--
CREATE SEQUENCE records_id_seq NO MAXVALUE NO CYCLE;
CREATE TABLE IF NOT EXISTS records (
id SERIAL PRIMARY KEY,
id INTEGER DEFAULT nextval('records_id_seq') PRIMARY KEY NOT NULL,
user_id VARCHAR(256) NOT NULL,
resource_name VARCHAR(256) NOT NULL,
last_modified TIMESTAMP NOT NULL,
Expand Down