Skip to content

Instantly share code, notes, and snippets.

@slabad
Last active June 28, 2022 13:07
Show Gist options
  • Save slabad/356dc8eeab9d6f7fe8e172e25994b140 to your computer and use it in GitHub Desktop.
Save slabad/356dc8eeab9d6f7fe8e172e25994b140 to your computer and use it in GitHub Desktop.
mysql test trans #mysql
--create database for testing rollback
create database if not exists dmstest;
use dmstest;
create table people(
id int auto_increment
, first_name varchar(50)
, last_name varchar(50)
, email varchar(50)
, gender varchar(50)
, ip_address varchar(50)
);
--change file
set autocommit=0;
BEGIN;
CREATE TABLE if not exists tmp.people_CHANGE_2345 (start_id int, end_id int);
truncate table tmp.people_CHANGE_2345;
INSERT INTO people (first_name, last_name, email, gender, ip_address)
VALUES ('Albert', 'Fields1', '[email protected]', 'Male', '55.100.238.49')
,('Albert', 'Fields1', '[email protected]', 'Male', '55.100.238.49');
INSERT INTO tmp.people_CHANGE_2345
SELECT LAST_INSERT_ID(), LAST_INSERT_ID() + ROW_COUNT() - 1;
INSERT INTO people (first_name, last_name, email, gender, ip_address)
VALUES ('Albert', 'Fields2', '[email protected]', 'Male', '55.100.238.49')
,('Albert', 'Fields2', '[email protected]', 'Male', '55.100.238.49')
;
INSERT INTO tmp.people_CHANGE_2345
SELECT LAST_INSERT_ID(), LAST_INSERT_ID() + ROW_COUNT() - 1;
commit;
--rollback file
set autocommit = 0;
begin;
DELETE FROM people
WHERE ID >= (SELECT start_id FROM tmp.people_CHANGE_2345)
and id <= (SELECT end_id FROM tmp.people_CHANGE_2345) ;
DROP TABLE tmp.people_CHANGE_2345;
commit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment