📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 10

2022-09-15 •Youtube

Detailed Notes

Focus for this episode: We debug a few column synchronization issues.

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 on sql in python
where we are building a database
synchronizing python script
application
and last time around we were
looking at synchronizing columns we got
so we have the ability to add tables to
add columns to add rows and now we sort
of flip back up a level to deal with
columns
now one of the things i want to do
is we ran through this and we're
basically spinning through doing our
describe of columns
and one of the things we want to do is
where there are differences then we come
in and we do this little uh update and
we print our update well what i want to
do is i'm going to do something where i
do a print that the difference is found
and i'm going to pull out
some of these other prints
that'll work fine
i think that'll do it
if i get that and then
that probably looks pretty good okay and
then i'll print the tables so
um yeah that's if update
so now let's try this again
and i'm going to go through let's make
sure i'm the right one let's see what
we're getting because some of these are
getting changes
so
let's look at some of these because i
think we're not reading these are where
we don't read stuff exactly right so
first one here
[Music]
we come up here
oh let's see
okay so
we have here there's a yes and a no uh
let's get what the
let's say difference found
um
let's do
this let's go ahead let's actually make
this a little easier so we're going to
print
see do i want
oh because i got rid of those two that's
what i don't want so here we go um
oh well i can do i'm going to go and
print the
index
yeah idx
okay
oh let's do this while length of source
column
so let's do this let's just print
um
source column
wait that's not what i want because i
need to go up here
okay so that's where the name matches
and then what i want to do is uh
yeah i can probably do source column
and destination column
let's go look at that let's see if this
will give it to me
all right so here for example
so i've got a current time stamp
but it's the default generated
so in this case that's going to be good
what i want to do is i want to do the
modify
update so i'm going to go look at that
default generated key because i think
that's what that is
so that's one of the things we're going
to fix so let's look here we're just
going to do
fix
default
generated
oh and that's going to be there so
that's actually part of the
those are two different ways to so we
need to do is we need to fix that in our
time so if we go up here to where's our
time
uh see did we do oh here we go
so if he is
that
oh wait that's not where i want the
default to be
ah
default default default
false michael none and i done and that
is
okay so that's just gonna say that's
those two so we're gonna have to fix
that one okay so let's go see if we can
find something else as well so here
we've got
a mole so this is a multiple prime uh
key
so i think we're gonna find these so
what we're probably going to do is we're
going to be running into key issues here
uh
let's see
multiple
uh unique
so what we're going to have to do is
we're going to actually dig up those
uh those keys those indexes in those
cases
uh let's see timestamp no that's whether
it's in default or whether it's nullable
or not
so this one should be
okay so the destination is the second
one
and so it's saying that there it is
uh can be defaulted but it's saying no
so i need to check
uh let's see
oh i need the null did i not get the
null piece oh i didn't get the null
piece
so let's go look here to
um
is that what i want
uh default let's see
no
i need not know here we go
so i'm going to need this one basically
so that was in column two
so i need to say here
[Music]
let's see
sync my rows sync my columns
this is going to be sort of a problem so
i'm going to say here
um
this is going to be
uh this is going to be lf idx
equals
is that one
no two
two
so far i gotta make that that guy
so i get my bites here i go there there
there
so here
um
if
um so this is going to be source call
right uh this is cur source
whoops
dot upper
equals yes
then
i need to do
i'm going to do like a null string right
default
so i'm going to need here
and he's going to need to do another one
so i'm going to have to do a plus
default i'm also going to have to do a
i'm going to call that the null string
and the null string is up here in two
i'm going to sit here so
i'm going to say
null string equals
uh so i'm going to say if it's no uh if
it's nullable yes so it's going to be
not null
and here
if he's yes
then null string
equals no
okay so now let's go take a look at
those
whoops doesn't like that
data truncated
oh that's because of the columns sync
rows i don't need to do sync rows let's
get rid of that real quick
that's a different issue oh maybe not
um let's go
so app user doesn't like that
let's see
if i do this
uh max rose equals
zero
let's go do this let's do my sequel
what is my little thing up here
tutor okay
tutor
and password
and i'm going to use tutorial actually
i'm going to do drop database tutorial
to tool oops if i can spell it right
drop database tutorial
create database tutorial
and now let's see what happens if i run
it
nope i'm still getting an issue here so
i've got a data current truncated so
let's see what if i get right before
that
alter table modify t1id
oh so i want to ignore zero one two
three so let's ignore the keys for now
and so now
uh let's see
i want to come in sync
columns
because i'm not going to i'm going to
deal with the uh
oh let's see i am going to deal with
make sure i got everything here oh no i
don't
and
i'll make sure i don't deal with the um
the indexes and such that zero one two
three so if an index
not equal to three
now let's try
here we go
so what we're doing here is let me put
that in there
ignore index
compare
here
so now i'm going to come through
it's going to do all the data so i'm
going to i've blown this away we're
going to bring all our data across
and then we'll get some alters here
can i give it a minute while it's doing
this yes i am just vamping a little bit
while this thing is cranking through
okay we finally got through all of that
so let's go back in here and what we
want to do
is uh we've got now the columns are all
coming across same so what i want to do
is go over here somewhere
if it's going to allow that
i need to reconnect
and what i'm going to do now is i'm just
going to go into a simple like i'm going
to go here
and i'm going to change that so i'm
going to go alter the table
and we're here it has address names 100
and city is 40. i'm going to change it
to 50 and 20.
and now what i should get is i should
get those two
to update as this thing comes through
and then what's going to happen is uh
we'll see these will kick back to what
was at 140.
so if we run through and do this i've
dropped the zeros the max rows to zero
and i just got rid of syncros so just go
quick
okay it took a few seconds to get
everything back in line but what we're
going to see here is that we can see our
differences
and here an address we've got one that
was a varchar50 one is a 100
one and we can see the difference found
was there and then it's going to modify
it
and it's going to make address name
equal to 50 which means we're grabbing
the wrong value uh and here it goes from
2040 so we're doing the wrong one so we
are
let's see when we create
the sink of rows
oh no i want some columns
then what we want
is we want it to be
it should be the source so let's see if
we're getting these right so like here
we're talking at the size
which is probably here if they're not
the same then we have this difference
and so
oh
difference is found
and that's going to be in the
name equals cursor if it's one if it's
four
which is zero one so we need one uh so
for one
let's see call type equals cur source
which should be right
oh we've got the two flipped
let's see
so
source is the source columns
which is
okay so you have source and destination
source which is dbc
yes
dbc is our source db2 is our
yep
destination
so we should have
here when we do sync columns
oh here it says the source column so the
source column is not getting the right
thing so source call
uh let's see
so i think what we want here
set source column that's destination
column
which we
want that to be
fixed here if we do the update
then we take the column type
which should be
where's call type
call type is nothing
interesting
so
i have that somewhere in here so let's
see
call type
there we go
so the call type should be the current
source
but the source is 50
which is not right because what we had
back here
was 50 was the address name
we're looking at the local
and so if we look here
let me make sure i got these right so
source is source
destination is dist
i'm going to update cursor which is the
destination cursor
uh source equals that query destination
equals that source columns is the source
destination columns is destination
source calling that destination call
that
if
the same they come in here name equals
source column
current source current destination got
that got that got that
got that got that got that
something is awry here
say columns
sync columns that's sorry the sync rows
sync columns
source
destination uh let's see and then an
update do i do an update yeah do an
update which is on the destination
source does a query destination does a
query
so let's go look here something's not
quite right there so that's probably
going to run through if i run it
and then i'm going to see the same thing
up now they're the same so let's see
maybe i've just printed those wrong
let's go back here
refresh the objects tree
no
local 50
and city is 20. if i go to
my demo did we get those flipped
uh if we go out here to our tutorial
did i just make a big mistake uh i don't
think i did but we're gonna find out in
a second
yep 15
20.
so let's go look at that again oops oh
he doesn't like that so he's got an
issue okay so i'm going to have to fix
that but i'm not sure why so if i come
through here let's go look again
db2
is my local host
oh which i'm probably working everything
except here so let's go look here did i
switch those two
let's see so this is dbc
and db2
which do not get changed anywhere
so source and destination so if i come
in here
source destination
i do the description on the source
description on the destination so it
should be at 5900
updates oops sorry that's synchros
is destination as well
and so i do uh source fetch all i do
destination fest hall
i'm really concerned about this because
that source
everything here is coming out of the
source except
that's okay so that's a column type
so let's start here
if we go all the way up here
let me see address name is there
city oh because it's comparing both of
the same so we have to change it out
this time
so we'll have to come back and we will
tackle that next time around we've got a
bug but i think we're going to go ahead
and call this and then we will come back
next time and track that bug down
as always go out there and have yourself
a great day a great week and we'll 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 on sql in python

