📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 12

2022-09-22 •Youtube

Detailed Notes

Focus for this episode: We continue our exploration of index creation and start into multi-keyed indexes.

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 season series i
guess we're the more series where we're
talking about
python and sql we're sort of
taking the last couple of tutorial
series that we've gone through
and combining them into an application
which is our database sync script
uh written in python for mysql
last time around we were working on
we'd added
indexes essentially and foreign keys
primary keys things like that
as we're going through and we're
creating our indexes that don't exist on
the destination
and we ran into a situation where
we had a problem with the names
and it threw an exception so what i want
to do with this one a little different
what we did in the past
is we're going to do if you've
done anything with it if you thought
about it
we're going to do the try and accept
and we're basically going to say well
we're going to keep on going
so
what we're going to do here that's a
python thing so let me put
here
actually i guess i don't really need to
do it
let's do it here
i'm going to do it i'm going to create
my cursor
and i'm going to do the execute
and then no matter what i'm going to do
i'm going to close the cursor anyways
so i'm going to go through i'm gonna try
to create this thing
and
what is it complaining about
uh
it says too broad but i think i'm okay
so what i'm gonna do here
is i'm gonna say
i fail to create
index
and then i'm going to give it my query
idx
and
i think i'm gonna go with that for now
so let's see i'm not gonna print it
now unless it's a mistake
and
we'll just go with that for now
so what we're going to do
if we run it
is get nothing of use
let's see did we quit out of this early
let's see
oh
these tables that indexes are not in the
target
and there were none
so everything synced up because we
created everything i guess that's the
only one last time
and we create what happened is we
created this
first one
and for some reason oh
let's go look at that
uh horse bo index on horses
i bet you what we're gonna find
somewhere in here
[Music]
is
uh if i go out to
that where is that that is on the
this guy
if i go out to the source
for horses
and i look at indexes
there we go
so if we look uh let me go horse
and it's not connected right now let's
see let's try this
reconnect
well i think what it was is that we got
a
we got ult we got
secondary indexes on that so if we go to
um
let's do this
uh how do i want to do oh i guess i can
do it all from here so if i do
uh let's just force a disconnect let's
get out of here for a second
so if i go to my local
and i'm gonna do uh show indexes on what
was that horses
okay it's not going to like it good now
it's going to connect
and if i go to tutorial
uh actually it's not is it it's describe
indexes
and we come up here up columns indexes
here we go show indexes from not on
shoot
so let's go look at this uh if we do
that and so this is where we made a
mistake so here's our
indexes
now what we want to do
uh let's go to local local local local
local
where is local local ah that's demo
there we go
the thing that's in bold rob so i want
to go in here and i'm going to go to
this table
and i'm going to make a change because i
want to look at how this comes up if we
do alter table
well i had a little bit of a
technical difficulty there so let's just
i had to jump out so now i go back and
let's look at our show indexes so this
is from our source
and what we see here
is that we actually have
um between the two we've got an index so
we need this sequence and index which is
zero one two three
and so we're gonna need to deal with
that when we're creating our indexes
we need to
figure out if it's going to be a
multi-column or not
and so we need to do
let's say so for table and there we're
going to show our indexes we're going to
go through each index
okay so here
we have to change this around a little
bit so we're going to do
column names
and let's do we're going to call it a
prior
prior calm
and we're going to do this
and so here we're going to say okay if
we're in this
uh we probably need to do each of these
so
let's ch we're probably going to change
this around a little bit
um first we're going to do we're going
to say
how do we want to do this it's going to
be really interesting to do i wonder if
we can do it with an order by
i don't think we can do an order let's
try this order by
sequence in
index
this is probably not going to allow us
to do it but let's see what happens
yep we can't do that
so what we're gonna need to do
is we're gonna have to figure out we
have to walk through each of these
and if it has a two
then we're gonna have to go find the
other one
with that key name
and let's say let's see if i can just
select
key name
from
whoops
this is going to be really
interesting
so we're going to try this let's see if
we can do like this select indexes from
horses
i'm going to say s source and let's just
see if this will work i don't think
it'll work
uh unknown column indexes
oh sorry show indexes
yep it's not going to allow us to do it
so
we're going to have to get a little more
tricky with the indexes
so we're going to do is we're going to
go through each of these and we're going
to do um
we're going to basically do it like this
we're going to have to walk through a
little bit differently so
let's um
let's pull this creation out for now
and what we're going to do is we're
going to come in here let's say
oops we do want that
okay so if it's not in the destination
names
then that's going to be in keys
or let's call it
let's call this names
actually let's call this new names
so what we're going to do here is if
it's not in there
then we're going to do
uh new names
dot append
and we're gonna append the name
and let's just get rid of
that
whoops
and
um
well we'll do this
we'll keep it the same
but when we go down here
i'm going to take this guy and we are
going to
not
exec
everything
uh actually let's quit
yeah
let's quit here for now
so we create the indexes
so now if we do it
um and let's go to
whoops
let's say
in the source
uh let's see if i can do this let me see
if i can get my tool
my database a little engineer thing here
to work
oh i think i just want to do uh drop
oops
um let's do this connect and execute on
local
there we go
and then i'm gonna do drop index
uh
horse b underscore oh
i need to do on horses
i think it's on horses i think how i do
my drop
there we go
so now if i do my show index bill i
don't have it so now
uh it's not going to execute so i should
be able to run it through
and uh it's gonna blow up oh
because call name is not defined and i
gotta go back up here
i'm gonna take this little bit
partially here
and we're going to call it name
um
we're just going to call
it that one
for now
okay so now it's going to say that it's
going to come through and it's going to
create because it doesn't exist it's
going to try to create it
and now what i want to do
is say
and it gets that in there twice
so i can see where he exists twice
so what i want to do is i'm going to
actually change this a little bit
and
i'm going to well i'm not going to
create that yet so now what i'm going to
do this time around is i'm going to say
in the
new names did i get the new names of pen
yeah
so i'm going to come out here and i'm
going to do print
new names
let's see how that works
and
uh
okay so there's nothing in new names
wait what did i do
oh whoops
oh i can do that let's do that
let's just put it like this
we'll print our new names there we go
so each one
and here we go
oh no those are
the column names
so
new names needs to be actually just the
name
and so now i'm gonna see there we go so
if i see the name twice
then i'm gonna need to do some let's see
so how do i want to do that
i probably want to do it as
so i'm going to come through here once i
get done
let's see so i've got my new names and
so this is where i'm going to actually
build out my
stuff
so i'm going to do a
four name
in
new names
and i'm going to do
um let's do this
created
whoops
equals this thing
and what i'm going to do with each one
is i'm going to say
if
name
in
created
so that means it already exists
else
created dot append
name
and then what i want to do
so if the name is in created
so if i'm or if it already exists and i
need to get rid of this otherwise
i'm going to append it and i'm going to
do something with it
actually what i probably need to do
is change this to
uh fields
so what i need to do yeah i'm gonna have
to do this a little differently so okay
so this is gonna be um
this will be
index names
and so when i come through here
where's index names
let's see if i can do this name
equals and then i need to get the column
i need to get the uh
the name of the column
and so now what i'm going to do
is turn this into a little bit of a
dictionary type of an approach
and let me make sure i've got that right
because i think i don't
that's what i thought as i'm going to do
that so
that's what i want to do okay so my i
start with
this
whoop
this
and i'm going to do
for each of those
i'm gonna do
i think that's how i want it to do right
oh nope
i'm okay
so i can do it like that
dot append
name and a pin so
okay
there we go so that's going to give me
my columns that'll work
let's see if that worked uh it probably
works that way
so now
um for name in
idx names
and let's just do this for now let's
just do
let's get rid of that i'm gonna do print
name
and see what happens
because we are
there we go
so running some issues here getting this
i'm going to because of time constraints
i am going to
pause this here
and we'll come back next time we're
going to start digging through this as
we are having to rewrite this because
what we're going to do is we're going to
build out a dictionary of columns or i'm
sorry of index names and then with each
of those we'll have an array of columns
and then we'll use that to build our sql
that being said it's time for us 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
you
Transcript Segments
0.43

[Music]

27.119

well hello and welcome back

28.88

we're continuing our season series i

31.76

guess we're the more series where we're

33.84

talking about

35.04

python and sql we're sort of

37.44

taking the last couple of tutorial

40.239

series that we've gone through

42.16

and combining them into an application

44.16

which is our database sync script

48

uh written in python for mysql

50.719

last time around we were working on

53.199

we'd added

54.64

indexes essentially and foreign keys

57.199

primary keys things like that

58.96

as we're going through and we're

60

creating our indexes that don't exist on

62.32

the destination

63.92

and we ran into a situation where

66.72

we had a problem with the names

69.04

and it threw an exception so what i want

71.439

to do with this one a little different

73.36

what we did in the past

75.36

is we're going to do if you've

77.439

done anything with it if you thought

78.72

about it

79.6

we're going to do the try and accept

83.2

and we're basically going to say well

84.479

we're going to keep on going

86.64

so

87.439

what we're going to do here that's a

89.04

python thing so let me put

93.28

here

94.479

actually i guess i don't really need to

95.84

do it

97.36

let's do it here

98.56

i'm going to do it i'm going to create

100.72

my cursor

103.119

and i'm going to do the execute

107.2

and then no matter what i'm going to do

109.04

i'm going to close the cursor anyways

112.32

so i'm going to go through i'm gonna try

113.6

to create this thing

115.36

and

116.56

what is it complaining about

118.56

uh

120

it says too broad but i think i'm okay

122.56

so what i'm gonna do here

124.479

is i'm gonna say

130

i fail to create

133.68

index

135.04

and then i'm going to give it my query

138.56

idx

139.84

and

140.959

i think i'm gonna go with that for now

144

so let's see i'm not gonna print it

146.959

now unless it's a mistake

150.239

and

152.08

we'll just go with that for now

155.12

so what we're going to do

157.2

if we run it

160.8

is get nothing of use

166.4

let's see did we quit out of this early

169.12

let's see

170.64

oh

171.44

these tables that indexes are not in the

172.959

target

175.519

and there were none

178.159

so everything synced up because we

180.239

created everything i guess that's the

181.36

only one last time

185.12

and we create what happened is we

186.56

created this

188.08

first one

189.519

and for some reason oh

192

let's go look at that

193.76

uh horse bo index on horses

199.28

i bet you what we're gonna find

201.519

somewhere in here

202.53

[Music]

204.319

is

205.76

uh if i go out to

208.319

that where is that that is on the

211.84

this guy

214.879

if i go out to the source

217.2

for horses

220.64

and i look at indexes

225.36

there we go

226.4

so if we look uh let me go horse

235.2

and it's not connected right now let's

237.04

see let's try this

238.879

reconnect

246.4

well i think what it was is that we got

249.12

a

251.28

we got ult we got

253.28

secondary indexes on that so if we go to

257.759

um

261.519

let's do this

267.199

uh how do i want to do oh i guess i can

269.04

do it all from here so if i do

272.16

uh let's just force a disconnect let's

273.759

get out of here for a second

275.28

so if i go to my local

276.96

and i'm gonna do uh show indexes on what

280.479

was that horses

285.44

okay it's not going to like it good now

287.36

it's going to connect

294.24

and if i go to tutorial

300.08

uh actually it's not is it it's describe

302.96

indexes

311.52

and we come up here up columns indexes

313.84

here we go show indexes from not on

315.84

shoot

319.44

so let's go look at this uh if we do

321.6

that and so this is where we made a

323.44

mistake so here's our

325.28

indexes

327.199

now what we want to do

329.44

uh let's go to local local local local

332.08

local

335.199

where is local local ah that's demo

338.24

there we go

339.199

the thing that's in bold rob so i want

341.36

to go in here and i'm going to go to

342.88

this table

345.039

and i'm going to make a change because i

346.8

want to look at how this comes up if we

348.8

do alter table

350.88

well i had a little bit of a

352.639

technical difficulty there so let's just

355.68

i had to jump out so now i go back and

358.24

let's look at our show indexes so this

359.84

is from our source

362.24

and what we see here

364.96

is that we actually have

367.199

um between the two we've got an index so

370.479

we need this sequence and index which is

372.24

zero one two three

375.36

and so we're gonna need to deal with

376.56

that when we're creating our indexes

380.639

we need to

382.16

figure out if it's going to be a

383.12

multi-column or not

386.88

and so we need to do

389.44

let's say so for table and there we're

391.44

going to show our indexes we're going to

393.52

go through each index

395.6

okay so here

398.479

we have to change this around a little

399.759

bit so we're going to do

401.36

column names

403.919

and let's do we're going to call it a

406.72

prior

410.479

prior calm

414.479

and we're going to do this

417.36

and so here we're going to say okay if

419.52

we're in this

421.68

uh we probably need to do each of these

424.96

so

430.88

let's ch we're probably going to change

432.479

this around a little bit

435.52

um first we're going to do we're going

437.039

to say

441.759

how do we want to do this it's going to

443.12

be really interesting to do i wonder if

445.599

we can do it with an order by

451.919

i don't think we can do an order let's

453.28

try this order by

456.08

sequence in

457.599

index

458.88

this is probably not going to allow us

460.16

to do it but let's see what happens

463.12

yep we can't do that

465.44

so what we're gonna need to do

467.919

is we're gonna have to figure out we

469.28

have to walk through each of these

472.16

and if it has a two

474.879

then we're gonna have to go find the

476.24

other one

477.52

with that key name

480.8

and let's say let's see if i can just

482.56

select

485.599

key name

487.039

from

488.08

whoops

497.36

this is going to be really

499.12

interesting

502.479

so we're going to try this let's see if

504.08

we can do like this select indexes from

507.68

horses

517.36

i'm going to say s source and let's just

519.2

see if this will work i don't think

520.32

it'll work

521.839

uh unknown column indexes

526.48

oh sorry show indexes

532.399

yep it's not going to allow us to do it

534

so

535.44

we're going to have to get a little more

537.12

tricky with the indexes

539.04

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

540

go through each of these and we're going

541.6

to do um

543.519

we're going to basically do it like this

544.8

we're going to have to walk through a

546

little bit differently so

548.56

let's um

550.959

let's pull this creation out for now

555.68

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

557.6

going to come in here let's say

559.519

oops we do want that

564.08

okay so if it's not in the destination

566.399

names

567.839

then that's going to be in keys

573.12

or let's call it

575.68

let's call this names

579.44

actually let's call this new names

583.44

so what we're going to do here is if

584.959

it's not in there

588.48

then we're going to do

590.959

uh new names

599.12

dot append

602

and we're gonna append the name

606.399

and let's just get rid of

609.2

that

610.079

whoops

615.279

and

616.72

um

618.88

well we'll do this

620.56

we'll keep it the same

622.72

but when we go down here

625.279

i'm going to take this guy and we are

627.279

going to

628.48

not

629.36

exec

630.72

everything

635.68

uh actually let's quit

638.959

yeah

640.56

let's quit here for now

643.04

so we create the indexes

645.2

so now if we do it

647.04

um and let's go to

650.24

whoops

652.8

let's say

654.399

in the source

659.12

uh let's see if i can do this let me see

660.88

if i can get my tool

662.32

my database a little engineer thing here

664.48

to work

666

oh i think i just want to do uh drop

669.279

oops

671.839

um let's do this connect and execute on

675.12

local

676.24

there we go

677.519

and then i'm gonna do drop index

680.88

uh

682.16

horse b underscore oh

688.48

i need to do on horses

691.68

i think it's on horses i think how i do

693.519

my drop

694.959

there we go

696.079

so now if i do my show index bill i

698.24

don't have it so now

699.92

uh it's not going to execute so i should

701.519

be able to run it through

704.32

and uh it's gonna blow up oh

706.88

because call name is not defined and i

709.44

gotta go back up here

712.079

i'm gonna take this little bit

714.079

partially here

717.36

and we're going to call it name

721.519

um

725.6

we're just going to call

728.839

it that one

731.44

for now

735.68

okay so now it's going to say that it's

737.04

going to come through and it's going to

738.24

create because it doesn't exist it's

740.48

going to try to create it

744.399

and now what i want to do

746.56

is say

749.839

and it gets that in there twice

751.92

so i can see where he exists twice

755.2

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

757.12

actually change this a little bit

764.72

and

765.519

i'm going to well i'm not going to

766.8

create that yet so now what i'm going to

767.839

do this time around is i'm going to say

769.519

in the

770.48

new names did i get the new names of pen

773.279

yeah

774.399

so i'm going to come out here and i'm

775.68

going to do print

778.32

new names

782

let's see how that works

784.639

and

785.519

uh

787.519

okay so there's nothing in new names

789.36

wait what did i do

791.04

oh whoops

797.04

oh i can do that let's do that

801.68

let's just put it like this

804

we'll print our new names there we go

806.959

so each one

809.12

and here we go

810.32

oh no those are

812.72

the column names

821.839

so

822.639

new names needs to be actually just the

824.8

name

831.44

and so now i'm gonna see there we go so

834

if i see the name twice

840

then i'm gonna need to do some let's see

845.199

so how do i want to do that

849.839

i probably want to do it as

852.8

so i'm going to come through here once i

854.72

get done

856.8

let's see so i've got my new names and

858.72

so this is where i'm going to actually

861.519

build out my

863.12

stuff

866

so i'm going to do a

868.44

four name

871.76

in

874.16

new names

882.16

and i'm going to do

886.24

um let's do this

889.68

created

892.399

whoops

894.959

equals this thing

897.279

and what i'm going to do with each one

898.959

is i'm going to say

901.92

if

904.079

name

905.12

in

907.04

created

908.72

so that means it already exists

912.959

else

916.959

created dot append

921.04

name

926.24

and then what i want to do

932.639

so if the name is in created

936.88

so if i'm or if it already exists and i

939.04

need to get rid of this otherwise

942.24

i'm going to append it and i'm going to

943.6

do something with it

948

actually what i probably need to do

950.8

is change this to

956.639

uh fields

961.839

so what i need to do yeah i'm gonna have

963.36

to do this a little differently so okay

965.44

so this is gonna be um

969.519

this will be

970.72

index names

974.639

and so when i come through here

976.8

where's index names

988.16

let's see if i can do this name

991.6

equals and then i need to get the column

994

i need to get the uh

996.24

the name of the column

1002.56

and so now what i'm going to do

1005.68

is turn this into a little bit of a

1007.04

dictionary type of an approach

1014.72

and let me make sure i've got that right

1016.24

because i think i don't

1019.68

that's what i thought as i'm going to do

1021.04

that so

1026.16

that's what i want to do okay so my i

1028.559

start with

1029.839

this

1031.6

whoop

1033.039

this

1034.559

and i'm going to do

1037.36

for each of those

1040.88

i'm gonna do

1045.76

i think that's how i want it to do right

1047.679

oh nope

1048.799

i'm okay

1051.2

so i can do it like that

1055.919

dot append

1063.12

name and a pin so

1065.679

okay

1066.96

there we go so that's going to give me

1068.24

my columns that'll work

1072.799

let's see if that worked uh it probably

1074.799

works that way

1076.4

so now

1077.6

um for name in

1082.24

idx names

1086.88

and let's just do this for now let's

1088.559

just do

1091.919

let's get rid of that i'm gonna do print

1093.52

name

1096.16

and see what happens

1098.4

because we are

1100.72

there we go

1101.919

so running some issues here getting this

1104.08

i'm going to because of time constraints

1106.799

i am going to

1108.72

pause this here

1110.24

and we'll come back next time we're

1111.44

going to start digging through this as

1112.64

we are having to rewrite this because

1114.48

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

1115.44

build out a dictionary of columns or i'm

1118.559

sorry of index names and then with each

1120.4

of those we'll have an array of columns

1122.32

and then we'll use that to build our sql

1126.32

that being said it's time for us to wrap

1128.08

this one up so go out there and have

1130.08

yourself a great day a great week and we

1132.64

will talk to you

1134.32

next time

1151.44

you