📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 11

2022-09-20 •Youtube

Detailed Notes

Focus for this episode: We look at synchronizing indexes and start that process.

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 are
continuing our series of tutorials that
are really crossing lines here between
uh python and
mysql
last time around uh we finished up i was
just dealing with a little bit of a
synchronization bug on the
the columns
and
it turned out that what i had was i was
actually calling the sync columns i had
that twice
and the first time i was calling it i
had the two databases reversed which
doesn't help very much that actually was
throwing it off quite a bit so that was
just one of those little things
particularly when you're running a
script like this
you want to make sure that you
understand what your source and your
destination is
this time around um because one of the
things that we bailed out on before
is we bailed out on
indexes when we were looking at the
columns so i want to actually take that
one
um specifically
i want to make that its own little thing
so let's see when we did let's do
missing columns
and we're going to
basically copy and paste this because
we're going to turn it around and this
is going to be
missing indexes
and we're going to get the same thing
here let's see missing columns we don't
need that uh i don't think we're going
to return it this time i don't think
there's a need for that
and then instead let's see so i don't
need that missing columns thing i don't
need it here
well
actually let's do it that way let's keep
it that way so we'll do this the same
way is what we're going to do is we're
going to return the missing indexes
and we will build all of our
sql together
and then when we're done
we'll go through and execute it because
it may take a second anyway so
what we're going to do is we're going to
take the missing columns way down here
um
i don't need that run twice i'm not sure
i had that
let's do
let's see so this is going to be
called this idx's and this could be
missing indexes
and i'll show you
oh
that's idx's and
idx's
i'll call it idx
we'll go ahead and print it because we
can
uh
there we go
oh except for the kind oh that is a
command but that's all right we'll do it
this way
okay so that gets us started on it
because now we want to go back up here
and we're going to look at this
missing indexes so actually right now
first let's
not
we're just going to quit right here
because we're going to get our missing
indexes and the way we're going to do
that is we're going to come in
here
and instead of
the describe
we are going to use
show indexes and the same thing or show
index is from and we're still going to
have that table
so show indexes from
and what i want to do here is
let's see
i'm going to do that for both of these
i'm going to call it well let's clean it
up so we're going to call it the idx
query instead of the col query
and
let's see so there's our cursors source
idx
with an s
destination idx
with an s
uh destination name so it's me
com index
in
destination indexes
and then
let's call idx
and then this is going to be
for column row and
[Music]
let's do this we'll do
uh
we'll do this we'll call it idx row
hello that's in
source idx's
and then we're gonna come in here and
that's gonna be the idx row
let's do it this way so we have call row
col roll we're gonna change that over
yeah i know it's taking a little time i
apologize so now this part let's just
get rid of this thing completely because
what we want to do
is
um
[Music]
let's
we'll come back because this this is
going to be basically this is going to
be a create index
and we know we need to do create index
and it's going to be uh let's see
plus
name
and let's call uh yeah let's call it
that
create that
and
we will go ahead and use our little
uh things here just in case it's
something weird
so we create that index on
and then we're gonna have to add the
table whoops
and then this is going to be
we're going to need the
column name
and it looks something like
that so if we go look at a create index
example
we can see here here i'm just going to
snag one just so you can see what it
looks like just a reminder
and this one's actually an example that
is a multi-column but basically we need
this create index we give it the index
name on whatever the table and then in
parenthesis it's going to be the column
and so in this case we're going to
assume a single column
but first let's see so now we're coming
through here i think we have
let's do this first let's just come
through for each one
that's for destination indexes
and i'm not even sure so let's let's
just do it this way because it's going
to be easier as opposed to right through
so when we do a
show indexes
we get something a little more
complicated than what we've seen before
so what we see here is in zero is going
to be the table name
um so what we need to do so that
what we're going to need to do here
is we're going to have to match
the
actually i guess we can do it by the
oh see so it doesn't give us
the
index name
because it's oh because it's a primary
key
let's go look at that real quick let's
see if we can get
show indexes
i want to see
and it's not going to give me the actual
index name
so
what i'm going to need to do is i'm
going to have to compare and it's going
to be
that i want the table
and the column name
actually this is probably a combo of
stuff
so
actually let's just take the whole thing
so if we look at destiny oops
we're just going to take the whole thing
so we're going to take uh
call idx
and we're just going to compare
the entire array because it should be
the same anything's different we're
going to rebuild basically
and so now we're gonna come in here
and then we're gonna say
the name is equal to
let's do this
so it'll be that it'll be table name
plus
um
let's do column name which is uh one two
three four
idx row 4
[Music]
because this is in yeah so this will be
from there
and we need to know if
let's see if this will work idx row is
not in destination names then i'm going
to print it so let's just see how this
works
and i'm not going to make any real
changes here
so let's go play with that for a second
so seeing what we've got now if we run
it
somewhere in
here all right it's going to blow up a
little bit because
oh because we're doing something here oh
here um
we're doing this little creation which
we don't need
we don't need the column type we're
gonna print the row
um
so call name is let's do this
that's cll name
and we need an actual name oh i'm sorry
whoops
we do need we want the name
and
the column name
is going to be equal to
idx4
and we're just going to keep the same
name
so let's see what we kick out here
so doing this we're going to come in and
we're going to see this uh here
okay so we've got one that's not in the
target
and we can see
oh actually quite a few are not in the
target
and it's not printing out
my rows okay so let's go look at this
real quick
what did we do
okay those columns are not oh here we go
so here's where
i step into it
so i'm going to see here's all of my
indexes that are in source
and i can see that all of those are not
in the target
and then so for each one of those like
it says
here
it says that this thing
does not exist
in my
target so let's go try that one real
quick so let's take a look at that
create index
whoops let's do it this way
so he's
address t1id what i want to do i'm going
to change this to be
that name where do i create the name
when i create the name on that i want it
to be
here we go i'm going to add an
underscore
just because just because i can
so it'll make this look better instead
of this address t underscore one lb
address underscore t1
um so let's just do that real quick
so now let's create that index and
see if i go over here and i create this
index
let's clean all this other crap out
oops um
let me use tutorial
okay so it allowed me to create that
and so i should see on most of these i'm
going to see indexes and i don't think i
had any multiples
so let's go ahead
and try this let's first run this again
and now we're going to see
that it's still not matching up
so here we have
uh address so i need to say
let's see
so
[Music]
is that right
so it's saying
that this row does not exist
here so what i want to do here is i'm
going to do um
so i'm probably gonna have to get deeper
into this and so let's just do it this
way
so now when i come through it's saying
that that row
does not exist but it does oh no it
doesn't
so it's because of that value right
let's see address primary blah blah blah
blah
and i'm not sure what that is so if we
do let's go back into that let's look at
that and see
what that um
three is
ah so see i created that one and i also
have this there's a cardinality to it so
i can probably get rid of that so what
i'm gonna do is now i'm gonna have to
get a little more complicated in this
is i'm gonna have to build out a combo
which is gonna be
uh let's see in destinations what i need
to do is i want to do uh
i'm going to call this row string
and he's going to be equal to or she i
don't want to misgender it
index
0 which is going to be the
table plus
the
[Music]
let's do the key name
which is
2. and really
we should be able to do those
um
because if we're in a table
oh and this is in the same table so we
know it's going to be the same table
so
really i just need that
so i can probably do that let's do that
let's see uh let's just do it this way
name equals
and
oh
wow let's fight quite a bit so maybe i
can get away with it using the key name
and then uh let's see so here
i'm going to do this i'm going to say if
the name
oh here name equals 2
row 2
or column 2 again and
if name is not in destination names
then i'm gonna do that so now let's see
what it looks like
[Music]
oop
and if we run it we're going to see here
so
it's going to say that hey this doesn't
exist oh let me print out
sql
uh
print table sequel
now let's look at it
so if we see here so i've got this guy
exists
right it says it's not the same
uh the primary is there and it's because
i built this one here
um
which may mean i have the time those two
swapped again on me
um
no missing indexes
db db c2
let's go look up here
so now let's look at missing index so
here let's combine these two i may not
maybe i don't have much uh i want to do
um
oh
let's do name and names
let's see so
here we go
so if name is not in name
which it says it's not
oh and so it's off of type id okay that
makes sense the other one is showing up
so
we should be
good it's interesting that it's giving
me that type id
let me go back to mine i may have those
i may have miscounted on my
oops
let's try this again
and we'll prob
one more time
there we go
so if i see the indexes
key name
should be two
so those should be my two names
those are my two names
but he's coming in he's saying to create
an index
which should not occur
i wonder if we got some extra prints in
here
let's do this
uh let's see that's missing columns okay
so we're just going to do this real
quick
we're going to do a
start
missing index
we have a domain
okay
so the first thing we come into here we
do missing index is type id
and it's saying that that doesn't exist
in these so the two lists it built uh
same column
oh so that means it's not there okay
so now let me
uh let me go do that
so i've moved on to the next one um
whoops
let's get rid of that
same columns missing indexes let's get
rid of a couple of these prints
let's do that
and if we go down here
okay and now we're gonna get our sql so
we should see that there's gonna be a
bunch missing
so also on index we have on address we
have this one
so now we should see if we create this
oh if i create it right
then next time through
i'm going to see nothing in address so
it's now going to run through all of
them
and if i don't quit
i am going to not sync my rows because
that's going to take a second
i'm not going to sync my columns
yeah let's just do quit right here just
to be safe
so i'm going to go through and fix all
the indexes
so now it's creating all those indexes
but now i've got a duplicate
so i've got an issue oh because i've got
the
uh i've got a duplicate name so we're
gonna have to look into that one and
that'll be what we take care of next
time
so go out there and have yourself a
great day a great week and we will talk
to you
next time
you
Transcript Segments
0.43

