📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 22

2022-10-27 •Youtube

Detailed Notes

Focus for this episode: We do some code cleanup and create an easier framework for running this script across a wide range of environments in this episode.

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're
continuing our series of basically this
combo tutorial SQL and python and we're
getting deeper into this we've got you
know a basic application working where
we can can copy some uh tables and
synchronize them and deal with the
columns the data
the triggers store procedures functions
you know all that good stuff
and now what I want to do is expand on
this a little bit now we started
um you know cleaning this up a little
bit we got into this where we've got now
this MySQL synchronized class
and what I want to do now is I'm going
to pull a couple of things out to make
this a little bit more
uh generic I guess so that we can put
a little more into our main script and
um not so much that specific in our uh
you know our class itself one of the
things we're going to add this time is
we're going to have uh we can take the
database name for the source and the
destination this is something that will
allow us at some point maybe even to do
from one database to another so it
doesn't have to have the same database
name but more importantly right now
we're going to do
is we're going to come in here and this
is our little script
and what we're going to do here is we're
going to create
uh we're going to call it we'll call it
databases
and what we're going to do is we're
going to have a list of databases that
we're going to actually synchronize and
let's do three of them so we're going to
have tutorial that we have always used
we're going to use uh let's see what
else do we want to do let's use uh
giganator
database there and let's use I think I
had another one oh let's use H goals
it's a nice simple one
those are just because these are smaller
databases
that's what we want to be able to do now
is we're going to say
4 DB oops in
databases
and now we're going to do is we're going
to go through each of the databases and
synchronize each one of them now this
may or may not be something that you
need but if you're trying to do a server
that's got either you know maybe you've
split your data into a couple database
data into a couple of different
databases or you've got like a you know
like a Dev server or something like that
that you want to synchronize some data
across you know beyond a single database
this is going to allow you to do it and
what we need to do here is we just
actually we don't even need that
so we're going to come in each time
we're going to do a MySQL synchronize
we're going to go ahead and set the
database names and here both of them are
equal to the same one so it's pretty
straightforward
and then we do have to come through here
there's a couple places where we used
like here
before we sent the database name in
and we're just going to do in both cases
we're just going to send that in
technically we probably don't even need
that
because now we could use the
databases
database names that we've stored as
parameters so let's actually go look at
that let me see make sure so let's look
at missing triggers
triggers
so here we have the we've got a source
name and a destination name what we can
do
is we can say Source name equals self
dot Source database name
and then destination name equals
self Dot
destination DB name
and now we don't need those
parameters at all
so we come back to our main and that was
for missing triggers
we also have that for missing procedures
and missing functions so let's do that
for each of those
so we've got less stuff we're having to
pass around missing procedures missing
functions
the same procedures
uh let's see whoops
let's go back to
where is that missing functions
missing triggers here we go
so we can actually clean these up pretty
easy
and I've come up here to our missing
procedures
and paste and we're good there now uh
one of the things I need to do is I need
to check to see if these databases even
exist locally
now what we could do
is you know check in our source and say
hey if we don't have a database that
exists in the destination go ahead and
create the database but we're gonna pass
on that because it's sort of a we could
do it but right now we're not going to
bother with that too much so we're going
to go in and manually create those if we
need to so
uh let's see if I can log into my
database oops
oops let me make sure I pass password is
typed correctly there we go sure if I
need to show databases
okay so here I have tutorial I did not
have giganator H goals so let's do that
create database H goals
and so we're just going to pull those
directly across again later
and let's go ahead and do our
synchronized rows
oh and here we use the DB again
uh so let's go to syncrose
[Music]
so it is the destination database name
which is uh let's see I think I used it
here
oh there we go
I guess I could have just used it
instead
so that's going to be a destination
DB name
and let's see let's make sure I use that
right
oh because I just stepped on that name
let's see
let's see
oh I may not need that in Sacramento so
I wonder why I sent that
may not need it at all
so I'm going to drop the table
Target tables
okay so we're probably okay there
so let's see so we go through each of
these we're going to go ahead and just
take all the rows so we're going to run
this through all the way now
um
Let's see we come through we're going to
connect to the source
um
and here's something else I'm going to
do
is let me go ahead and create a settings
file
and actually now
instead of all the way up here before
what we were doing is we were just
creating the configuration as it existed
and so we would come in here and
we'd settle our values so what I'm going
to do now is I'm just going to keep this
pretty simple and from settings I'm
going to import The Source config and
the cart Target config so you go Source
config and then Target config
and import those in and then I'm going
to send those into the Connect Now
technically now I could do two different
I could do one connect and then just
pass back
you know assign out the connector that
comes back
but I'll keep it as source and
destination just for I don't know
cleanliness understanding
and now we've got that so we've got
we've extracted our setups into our
configuration settings into a settings
file
so we're going to come in we're going to
pull that in
what we could do is we could even
pull the databases out
bases
and if I go to settings I'll just push
that up here
and so now you have everything actually
extracted out into
a settings file or at least a lot more
of it now what we're also going to have
here is let's go ahead and do Max rows
and we'll pull that out as well so now
there's just less we have to worry about
so now in here you're starting to get
something that's pretty darn clean uh
and it's sort of a generic kind of
script that we have got created out
which is
um
one of the things we wanted to run into
here uh here we go generalize extract
command line parameters we actually are
going to pull those out into a settings
file
now let's go ahead and run this
and what we will see
um
uh actually let me not sync the data so
let's do that first without
I'm just going to get rid of synchros
not going to use that right now now what
we can do is we can actually pull little
Flags in and put the settings to be a
whole series of flags whether we do
triggers procedures functions indexes
and all that kind of good stuff right
now we're not going to do it that way
we're going to just create our database
real quick let's see so
this is Main
and
whoops
[Music]
what did I miss oh gosh I didn't even
lose this
I used to lose one of my imports so now
there we go there we go and boom so now
if we go look at uh locally refresh this
all right let's just reconnect because
we probably need to so now we can see
that we have a giganator that doesn't
have any tables oh interesting and we
probably have an H goals that maybe
doesn't have any tables nope it's not
how many tables so
let's go see what we got here
oh failed to create index
hmm
so for some reason
oh probably because it's already there
so what we want to do in missing indexes
oh well that says it should be
already missing so we've got something
there that didn't quite work
but we did a try so it came in what did
it do oh interesting
so we come out we do missing indexes for
DB in databases
let's do this
um
current DB
and let's see what it does here
yeah so for some reason
index
oh we're missing something here uh oh oh
oh oh oh oh oh oh oh oh oh oh oh it's
because the database there we have to
change the database for each of those
uh so that's going to be
so what we really want to do here
is that the source config
which I think we can do here so let's do
uh Source config
but first what we want to do is we want
to do Source config
um
let's see if we can do
database
equals DB
and then we're just going to quit
because we're going to have to update
our database there
oh
look at that
yeah we can't assign that so it's not a
function call
try that
there you go so let's see we need to set
so we've got to do a dictionary key kind
of thing here
and let's do this uh see
python set dictionary
value
oh that's right I think it's a get and a
set assigning values
so
oh that's okay yeah that's okay because
that is correct because that is the
first
uh so let's go here
uh let's see Target config
well don't need that don't need that now
let's take a look
so take a look at this again
let's do it this way let's go to whoop
settings
and let's just go ahead and get rid of
that as the first one
okay
so now if we come in
and let's do this okay so let's do print
DB
foreign
so let's see how this works
see what we got here okay so it does
that
and then let's print
Source config
and we've got our database has changed
so we should be good
so if we look at
let's go to missing tables
[Music]
tables whoop
and we will look real quick so it says
here it's going to be print for a table
that's in missing tables
for each missing table get details but
we've got to get self dot missing tables
which we do so we do set missing tables
uh here we go
so we do compare tables
so let's do that we'll do set missing
tables let's just do this real quick
self
dot Source tables
and Target tables
let's go look at that real quick
and we'll see
so there you go so we have oh we have
source and Target flipped
somehow we got source and Target flip so
we go here
is that right
uh oh
that is flipped so
that's Target
that Source it's amazing how that works
out so let's try that again
uh let's see so now if we go into Main
and we run it
wow it works amazingly better when you
get your settings record properly so now
we're going to see that this is going to
update
and we're starting to see all of our
little tables show up
so I think we'll wrap it up this time
um I'll probably go back and edit a
little bit shorten this out a little bit
but we uh have straightened that out in
our settings and now we're actually able
to to send multiple databases across and
do our synchronizing uh actually what
I'll do is I will sync rows
and
um we'll kick this off and it'll
probably be pretty close to next time
before this is all done anyways and
we'll get all that stuff taken care of
and we're gonna come back and we're
probably going to dive into one of the
more complicated things here uh dealing
with enumerated values for a column
because that is something we've run into
now a couple of times and I want to make
sure we get that one fixed
that being said 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.24

