📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 18

2022-10-13 •Youtube

Detailed Notes

Focus for this episode: In this episode, we dig into synchronizing our triggers across databases.

This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.

Repository For Code: git clone [email protected]:rbroadhead/dbsync.git

Transcript Text
thank you
[Music]
well hello and welcome back we are
continuing our series of python and SQL
tutorials we're working on our database
synchronization and we've worked our way
up to triggers we looked at proceeds or
procedures looked at functions and now
we started looking at it last time
looked at what triggers sort of looked
like
and we're going to see if we can do that
for our uh we use that to create our
triggers
and so what we're going to do is we're
going to go ahead and
we're going to do let's see so we've got
our triggers and we're going to do where
event
object schema
equals because we need to make sure that
we've got that right
here well let's do trigger schema
so
we definitely need to get our database
there
and that's probably it for right now
so
let's take that over what we'll do is
we're going to end up
uh let's see
still like this
um
go ahead and pull this guy here whoops
like that
foreign
specify a little bit
better
what we're working with here if we get a
little further down so we're going to go
through each one again
and we're going to do it by the name
which is going to be so trigger
underscore name is going to be first
let's just do this
it doesn't like that that's not valid
but we'll fix it in a minute
oh well let's just get rid of that
anyways okay so we're gonna have that so
that'll give us our names
and now we're going to say
I need to create trigger
and so let's see
if we can get away with simply
uh where did we start our
uh I need to create query blah blah blah
blah blah blah blah blah
here we go
so here's where we start
[Music]
so what we want to do oops is create a
replace trigger
and I'm using this little uh
let's do uh it's like this
so we can see a little better and maybe
not
so I'm going to use this so we're going
to do create a replace trigger not
Tigger but trigger
and then we're going to give it its
source name
which is whatever the name is and then
Source name so proc name
we're gonna have to take a look at that
now what we've got here is we probably
want to keep that Source name we didn't
do it here
but it really because I think we can
make it
tutorial dot we can do something like
that
um so what we probably want to do is
leave our source name our trigger name
which is
our product name is going to be zero so
let's say that now we're going to do
this before update so
what we can see here there's the event
manipulation
and I bet and there's an action timing
so
let's look at this what we need is
uh trigger name
comma
action timing
it was what before update
uh before update on address okay so
that's going to be the table
okay
so what we're going to need is we're
going to need our
uh
whoops where did I see that before that
was the action timing okay
and then I need
um event manipulation
manipulation
and I need to spell that right
and let's go ahead and get that but now
we're also going to need we need the
object table
we know we're going to need that
and then we're going to need what do we
need next uh let's see
for each row and so what we're probably
going to see is
yeah
whoops that's loud for
nope let's see so I don't think we need
that I think we just need each row
so it's gonna be four each and we need
action orientation
orientation
and then we need we need the command
just going to be the action statement
statement so
that should be well let's do it this way
um
we don't want to lose our little piece
there so we go back up to our
query
is that right yep so that's our trigger
schema
that's our
trigger schema
which I assume is not here anywhere
uh well it is but then do we need
yeah I think we do need that
so let's leave that so we're going to do
create a replace
Source name and then this is going to be
whoops
this does not need that instead it needs
a space because we give it the name
and then we're going to give it it's not
actually proc name but that's okay
uh
oh we want it in palmro okay
so this is going to be a
is that right nope we don't want palmro
uh we don't need that we don't need that
we do need let's make sure this is right
so Source proc name and prox okay so
that's
okay there we go okay so now we're going
to do that plus
now we're gonna have to sort of walk our
way through this one a little bit
so the next thing we're going to do is
the action timing so that gives us a for
example before
plus space
Plus
the event manipulation
and then uh
this is going to be
uh let's do this uh proc
Al equals
proc SQL Plus
so now we've got uh let's see for update
so this is going to be
on and then we've got to give it
our name which is going to be three
which is one Let's see Trigger name two
three zero one two three which is the
event object table cool we got that and
then this is going to be plus
for each and then we're going to have to
give it that scope
which is the action orientation
and then
we're going to add a space let's see it
this way
and then after that we do a space
and we do the statement itself which is
five
and that should do it
so let's try that uh let's see so
we don't need a bar cursor anymore and
let's not worry about executing it
uh let's do yeah let's not worry about
executing it
and uh
uh let's do that so let's come down here
we're gonna do our missing triggers
missing index
missing functions missing procedures
let's move this up a little bit
and this is going to be
missing triggers
and that should all be good that should
be good we're going to spell this right
and then
if we run it oh I'm in the wrong place
there now if I come in and run it
let's see what happens
bam okay so we came in here
and we've got something here uh start
missing triggers command query blah blah
blah 462. okay
oops what did we miss
uh
oops so if we go to 137
oops
uh that's what we don't need
let's try that uh let's go back and try
this real quick did we do that right
oh
okay so it's getting us something
let's try it again
there we go so now first trigger is
going to be create or replace
that guy and then the second we're going
to do it so let's see if those execute
uh that looks good
and let's see if that runs
boom those were created let's see if it
does nothing this time and it does
nothing this time so now we have
provided our triggers
that gives us a solid piece of work here
um let's see I don't need any of that I
don't need those notes I will be
updating this stuff uh
got indexes now
the challenge
is going to become here
when we deal with our rows
now with our current database we're
going to go through and we're just going
to blow stuff away
and so let's see if that even works
right now so if we do it and we say
let's take the whole
thing that we're working with let's see
I don't think I have any more quits I'll
make sure before I
go too far
good okay I don't have any more quits
there's no quitting this
[Music]
and so now we're going to see which it
looks like it's doing pretty good uh
we're getting through here
I probably don't need all of those
insert statements
see let's do
when I do sync rows
uh let's see so I don't want to print
the insert statement
I just want to print
uh
let's do it here
let's do
data sync
for
and give it a table
and so we're going to tweak this a
little bit now here we go so now we're
getting some more stuff
and again not really seeing an error so
it's looking good the challenge becomes
in two cases one is when we have data
that is linked that is uh where you've
got some sort of foreign key
relationship because you cannot delete
the parent before you delete the child
so we have to in that case
if we're going to do something like that
if we're going to actually delete then
what we're going to need to do is we're
going to have to have some way to
essentially find those foreign key
relationships and then walk the key
and so let's put these in our nodes here
we'll have a little bit of notes Here
notes for data sync
so one
is we need to figure out
how to walk the
foreign key
foreign key
ladder basically that's called hierarchy
this thing so going oh yeah it's still
going oh
way down there there we go
so it looked like it all ran
which is good but now we're going to
figure out how to walk the foreign key
hierarchy we need to figure out
um
and that's for you know that cases and
then we also need to do how to
uh let's say import
related data
for a table
Max rows won't work
because for example let's say we only
take the first 10 rows and let's say we
have
um
15 users
but they only use
or actually let's say we have 10 users
and they use across them they have 15
addresses
so when you do a limit of rows which you
know if you go back to what we did when
we did this we just did uh
if we look at
columns saying data or sync rows we just
came in
and we did this limit
so we did the rows and we did a limit
and remember we started at the the most
recent stuff however
that could cause US problems
because we could have a situation where
we are
limiting our rows and we're losing data
that we need you know those 10 customers
may need 15 addresses or what would be a
bigger problem is we grab 10 addresses
but we don't grab them for the right
customers and depending on how that
relationship works then of course that
would break
so we're probably going to need to do
is provide a couple of different ways
and we'll take this down here so we're
going to do
multiple ways
multiple if I can spell it right
multiple ways to limit data moved
because we have to think through that
one and that's going to be a non-trivial
a little piece of work
one of the things I also want to do is
which will probably come sooner is
generalize extract
the key functions
and
provide a different
let's say a way to walk
to walk
multiple databases
because we're going to see this at times
as well
so we want to be able to uh I just want
to say sort of clean up is we're going
to pull some of this stuff out we're
going to clean a couple of these things
up
and we probably want along with that is
we're going to have some parameters
let's see command line parameters
for what to sync
so now we're starting to get a little
more complicated here and then what's
the let's see with the data uh yeah
we'll probably go with that for now so
these are some things coming up I wanted
to sort of list these out and just do
them as part of this you know so it's a
nice little quick kind of episode here
for triggers and we're going to start
getting into the complicated stuff next
and how can we possibly do some of these
things and probably we're going to start
maybe we'll pull some stuff out first
but I think one of the first things
we're probably gonna have to do is we're
going to have to figure out this foreign
key hierarchy is we're going to have to
figure out like what are the the tables
that do not have anything that rely on
them and then figure out what are the
parents and the children and the
grandchildren and such and so forth
we've actually seen some of that already
we've got some of that information it's
just we're going to have to actually
think through it and do it in a little
bit more intentional approach
that being said I think it's time to
wrap this one up so go out there and
have yourself a great day a great week
and we will talk to you next time
[Music]
Transcript Segments
10.7