[Music]

27.039

well hello and welcome back we are

28.64

continuing our series of tutorials that

30.96

are really crossing lines here between

33.2

uh python and

35.36

mysql

37.2

last time around uh we finished up i was

39.04

just dealing with a little bit of a

40.8

synchronization bug on the

43.76

the columns

45.28

and

46.16

it turned out that what i had was i was

48.399

actually calling the sync columns i had

50.8

that twice

52.32

and the first time i was calling it i

54.079

had the two databases reversed which

57.6

doesn't help very much that actually was

59.359

throwing it off quite a bit so that was

61.28

just one of those little things

63.12

particularly when you're running a

64.159

script like this

65.439

you want to make sure that you

66.96

understand what your source and your

68.4

destination is

70.4

this time around um because one of the

72.88

things that we bailed out on before

76.32

is we bailed out on

78.799

indexes when we were looking at the

80.72

columns so i want to actually take that

83.04

one

84.08

um specifically

86.799

i want to make that its own little thing

88.64

so let's see when we did let's do

90.799

missing columns

95.119

and we're going to

96.32

basically copy and paste this because

98

we're going to turn it around and this

99.36

is going to be

102.159

missing indexes

107.52

and we're going to get the same thing

109.04

here let's see missing columns we don't

110.96

need that uh i don't think we're going

