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.