From fe9f13db76d4e1993effad9e88c41e48d72bdf20 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Fri, 27 Feb 2015 01:59:13 +0100 Subject: [PATCH] WIP sequence put --- cliquet/storage/postgresql/__init__.py | 15 ++++++++++++--- cliquet/storage/postgresql/schema.sql | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cliquet/storage/postgresql/__init__.py b/cliquet/storage/postgresql/__init__.py index bd46a1f4..3c779f53 100644 --- a/cliquet/storage/postgresql/__init__.py +++ b/cliquet/storage/postgresql/__init__.py @@ -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;" + """ + cursor.execute(bump_serial, (max_id,)) record = record.copy() record[resource.id_field] = record_id diff --git a/cliquet/storage/postgresql/schema.sql b/cliquet/storage/postgresql/schema.sql index c382619f..f9850116 100644 --- a/cliquet/storage/postgresql/schema.sql +++ b/cliquet/storage/postgresql/schema.sql @@ -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,