112.32

to return it this time i don't think

114.24

there's a need for that

117.759

and then instead let's see so i don't

119.36

need that missing columns thing i don't

121.439

need it here

125.92

well

130

actually let's do it that way let's keep

132

it that way so we'll do this the same

133.28

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

134.959

going to return the missing indexes

138.8

and we will build all of our

141.28

sql together

142.8

and then when we're done

144.4

we'll go through and execute it because

146.48

it may take a second anyway so

151.36

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

152.4

take the missing columns way down here

156.879

um

158.72

i don't need that run twice i'm not sure

160.56

i had that

162.239

let's do

164.8

let's see so this is going to be

168.879

called this idx's and this could be

170.72

missing indexes

175.36

and i'll show you

185.68

oh

186.72

that's idx's and

189.28

idx's

192.239

i'll call it idx

195.92

we'll go ahead and print it because we

197.36

can

199.76

uh

200.56

there we go

203.28

oh except for the kind oh that is a

204.879

command but that's all right we'll do it

206.239

this way

207.599

okay so that gets us started on it

210.319

because now we want to go back up here

212.159

and we're going to look at this

214.799

missing indexes so actually right now

217.12

first let's

218.879

not

220

we're just going to quit right here

223.36

because we're going to get our missing

225.84

indexes and the way we're going to do