thank you

18.89

[Music]

27.18

well hello and welcome back we are

29.519

continuing our series of python and SQL

32.759

tutorials we're working on our database

35.64

synchronization and we've worked our way

37.86

up to triggers we looked at proceeds or

41.399

procedures looked at functions and now

43.62

we started looking at it last time

45.3

looked at what triggers sort of looked

47.1

like

48.12

and we're going to see if we can do that

51.239

for our uh we use that to create our

54.36

triggers

55.92

and so what we're going to do is we're

58.02

going to go ahead and

61.379

we're going to do let's see so we've got

64.559

our triggers and we're going to do where

67.74

event

69.32

object schema

72.54

equals because we need to make sure that

75.18

we've got that right

78

here well let's do trigger schema

83.1

so

85.08

we definitely need to get our database

88.32

there

90.619

and that's probably it for right now

95.18

so

97.14

let's take that over what we'll do is

99.6

we're going to end up

102.9

uh let's see

106.259

still like this

108.6

um

110.52

go ahead and pull this guy here whoops

112.68

like that

114.42

foreign

116.899

specify a little bit

119.159

better

122.52

what we're working with here if we get a

124.86

little further down so we're going to go

126.119

through each one again

127.5

and we're going to do it by the name

130.619

which is going to be so trigger

