473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Safe delete idea for discussion

Hello,

I think this will apply to alot of web applications:

users want the ability to delete a record in table x

this record is related to records in other tables
and those to others in other tables etc.

in other words we must use cascade delete to do
this in one go

my problem is that users make mistakes

and one day they will accidently delete the wrong
record in table x

and the cascade delete will take out a large chunk
of the database

and they will ring me and say "we want to put it back"

that got me thinking about some approaches

1 - have a little ui that shows them all the records
they are about to delete so they think see the
consequences of their actions, this will lower the
number of mistakes

2 - leverage the current interface to guide them through
the cascade manually (ie: not in sql) so that they are
forced to confront the consequences bit by bit
and it takes a long time so it is dull and they won't
do it in a whim

but then i realised whatever i do they will make mistakes
and why write code when you don't have to

thus i came up with this approach that seems to fulfill
all requirements and take almost no coding whatosever:

enable cascade delete in the database

create a delete trigger on the table in question
that writes an entry into a DeleteLog table
including three pieces of information:

timestamp (of the sql box!)
tablename (of the table with the record to be deleted)
recordid (of the deleted record or records)

those two things wrapped in a transaction
to stop the risk of updates to the target
record after the log entry and before the delete

now!

users can delete whatever they like
and when they make a mistake (infrequently)
i can use a prewritten script to do a point
in time restore of the database
(to a different dbname - i call this docking)
using timestamp in the deletelog table
and run an easy but boring to write script that pulls
the data back in

what do you think?

Nov 19 '05 #1
3 2077
Cripes that was easy:

create table LogDeletions (
LogID int primary key identity
, LogDate datetime not null default getdate()
, TableName varchar(255) not null
, RecordID int not null
)
create trigger trAuditLogDelete
on auditlog
for delete
as
--
insert into LogDeletions (LogDate, TableName, RecordID)
select getdate(), 'AuditLog', AuditLogID
from deleted
--(end)

why didn't i think of this years ago?
100% reversable deletions, yeah!
John Rivers wrote:
Hello,

I think this will apply to alot of web applications:

users want the ability to delete a record in table x

this record is related to records in other tables
and those to others in other tables etc.

in other words we must use cascade delete to do
this in one go

my problem is that users make mistakes

and one day they will accidently delete the wrong
record in table x

and the cascade delete will take out a large chunk
of the database

and they will ring me and say "we want to put it back"

that got me thinking about some approaches

1 - have a little ui that shows them all the records
they are about to delete so they think see the
consequences of their actions, this will lower the
number of mistakes

2 - leverage the current interface to guide them through
the cascade manually (ie: not in sql) so that they are
forced to confront the consequences bit by bit
and it takes a long time so it is dull and they won't
do it in a whim

but then i realised whatever i do they will make mistakes
and why write code when you don't have to

thus i came up with this approach that seems to fulfill
all requirements and take almost no coding whatosever:

enable cascade delete in the database

create a delete trigger on the table in question
that writes an entry into a DeleteLog table
including three pieces of information:

timestamp (of the sql box!)
tablename (of the table with the record to be deleted)
recordid (of the deleted record or records)

those two things wrapped in a transaction
to stop the risk of updates to the target
record after the log entry and before the delete

now!

users can delete whatever they like
and when they make a mistake (infrequently)
i can use a prewritten script to do a point
in time restore of the database
(to a different dbname - i call this docking)
using timestamp in the deletelog table
and run an easy but boring to write script that pulls
the data back in

what do you think?


Nov 19 '05 #2
Wow, I'm flying this morning!

--example point in time restore script

drop database Recovery
go
restore database Recovery
from disk = 'filename of last full backup', norecovery
go
restore log Recovery
from disk = 'filename of suitable log file', stopat = '2005-05-05
12:01:32.435'
go

--example recovery script for simple chain of 3 tables
--maintable (1-*) firstdependenttable (1-*) seconddependenttable

select primarykey into #temp1
from firstdependenttable where maintable.foreignkey = the one deleted
from the maintable