well hello and welcome back we're

29.22

continuing our series of basically this

32.099

combo tutorial SQL and python and we're

36.18

getting deeper into this we've got you

38.579

know a basic application working where

40.379

we can can copy some uh tables and

43.62

synchronize them and deal with the

46.5

columns the data

48.899

the triggers store procedures functions

52.02

you know all that good stuff

54.78

and now what I want to do is expand on

57.3

this a little bit now we started

60

um you know cleaning this up a little

61.5

bit we got into this where we've got now

63.18

this MySQL synchronized class

66.36

and what I want to do now is I'm going

70.439

to pull a couple of things out to make

73.08

this a little bit more

75.18

uh generic I guess so that we can put

79.5

a little more into our main script and

83.52

um not so much that specific in our uh

86.58

you know our class itself one of the

88.979

things we're going to add this time is

90

we're going to have uh we can take the

91.979

database name for the source and the

95.159

destination this is something that will

97.86

allow us at some point maybe even to do

99.78

from one database to another so it

102.6

doesn't have to have the same database

103.74

name but more importantly right now

105.84

we're going to do

107.159

is we're going to come in here and this

109.92

is our little script

112.14

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

113.759

going to create

114.96

uh we're going to call it we'll call it

117.6

databases

120.72

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

121.92

going to have a list of databases that

