473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2096
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 trAuditLogDelet e
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-*) firstdependentt able (1-*) seconddependent table

select primarykey into #temp1
from firstdependentt able where maintable.forei gnkey = the one deleted
from the maintable

insert into originaldatabas e.dbo.seconddep endenttable
select *
from seconddependent table
where firstdependentt able.foreignkey in (select primarykey from #temp1)

insert into originaldatabas e.dbo.firstdepe ndenttable
select *
from firstdependentt able
where primarykey in (select primarykey from #temp1)

insert into originaldatabas e.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 trAuditLogDelet e
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 spAuditLogDelet e
@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-*) firstdependentt able (1-*) seconddependent table

select primarykey into #temp1
from firstdependentt able where maintable.forei gnkey = the one deleted
from the maintable

insert into originaldatabas e.dbo.seconddep endenttable
select *
from seconddependent table
where firstdependentt able.foreignkey in (select primarykey from #temp1)

insert into originaldatabas e.dbo.firstdepe ndenttable
select *
from firstdependentt able
where primarykey in (select primarykey from #temp1)

insert into originaldatabas e.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 trAuditLogDelet e
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
2571
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 only option? I would expect that it is fast and safe because it (the xdr spec) has been around for so long. Or are there better options (perhaps 3rd party libraries)?
2
5813
by: qazmlp | last post by:
Say, ptr=0 / ptr=NULL. delete ptr; //is safe // Is delete ptr; // also safe ?
3
9611
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 platforms? Also if I delete a pointer twice, and don't nullify the pointer after the first delete, it's going to trap. If I do the nullify, no trap. Does it just confirm that it's safe
2
3630
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
2119
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 meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
1
3588
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 some files and work on it. Let say, I want add new page to web project named websiteOrder.sln, i will open websiteOrder.sln in my local computer, connected to websiteOrder.sln located in Visual Source Safe 6.0(source safe located in another...
95
5020
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 ____________________________________ | | | ------------------ | | | BUTTON | | | ...
29
4217
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 a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
2
1594
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
7885
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8271
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8031
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.