insert into originaldatabase.dbo.seconddependenttable
select *
from seconddependenttable
where firstdependenttable.foreignkey in (select primarykey from #temp1)

insert into originaldatabase.dbo.firstdependenttable
select *
from firstdependenttable
where primarykey in (select primarykey from #temp1)

insert into originaldatabase.dbo.maintable
select *
from maintable
where primarykey = the one deleted from the maintable

the goods news is, that however complicated your database
scheme, relationships etc. a dependency tree
can always be reduced to a simple ordered list

so this pattern can be extended to handle any
database schema

so now we could move to the next level
and write code to examine the database schema
and generate the data insertion code above
automatically

plus another bit of code to track
the database backup files and
work out the right restore script

but that would be over doing it

that is the solution finished

i hope you like it
John Rivers wrote:
Cripes that was easy:

create table LogDeletions (
LogID int primary key identity
, LogDate datetime not null default getdate()
, TableName varchar(255) not null
, RecordID int not null
)
create trigger trAuditLogDelete
on auditlog
for delete
as
--
insert into LogDeletions (LogDate, TableName, RecordID)
select getdate(), 'AuditLog', AuditLogID
from deleted
--(end)

why didn't i think of this years ago?
100% reversable deletions, yeah!
John Rivers wrote:
Hello,

I think this will apply to alot of web applications:

users want the ability to delete a record in table x

this record is related to records in other tables
and those to others in other tables etc.

in other words we must use cascade delete to do
this in one go

my problem is that users make mistakes

and one day they will accidently delete the wrong
record in table x

and the cascade delete will take out a large chunk
of the database

and they will ring me and say "we want to put it back"

that got me thinking about some approaches

1 - have a little ui that shows them all the records
they are about to delete so they think see the
consequences of their actions, this will lower the
number of mistakes

2 - leverage the current interface to guide them through
the cascade manually (ie: not in sql) so that they are
forced to confront the consequences bit by bit
and it takes a long time so it is dull and they won't
do it in a whim

but then i realised whatever i do they will make mistakes
and why write code when you don't have to

thus i came up with this approach that seems to fulfill
all requirements and take almost no coding whatosever:

enable cascade delete in the database

create a delete trigger on the table in question
that writes an entry into a DeleteLog table
including three pieces of information:

timestamp (of the sql box!)
tablename (of the table with the record to be deleted)
recordid (of the deleted record or records)

those two things wrapped in a transaction
to stop the risk of updates to the target
record after the log entry and before the delete

now!

users can delete whatever they like
and when they make a mistake (infrequently)
i can use a prewritten script to do a point
in time restore of the database
(to a different dbname - i call this docking)
using timestamp in the deletelog table
and run an easy but boring to write script that pulls
the data back in

what do you think?


Nov 19 '05 #3
Just thought of a small problem

the getdate() inside the trigger

might end up returning a time AFTER

the delete time in the log file

no way to know how sql server does that internally

if that is the case the solution would be

to use a stored procedure instead of trigger

something like: (just a sketch)

create proc spAuditLogDelete
@AuditLogID int
as
--
declare @returnvalue int
set @returnvalue = 1
--
begin tran
if @@error <> 0
goto label_end
--
insert into LogDeletions (LogDate, TableName, RecordID)
select getdate(), 'AuditLog', AuditLogID
from deleted
if @@error <> 0
goto label_rollback
--
delete
from auditlog
where auditlogid = @auditlogid
if @@error <> 0
goto label_rollback
--
commit tran
if @@error <> 0
goto label_rollback
--
set @returnvalue = 0
goto label_end
--
label_rollback:
rollback tran
--
label_end:
return @returnvalue
--(end)

which is fine and dandy but a bit less robust

or just take of a couple of milliseconds off the timestamp???

John Rivers wrote:
Wow, I'm flying this morning!

--example point in time restore script

drop database Recovery
go
restore database Recovery
from disk = 'filename of last full backup', norecovery
go
restore log Recovery
from disk = 'filename of suitable log file', stopat = '2005-05-05
12:01:32.435'
go

--example recovery script for simple chain of 3 tables
--maintable (1-*) firstdependenttable (1-*) seconddependenttable

select primarykey into #temp1
from firstdependenttable where maintable.foreignkey = the one deleted
from the maintable

insert into originaldatabase.dbo.seconddependenttable
select *
from seconddependenttable
where firstdependenttable.foreignkey in (select primarykey from #temp1)

insert into originaldatabase.dbo.firstdependenttable
select *
from firstdependenttable
where primarykey in (select primarykey from #temp1)

insert into originaldatabase.dbo.maintable
select *
from maintable
where primarykey = the one deleted from the maintable

the goods news is, that however complicated your database
scheme, relationships etc. a dependency tree
can always be reduced to a simple ordered list

so this pattern can be extended to handle any
database schema

so now we could move to the next level
and write code to examine the database schema
and generate the data insertion code above
automatically

plus another bit of code to track
the database backup files and
work out the right restore script

but that would be over doing it

that is the solution finished

i hope you like it
John Rivers wrote:
Cripes that was easy:

create table LogDeletions (
LogID int primary key identity
, LogDate datetime not null default getdate()
, TableName varchar(255) not null
, RecordID int not null
)
create trigger trAuditLogDelete
on auditlog
for delete
as
--
insert into LogDeletions (LogDate, TableName, RecordID)
select getdate(), 'AuditLog', AuditLogID
from deleted
--(end)

why didn't i think of this years ago?
100% reversable deletions, yeah!
John Rivers wrote:
Hello,

I think this will apply to alot of web applications:

users want the ability to delete a record in table x

this record is related to records in other tables
and those to others in other tables etc.

in other words we must use cascade delete to do
this in one go

my problem is that users make mistakes

and one day they will accidently delete the wrong
record in table x

and the cascade delete will take out a large chunk
of the database

and they will ring me and say "we want to put it back"

that got me thinking about some approaches

1 - have a little ui that shows them all the records
they are about to delete so they think see the
consequences of their actions, this will lower the
number of mistakes

2 - leverage the current interface to guide them through
the cascade manually (ie: not in sql) so that they are
forced to confront the consequences bit by bit
and it takes a long time so it is dull and they won't
do it in a whim

but then i realised whatever i do they will make mistakes
and why write code when you don't have to

thus i came up with this approach that seems to fulfill
all requirements and take almost no coding whatosever:

enable cascade delete in the database

create a delete trigger on the table in question
that writes an entry into a DeleteLog table
including three pieces of information:

timestamp (of the sql box!)
tablename (of the table with the record to be deleted)
recordid (of the deleted record or records)

those two things wrapped in a transaction
to stop the risk of updates to the target
record after the log entry and before the delete

now!

users can delete whatever they like
and when they make a mistake (infrequently)
i can use a prewritten script to do a point
in time restore of the database
(to a different dbname - i call this docking)
using timestamp in the deletelog table
and run an easy but boring to write script that pulls
the data back in

what do you think?


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Irmen de Jong | last post by:
Pickle and marshal are not safe. They can do harmful things if fed maliciously constructed data. That is a pity, because marshal is fast. I need a fast and safe (secure) marshaler. Is xdrlib the...
2
by: qazmlp | last post by:
Say, ptr=0 / ptr=NULL. delete ptr; //is safe // Is delete ptr; // also safe ?
3
by: Songling | last post by:
Hi gurus, Just learnt from my co-worker that it's safe to delete a null pointer. I tried it on sun box (and linux), nothing bad happens. So it seems to be true. But does it apply to all...
2
by: Eugene | last post by:
Can we use Visual Source Safe to keep MS Access files? What are the pros and cons. Any refernces and links on that topic. Thanks, Eugene
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: SeniorLee | last post by:
in the book EC++ chapter 11, with this code ( pb is a member of class ) WIdget& WIdget::operator=(const WIdget& rhs) { Bitmap *pOrig = pb; pb = new Bitmap(*rhs.pb); delete pOrig;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.