133.319

underscore name is going to be first

139.44

let's just do this

141.72

it doesn't like that that's not valid

143.4

but we'll fix it in a minute

145.26

oh well let's just get rid of that

146.879

anyways okay so we're gonna have that so

148.68

that'll give us our names

151.62

and now we're going to say

153.959

I need to create trigger

158.16

and so let's see

162.48

if we can get away with simply

168.66

uh where did we start our

173.64

uh I need to create query blah blah blah

176.34

blah blah blah blah blah

178.92

here we go

180.18

so here's where we start

181.28

[Music]

182.519

so what we want to do oops is create a

186.9

replace trigger

191.519

and I'm using this little uh

194.64

let's do uh it's like this

198.06

so we can see a little better and maybe

200.099

not

201

so I'm going to use this so we're going

202.2

to do create a replace trigger not

204.42

Tigger but trigger

206.94

and then we're going to give it its

208.5

source name

210.18

which is whatever the name is and then

213.3

Source name so proc name

217.44

we're gonna have to take a look at that

220.62

now what we've got here is we probably

223.14

want to keep that Source name we didn't

225.12

do it here

226.56

but it really because I think we can

229.26

make it

230.659

tutorial dot we can do something like

232.86

that

235.86

um so what we probably want to do is

238.739

leave our source name our trigger name

241.08

which is

242.819

our product name is going to be zero so

244.739

let's say that now we're going to do

246.54

this before update so

250.68

what we can see here there's the event

252.06

manipulation

253.319

and I bet and there's an action timing

256.979

so

260.519

let's look at this what we need is

264.72

uh trigger name

267.6

comma

269.94

action timing

275.82

it was what before update

279.36

uh before update on address okay so

282.6

that's going to be the table

284.759

okay

286.62

so what we're going to need is we're

288.18

going to need our

290.16

uh

292.32

whoops where did I see that before that

294.06

was the action timing okay

297.36

and then I need

299.34

um event manipulation

304.82

manipulation

306.74

and I need to spell that right

310.74

and let's go ahead and get that but now

313.08

we're also going to need we need the

314.699

object table

318.06

we know we're going to need that

322.38

and then we're going to need what do we

324.36

need next uh let's see

326.88

for each row and so what we're probably

330.12

going to see is

332.88

yeah

334.46

whoops that's loud for

340.08

nope let's see so I don't think we need

342.419

that I think we just need each row

349.44

so it's gonna be four each and we need

351.24

action orientation

357.9

orientation

362.46

and then we need we need the command

367.139

just going to be the action statement

371.84

statement so

374.28

that should be well let's do it this way

378.24

um

379.139

we don't want to lose our little piece

381.36

there so we go back up to our

383.88

query

388.5

is that right yep so that's our trigger

390.96

schema

392.94

that's our

394.68

trigger schema

397.02

