From ef6042ff461e490c2a3040f18f0a3688b2e601a0 Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Wed, 13 Aug 2014 01:39:09 +0200 Subject: Adding a tablespace options for postgresql create table --- lib/sqlalchemy/dialects/postgresql/base.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index c2b1d66f4..0f008642e 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1448,6 +1448,13 @@ class PGDDLCompiler(compiler.DDLCompiler): text += self.define_constraint_deferrability(constraint) return text + def post_create_table(self, table): + table_opts = [] + if table.dialect_options['postgresql']['tablespace']: + table_opts.append('TABLESPACE %s' % table.dialect_options['postgresql']['tablespace']) + + return ' '.join(table_opts) + class PGTypeCompiler(compiler.GenericTypeCompiler): @@ -1707,7 +1714,8 @@ class PGDialect(default.DefaultDialect): "ops": {} }), (schema.Table, { - "ignore_search_path": False + "ignore_search_path": False, + "tablespace": None }) ] -- cgit v1.2.1 From d6873904c40134df787ffe5459d61d3663bf5d5f Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Sun, 17 Aug 2014 00:39:36 +0200 Subject: Adding oids and on_commit table options --- lib/sqlalchemy/dialects/postgresql/base.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 0f008642e..9b30bf894 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1450,6 +1450,14 @@ class PGDDLCompiler(compiler.DDLCompiler): def post_create_table(self, table): table_opts = [] + if table.dialect_options['postgresql']['withoids'] is not None: + if table.dialect_options['postgresql']['withoids']: + table_opts.append('WITH OIDS') + else: + table_opts.append('WITHOUT OIDS') + if table.dialect_options['postgresql']['on_commit']: + on_commit_options = table.dialect_options['postgresql']['on_commit'].replace("_", " ").upper() + table_opts.append('ON COMMIT %s' % on_commit_options) if table.dialect_options['postgresql']['tablespace']: table_opts.append('TABLESPACE %s' % table.dialect_options['postgresql']['tablespace']) @@ -1715,7 +1723,9 @@ class PGDialect(default.DefaultDialect): }), (schema.Table, { "ignore_search_path": False, - "tablespace": None + "tablespace": None, + "withoids" : None, + "on_commit" : None, }) ] -- cgit v1.2.1 From 8e03430acdb98d7e5fef4a48a3120b928ed3266d Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Sun, 17 Aug 2014 01:39:14 +0200 Subject: quoting tablespace name in create table command in postgresql dialect --- lib/sqlalchemy/dialects/postgresql/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 9b30bf894..b09eaba72 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1459,7 +1459,8 @@ class PGDDLCompiler(compiler.DDLCompiler): on_commit_options = table.dialect_options['postgresql']['on_commit'].replace("_", " ").upper() table_opts.append('ON COMMIT %s' % on_commit_options) if table.dialect_options['postgresql']['tablespace']: - table_opts.append('TABLESPACE %s' % table.dialect_options['postgresql']['tablespace']) + tablespace_name = table.dialect_options['postgresql']['tablespace'] + table_opts.append('TABLESPACE %s' % self.preparer.quote(tablespace_name)) return ' '.join(table_opts) -- cgit v1.2.1 From 9eacc8d42ad49527c7fd0fe7e37100edba9eb1dc Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Sun, 17 Aug 2014 02:48:21 +0200 Subject: Correcting options name from withoids to with_oids --- lib/sqlalchemy/dialects/postgresql/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index b09eaba72..057a5e072 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1450,8 +1450,8 @@ class PGDDLCompiler(compiler.DDLCompiler): def post_create_table(self, table): table_opts = [] - if table.dialect_options['postgresql']['withoids'] is not None: - if table.dialect_options['postgresql']['withoids']: + if table.dialect_options['postgresql']['with_oids'] is not None: + if table.dialect_options['postgresql']['with_oids']: table_opts.append('WITH OIDS') else: table_opts.append('WITHOUT OIDS') @@ -1725,7 +1725,7 @@ class PGDialect(default.DefaultDialect): (schema.Table, { "ignore_search_path": False, "tablespace": None, - "withoids" : None, + "with_oids" : None, "on_commit" : None, }) ] -- cgit v1.2.1 From faa5a9067661039dcc8663e00bdcea2d098c9989 Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Sun, 17 Aug 2014 16:56:53 +0200 Subject: Adding postgres create table options documentation --- lib/sqlalchemy/dialects/postgresql/base.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 057a5e072..34932520f 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -417,6 +417,22 @@ of :class:`.PGInspector`, which offers additional methods:: .. autoclass:: PGInspector :members: +PostgreSQL specific table options +--------------------------------- + +PostgreSQL provides several CREATE TABLE specific options allowing to +specify how table data are stored. The following options are currently +supported: ``TABLESPACE``, ``ON COMMIT``, ``WITH OIDS``. + +``postgresql_tablespace`` is probably the more common and allows to specify +where in the filesystem the data files for the table will be created (see +http://www.postgresql.org/docs/9.3/static/manage-ag-tablespaces.html) + +.. seealso:: + + `Postgresql CREATE TABLE options + `_ - + on the PostgreSQL website """ from collections import defaultdict -- cgit v1.2.1 From b490534657229cbc44f1f5735a39539ceaf776a3 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 23 Aug 2014 15:21:16 -0400 Subject: - pep8 formatting for pg table opts feature, tests - add support for PG INHERITS - fix mis-named tests - changelog fixes #2051 --- lib/sqlalchemy/dialects/postgresql/base.py | 83 +++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 24 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql/base.py') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 34932520f..39de0cf92 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -417,22 +417,42 @@ of :class:`.PGInspector`, which offers additional methods:: .. autoclass:: PGInspector :members: -PostgreSQL specific table options ---------------------------------- +.. postgresql_table_options: + +PostgreSQL Table Options +------------------------- + +Several options for CREATE TABLE are supported directly by the PostgreSQL +dialect in conjunction with the :class:`.Table` construct: + +* ``TABLESPACE``:: + + Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace') + +* ``ON COMMIT``:: + + Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS') -PostgreSQL provides several CREATE TABLE specific options allowing to -specify how table data are stored. The following options are currently -supported: ``TABLESPACE``, ``ON COMMIT``, ``WITH OIDS``. +* ``WITH OIDS``:: -``postgresql_tablespace`` is probably the more common and allows to specify -where in the filesystem the data files for the table will be created (see -http://www.postgresql.org/docs/9.3/static/manage-ag-tablespaces.html) + Table("some_table", metadata, ..., postgresql_with_oids=True) + +* ``WITHOUT OIDS``:: + + Table("some_table", metadata, ..., postgresql_with_oids=False) + +* ``INHERITS``:: + + Table("some_table", metadata, ..., postgresql_inherits="some_supertable") + + Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...)) + +.. versionadded:: 1.0.0 .. seealso:: `Postgresql CREATE TABLE options - `_ - - on the PostgreSQL website + `_ """ from collections import defaultdict @@ -1466,19 +1486,33 @@ class PGDDLCompiler(compiler.DDLCompiler): def post_create_table(self, table): table_opts = [] - if table.dialect_options['postgresql']['with_oids'] is not None: - if table.dialect_options['postgresql']['with_oids']: - table_opts.append('WITH OIDS') - else: - table_opts.append('WITHOUT OIDS') - if table.dialect_options['postgresql']['on_commit']: - on_commit_options = table.dialect_options['postgresql']['on_commit'].replace("_", " ").upper() - table_opts.append('ON COMMIT %s' % on_commit_options) - if table.dialect_options['postgresql']['tablespace']: - tablespace_name = table.dialect_options['postgresql']['tablespace'] - table_opts.append('TABLESPACE %s' % self.preparer.quote(tablespace_name)) + pg_opts = table.dialect_options['postgresql'] + + inherits = pg_opts.get('inherits') + if inherits is not None: + if not isinstance(inherits, (list, tuple)): + inherits = (inherits, ) + table_opts.append( + '\n INHERITS ( ' + + ', '.join(self.preparer.quote(name) for name in inherits) + + ' )') + + if pg_opts['with_oids'] is True: + table_opts.append('\n WITH OIDS') + elif pg_opts['with_oids'] is False: + table_opts.append('\n WITHOUT OIDS') + + if pg_opts['on_commit']: + on_commit_options = pg_opts['on_commit'].replace("_", " ").upper() + table_opts.append('\n ON COMMIT %s' % on_commit_options) + + if pg_opts['tablespace']: + tablespace_name = pg_opts['tablespace'] + table_opts.append( + '\n TABLESPACE %s' % self.preparer.quote(tablespace_name) + ) - return ' '.join(table_opts) + return ''.join(table_opts) class PGTypeCompiler(compiler.GenericTypeCompiler): @@ -1741,8 +1775,9 @@ class PGDialect(default.DefaultDialect): (schema.Table, { "ignore_search_path": False, "tablespace": None, - "with_oids" : None, - "on_commit" : None, + "with_oids": None, + "on_commit": None, + "inherits": None }) ] -- cgit v1.2.1