📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 17

2022-10-11 •Youtube

Detailed Notes

Focus for this episode: In this episode, we look at a sync for functions between our 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
[Music]
well hello and welcome back we're
continuing our series our
yeah i guess it's our series of
sql tutorials and then we had some
python tutorials actually i think we did
those reverse order but now we're
combining those into our database
synchronization script this is in python
and this is where we're
basically looking at one set of
uh one database and it's tables and
functions and things like that
and
data and being able to replicate that
into another one actually synchronize it
in
in particular it's just great if you're
doing development and things of that
nature it's one of those tools i've
wanted to have for a while and figured
i'm going to go ahead and build it
myself
this episode we're going to continue
sort of along the lines what we're
looking at last time we were doing store
procedures
and i think we're going to find that for
functions
it's going to be very similar so if we
look at what we did for
our store procedures
then what we're going to have here
is we should be able to do essentially
the same thing and if we look in our
database
let's see
well let's try this let's do without
routine
yeah that's like everything in the world
so what we do need is we need our schema
and let me see if i can do this i can
read it
uh oh yeah tutorial
did i have that twice uh oh one source
once destination okay that makes sense
so here we go for our procedures we can
see how that works now let's see if
i bet if we call it function we're gonna
see
our functions boom and we have one
function
add three so let's go ahead and see
if we can follow that thought through
and do this for functions
so did i say function i said function
so i'm going to come through here and do
function
and it's going to be very simple so i
need to create a function
we're just going to leave it as we'll
leave our variable names the same
and let's actually jump all the way down
here
where we did missing procedures
this is going to be
missing functions
this would be great if this is one of
those things where
there we go so let's go back up here be
great if this is one of those we can
pretty quickly
replicate our functionality
and cover more so now we're going to
come in here and then when we create it
we've got to get the definitions so
we're going to do the same thing here
and this one's going to be a little more
complicated let's see
okay so let's start with this let's try
to get our
source
so if we try to go get our definition
function schema name is going to be
tutorial
tutorial
and procedure name is going to be add
three
and if we do that let's see if that
works
it does look it's very adding three so
it's a very simple one we look at our
source so now let's take the same thing
and we built
our variables here
ah see and i think oh we may be able to
do this exactly the same
uh and this is gonna be create a replace
function
so what we do need here
is we're going i don't know if it'll
show me the
uh let's see if i'll do script function
clipboard
create let's go look at what yeah see i
don't know it doesn't like that right
now
so
let's take this
uh specific schema
this tutorial so we'll just put that
there
specific name it's going to be the
function which is add three
so let's see oh
i just saw that there it doesn't like
that let's do this
okay so we have our parameters we've got
one parameter so now what we need to
know
and right here so it's giving us return
so we should be able to build our
function completely
based on
that so let's see what happens
and if we run it okay so it doesn't
uh
oh
let's see if we do
so here it was saying
[Music]
so that's what it's saying that we
should be able to do
so let's do
that
so for now it's going to be local uh so
if we go to our
[Music]
local mysql which i never can find right
somewhere in here a little my sequel rep
that's not really the one i want but
that'll probably do
um so if we do this let's do this
okay oh it doesn't like that so let me
go change to there we go
okay so we have create a replace
function
and we do it again
and if we take that i think we want to
do the entire
let's see
uh
let's run here
okay so let's go
jump to
oh not that
okay we want to do a
function
[Music]
and we're missing something so let's go
do a quick look here
and we're going to go
here
and
uh
and there it is so we're gonna do cat
day 19
and let's see
ah so see we have this returns
so we need our return
so
so let's go
let's see
let's see
let's do
let's see if we can
stored function
uh chrissy let's see
so my sequel where is the return
value stored for a function
and
and
so we're gonna have to do i think we
have to do
a little bit of research
so if we go back over here up here here
where oh
that one
so we need
parameters now let's go look at our
information schema
and let's see if we have return values
we
have
routines
but that's what we're only looking at um
let's see let's go here let's uh select
star
from routines
for this one
and let's see if we can find our
returns afloat
here we go so that's data type
and let's see if it tells me
deterministic doesn't tell me that
so if i take the same thing and i do
uh returns float
oh that probably will work so if i come
over here and i do
um
that comes after this
returns
float
and run it uh do i need a
nope should be okay
there okay so it runs so now i can do
drop function
add three
uh tutorial dot add three
and wrong
there we go okay so i need to get my
returns so what i need to do is from the
uh whoops from the
so when i get the information schema i
need the routine name but i also want
the
data type
oh i don't want that i don't want to
save to a random file
so up here
routine name comma data type
and i might as well get it from both
and then i'm going to come into ho so
there's my there's that
so here
uh what i want is
so that's a destination so here
okay so i've got proc name 0 but it's
actually proc name 1 is what i'm going
to need for this so i'm going to need
once i build all of these
i'm going to need to do a returns
plus
proc name one
plus
a space
and i should be good
let's see
if that works for me
uh if i come here and i do a main
whoa
it worked
so now i have on my local
um well
if i can go here and if i go local
and
let's find all my functions i'm going to
see that boom i've got my function that
i just created a few moments ago
and i should be ready so if i look at
that same one
and demo he should be the same thing uh
let's do this
we'll just store these values just to be
safe
and we can see
function who built it dates definer
get your little character set stuff but
we're good
so if we go to our
uh select
is going to be
a little more complicated but
um
we'll probably leave it for now because
i think we've got what we do is we have
what we need so now if we do call
and three i'm in the right one no i want
to do it locally
local call add three two five
uh oops i need to select my database
and
uh tutorial ad three does not exist
um
oh it's probably c so let's see
select that's what i want
that's it okay
i forgot it's not a call
and so now what we have done is we have
fairly quickly gotten our procedures
added in
which helps us quite a bit which leaves
us
really with triggers are probably the
thing that we're going to hit next and i
don't know that i have any
let's see if we got any in our little
testing oh we do so i've got a pair of
triggers
let's see
while we're here
because we probably won't get it this
time but we can go
triggers
so let's do a little select star
from triggers
and we're going to look for
uh named
my
first or actually put trigger in the
name
or my so if we do select star uh
tutorial.oops
start from informationschema.triggers
there we go oh so it doesn't have any in
this so we are not seeing that so let's
go
let's find a select
distinct
routine
type
from information schema routine so see
if we've got something else here that we
can look at
oops i should spell that right
oh
local i don't want the local i want the
demo
i don't want
tutorial
let's go see maybe
there we go
so i've got my triggers
so that's going to be our next thing
we're going to do is it looks like we're
going to be able to dive into that we
actually have our action statement we
have our condition so we'll have to go
back and look at how do we create a
trigger
and then i think we've got what we need
so we'll be able to do that build that
out
and start getting those synchronized as
well so we're getting pretty far
uh we're really down to you know the big
thing is going to be we're going to
swing back
and deal with uh some of our data issues
as far as doing partial data because
there's going to be some there's going
to be some challenges there
uh and some of the things we do
particularly because we were cleaning
the data out you have some things have
to be done in a specific order
that being said
we can call this one a wrap
so we'll wrap this up we'll get it out
there stuff will be out in the show
links as always we will you know we'll
have updates she can go dig through and
find it at our github account you can
check out the source for this
and play around and we will be adding
more stuff down the road here as we're
going through it
to make it even more general purpose and
maybe even useful for you
so that being said go out there and have
yourself a great day a great week and we
will talk to you
next time
[Music]
you
Transcript Segments
0.43

[Music]

27.199

well hello and welcome back we're

28.72

continuing our series our

30.8

yeah i guess it's our series of

33.52

sql tutorials and then we had some

36.079

python tutorials actually i think we did

37.6

those reverse order but now we're

39.28

combining those into our database

41.76

synchronization script this is in python

44.559

and this is where we're

45.84

basically looking at one set of

49.12

uh one database and it's tables and

51.92

functions and things like that

54.079

and

54.719

data and being able to replicate that

57.36

into another one actually synchronize it

59.359

in

60.16

in particular it's just great if you're

61.6

doing development and things of that

62.879

nature it's one of those tools i've

64.72

wanted to have for a while and figured

67.119

i'm going to go ahead and build it

68.4

myself

69.36

this episode we're going to continue

71.6

sort of along the lines what we're

72.72

looking at last time we were doing store

74.24

procedures

75.52

and i think we're going to find that for

77.6

functions

78.72

it's going to be very similar so if we

80.64

look at what we did for

83.6

our store procedures

87.36

then what we're going to have here

89.92

is we should be able to do essentially

91.2

the same thing and if we look in our

93.04

database

96.24

let's see

98.079

well let's try this let's do without

100.32

routine

104

yeah that's like everything in the world

105.36

so what we do need is we need our schema

110

and let me see if i can do this i can

111.759

read it

114.479

uh oh yeah tutorial

122.24

did i have that twice uh oh one source

124.96

once destination okay that makes sense

126.719

so here we go for our procedures we can

128.959

see how that works now let's see if

132.48

i bet if we call it function we're gonna

134.72

see

136.56

our functions boom and we have one

139.12

function

141.04

add three so let's go ahead and see

145.04

if we can follow that thought through

149.84

and do this for functions

152.72

so did i say function i said function