32.64

where we are building a database

34.559

synchronizing python script

37.76

application

39.04

and last time around we were

41.36

looking at synchronizing columns we got

44.64

so we have the ability to add tables to

46.96

add columns to add rows and now we sort

50.16

of flip back up a level to deal with

51.68

columns

53.12

now one of the things i want to do

54.96

is we ran through this and we're

56.48

basically spinning through doing our

58.559

describe of columns

60.64

and one of the things we want to do is

62.48

where there are differences then we come

64.72

in and we do this little uh update and

67.439

we print our update well what i want to

69.439

do is i'm going to do something where i

71.28

do a print that the difference is found

74.24

and i'm going to pull out

77.759

some of these other prints

79.84

that'll work fine

82.479

i think that'll do it

86.159

if i get that and then

88.799

that probably looks pretty good okay and

90.24

then i'll print the tables so

93.119

um yeah that's if update

95.36

so now let's try this again

98.079

and i'm going to go through let's make

99.759

sure i'm the right one let's see what

101.36

we're getting because some of these are

102.32

getting changes

104.159

so

105.439

let's look at some of these because i

106.56

think we're not reading these are where

108

we don't read stuff exactly right so

110.079

first one here

111.08

[Music]

113.439

we come up here

115.52

oh let's see

117.92

