diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-13 19:32:00 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-13 19:32:00 -0500 |
commit | 2e4da52221c9f231117b93c9709a36dc65b8c9b0 (patch) | |
tree | 7a53c145fbfb14d89351e1054b4ae1ccfbcd5f69 /lib/sqlalchemy/engine/ddl.py | |
parent | 15520d5d81b9c45f6735df82ce0d593e93ad2bdf (diff) | |
download | sqlalchemy-2e4da52221c9f231117b93c9709a36dc65b8c9b0.tar.gz |
- The Sequence() construct, when passed a MetaData()
object via its 'metadata' argument, will be
included in CREATE/DROP statements within
metadata.create_all() and metadata.drop_all(),
including "checkfirst" logic. [ticket:2055]
Diffstat (limited to 'lib/sqlalchemy/engine/ddl.py')
-rw-r--r-- | lib/sqlalchemy/engine/ddl.py | 102 |
1 files changed, 72 insertions, 30 deletions
diff --git a/lib/sqlalchemy/engine/ddl.py b/lib/sqlalchemy/engine/ddl.py index 6b5684f64..dca183f9a 100644 --- a/lib/sqlalchemy/engine/ddl.py +++ b/lib/sqlalchemy/engine/ddl.py @@ -22,33 +22,58 @@ class SchemaGenerator(DDLBase): self.preparer = dialect.identifier_preparer self.dialect = dialect - def _can_create(self, table): + def _can_create_table(self, table): self.dialect.validate_identifier(table.name) if table.schema: self.dialect.validate_identifier(table.schema) - return not self.checkfirst or not self.dialect.has_table(self.connection, table.name, schema=table.schema) + return not self.checkfirst or \ + not self.dialect.has_table(self.connection, + table.name, schema=table.schema) + + def _can_create_sequence(self, sequence): + return self.dialect.supports_sequences and \ + ( + (not self.dialect.sequences_optional or + not sequence.optional) and + ( + not self.checkfirst or + not self.dialect.has_sequence( + self.connection, + sequence.name, + schema=sequence.schema) + ) + ) def visit_metadata(self, metadata): if self.tables: tables = self.tables else: tables = metadata.tables.values() - collection = [t for t in sql_util.sort_tables(tables) if self._can_create(t)] + collection = [t for t in sql_util.sort_tables(tables) + if self._can_create_table(t)] + seq_coll = [s for s in metadata._sequences + if s.column is None and self._can_create_sequence(s)] metadata.dispatch.before_create(metadata, self.connection, - tables=collection) + tables=collection, + checkfirst=self.checkfirst) + + for seq in seq_coll: + self.traverse_single(seq, create_ok=True) for table in collection: self.traverse_single(table, create_ok=True) metadata.dispatch.after_create(metadata, self.connection, - tables=collection) + tables=collection, + checkfirst=self.checkfirst) def visit_table(self, table, create_ok=False): - if not create_ok and not self._can_create(table): + if not create_ok and not self._can_create_table(table): return - table.dispatch.before_create(table, self.connection) + table.dispatch.before_create(table, self.connection, + checkfirst=self.checkfirst) for column in table.columns: if column.default is not None: @@ -60,15 +85,13 @@ class SchemaGenerator(DDLBase): for index in table.indexes: self.traverse_single(index) - table.dispatch.after_create(table, self.connection) + table.dispatch.after_create(table, self.connection, + checkfirst=self.checkfirst) - def visit_sequence(self, sequence): - if self.dialect.supports_sequences: - if ((not self.dialect.sequences_optional or - not sequence.optional) and - (not self.checkfirst or - not self.dialect.has_sequence(self.connection, sequence.name, schema=sequence.schema))): - self.connection.execute(schema.CreateSequence(sequence)) + def visit_sequence(self, sequence, create_ok=False): + if not create_ok and not self._can_create_sequence(sequence): + return + self.connection.execute(schema.CreateSequence(sequence)) def visit_index(self, index): self.connection.execute(schema.CreateIndex(index)) @@ -87,31 +110,52 @@ class SchemaDropper(DDLBase): tables = self.tables else: tables = metadata.tables.values() - collection = [t for t in reversed(sql_util.sort_tables(tables)) if self._can_drop(t)] + collection = [t for t in reversed(sql_util.sort_tables(tables)) + if self._can_drop_table(t)] + seq_coll = [s for s in metadata._sequences + if s.column is None and self._can_drop_sequence(s)] metadata.dispatch.before_drop(metadata, self.connection, - tables=collection) + tables=collection, + checkfirst=self.checkfirst) for table in collection: self.traverse_single(table, drop_ok=True) + for seq in seq_coll: + self.traverse_single(seq, drop_ok=True) + metadata.dispatch.after_drop(metadata, self.connection, - tables=collection) + tables=collection, + checkfirst=self.checkfirst) - def _can_drop(self, table): + def _can_drop_table(self, table): self.dialect.validate_identifier(table.name) if table.schema: self.dialect.validate_identifier(table.schema) - return not self.checkfirst or self.dialect.has_table(self.connection, table.name, schema=table.schema) + return not self.checkfirst or self.dialect.has_table(self.connection, + table.name, schema=table.schema) + + def _can_drop_sequence(self, sequence): + return self.dialect.supports_sequences and \ + ((not self.dialect.sequences_optional or + not sequence.optional) and + (not self.checkfirst or + self.dialect.has_sequence( + self.connection, + sequence.name, + schema=sequence.schema)) + ) def visit_index(self, index): self.connection.execute(schema.DropIndex(index)) def visit_table(self, table, drop_ok=False): - if not drop_ok and not self._can_drop(table): + if not drop_ok and not self._can_drop_table(table): return - table.dispatch.before_drop(table, self.connection) + table.dispatch.before_drop(table, self.connection, + checkfirst=self.checkfirst) for column in table.columns: if column.default is not None: @@ -119,12 +163,10 @@ class SchemaDropper(DDLBase): self.connection.execute(schema.DropTable(table)) - table.dispatch.after_drop(table, self.connection) + table.dispatch.after_drop(table, self.connection, + checkfirst=self.checkfirst) - def visit_sequence(self, sequence): - if self.dialect.supports_sequences: - if ((not self.dialect.sequences_optional or - not sequence.optional) and - (not self.checkfirst or - self.dialect.has_sequence(self.connection, sequence.name, schema=sequence.schema))): - self.connection.execute(schema.DropSequence(sequence)) + def visit_sequence(self, sequence, drop_ok=False): + if not drop_ok and not self._can_drop_sequence(sequence): + return + self.connection.execute(schema.DropSequence(sequence)) |