227.599

that is we're going to come in

230.959

here

231.92

and instead of

234

the describe

240.48

we are going to use

242.799

show indexes and the same thing or show

245.439

index is from and we're still going to

246.879

have that table

249.519

so show indexes from

252.72

and what i want to do here is

255.2

let's see

256.88

i'm going to do that for both of these

258.88

i'm going to call it well let's clean it

260.079

up so we're going to call it the idx

262.079

query instead of the col query

271.759

and

272.88

let's see so there's our cursors source

275.84

idx

277.919

with an s

279.6

destination idx

281.52

with an s

284.4

uh destination name so it's me

287.759

com index

289.759

in

290.96

destination indexes

293.759

and then

295.759

let's call idx

300.16

and then this is going to be

302.32

for column row and

304.18

[Music]

308.4

let's do this we'll do

313.759

uh

316.56

we'll do this we'll call it idx row

318.16

hello that's in

320.639

source idx's

324.08

and then we're gonna come in here and

325.919

that's gonna be the idx row

329.199

let's do it this way so we have call row

333.68

col roll we're gonna change that over

336.16

yeah i know it's taking a little time i

337.44

apologize so now this part let's just

339.28

get rid of this thing completely because

340.8

what we want to do

343.12

is

344.639

um

345.06

[Music]

348.8

let's

351.44

we'll come back because this this is

352.8

going to be basically this is going to

354.32

be a create index

358.319

and we know we need to do create index

362.24

and it's going to be uh let's see

365.6

plus

367.36

name

369.919

and let's call uh yeah let's call it

372.24

that

374.96

create that

377.6

and

378.4

we will go ahead and use our little

381.759

uh things here just in case it's

383.84

something weird

385.28

so we create that index on

388.16

and then we're gonna have to add the

389.28

table whoops

395.84

and then this is going to be

400.479

we're going to need the

401.919

column name

407.68

and it looks something like

410.96

that so if we go look at a create index

414

example

415.919

we can see here here i'm just going to

417.68

snag one just so you can see what it

419.36

looks like just a reminder

423.759

and this one's actually an example that

425.599

is a multi-column but basically we need

428.24

this create index we give it the index

430.319

name on whatever the table and then in

433.12

parenthesis it's going to be the column

434.88

and so in this case we're going to

435.84

assume a single column

438.319

but first let's see so now we're coming

440.56

through here i think we have

445.039

let's do this first let's just come

447.44

through for each one

451.12

that's for destination indexes

454.72

and i'm not even sure so let's let's

456.24

just do it this way because it's going

457.36

to be easier as opposed to right through

458.88

so when we do a

460.4

show indexes

462.08

we get something a little more

463.36

complicated than what we've seen before

465.44

so what we see here is in zero is going

468.639

to be the table name

470.639

um so what we need to do so that

473.52

what we're going to need to do here

476.879

is we're going to have to match

478.96

the

479.759

actually i guess we can do it by the

484.16

oh see so it doesn't give us

486.879

the

488.08

index name

489.919

because it's oh because it's a primary

491.44

key

492.879

let's go look at that real quick let's

494.08

see if we can get

496.96

show indexes

503.28

i want to see

505.36

and it's not going to give me the actual

507.28

index name

510.16

so

512.159

what i'm going to need to do is i'm

513.839

going to have to compare and it's going

515.599

to be

516.719

that i want the table

519.68

and the column name

522.64

actually this is probably a combo of

524.64

stuff

526.16

so

527.68

actually let's just take the whole thing

532.24

so if we look at destiny oops

535.12

we're just going to take the whole thing

536.32

so we're going to take uh

538.64

call idx

540.24

and we're just going to compare

541.92

the entire array because it should be

545.12

the same anything's different we're

546.399

going to rebuild basically

549.68

and so now we're gonna come in here

551.6

and then we're gonna say

553.76

the name is equal to

558.32

let's do this

560.48

so it'll be that it'll be table name

563.519

plus

565.6

um

567.76

let's do column name which is uh one two