okay so

120.079

we have here there's a yes and a no uh

123.2

let's get what the

125.84

let's say difference found

129.2

um

130.8

let's do

134.16

this let's go ahead let's actually make

136.319

this a little easier so we're going to

137.76

print

140.84

see do i want

147.52

oh because i got rid of those two that's

149.28

what i don't want so here we go um

152.8

oh well i can do i'm going to go and

154.319

print the

155.44

index

156.4

yeah idx

158.72

okay

168.8

oh let's do this while length of source

171.2

column

173.36

so let's do this let's just print

177.04

um

179.519

source column

181.2

wait that's not what i want because i

182.8

need to go up here

189.519

okay so that's where the name matches

192

and then what i want to do is uh

196.319

yeah i can probably do source column

198.959

and destination column

201.12

let's go look at that let's see if this

202.48

will give it to me

207.12

all right so here for example

212.159

so i've got a current time stamp

214.879

but it's the default generated

217.76

so in this case that's going to be good

219.44

what i want to do is i want to do the

223.76

modify

224.84

update so i'm going to go look at that

227.44

default generated key because i think

229.599

that's what that is

231.36

so that's one of the things we're going

232.72

to fix so let's look here we're just

234.08

going to do

236.56

fix

238.159

default

239.439

generated

244

oh and that's going to be there so

245.36

that's actually part of the

248.72

those are two different ways to so we

250.4

need to do is we need to fix that in our

251.92

time so if we go up here to where's our

254.56

time

256.479

uh see did we do oh here we go

260.4

so if he is

263.6

that

266

oh wait that's not where i want the

267.36

default to be

272.56

ah

274.32

default default default

277.919

false michael none and i done and that

280

is

282

okay so that's just gonna say that's

283.36

those two so we're gonna have to fix

284.56

that one okay so let's go see if we can

286

find something else as well so here

287.44

we've got

289.04

a mole so this is a multiple prime uh

292.32

key

293.759

so i think we're gonna find these so

295.12

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

296

going to be running into key issues here

299.759

uh

301.039

let's see

302.639

multiple

304.56

uh unique

308.88

so what we're going to have to do is

309.919

we're going to actually dig up those

312.4

uh those keys those indexes in those

315.52

cases

317.12

uh let's see timestamp no that's whether

319.919

it's in default or whether it's nullable

321.84

or not

322.96

so this one should be

330

okay so the destination is the second

332.639

one

334.24

and so it's saying that there it is

338.639

uh can be defaulted but it's saying no

340.88

so i need to check

343.68

uh let's see

346.639

oh i need the null did i not get the

348.32

null piece oh i didn't get the null

349.759

piece

350.88

so let's go look here to

354.72

um

356.96

is that what i want

360

uh default let's see

361.919

no

362.639

i need not know here we go

367.199

so i'm going to need this one basically

368.96

so that was in column two

371.52

so i need to say here

376.81

[Music]

378

let's see

379.36

sync my rows sync my columns

384

this is going to be sort of a problem so

386

i'm going to say here

388.479

um

390.4

this is going to be

394.16

uh this is going to be lf idx

397.919

equals

400.8

