Skip to content

Commit 4ce3316

Browse files
authored
Merge pull request #33 from eyadmba/main
Include schema name when checking for the existence of a hypertable
2 parents 5cb4f04 + 43ac06b commit 4ce3316

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

timescale/db/backends/postgis/schema.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55

66
class TimescaleSchemaEditor(PostGISSchemaEditor):
7-
sql_is_hypertable = 'SELECT * FROM timescaledb_information.hypertables WHERE hypertable_name = {table}'
7+
sql_is_hypertable = '''SELECT * FROM timescaledb_information.hypertables
8+
WHERE hypertable_name = {table}{extra_condition}'''
89

910
sql_assert_is_hypertable = (
1011
'DO $do$ BEGIN '
@@ -38,14 +39,20 @@ class TimescaleSchemaEditor(PostGISSchemaEditor):
3839

3940
sql_set_chunk_time_interval = 'SELECT set_chunk_time_interval({table}, interval {interval})'
4041

42+
sql_hypertable_is_in_schema = '''hypertable_schema = {schema_name}'''
43+
4144
def _assert_is_hypertable(self, model):
4245
"""
4346
Assert if the table is a hyper table
4447
"""
4548
table = self.quote_value(model._meta.db_table)
4649
error_message = self.quote_value("assert failed - " + table + " should be a hyper table")
4750

48-
sql = self.sql_assert_is_hypertable.format(table=table, error_message=error_message)
51+
extra_condition = self._get_extra_condition()
52+
53+
sql = self.sql_assert_is_hypertable.format(table=table, error_message=error_message,
54+
extra_condition=extra_condition)
55+
4956
self.execute(sql)
5057

5158
def _assert_is_not_hypertable(self, model):
@@ -55,7 +62,11 @@ def _assert_is_not_hypertable(self, model):
5562
table = self.quote_value(model._meta.db_table)
5663
error_message = self.quote_value("assert failed - " + table + " should not be a hyper table")
5764

58-
sql = self.sql_assert_is_not_hypertable.format(table=table, error_message=error_message)
65+
extra_condition = self._get_extra_condition()
66+
67+
sql = self.sql_assert_is_not_hypertable.format(table=table, error_message=error_message,
68+
extra_condition=extra_condition)
69+
5970
self.execute(sql)
6071

6172
def _drop_primary_key(self, model):
@@ -139,3 +150,15 @@ def alter_field(self, model, old_field, new_field, strict=False):
139150
and old_field.interval != new_field.interval:
140151
# change chunk-size
141152
self._set_chunk_time_interval(model, new_field)
153+
154+
def _get_extra_condition(self):
155+
extra_condition = ''
156+
157+
try:
158+
if self.connection.schema_name:
159+
schema_name = self.quote_value(self.connection.schema_name)
160+
extra_condition = ' AND ' + self.sql_hypertable_is_in_schema.format(schema_name=schema_name)
161+
except:
162+
pass
163+
164+
return extra_condition

timescale/db/backends/postgresql/schema.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
class TimescaleSchemaEditor(DatabaseSchemaEditor):
8-
sql_is_hypertable = 'SELECT * FROM timescaledb_information.hypertables WHERE hypertable_name = {table}'
8+
sql_is_hypertable = '''SELECT * FROM timescaledb_information.hypertables
9+
WHERE hypertable_name = {table}{extra_condition}'''
910

1011
sql_assert_is_hypertable = (
1112
'DO $do$ BEGIN '
@@ -39,14 +40,20 @@ class TimescaleSchemaEditor(DatabaseSchemaEditor):
3940

4041
sql_set_chunk_time_interval = 'SELECT set_chunk_time_interval({table}, interval {interval})'
4142

43+
sql_hypertable_is_in_schema = '''hypertable_schema = {schema_name}'''
44+
4245
def _assert_is_hypertable(self, model):
4346
"""
4447
Assert if the table is a hyper table
4548
"""
4649
table = self.quote_value(model._meta.db_table)
4750
error_message = self.quote_value("assert failed - " + table + " should be a hyper table")
4851

49-
sql = self.sql_assert_is_hypertable.format(table=table, error_message=error_message)
52+
extra_condition = self._get_extra_condition()
53+
54+
sql = self.sql_assert_is_hypertable.format(table=table, error_message=error_message,
55+
extra_condition=extra_condition)
56+
5057
self.execute(sql)
5158

5259
def _assert_is_not_hypertable(self, model):
@@ -56,7 +63,11 @@ def _assert_is_not_hypertable(self, model):
5663
table = self.quote_value(model._meta.db_table)
5764
error_message = self.quote_value("assert failed - " + table + " should not be a hyper table")
5865

59-
sql = self.sql_assert_is_not_hypertable.format(table=table, error_message=error_message)
66+
extra_condition = self._get_extra_condition()
67+
68+
sql = self.sql_assert_is_not_hypertable.format(table=table, error_message=error_message,
69+
extra_condition=extra_condition)
70+
6071
self.execute(sql)
6172

6273
def _drop_primary_key(self, model):
@@ -140,3 +151,15 @@ def alter_field(self, model, old_field, new_field, strict=False):
140151
and old_field.interval != new_field.interval:
141152
# change chunk-size
142153
self._set_chunk_time_interval(model, new_field)
154+
155+
def _get_extra_condition(self):
156+
extra_condition = ''
157+
158+
try:
159+
if self.connection.schema_name:
160+
schema_name = self.quote_value(self.connection.schema_name)
161+
extra_condition = ' AND ' + self.sql_hypertable_is_in_schema.format(schema_name=schema_name)
162+
except:
163+
pass
164+
165+
return extra_condition

0 commit comments

Comments
 (0)