Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't externalize access method if it is btree, but allow it on input.

 * docs/{dbtoyaml.rst,table.rst}: Remove access_method btree from
   examples.
 * pyrseas/dbobject/constraint.py (PrimaryKey.to_map,
   UniqueConstraint.to_map): Delete access_method if btree.
 * pyrseas/dbobject/index.py (Index.to_map): Delete access_method if
   btree.  (Index.create): Don't output USING clause if access method
   is btree.  (IndexDict.from_map): If input doesn't include
   access_method, add it as btree to ease comparisons.
 * tests/dbobject/{test_constraint.py, test_index.py,
   test_tablespace.py}: Remove most access_method btree or USING btree
   instances.
 * tests/dbobject/test_index.py: Remove duplicate test
   (test_create_index_col_opts)
  • Loading branch information...
commit eb09df3c12ff96303cb6d70d4dcb8e1ad8516b5d 1 parent 77039da
Joe Abbate jmafc authored
2  docs/dbtoyaml.rst
View
@@ -43,7 +43,6 @@ The output format is as follows::
type: text
primary_key:
t1_pkey:
- access_method: btree
columns:
- c1
foreign_keys:
@@ -65,7 +64,6 @@ The output format is as follows::
type: character varying(16)
primary_key:
t2_pkey:
- access_method: btree
columns:
- c21
2  docs/table.rst
View
@@ -78,7 +78,7 @@ The map returned by :meth:`to_map` and expected as argument by
'description': "this is the comment for table t1",
'primary_key':
{'t1_prim_key':
- {'columns': ['c1', 'c2'], 'access_method': 'btree'}
+ {'columns': ['c1', 'c2']}
},
'foreign_keys':
{'t1_fgn_key1':
4 pyrseas/dbobject/constraint.py
View
@@ -129,6 +129,8 @@ def to_map(self, dbcols):
:return: dictionary
"""
dct = self._base_map()
+ if dct['access_method'] == 'btree':
+ del dct['access_method']
del dct['_table']
dct['columns'] = [dbcols[k - 1] for k in self.keycols]
del dct['keycols']
@@ -232,6 +234,8 @@ def to_map(self, dbcols):
:return: dictionary
"""
dct = self._base_map()
+ if dct['access_method'] == 'btree':
+ del dct['access_method']
del dct['_table']
dct['columns'] = []
dct['columns'] = [dbcols[k - 1] for k in self.keycols]
9 pyrseas/dbobject/index.py
View
@@ -45,6 +45,8 @@ def to_map(self):
:return: dictionary
"""
dct = self._base_map()
+ if dct['access_method'] == 'btree':
+ del dct['access_method']
return {self.name: dct}
def create(self):
@@ -57,8 +59,9 @@ def create(self):
if pth:
stmts.append(pth)
unq = hasattr(self, 'unique') and self.unique
- acc = hasattr(self, 'access_method') \
- and 'USING %s ' % self.access_method or ''
+ acc = ''
+ if hasattr(self, 'access_method') and self.access_method != 'btree':
+ acc = 'USING %s ' % self.access_method
tblspc = ''
if hasattr(self, 'tablespace'):
tblspc = '\n TABLESPACE %s' % self.tablespace
@@ -224,6 +227,8 @@ def from_map(self, table, inindexes):
for attr in ['access_method', 'unique', 'tablespace']:
if attr in val:
setattr(idx, attr, val[attr])
+ if not hasattr(idx, 'access_method'):
+ idx.access_method = 'btree'
if not hasattr(idx, 'unique'):
idx.unique = False
if 'description' in val:
53 tests/dbobject/test_constraint.py
View
@@ -79,19 +79,16 @@ class PrimaryKeyToMapTestCase(DatabaseToMapTestCase):
map_pkey1 = {'columns': [{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'text'}}],
- 'primary_key': {'t1_pkey': {'columns': ['c1'],
- 'access_method': 'btree'}}}
+ 'primary_key': {'t1_pkey': {'columns': ['c1']}}}
map_pkey2 = {'columns': [
{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'character(5)', 'not_null': True}},
{'c3': {'type': 'text'}}],
- 'primary_key': {'t1_pkey': {'columns': ['c2', 'c1'],
- 'access_method': 'btree'}}}
+ 'primary_key': {'t1_pkey': {'columns': ['c2', 'c1']}}}
map_pkey3 = {'columns': [{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'text'}}],
- 'primary_key': {'t1_prim_key': {'columns': ['c1'],
- 'access_method': 'btree'}}}
+ 'primary_key': {'t1_prim_key': {'columns': ['c1']}}}
def test_primary_key_1(self):
"Map a table with a single-column primary key"
@@ -145,8 +142,7 @@ def test_create_with_primary_key(self):
'columns': [{'c1': {'type': 'text'}},
{'c2': {'type': 'integer'}}],
'primary_key': {'t1_pkey': {
- 'columns': ['c2'],
- 'access_method': 'btree'}}}})
+ 'columns': ['c2']}}}})
sql = self.to_sql(inmap)
self.assertEqual(fix_indent(sql[0]),
"CREATE TABLE t1 (c1 text, c2 integer)")
@@ -165,8 +161,7 @@ def test_add_primary_key(self):
{'c2': {'type': 'integer', 'not_null': True}},
{'c3': {'type': 'text'}}],
'primary_key': {'t1_pkey': {
- 'columns': ['c1', 'c2'],
- 'access_method': 'btree'}}}})
+ 'columns': ['c1', 'c2']}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(fix_indent(sql[0]),
"ALTER TABLE t1 ADD CONSTRAINT t1_pkey "
@@ -220,8 +215,7 @@ class ForeignKeyToMapTestCase(DatabaseToMapTestCase):
{'c3': {'type': 'integer'}},
{'c4': {'type': 'date'}},
{'c5': {'type': 'text'}}],
- 'primary_key': {'t1_prim_key': {'columns': ['c1', 'c2'],
- 'access_method': 'btree'}},
+ 'primary_key': {'t1_prim_key': {'columns': ['c1', 'c2']}},
'foreign_keys': {'t1_fgn_key1': {
'columns': ['c2', 'c3', 'c4'],
'references': {'schema': 'public', 'table': 't2',
@@ -310,15 +304,12 @@ def test_cross_schema_foreign_key(self):
dbmap = self.to_map(stmts)
t2map = {'columns': [{'pc1': {'type': 'integer', 'not_null': True}},
{'pc2': {'type': 'text'}}],
- 'primary_key': {'t2_pkey': {
- 'columns': ['pc1'], 'access_method': 'btree'}}}
+ 'primary_key': {'t2_pkey': {'columns': ['pc1']}}}
t1map = {'table t1': {
'columns': [{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'integer'}},
{'c3': {'type': 'text'}}],
- 'primary_key': {'t1_pkey': {
- 'columns': ['c1'],
- 'access_method': 'btree'}},
+ 'primary_key': {'t1_pkey': {'columns': ['c1']}},
'foreign_keys': {'t1_c2_fkey': {
'columns': ['c2'],
'references': {'schema': 'public', 'table': 't2',
@@ -335,8 +326,7 @@ def test_multiple_foreign_key(self):
dbmap = self.to_map(stmts)
t1map = {'columns': [{'pc1': {'type': 'integer', 'not_null': True}},
{'pc2': {'type': 'text'}}],
- 'primary_key': {'t1_pkey': {
- 'columns': ['pc1'], 'access_method': 'btree'}}}
+ 'primary_key': {'t1_pkey': {'columns': ['pc1']}}}
t2map = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'integer'}},
{'c3': {'type': 'text'}},
@@ -509,16 +499,14 @@ def test_add_foreign_key(self):
{'c12': {'type': 'integer', 'not_null': True}},
{'c13': {'type': 'text'}}],
'primary_key': {'t1_pkey': {
- 'columns': ['c11', 'c12'],
- 'access_method': 'btree'}}},
+ 'columns': ['c11', 'c12']}}},
'table t2': {'columns': [
{'c21': {'type': 'integer', 'not_null': True}},
{'c22': {'type': 'text'}},
{'c23': {'type': 'integer'}},
{'c24': {'type': 'integer'}}],
'primary_key': {'t2_pkey': {
- 'columns': ['c21'],
- 'access_method': 'btree'}},
+ 'columns': ['c21']}},
'foreign_keys': {'t2_c23_fkey': {
'columns': ['c23', 'c24'],
'references': {'columns': ['c11', 'c12'],
@@ -597,19 +585,18 @@ class UniqueConstraintToMapTestCase(DatabaseToMapTestCase):
map_unique1 = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
- 'unique_constraints': {'t1_c1_key': {
- 'columns': ['c1'], 'access_method': 'btree'}}}
+ 'unique_constraints': {'t1_c1_key': {'columns': ['c1']}}}
map_unique2 = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'character(5)'}},
{'c3': {'type': 'text'}}],
'unique_constraints': {'t1_c1_c2_key': {
- 'columns': ['c1', 'c2'], 'access_method': 'btree'}}}
+ 'columns': ['c1', 'c2']}}}
map_unique3 = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'unique_constraints': {'t1_unique_key': {
- 'columns': ['c1'], 'access_method': 'btree'}}}
+ 'columns': ['c1']}}}
def test_unique_1(self):
"Map a table with a single-column unique constraint"
@@ -629,7 +616,7 @@ def test_unique_3(self):
dbmap = self.to_map(stmts)
if self.db.version < 90000:
self.map_unique2.update({'unique_constraints': {'t1_c1_key': {
- 'columns': ['c1', 'c2'], 'access_method': 'btree'}}})
+ 'columns': ['c1', 'c2']}}})
self.assertEqual(dbmap['schema public']['table t1'], self.map_unique2)
def test_unique_4(self):
@@ -657,8 +644,7 @@ def test_create_w_unique_constraint(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'unique_constraints': {'t1_c1_key': {
- 'columns': ['c1'],
- 'access_method': 'btree'}}}})
+ 'columns': ['c1']}}}})
sql = self.to_sql(inmap)
self.assertEqual(fix_indent(sql[0]),
"CREATE TABLE t1 (c1 integer, c2 text)")
@@ -723,7 +709,7 @@ def test_comment_on_primary_key(self):
'columns': [{'c1': {'type': 'text', 'not_null': True}},
{'c2': {'type': 'integer'}}],
'primary_key': {'cns1': {
- 'columns': ['c2'], 'access_method': 'btree',
+ 'columns': ['c2'],
'description': 'Test constraint cns1'}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, [COMMENT_STMT])
@@ -738,8 +724,7 @@ def test_drop_foreign_key_comment(self):
inmap['schema public'].update({'table t2': {
'columns': [{'c21': {'type': 'integer', 'not_null': True}},
{'c22': {'type': 'text'}}],
- 'primary_key': {'t2_pkey': {
- 'columns': ['c21'], 'access_method': 'btree'}}},
+ 'primary_key': {'t2_pkey': {'columns': ['c21']}}},
'table t1': {
'columns': [{'c11': {'type': 'integer'}},
{'c12': {'type': 'text'}},
@@ -760,7 +745,7 @@ def test_change_unique_constraint_comment(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'unique_constraints': {'cns1': {
- 'columns': ['c1'], 'access_method': 'btree',
+ 'columns': ['c1'],
'description': "Changed constraint cns1"}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, ["COMMENT ON CONSTRAINT cns1 ON t1 IS "
71 tests/dbobject/test_index.py
View
@@ -19,8 +19,7 @@ def test_index_1(self):
dbmap = self.to_map([CREATE_TABLE_STMT, CREATE_STMT])
expmap = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
- 'indexes': {'t1_idx': {'keys': ['c1'],
- 'access_method': 'btree'}}}
+ 'indexes': {'t1_idx': {'keys': ['c1']}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_2(self):
@@ -32,8 +31,7 @@ def test_index_2(self):
{'c2': {'type': 'character(5)'}},
{'c3': {'type': 'text'}}],
'indexes': {'t1_idx': {'keys': ['c1', 'c2'],
- 'unique': True,
- 'access_method': 'btree'}}}
+ 'unique': True}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_3(self):
@@ -46,10 +44,9 @@ def test_index_3(self):
{'c2': {'type': 'character(5)'}},
{'c3': {'type': 'tsvector'}}],
'indexes': {'t1_idx_1': {'keys': ['c1', 'c2'],
- 'unique': True,
- 'access_method': 'btree'},
+ 'unique': True},
't1_idx_2': {'keys': ['c3'],
- 'access_method': 'gin'}}}
+ 'access_method': 'gin'}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_function(self):
@@ -59,8 +56,7 @@ def test_index_function(self):
expmap = {'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {'keys': [
- {'lower(c2)': {'type': 'expression'}}],
- 'access_method': 'btree'}}}
+ {'lower(c2)': {'type': 'expression'}}]}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_function_multi(self):
@@ -73,8 +69,7 @@ def test_index_function_multi(self):
{'c3': {'type': 'text'}}],
'indexes': {'t1_idx': {
'keys': [{'lower(c2)': {'type': 'expression'}},
- {'lower(c3)': {'type': 'expression'}}],
- 'access_method': 'btree'}}}
+ {'lower(c3)': {'type': 'expression'}}]}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_col_opts(self):
@@ -87,8 +82,7 @@ def test_index_col_opts(self):
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {'keys': [
{'c1': {'opclass': 'cidr_ops', 'nulls': 'first'}},
- {'c2': {'order': 'desc'}}],
- 'access_method': 'btree'}}}
+ {'c2': {'order': 'desc'}}]}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_index_mixed(self):
@@ -104,8 +98,7 @@ def test_index_mixed(self):
'keys': [{"btrim(c3, 'x'::text)": {
'type': 'expression', 'nulls': 'first'}},
'c1', {'lower(c2)': {
- 'type': 'expression', 'order': 'desc'}}],
- 'access_method': 'btree'}}}
+ 'type': 'expression', 'order': 'desc'}}]}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
def test_map_index_comment(self):
@@ -124,13 +117,11 @@ def test_create_table_with_index(self):
inmap['schema public'].update({'table t1': {
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
- 'indexes': {'t1_idx': {
- 'keys': ['c1'],
- 'access_method': 'btree'}}}})
+ 'indexes': {'t1_idx': {'keys': ['c1']}}}})
sql = self.to_sql(inmap)
self.assertEqual(fix_indent(sql[0]),
"CREATE TABLE t1 (c1 integer, c2 text)")
- self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 USING btree (c1)")
+ self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 (c1)")
def test_add_index(self):
"Add a two-column unique index to an existing table"
@@ -157,8 +148,9 @@ def test_add_index_back_compat(self):
{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'integer', 'not_null': True}},
{'c3': {'type': 'text'}}],
- 'indexes': {'t1_idx': {'columns': ['c2', 'c1'],
- 'unique': True}}}})
+ 'indexes': {'t1_idx': {
+ 'columns': ['c2', 'c1'], 'unique': True,
+ 'access_method': 'btree'}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, ["CREATE UNIQUE INDEX t1_idx ON t1 (c2, c1)"])
@@ -178,25 +170,9 @@ def test_create_index_function(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {'keys': [{
- 'lower(c2)': {'type': 'expression'}}],
- 'access_method': 'btree'}}}})
- sql = self.to_sql(inmap)
- self.assertEqual(sql[1],
- "CREATE INDEX t1_idx ON t1 USING btree (lower(c2))")
-
- def test_create_index_col_opts(self):
- "Create table and an index with column options"
- inmap = self.std_map()
- inmap['schema public'].update({'table t1': {
- 'columns': [{'c1': {'type': 'cidr'}},
- {'c2': {'type': 'text'}}],
- 'indexes': {'t1_idx': {'keys': [
- {'c1': {'opclass': 'cidr_ops',
- 'nulls': 'first'}}, 'c2'],
- 'access_method': 'btree'}}}})
+ 'lower(c2)': {'type': 'expression'}}]}}}})
sql = self.to_sql(inmap)
- self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 USING btree "
- "(c1 cidr_ops NULLS FIRST, c2)")
+ self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 (lower(c2))")
def test_create_index_col_opts(self):
"Create table and an index with column options"
@@ -206,10 +182,9 @@ def test_create_index_col_opts(self):
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {'keys': [
{'c1': {'opclass': 'cidr_ops',
- 'nulls': 'first'}}, 'c2'],
- 'access_method': 'btree'}}}})
+ 'nulls': 'first'}}, 'c2']}}}})
sql = self.to_sql(inmap)
- self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 USING btree "
+ self.assertEqual(sql[1], "CREATE INDEX t1_idx ON t1 "
"(c1 cidr_ops NULLS FIRST, c2)")
def test_index_mixed(self):
@@ -225,10 +200,9 @@ def test_index_mixed(self):
'type': 'expression',
'nulls': 'first'}}, 'c1',
{'lower(c2)': {'type': 'expression',
- 'order': 'desc'}}],
- 'access_method': 'btree'}}}})
+ 'order': 'desc'}}]}}}})
sql = self.to_sql(inmap, stmts)
- self.assertEqual(sql[0], "CREATE INDEX t1_idx ON t1 USING btree ("
+ self.assertEqual(sql[0], "CREATE INDEX t1_idx ON t1 ("
"btrim(c3, 'x'::text) NULLS FIRST, c1, "
"lower(c2) DESC)")
@@ -240,7 +214,7 @@ def test_comment_on_index(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {
- 'keys': ['c1'], 'access_method': 'btree',
+ 'keys': ['c1'],
'description': 'Test index t1_idx'}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, [COMMENT_STMT])
@@ -252,8 +226,7 @@ def test_drop_index_comment(self):
inmap['schema public'].update({'table t1': {
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
- 'indexes': {'t1_idx': {
- 'keys': ['c1'], 'access_method': 'btree'}}}})
+ 'indexes': {'t1_idx': {'keys': ['c1']}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, ["COMMENT ON INDEX t1_idx IS NULL"])
@@ -265,7 +238,7 @@ def test_change_index_comment(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {
- 'keys': ['c1'], 'access_method': 'btree',
+ 'keys': ['c1'],
'description': 'Changed index t1_idx'}}}})
sql = self.to_sql(inmap, stmts)
self.assertEqual(sql, [
11 tests/dbobject/test_tablespace.py
View
@@ -32,7 +32,6 @@ def test_map_primary_key(self):
expmap = {'columns': [{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'text'}}],
'primary_key': {'t1_pkey': {'columns': ['c1'],
- 'access_method': 'btree',
'tablespace': 'ts1'}}}
self.assertEqual(dbmap['schema public']['table t1'], expmap)
@@ -42,7 +41,7 @@ def test_map_index(self):
"CREATE UNIQUE INDEX t1_idx ON t1 (c1) "
"TABLESPACE ts1"])
expmap = {'t1_idx': {'keys': ['c1'], 'tablespace': 'ts1',
- 'unique': True, 'access_method': 'btree'}}
+ 'unique': True}}
self.assertEqual(dbmap['schema public']['table t1']['indexes'], expmap)
@@ -77,7 +76,6 @@ def test_create_primary_key(self):
'columns': [{'c1': {'type': 'integer', 'not_null': True}},
{'c2': {'type': 'text'}}],
'primary_key': {'t1_pkey': {'columns': ['c1'],
- 'access_method': 'btree',
'tablespace': 'ts2'}},
'tablespace': 'ts1'}})
sql = self.to_sql(inmap)
@@ -94,11 +92,11 @@ def test_create_index(self):
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {
'keys': ['c1'], 'tablespace': 'ts2',
- 'unique': True, 'access_method': 'btree'}},
+ 'unique': True}},
'tablespace': 'ts1'}})
sql = self.to_sql(inmap, [CREATE_TABLE])
self.assertEqual(fix_indent(sql[0]), "CREATE UNIQUE INDEX t1_idx "
- "ON t1 USING btree (c1) TABLESPACE ts2")
+ "ON t1 (c1) TABLESPACE ts2")
def test_move_index(self):
"Move a index from one tablespace to another"
@@ -107,8 +105,7 @@ def test_move_index(self):
'columns': [{'c1': {'type': 'integer'}},
{'c2': {'type': 'text'}}],
'indexes': {'t1_idx': {
- 'keys': ['c1'], 'tablespace': 'ts2',
- 'access_method': 'btree'}}}})
+ 'keys': ['c1'], 'tablespace': 'ts2'}}}})
stmts = ["CREATE TABLE t1 (c1 integer, c2 text)",
"CREATE INDEX t1_idx ON t1 (c1) TABLESPACE ts1"]
sql = self.to_sql(inmap, stmts)
Please sign in to comment.
Something went wrong with that request. Please try again.