-
Notifications
You must be signed in to change notification settings - Fork 27
/
postgres.py
31 lines (25 loc) · 1.02 KB
/
postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django.db import connection
class PostgresQL:
@staticmethod
def create_seq(seq_name, owned_by, min_value=0, start=1):
with connection.cursor() as cursor:
cursor.execute(
f"CREATE SEQUENCE IF NOT EXISTS {seq_name} MINVALUE {min_value} START {start} OWNED BY {owned_by};")
@staticmethod
def update_seq(seq_name, start):
with connection.cursor() as cursor:
cursor.execute(f"SELECT setval('{seq_name}', {start}, true);")
@staticmethod
def drop_seq(seq_name):
with connection.cursor() as cursor:
cursor.execute(f"DROP SEQUENCE IF EXISTS {seq_name};")
@staticmethod
def next_value(seq_name):
with connection.cursor() as cursor:
cursor.execute(f"SELECT nextval('{seq_name}');")
return cursor.fetchone()[0]
@staticmethod
def last_value(seq_name):
with connection.cursor() as cursor:
cursor.execute(f"SELECT last_value from {seq_name};")
return cursor.fetchone()[0]