571.12

three four

575.519

idx row 4

577.1

[Music]

578.16

because this is in yeah so this will be

580

from there

582.16

and we need to know if

584.88

let's see if this will work idx row is

587.36

not in destination names then i'm going

589.36

to print it so let's just see how this

590.72

works

591.92

and i'm not going to make any real

593.12

changes here

595.76

so let's go play with that for a second

598.24

so seeing what we've got now if we run

600.32

it

601.839

somewhere in

602.839

here all right it's going to blow up a

604.8

little bit because

607.6

oh because we're doing something here oh

609.36

here um

611.44

we're doing this little creation which

613.12

we don't need

615.6

we don't need the column type we're

617.12

gonna print the row

618.959

um

621.36

so call name is let's do this

626.16

that's cll name

630

and we need an actual name oh i'm sorry

634.24

whoops

635.76

we do need we want the name

639.12

and

640

the column name

642.64

is going to be equal to

645.279

idx4

650.399

and we're just going to keep the same

651.44

name

652.88

so let's see what we kick out here

656.8

so doing this we're going to come in and

658.72

we're going to see this uh here

661.519

okay so we've got one that's not in the

663.519

target

664.8

and we can see

668.48

oh actually quite a few are not in the

670.72

target

674.24

and it's not printing out

678.16

my rows okay so let's go look at this

680.16

real quick

683.36

what did we do

688

okay those columns are not oh here we go

690.48

so here's where

691.92

i step into it

694

so i'm going to see here's all of my

695.44

indexes that are in source

697.44

and i can see that all of those are not

699.36

in the target

700.8

and then so for each one of those like

703.2

it says

705.2

here

706.959

it says that this thing

709.36

does not exist

711.6

in my

712.8

target so let's go try that one real

714.399

quick so let's take a look at that

715.839

create index

717.44

whoops let's do it this way

721.2

so he's

722.399

address t1id what i want to do i'm going

725.44

to change this to be

727.92

that name where do i create the name

731.519

when i create the name on that i want it

734.24

to be

736.88

here we go i'm going to add an

738.639

underscore

739.839

just because just because i can

744.16

so it'll make this look better instead

745.44

of this address t underscore one lb

747.6

address underscore t1

749.6

um so let's just do that real quick

752.24

so now let's create that index and

756.839

see if i go over here and i create this

760.32

index

762.56

let's clean all this other crap out

766.88

oops um

768.399

let me use tutorial

775.76

okay so it allowed me to create that

778.399

and so i should see on most of these i'm

780.72

going to see indexes and i don't think i

783.519

had any multiples

785.279

so let's go ahead

786.959

and try this let's first run this again

791.44

and now we're going to see

794

that it's still not matching up

796.399

so here we have

798.32

uh address so i need to say

800.959

let's see

805.68

so

806.09

[Music]

807.44

is that right

810.56

so it's saying

812.8

that this row does not exist

818.56

here so what i want to do here is i'm

821.04

going to do um

825.279

so i'm probably gonna have to get deeper

826.639

into this and so let's just do it this

828.32

way

831.519

so now when i come through it's saying

833.44

that that row

837.279

does not exist but it does oh no it

839.839

doesn't

842.72

so it's because of that value right

844.72

let's see address primary blah blah blah

846.72

blah

848.72

and i'm not sure what that is so if we

850.959

do let's go back into that let's look at

852.8

that and see

854.72

what that um

857.68

three is

863.519

ah so see i created that one and i also

866.72

have this there's a cardinality to it so

868.48

i can probably get rid of that so what

870.56

i'm gonna do is now i'm gonna have to

872

get a little more complicated in this

875.36

is i'm gonna have to build out a combo

879.44

which is gonna be

880.72

uh let's see in destinations what i need

883.12

to do is i want to do uh

885.76

i'm going to call this row string

888.32

and he's going to be equal to or she i

890.639

don't want to misgender it

893.199

index

894.639

0 which is going to be the

896.839

table plus

899.36

the

900.68

[Music]

902.079

let's do the key name

905.12

which is

910.839

2. and really

913.199

we should be able to do those

915.68

um

916.56

because if we're in a table