160.48

so i'm going to come through here and do

162.239

function

163.68

and it's going to be very simple so i

164.959

need to create a function

166.72

we're just going to leave it as we'll

167.84

leave our variable names the same

171.68

and let's actually jump all the way down

174.08

here

176.08

where we did missing procedures

178.8

this is going to be

180.239

missing functions

186.8

this would be great if this is one of

188.64

those things where

193.36

there we go so let's go back up here be

195.519

great if this is one of those we can

196.72

pretty quickly

198.48

replicate our functionality

201.2

and cover more so now we're going to

202.72

come in here and then when we create it

204.48

we've got to get the definitions so

206.4

we're going to do the same thing here

212.159

and this one's going to be a little more

213.519

complicated let's see

218.959

okay so let's start with this let's try

220.56

to get our

221.76

source

225.2

so if we try to go get our definition

227.92

function schema name is going to be

230.799

tutorial

234.64

tutorial

240.56

and procedure name is going to be add

242.56

three

250.4

and if we do that let's see if that

252.239

works

254.959

it does look it's very adding three so

257.28

it's a very simple one we look at our

259.44

source so now let's take the same thing

260.959

and we built

264.08

our variables here

269.28

ah see and i think oh we may be able to

272.4

do this exactly the same

275.36

uh and this is gonna be create a replace

277.04

function

282.72

so what we do need here

284.8

is we're going i don't know if it'll

285.919

show me the

289.52

uh let's see if i'll do script function

291.84

clipboard

293.759

create let's go look at what yeah see i

296.24

don't know it doesn't like that right

297.6

now

298.4

so

300.8

let's take this

303.759

uh specific schema

309.52

this tutorial so we'll just put that

311.84

there

314

specific name it's going to be the

316.4

function which is add three

320.24

so let's see oh

322

i just saw that there it doesn't like

323.84

that let's do this

327.68

okay so we have our parameters we've got

329.28

one parameter so now what we need to

330.96

know

335.52

and right here so it's giving us return

337.36

so we should be able to build our

339.68

function completely

342.72

based on

344.479

that so let's see what happens

349.759

and if we run it okay so it doesn't

352.56

uh

359.039

oh

360.479

let's see if we do

370.16

so here it was saying

372.96

[Music]

378.4

so that's what it's saying that we

380

should be able to do

381.84

so let's do

385.28

that

387.12

so for now it's going to be local uh so

389.36

if we go to our

390.48

[Music]

392.08

local mysql which i never can find right

398.72

somewhere in here a little my sequel rep

401.6

that's not really the one i want but

405.199

that'll probably do

406.639

um so if we do this let's do this

410.319

okay oh it doesn't like that so let me

412.56

go change to there we go

419.36

okay so we have create a replace

421.919

function

425.12

and we do it again

429.68

and if we take that i think we want to

431.36

do the entire

436.24

let's see

440.319

uh

441.599

let's run here

445.199

okay so let's go

446.8

jump to

448.08

oh not that

455.44

okay we want to do a

458.16

function

458.86

[Music]

460.319

and we're missing something so let's go

462.16

do a quick look here

467.68

and we're going to go

469.44

here

471.28

and

472

uh

475.919

and there it is so we're gonna do cat

478.08

day 19

482.319

and let's see

485.28

ah so see we have this returns

490.16

so we need our return

492.4

so

494.8

so let's go

496.08

let's see

497.759

let's see

501.84

let's do

506

let's see if we can

510.639

stored function

512.08

uh chrissy let's see

514.88

so my sequel where is the return

519.279

value stored for a function

526.56

and

534.88

and

535.76

so we're gonna have to do i think we

537.76

have to do

538.959

a little bit of research

541.76

so if we go back over here up here here

545.839

where oh

547.76

that one

549.68

so we need

552.32

parameters now let's go look at our

554.48

information schema

556.88

and let's see if we have return values

561.36

we

562.399

have

564.959

routines

571.279

but that's what we're only looking at um

577.12

let's see let's go here let's uh select

580.399

star

584.48

from routines

586.48

for this one

589.6

and let's see if we can find our

594.24

returns afloat

596.48

here we go so that's data type

603.04

and let's see if it tells me

606.16

deterministic doesn't tell me that

608.959

so if i take the same thing and i do

611.519

uh returns float

614.079

oh that probably will work so if i come

616.32

over here and i do

618.88

um

620.959

that comes after this

623.92

returns

625.36

float

628.399

and run it uh do i need a

630.959

nope should be okay

634

there okay so it runs so now i can do

637.6

drop function

640.079

add three

643.36

uh tutorial dot add three

648.079

and wrong

649.6

there we go okay so i need to get my

652.079