which I assume is not here anywhere

405

uh well it is but then do we need

412.38

yeah I think we do need that

415.38

so let's leave that so we're going to do

416.88

create a replace

418.38

Source name and then this is going to be

422.9

whoops

424.5

this does not need that instead it needs

427.199

a space because we give it the name

430.139

and then we're going to give it it's not

432.6

actually proc name but that's okay

440.759

uh

443.759

oh we want it in palmro okay

447.24

so this is going to be a

450.96

is that right nope we don't want palmro

454.44

uh we don't need that we don't need that

459.72

we do need let's make sure this is right

465.24

so Source proc name and prox okay so

468.24

that's

472.319

okay there we go okay so now we're going

476.759

to do that plus

479.58

now we're gonna have to sort of walk our

480.96

way through this one a little bit

482.639

so the next thing we're going to do is

484.199

the action timing so that gives us a for

486.72

example before

488.099

plus space

490.56

Plus

492.72

the event manipulation

498.539

and then uh

501.599

this is going to be

504

uh let's do this uh proc

508.319

Al equals

511.199

proc SQL Plus

514.26

so now we've got uh let's see for update

518.399

so this is going to be

520.919

on and then we've got to give it

524.52

our name which is going to be three

531.899

which is one Let's see Trigger name two

534.66

three zero one two three which is the

537.839

event object table cool we got that and

541.26

then this is going to be plus

543.56

for each and then we're going to have to

546

give it that scope

548.64

which is the action orientation

555

and then

557.1

we're going to add a space let's see it

560.16

this way

562.32

and then after that we do a space

565.44

and we do the statement itself which is

568.56

five

571.16

and that should do it

577.44

so let's try that uh let's see so

582.24

we don't need a bar cursor anymore and

584.339

let's not worry about executing it

588.54

uh let's do yeah let's not worry about

590.399

executing it

593.54

and uh

597.959

uh let's do that so let's come down here

599.399

we're gonna do our missing triggers

602.64

missing index

605

missing functions missing procedures

607.94

let's move this up a little bit

611.519

and this is going to be

615.72

missing triggers

620.94

and that should all be good that should

623.1

be good we're going to spell this right

627.24

and then

629.58

if we run it oh I'm in the wrong place

631.92

there now if I come in and run it

634.98

let's see what happens

637.68

bam okay so we came in here

641.64

and we've got something here uh start

644.16

missing triggers command query blah blah

647.94

blah 462. okay

650.339

oops what did we miss

655.8

uh

662.279

oops so if we go to 137

670.019

oops

671.82

uh that's what we don't need

678.72

let's try that uh let's go back and try

681.12

this real quick did we do that right

683.459

oh

685.62

okay so it's getting us something

688.019

let's try it again

692.04

there we go so now first trigger is

694.68

going to be create or replace

697.44

that guy and then the second we're going

699.18

to do it so let's see if those execute

707.779

uh that looks good

710.04

and let's see if that runs

712.8

boom those were created let's see if it

714.839

does nothing this time and it does

716.22

nothing this time so now we have

718.74

provided our triggers

722.76

that gives us a solid piece of work here

727.26

um let's see I don't need any of that I

728.82

don't need those notes I will be

730.2

updating this stuff uh

734.1

got indexes now

736.86

the challenge

739.74

is going to become here

742.2

when we deal with our rows

745.32

now with our current database we're

748.5

going to go through and we're just going

749.339

to blow stuff away

750.899

and so let's see if that even works

752.399

right now so if we do it and we say

753.779

let's take the whole

755.88

thing that we're working with let's see

758.16

I don't think I have any more quits I'll

759.839

make sure before I

761.279

go too far

763.98

good okay I don't have any more quits

766.2

there's no quitting this

768.74

[Music]

770.16

and so now we're going to see which it

772.079

looks like it's doing pretty good uh

773.88

we're getting through here

778.62

I probably don't need all of those

780.36

insert statements

785.279

see let's do

789.72

when I do sync rows

792.779

uh let's see so I don't want to print

794.82

the insert statement

797.94

I just want to print

802.8

uh

808.62

let's do it here

812.04

let's do

815.04

data sync

817.5

for

819.54

and give it a table

822.6

and so we're going to tweak this a

824.399

little bit now here we go so now we're

826.139

getting some more stuff

827.7

and again not really seeing an error so

829.86

it's looking good the challenge becomes

832.2

