Thursday, September 17, 2015

Drop It Like It's Not

I just ran the following script:

-- TABLES
FOR x IN (SELECT table_name FROM user_tables)
LOOP
  EXECUTE IMMEDIATE('DROP TABLE ' || x.table_name || ' CASCADE CONSTRAINTS');
END LOOP;

-- SEQUENCES
FOR x IN (SELECT sequence_name FROM user_sequences)
LOOP
  EXECUTE IMMEDIATE ('DROP SEQUENCE ' || x.sequence_name);
END LOOP;

-- VIEWS
FOR x IN (SELECT view_name FROM user_views)
LOOP
  EXECUTE IMMEDIATE ('DROP VIEW ' || x.view_name);
END LOOP;

Basically, drop all tables, views and sequences.  It worked great, cleaning out those objects in my schema without touching any packages, producers or functions.  The was just one problem:  I ran it in the wrong schema.

Maybe I didn't have enough coffee, or maybe I just wasn't paying attention, but I essentially wiped out a schema that I really would rather not have.  But I didn't even flinch, and here's why.

All tables & views were safely stored in my data model.  All sequences and triggers (and packages, procedures and functions) were safely stored in scripts.  And both the data model and associated scripts were safely checked in to version control.  So re-instantating this project was a mere inconvenience that took no more than the time it takes to drink a cup of coffee - something I clearly should have done more of earlier this morning.

Point here is simple: take the extra time to create a data model and a version control repository for your projects - and then make sure to use them!  I religiously check in code and then make sure that at least my TRUNK is backed up elsewhere.  Worst case for me, I'd lose a couple of hours or work, perhaps even less, which is far better than the alternative.

3 comments:

Noons said...

To this day I don't get it why there isn't an inbuilt utl_something to do a quick metadata only datapump of either a schema or the entire db into a standard directory - extremely useful for all kinds of reasons, one of which is this post.
Ah well, it must be now (all of a sudden) in the minds of the "experts"...

Jeffrey Kemp said...

It amazes me how often I come across places who still don't have any repository for their schema definitions (let alone version control). Setting up a code repository with some form of automated version control is a mandatory step #1 in any project. "If it's not checked in, it doesn't exist."

Scott said...

Agreed. I tell people, "I have all of the source code for X projects, right here on my laptop. If you spill that coffee on my laptop, no worries, as I can get it all back by the time you go out and get me a new coffee." Most are just completely baffled by that.