123.299

we're going to actually synchronize and

125.579

let's do three of them so we're going to

126.96

have tutorial that we have always used

129.66

we're going to use uh let's see what

132.66

else do we want to do let's use uh

135.18

giganator

139.8

database there and let's use I think I

143.459

had another one oh let's use H goals

145.319

it's a nice simple one

147.84

those are just because these are smaller

150.3

databases

151.56

that's what we want to be able to do now

153.06

is we're going to say

154.52

4 DB oops in

159.12

databases

162.18

and now we're going to do is we're going

163.44

to go through each of the databases and

164.94

synchronize each one of them now this

166.92

may or may not be something that you

168.599

need but if you're trying to do a server

171.06

that's got either you know maybe you've

174.12

split your data into a couple database

175.58

data into a couple of different

177.66

databases or you've got like a you know

180.54

like a Dev server or something like that

181.92

that you want to synchronize some data

183.48

across you know beyond a single database

187.019

this is going to allow you to do it and

189.239

what we need to do here is we just

190.92

actually we don't even need that

193.62

so we're going to come in each time

194.879

we're going to do a MySQL synchronize

196.92

we're going to go ahead and set the

198.54

database names and here both of them are

201.9

equal to the same one so it's pretty

203.519

straightforward

204.72

and then we do have to come through here

208.379

there's a couple places where we used

210.54

like here

212.819

before we sent the database name in

216.06

and we're just going to do in both cases

218.4

we're just going to send that in

219.739

technically we probably don't even need

222.54

that

224.459

because now we could use the

228.08

databases

229.799

database names that we've stored as

232.08

parameters so let's actually go look at

234.48

that let me see make sure so let's look

237.18

at missing triggers

243.379

triggers

245.22

so here we have the we've got a source

247.739

name and a destination name what we can

249.72

do

252.06

is we can say Source name equals self

255.239

dot Source database name

258.479

and then destination name equals

261.9

self Dot

263.58

destination DB name

266.58

and now we don't need those

269.759

parameters at all

272.28

so we come back to our main and that was

275.22

for missing triggers

277.86

we also have that for missing procedures

280.32

and missing functions so let's do that

282.6

for each of those

284.16

so we've got less stuff we're having to

286.02

pass around missing procedures missing

288.96

functions

293.699

the same procedures

296.54

uh let's see whoops

300.6

let's go back to

304.56

where is that missing functions

309.78

missing triggers here we go

316.139

so we can actually clean these up pretty

318.479

easy

321.24

and I've come up here to our missing

322.86

procedures

330.68

and paste and we're good there now uh

334.38

one of the things I need to do is I need

336.72

to check to see if these databases even

338.639

exist locally

340.979

now what we could do

343.979

is you know check in our source and say

346.68

hey if we don't have a database that

348.84

exists in the destination go ahead and

351.24

create the database but we're gonna pass

355.44

on that because it's sort of a we could