is that one

402.08

no two

404.88

two

407.199

so far i gotta make that that guy

411.199

so i get my bites here i go there there

414.479

there

417.28

so here

419.12

um

420.56

if

424.639

um so this is going to be source call

427.759

right uh this is cur source

431.199

whoops

435.28

dot upper

438.16

equals yes

441.52

then

444.319

i need to do

447.68

i'm going to do like a null string right

450.24

default

454.56

so i'm going to need here

459.759

and he's going to need to do another one

460.96

so i'm going to have to do a plus

461.919

default i'm also going to have to do a

470.24

i'm going to call that the null string

476.08

and the null string is up here in two

478.639

i'm going to sit here so

481.039

i'm going to say

482.319

null string equals

485.759

uh so i'm going to say if it's no uh if

487.84

it's nullable yes so it's going to be

491.44

not null

495.44

and here

499.919

if he's yes

501.759

then null string

504.84

equals no

506.96

okay so now let's go take a look at

508.4

those

510.639

whoops doesn't like that

512.959

data truncated

518.8

oh that's because of the columns sync

522

rows i don't need to do sync rows let's

523.44

get rid of that real quick

528.08

that's a different issue oh maybe not

533.12

um let's go

537.92

so app user doesn't like that

543.839

let's see

547.68

if i do this

549.839

uh max rose equals

552.08

zero

553.68

let's go do this let's do my sequel

557.04

what is my little thing up here

562

tutor okay

564.959

tutor

566.399

and password

570.959

and i'm going to use tutorial actually

573.2

i'm going to do drop database tutorial

579.04

to tool oops if i can spell it right

582.56

drop database tutorial

584.8

create database tutorial

588.88

and now let's see what happens if i run

590.64

it

592.64

nope i'm still getting an issue here so

594.32

i've got a data current truncated so

596.16

let's see what if i get right before

597.519

that

599.12

alter table modify t1id

603.279

oh so i want to ignore zero one two

605.839

three so let's ignore the keys for now

610.16

and so now

615.36

uh let's see

621.44

i want to come in sync

625.2

columns

626.56

because i'm not going to i'm going to

627.44

deal with the uh

630.399

oh let's see i am going to deal with

639.2

make sure i got everything here oh no i

641.519

don't

651.2

and

652.64

i'll make sure i don't deal with the um

655.04

the indexes and such that zero one two

657.04

three so if an index

659.279

not equal to three

663.36

now let's try

666.24

here we go

667.6

so what we're doing here is let me put

669.44

that in there

672.64

ignore index

676.16

compare

678.88

here

681.2

so now i'm going to come through

683.2

it's going to do all the data so i'm

684.399

going to i've blown this away we're

685.519

going to bring all our data across

689.04

and then we'll get some alters here

693.2

can i give it a minute while it's doing

695.2

this yes i am just vamping a little bit

697.44

while this thing is cranking through

699.92

okay we finally got through all of that

702.56

so let's go back in here and what we

704.8

want to do

706

is uh we've got now the columns are all

708.16

coming across same so what i want to do

710.44

is go over here somewhere

714.8

if it's going to allow that

719.279

i need to reconnect

723.6

and what i'm going to do now is i'm just

724.8

going to go into a simple like i'm going

726.24

to go here

728.639

and i'm going to change that so i'm

730.24

going to go alter the table

732.639

and we're here it has address names 100

735.2

and city is 40. i'm going to change it

737.04

to 50 and 20.

742.399

and now what i should get is i should

744.16

get those two

748.8

to update as this thing comes through

752.88

and then what's going to happen is uh

755.2

we'll see these will kick back to what

756.88

was at 140.

762.16

so if we run through and do this i've

763.519

dropped the zeros the max rows to zero

766.32

and i just got rid of syncros so just go

768.399

quick

770.32

okay it took a few seconds to get

772.32

everything back in line but what we're

773.519

going to see here is that we can see our

776.24

differences

777.68

and here an address we've got one that

779.76

was a varchar50 one is a 100

783.519

one and we can see the difference found

785.6

was there and then it's going to modify

787.76

it

789.04

and it's going to make address name

790.639

equal to 50 which means we're grabbing

792.32

the wrong value uh and here it goes from

794.399

2040 so we're doing the wrong one so we

796.8

are

799.04

let's see when we create

804.32

the sink of rows

809.12

oh no i want some columns

814.399

then what we want

817.839

is we want it to be

823.839

it should be the source so let's see if