919.92

oh and this is in the same table so we

921.519

know it's going to be the same table

923.36

so

924.839

really i just need that

931.6

so i can probably do that let's do that

934.16

let's see uh let's just do it this way

936.16

name equals

938.839

and

940.48

oh

942.079

wow let's fight quite a bit so maybe i

943.839

can get away with it using the key name

947.759

and then uh let's see so here

952.88

i'm going to do this i'm going to say if

955.04

the name

956.48

oh here name equals 2

962.24

row 2

966.16

or column 2 again and

969.12

if name is not in destination names

974.24

then i'm gonna do that so now let's see

975.759

what it looks like

976.77

[Music]

978.079

oop

981.519

and if we run it we're going to see here

986.399

so

987.68

it's going to say that hey this doesn't

989.12

exist oh let me print out

991.92

sql

996.72

uh

997.759

print table sequel

1003.199

now let's look at it

1005.199

so if we see here so i've got this guy

1007.6

exists

1010

right it says it's not the same

1012.24

uh the primary is there and it's because

1014.32

i built this one here

1017.519

um

1018.56

which may mean i have the time those two

1020.8

swapped again on me

1022.639

um

1024.959

no missing indexes

1027.52

db db c2

1030.799

let's go look up here

1034.24

so now let's look at missing index so

1035.919

here let's combine these two i may not

1037.76

maybe i don't have much uh i want to do

1040.959

um

1043.28

oh

1048.079

let's do name and names

1057.28

let's see so

1062.559

here we go

1065.76

so if name is not in name

1068.32

which it says it's not

1078.559

oh and so it's off of type id okay that

1080.799

makes sense the other one is showing up

1083.36

so

1084.559

we should be

1086.24

good it's interesting that it's giving

1087.84

me that type id

1093.36

let me go back to mine i may have those

1095.84

i may have miscounted on my

1098.08

oops

1101.12

let's try this again

1103.12

and we'll prob

1104.4

one more time

1107.12

there we go

1109.84

so if i see the indexes

1111.919

key name

1114.24

should be two

1121.44

so those should be my two names

1125.28

those are my two names

1128.4

but he's coming in he's saying to create

1130.16

an index

1135.679

which should not occur

1140.559

i wonder if we got some extra prints in

1142.08

here

1144.08

let's do this

1148.24

uh let's see that's missing columns okay

1152.799

so we're just going to do this real

1153.919

quick

1155.039

we're going to do a

1156.72

start

1158.4

missing index

1166.24

we have a domain

1168.16

okay

1169.039

so the first thing we come into here we

1171.52

do missing index is type id

1174.48

and it's saying that that doesn't exist

1177.12

in these so the two lists it built uh

1181.12

same column

1182.88

oh so that means it's not there okay

1186.72

so now let me

1189.84

uh let me go do that

1191.76

so i've moved on to the next one um

1193.679

whoops

1197.44

let's get rid of that

1200.24

same columns missing indexes let's get

1203.039

rid of a couple of these prints

1207.919

let's do that

1210

and if we go down here

1212.72

okay and now we're gonna get our sql so

1214.64

we should see that there's gonna be a

1215.679

bunch missing

1217.12

so also on index we have on address we

1220.08

have this one

1222.48

so now we should see if we create this

1226.24

oh if i create it right

1229.039

then next time through

1231.039

i'm going to see nothing in address so

1233.12

it's now going to run through all of

1234.32

them

1235.84

and if i don't quit

1239.2

i am going to not sync my rows because

1241.2

that's going to take a second

1244.08

i'm not going to sync my columns

1247.44

yeah let's just do quit right here just

1249.12

to be safe

1250.88

so i'm going to go through and fix all

1252.4

the indexes

1254.48

so now it's creating all those indexes

1256.799

but now i've got a duplicate

1258.88

so i've got an issue oh because i've got

1261.28

the

1262.88

uh i've got a duplicate name so we're

1264.559

gonna have to look into that one and

1266.24

that'll be what we take care of next

1267.76

time

1268.72

so go out there and have yourself a

1270

great day a great week and we will talk

1272.4

to you

1273.44

next time

1291.12

you