358.08

do it but right now we're not going to

360.12

bother with that too much so we're going

361.139

to go in and manually create those if we

362.94

need to so

364.979

uh let's see if I can log into my

367.139

database oops

374.639

oops let me make sure I pass password is

377.82

typed correctly there we go sure if I

380.46

need to show databases

382.74

okay so here I have tutorial I did not

385.68

have giganator H goals so let's do that

388.74

create database H goals

393.96

and so we're just going to pull those

395.46

directly across again later

403.759

and let's go ahead and do our

406.56

synchronized rows

411.06

oh and here we use the DB again

414.539

uh so let's go to syncrose

419.27

[Music]

422.759

so it is the destination database name

426.3

which is uh let's see I think I used it

429.84

here

432.66

oh there we go

436.5

I guess I could have just used it

438.9

instead

448.02

so that's going to be a destination

452.58

DB name

458.66

and let's see let's make sure I use that

461.819

right

470.18

oh because I just stepped on that name

472.74

let's see

483.539

let's see

493.8

oh I may not need that in Sacramento so

495.78

I wonder why I sent that

498.06

may not need it at all

505.08

so I'm going to drop the table

508.56

Target tables

513.36

okay so we're probably okay there

515.58

so let's see so we go through each of

518.279

these we're going to go ahead and just

520.44

take all the rows so we're going to run

521.64

this through all the way now

524.64

um

526.02

Let's see we come through we're going to

528.6

connect to the source

530.519

um

532.5

and here's something else I'm going to

534.42

do

535.74

is let me go ahead and create a settings

538.56

file

539.64

and actually now

543.14

instead of all the way up here before

546.959

what we were doing is we were just

548.279

creating the configuration as it existed

551.519

and so we would come in here and

555.8

we'd settle our values so what I'm going

558.839

to do now is I'm just going to keep this

560.459

pretty simple and from settings I'm

562.08

going to import The Source config and

563.64

the cart Target config so you go Source

565.86

config and then Target config

568.019

and import those in and then I'm going

570.06

to send those into the Connect Now

572.279

technically now I could do two different

575.519

I could do one connect and then just

578.16

pass back

579.48

you know assign out the connector that

582.54

comes back

583.98

but I'll keep it as source and

585.48

destination just for I don't know

588

cleanliness understanding

590.399

and now we've got that so we've got

592.62

we've extracted our setups into our

595.5

configuration settings into a settings

597.24

file

598.44

so we're going to come in we're going to

600.24

pull that in

601.56

what we could do is we could even

605.399

pull the databases out

611.76

bases

615.54

and if I go to settings I'll just push

617.459

that up here

619.38

and so now you have everything actually

621.48

extracted out into

624.18

a settings file or at least a lot more

626.519

of it now what we're also going to have

628.5

here is let's go ahead and do Max rows

637.64

and we'll pull that out as well so now

640.8

there's just less we have to worry about

644.279

so now in here you're starting to get

646.2

something that's pretty darn clean uh

648.899

and it's sort of a generic kind of

650.16

script that we have got created out

652.68

which is

654.839

um

655.92

one of the things we wanted to run into

657.42

here uh here we go generalize extract

660.42

command line parameters we actually are

662.64

going to pull those out into a settings

663.6

file

666.66

now let's go ahead and run this

671.459

and what we will see

676.079

um

676.74

uh actually let me not sync the data so

678.66

let's do that first without

681.12

I'm just going to get rid of synchros

683.459

not going to use that right now now what

685.56

we can do is we can actually pull little

687

Flags in and put the settings to be a

689.82

whole series of flags whether we do

691.56

triggers procedures functions indexes

693.42

and all that kind of good stuff right

694.86

now we're not going to do it that way

696.24

we're going to just create our database

697.8

real quick let's see so

700.68

this is Main

703.62

and

705.54

whoops

717.59

[Music]

719.88

what did I miss oh gosh I didn't even

726.26

lose this

732.48

I used to lose one of my imports so now

737.399

there we go there we go and boom so now

742.26

if we go look at uh locally refresh this

747.54

all right let's just reconnect because

748.98

we probably need to so now we can see

751.32

that we have a giganator that doesn't

753.959

have any tables oh interesting and we

756.6

probably have an H goals that maybe

757.92

doesn't have any tables nope it's not

759.839

how many tables so

769.32

let's go see what we got here

777.8

oh failed to create index

783