826.16

we're getting these right so like here

827.6

we're talking at the size

829.839

which is probably here if they're not

831.68

the same then we have this difference

836.16

and so

838.639

oh

840.32

difference is found

847.279

and that's going to be in the

853.519

name equals cursor if it's one if it's

855.839

four

857.44

which is zero one so we need one uh so

860.399

for one

868.399

let's see call type equals cur source

871.279

which should be right

879.44

oh we've got the two flipped

883.839

let's see

885.36

so

886.639

source is the source columns

889.199

which is

890.639

okay so you have source and destination

894.639

source which is dbc

897.36

yes

900.079

dbc is our source db2 is our

904.079

yep

905.519

destination

908.32

so we should have

912.959

here when we do sync columns

918.079

oh here it says the source column so the

920.56

source column is not getting the right

921.839

thing so source call

928.48

uh let's see

933.839

so i think what we want here

939.68

set source column that's destination

942.079

column

945.839

which we

947.199

want that to be

951.92

fixed here if we do the update

957.12

then we take the column type

962.079

which should be

963.44

where's call type

965.759

call type is nothing

973.199

interesting

976.959

so

977.68

i have that somewhere in here so let's

979.04

see

980.56

call type

982.56

there we go

986.32

so the call type should be the current

988.8

source

997.36

but the source is 50

1000.16

which is not right because what we had

1002.399

back here

1005.839

was 50 was the address name

1009.04

we're looking at the local

1014.079

and so if we look here

1016.24

let me make sure i got these right so

1017.68

source is source

1020.32

destination is dist

1022.48

i'm going to update cursor which is the

1024.079

destination cursor

1025.76

uh source equals that query destination

1028.959

equals that source columns is the source

1030.799

destination columns is destination

1033.28

source calling that destination call

1034.799

that

1036.079

if

1037.199

the same they come in here name equals

1039.199

source column

1042.959

current source current destination got

1044.64

that got that got that

1050.559

got that got that got that

1056.559

something is awry here

1066.4

say columns

1068.72

sync columns that's sorry the sync rows

1071.2

sync columns

1073.84

source

1078.84

destination uh let's see and then an

1081.84

update do i do an update yeah do an

1083.76

update which is on the destination

1092.64

source does a query destination does a

1095.36

query

1098.559

so let's go look here something's not

1100.16

quite right there so that's probably

1101.52

going to run through if i run it

1105.28

and then i'm going to see the same thing

1109.919

up now they're the same so let's see

1111.44

maybe i've just printed those wrong

1113.44

let's go back here

1115.84

refresh the objects tree

1119.919

no

1124.16

local 50

1125.76

and city is 20. if i go to

1130.32

my demo did we get those flipped

1133.679

uh if we go out here to our tutorial

1138.24

did i just make a big mistake uh i don't

1141.039

think i did but we're gonna find out in

1142.32

a second

1146.799

yep 15

1149.2

20.

1153.52

so let's go look at that again oops oh

1155.76

he doesn't like that so he's got an

1157.12

issue okay so i'm going to have to fix

1159.12

that but i'm not sure why so if i come

1161.039

through here let's go look again

1164.4

db2

1165.84

is my local host

1171.039

oh which i'm probably working everything

1173.039

except here so let's go look here did i

1175.52

switch those two

1178.72

let's see so this is dbc

1181.12

and db2

1183.039

which do not get changed anywhere

1189.84

so source and destination so if i come

1192.88

in here

1196.48

source destination

1201.36

i do the description on the source

1203.28

description on the destination so it

1205.28

should be at 5900

1208.559

updates oops sorry that's synchros

1212.32

is destination as well

1218.24

and so i do uh source fetch all i do

1221.6

destination fest hall

1228.72

i'm really concerned about this because

1230.32

that source

1236.88

everything here is coming out of the

1239.039

source except

1244.88

that's okay so that's a column type

1250.88

so let's start here

1259.84

if we go all the way up here

1264.32

let me see address name is there

1266.799

city oh because it's comparing both of

1268.64

the same so we have to change it out

1269.919

this time

1271.52

so we'll have to come back and we will

1273.52

tackle that next time around we've got a

1275.44

bug but i think we're going to go ahead

1277.28

and call this and then we will come back

1279.52

next time and track that bug down

1281.6

as always go out there and have yourself

1283.12

a great day a great week and we'll talk

1286.08

to you

1287.36

next time

1289.02

[Music]

1304.559

you