Permalink
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...
1 parent 77039da commit eb09df3c12ff96303cb6d70d4dcb8e1ad8516b5d @jmafc jmafc committed Jun 27, 2012
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
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':
@@ -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]
@@ -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:
@@ -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 "
Oops, something went wrong.

0 comments on commit eb09df3

Please sign in to comment.