hmm

784.079

so for some reason

789.54

oh probably because it's already there

796.92

so what we want to do in missing indexes

810.24

oh well that says it should be

814.32

already missing so we've got something

816.66

there that didn't quite work

820.38

but we did a try so it came in what did

824.04

it do oh interesting

827.579

so we come out we do missing indexes for

829.86

DB in databases

833.519

let's do this

835.26

um

838.68

current DB

842.399

and let's see what it does here

848.88

yeah so for some reason

855.66

index

863.899

oh we're missing something here uh oh oh

868.44

oh oh oh oh oh oh oh oh oh oh oh it's

871.26

because the database there we have to

873.899

change the database for each of those

876.54

uh so that's going to be

881.24

so what we really want to do here

885.72

is that the source config

889.68

which I think we can do here so let's do

892.92

uh Source config

898.199

but first what we want to do is we want

901.199

to do Source config

905.22

um

912.6

let's see if we can do

917.699

database

921

equals DB

924.6

and then we're just going to quit

926.639

because we're going to have to update

927.899

our database there

932.04

oh

935.1

look at that

939.959

yeah we can't assign that so it's not a

942.779

function call

945.66

try that

953.579

there you go so let's see we need to set

956.82

so we've got to do a dictionary key kind

959.639

of thing here

963.72

and let's do this uh see

967.26

python set dictionary

971.639

value

975.899

oh that's right I think it's a get and a

978.54

set assigning values

986.16

so

991.139

oh that's okay yeah that's okay because

994.74

that is correct because that is the

996.6

first

997.32

uh so let's go here

999.779

uh let's see Target config

1004.88

well don't need that don't need that now

1008

let's take a look

1017.18

so take a look at this again

1020.3

let's do it this way let's go to whoop

1025.4

settings

1029.839

and let's just go ahead and get rid of

1032.72

that as the first one

1039.679

okay

1041.24

so now if we come in

1048.079

and let's do this okay so let's do print

1051.62

DB

1055.16

foreign

1059.299

so let's see how this works

1062.12

see what we got here okay so it does

1064.94

that

1068.24

and then let's print

1072.679

Source config

1080.96

and we've got our database has changed

1083.179

so we should be good

1095.539

so if we look at

1105.26

let's go to missing tables

1114.63

[Music]

1117.16

tables whoop

1124.34

and we will look real quick so it says

1126.62

here it's going to be print for a table

1127.88

that's in missing tables

1131.539

for each missing table get details but

1133.76

we've got to get self dot missing tables

1136.28

which we do so we do set missing tables

1146.539

uh here we go

1153.32

so we do compare tables

1160.22

so let's do that we'll do set missing

1162.26

tables let's just do this real quick

1164.96

self

1167.6

dot Source tables

1171.74

and Target tables

1177.799

let's go look at that real quick

1183.74

and we'll see

1188.6

so there you go so we have oh we have

1191.48

source and Target flipped

1198.38

somehow we got source and Target flip so

1200.9

we go here

1207.26

is that right

1210.86

uh oh

1213.26

that is flipped so

1219.98

that's Target

1223.1

that Source it's amazing how that works

1225.38

out so let's try that again

1227.919

uh let's see so now if we go into Main

1232.7

and we run it

1235.7

wow it works amazingly better when you

1238.1

get your settings record properly so now

1241.46

we're going to see that this is going to

1243.5

update

1245.24

and we're starting to see all of our

1246.74

little tables show up

1249.02

so I think we'll wrap it up this time

1251.48

um I'll probably go back and edit a

1252.679

little bit shorten this out a little bit

1253.76

but we uh have straightened that out in

1256.7

our settings and now we're actually able

1258.38

to to send multiple databases across and

1261.32

do our synchronizing uh actually what

1263.66

I'll do is I will sync rows

1266.24

and

1267.919

um we'll kick this off and it'll

1269.66

probably be pretty close to next time

1271.88

before this is all done anyways and

1275.299

we'll get all that stuff taken care of

1276.679

and we're gonna come back and we're

1277.94

probably going to dive into one of the

1279.74

more complicated things here uh dealing

1282.2

with enumerated values for a column

1284.299

because that is something we've run into

1285.62

now a couple of times and I want to make

1287.78

sure we get that one fixed

1289.94

that being said go out there and have

1291.98

yourself a great day a great week and we

1294.679

will talk to you next time

1297.66

[Music]