in two cases one is when we have data

836.22

that is linked that is uh where you've

839.639

got some sort of foreign key

840.42

relationship because you cannot delete

843.72

the parent before you delete the child

847.98

so we have to in that case

851.1

if we're going to do something like that

852.36

if we're going to actually delete then

854.399

what we're going to need to do is we're

855.899

going to have to have some way to

858.56

essentially find those foreign key

860.76

relationships and then walk the key

863.88

and so let's put these in our nodes here

865.92

we'll have a little bit of notes Here

868.8

notes for data sync

873

so one

875.279

is we need to figure out

877.86

how to walk the

881.3

foreign key

884.399

foreign key

888.32

ladder basically that's called hierarchy

895.199

this thing so going oh yeah it's still

896.699

going oh

898.399

way down there there we go

901.32

so it looked like it all ran

904.199

which is good but now we're going to

906.72

figure out how to walk the foreign key

908.04

hierarchy we need to figure out

911.76

um

912.839

and that's for you know that cases and

915.54

then we also need to do how to

919.5

uh let's say import

922.019

related data

929.399

for a table

933.24

Max rows won't work

936.36

because for example let's say we only

939.36

take the first 10 rows and let's say we

942.18

have

943.26

um

944.16

15 users

945.66

but they only use

948.06

or actually let's say we have 10 users

950.519

and they use across them they have 15

953.1

addresses

955.139

so when you do a limit of rows which you

958.86

know if you go back to what we did when

960.899

we did this we just did uh

964.38

if we look at

967.339

columns saying data or sync rows we just

971.22

came in

972.24

and we did this limit

974.339

so we did the rows and we did a limit

976.44

and remember we started at the the most

978.48

recent stuff however

981.18

that could cause US problems

983.22

because we could have a situation where

985.32

we are

986.88

limiting our rows and we're losing data

989.76

that we need you know those 10 customers

992.94

may need 15 addresses or what would be a

996.899

bigger problem is we grab 10 addresses

999

but we don't grab them for the right

1000.38

customers and depending on how that

1002.42

relationship works then of course that

1004.279

would break

1005.839

so we're probably going to need to do

1008.24

is provide a couple of different ways

1011.899

and we'll take this down here so we're

1014.66

going to do

1017.74

multiple ways

1022.639

multiple if I can spell it right

1024.62

multiple ways to limit data moved

1029.839

because we have to think through that

1030.86

one and that's going to be a non-trivial

1033.74

a little piece of work

1036.14

one of the things I also want to do is

1038.179

which will probably come sooner is

1040.72

generalize extract

1044.6

the key functions

1047.959

and

1049.64

provide a different

1054.2

let's say a way to walk

1060.5

to walk

1062.2

multiple databases

1065.48

because we're going to see this at times

1066.799

as well

1069.559

so we want to be able to uh I just want

1072.62

to say sort of clean up is we're going

1073.94

to pull some of this stuff out we're

1075.02

going to clean a couple of these things

1076.64

up

1077.6

and we probably want along with that is

1080

we're going to have some parameters

1082.52

let's see command line parameters

1088.299

for what to sync

1094.28

so now we're starting to get a little

1095.48

more complicated here and then what's

1098

the let's see with the data uh yeah

1100.82

we'll probably go with that for now so

1102.02

these are some things coming up I wanted

1104.419

to sort of list these out and just do

1106.4

them as part of this you know so it's a

1108.08

nice little quick kind of episode here

1110.36

for triggers and we're going to start

1112.76

getting into the complicated stuff next

1114.38

and how can we possibly do some of these

1118.34

things and probably we're going to start

1120.62

maybe we'll pull some stuff out first

1122.299

but I think one of the first things

1123.799

we're probably gonna have to do is we're

1125.059

going to have to figure out this foreign

1126.38

key hierarchy is we're going to have to

1129.14

figure out like what are the the tables

1132.08

that do not have anything that rely on

1134.539

them and then figure out what are the

1136.039

parents and the children and the

1137.299

grandchildren and such and so forth

1139.46

we've actually seen some of that already

1141.32

we've got some of that information it's

1144.08

just we're going to have to actually

1145.1

think through it and do it in a little

1146.96

bit more intentional approach

1150.14

that being said I think it's time to

1152.059

wrap this one up so go out there and

1153.799

have yourself a great day a great week

1155.6

and we will talk to you next time

1159.49

[Music]