maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

Question - Dropping Postgresql / SQLAlchemy tables which have partitoned data #22

Closed RobertWSmith closed 8 years ago

RobertWSmith commented 8 years ago

I'm working on a data model, and in the process of creating it I'm finding that if partitions are created and I try to use SQLAlchemy's MetaData.drop_all() method with Postgresql 9.4 the table is not dropped due to other tables inheriting my parent table.

My motivation for this question is to be able to fully automate my unit / integration tests. I'd like to be able to drop my project on a clean server with no outside dependencies except for what I've declared.

I've been trying to go over the SQLAlchemy docs to see if there's an easy way to overload the 'DROP TABLE' clause to use Postgresql's 'CASCADING' option to no avail.

Additionally, I'd like to be able to clean up my data model to drop the functions which architect installs to partition the table's data.

My current solution is to do these tasks with a SQLAlchemy event monitor for 'before_drop' where I run some queries against the information_schema. Unfortunately this must be done on a table-by-table basis so I was wondering if there's a cleaner way to perform these tasks.

maxtepkeev commented 8 years ago

Hi Robert,

I'm not an expert in SQLAlchemy's API, so I would advice you to ask the same question in SQLAlchemy's issues list.

One advice I can give you is to create a separate schema for your data model, this way you can use DROP SCHEMA name IF EXISTS CASCADE which will drop all tables, triggers, functions etc that are contained within this schema.

If for some reason that's not an option for you, then as far as I know there's no other way then to use before drop event monitor which you're already using, but again, I'm not an expert in SQLAlchemy's API and I don't know all of it's possibilities, so you better ask it's author to be sure.

maxtepkeev commented 8 years ago

I believe this can be closed.