returns so what i need to do is from the

656.959

uh whoops from the

660.64

so when i get the information schema i

663.279

need the routine name but i also want

665.92

the

666.72

data type

670.24

oh i don't want that i don't want to

671.6

save to a random file

673.12

so up here

677.6

routine name comma data type

682.48

and i might as well get it from both

690.72

and then i'm going to come into ho so

692.48

there's my there's that

695.04

so here

696.64

uh what i want is

704.24

so that's a destination so here

708.56

okay so i've got proc name 0 but it's

710.64

actually proc name 1 is what i'm going

712.399

to need for this so i'm going to need

715.68

once i build all of these

721.92

i'm going to need to do a returns

727.2

plus

730.56

proc name one

736.88

plus

738.24

a space

740.639

and i should be good

742.56

let's see

743.76

if that works for me

746.079

uh if i come here and i do a main

749.519

whoa

750.639

it worked

752

so now i have on my local

756.48

um well

758.48

if i can go here and if i go local

762.8

and

763.839

let's find all my functions i'm going to

765.68

see that boom i've got my function that

767.36

i just created a few moments ago

770.16

and i should be ready so if i look at

771.76

that same one

773.2

and demo he should be the same thing uh

777.2

let's do this

778.959

we'll just store these values just to be

780.56

safe

783.36

and we can see

784.959

function who built it dates definer

789.04

get your little character set stuff but

791.12

we're good

793.519

so if we go to our

796.48

uh select

800.079

is going to be

802.639

a little more complicated but

804.8

um

806.16

we'll probably leave it for now because

807.279

i think we've got what we do is we have

808.72

what we need so now if we do call

811.68

and three i'm in the right one no i want

814.56

to do it locally

817.04

local call add three two five

823.2

uh oops i need to select my database

829.44

and

830.56

uh tutorial ad three does not exist

839.04

um

845.68

oh it's probably c so let's see

849.519

select that's what i want

851.92

that's it okay

853.76

i forgot it's not a call

855.68

and so now what we have done is we have

858.16

fairly quickly gotten our procedures

860.8

added in

862.8

which helps us quite a bit which leaves

864.88

us

866.24

really with triggers are probably the

867.839

thing that we're going to hit next and i

869.92

don't know that i have any

873.12

let's see if we got any in our little

875.04

testing oh we do so i've got a pair of

876.88

triggers

880.079

let's see

881.68

while we're here

883.04

because we probably won't get it this

884.48

time but we can go

887.04

triggers

888.48

so let's do a little select star

891.92

from triggers

894.32

and we're going to look for

897.12

uh named

899.36

my

900.48

first or actually put trigger in the

902

name

903.199

or my so if we do select star uh

906.16

tutorial.oops

908.88

start from informationschema.triggers

918.399

there we go oh so it doesn't have any in

922.32

this so we are not seeing that so let's

925.04

go

927.04

let's find a select

930.8

distinct

932.88

routine

934.72

type

938.16

from information schema routine so see

940

if we've got something else here that we

941.6

can look at

943.6

oops i should spell that right

948.079

oh

949.199

local i don't want the local i want the

950.72

demo

951.759

i don't want

952.839

tutorial

954.48

let's go see maybe

956.48

there we go

958.32

so i've got my triggers

960

so that's going to be our next thing

961.199

we're going to do is it looks like we're

962.399

going to be able to dive into that we

964.48

actually have our action statement we

965.92

have our condition so we'll have to go

968

back and look at how do we create a

970

trigger

971.12

and then i think we've got what we need

973.519

so we'll be able to do that build that

975.12

out

976.079

and start getting those synchronized as

978.48

well so we're getting pretty far

980.24

uh we're really down to you know the big

981.92

thing is going to be we're going to

983.36

swing back

984.56

and deal with uh some of our data issues

987.12

as far as doing partial data because

989.12

there's going to be some there's going

990.399

to be some challenges there

992.079

uh and some of the things we do

993.199

particularly because we were cleaning

994.399

the data out you have some things have

996.8

to be done in a specific order

999.199

that being said

1000.48

we can call this one a wrap

1002.8

so we'll wrap this up we'll get it out

1004.639

there stuff will be out in the show

1006.32

links as always we will you know we'll

1008.079

have updates she can go dig through and

1010.48

find it at our github account you can

1012.56

check out the source for this

1014.16

and play around and we will be adding

1016

more stuff down the road here as we're

1017.519

going through it

1018.8

to make it even more general purpose and

1021.12

maybe even useful for you

1022.88

so that being said go out there and have

1024.959

yourself a great day a great week and we

1027.6

will talk to you

1029.199

next time

1030.859

[Music]

1046.4

you