📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 9

2022-09-13 •Youtube

Detailed Notes

Focus for this episode: We start into a deeper look at synchronizing columns

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 season our series where
we're looking at
uh if i can get back here we're looking
at python and at sql server and sql in
general i guess and we are building a
synchronization
and we've started with tables
we've added columns we've added data so
in each case this is
adding things that don't exist from a
source to a destination as first tables
are concerned um really there's not much
there it's either it's there or it's not
and since this is a
essentially a one-way synchronization
our goal is
just to find tables that exist in the
source and
go ahead and add them into the
destination
we could go vice versa or i guess say
anything that doesn't exist in the
source we want to drop that table
in the destination but i think we'll
keep it now that we probably will swing
back around to that later as a
maybe a setting or something like that
but for now i want to take that next
step
which is with the table
what do we do if a because we've we've
got columns uh first cases column exists
in source
doesn't exist in destination
another situation is
it exists in both but it's different
and that's what we're going to start
into uh today
so before we had this syncrose where
we're just going to synchronize the rows
from one to the other
and what we want to do this time is
we're going to actually do the same
thing essentially but we're going to do
and actually that should have been adros
but we're going to do it with the
columns
and so the way we need to come through
here
where is this at
so with this one what we're going to do
is we're going to start with our we've
got our source our destination databases
and then a list of tables and that
tables list
which is right here source tables
is the list of tables from the source
table uh source database so we're just
going to go through that source results
at all our tables uh missing columns
sync columns here we go
and we're going to need a couple cursors
so we come in we're going to send those
in
and we're going to have a cursor for the
source database
and then we're going to have two
different cursors for the
destination database
because we're going to be
spinning through that a couple different
ways
and we have this missing columns which
we really don't need i don't know why
it's because i copy and paste error so
we're going to do is for each table in
the list
we're going to walk through
and the first thing we do is going to
get and this is the source
we're going to walk through and we're
going to get a description we're going
to get the details of the table in the
source database
and actually what we're going to do is
we're going to get it in both so if you
can see here
get the list of columns in the first in
the source list of columns in the
destination
and then we come through and we
load those into source columns and
destination columns
and then what we're going to do is for
each column in source columns so for
source column in there
we're going to then for each one of
those we're going to walk through the
destination columns
and we're basically going to say that
uh if destination column name
uh let's see is that right yeah i'm
sorry
that's your destination
column
uh for each one of those if
the destination column is equal to
the source column
which is the name then we're going to do
something otherwise
we don't do anything at all so basically
first we're going to do is we're going
to say hey if the name is the same so
we're not going to allow for name
changes because we don't really have a
way to track that that's the same column
so what we're going to do is go ahead
and just assume that we're good
uh other than if the chain if we have
the same name so if the name is a chain
same
they're going to walk through we're
going to assume that we don't need to
update it
uh and you'll sort of see as we go into
it
but otherwise we're going to do is we're
going to walk through
let's see so we're going through
we're going to go through the length of
source column
uh remember these are
arrays essentially so
we're going to get the length of source
column and then we're going to basically
walk through each of them and actually i
guess we could start it at 1.
and right now we're going to print each
of those we're going to print that
should be
source column
and then destination column so we're
going to print what is the
details of each column and it may be
things like you know we skip the name
but it could be type
we've got functions we've got whether
it's primary key or
key or not some things like that
now first thing we need to do is
we have some instances where in either
of those
we're going to have a um and let's
actually just do it like this we're
going to do
a current destination
equals something let's do that and
current source
equals
because what we need to do is we need to
figure out if the type of the row uh
let's see source column because we get
these a little bit differently from time
to time
sometimes they come back in strings
sometimes sometimes they come back as a
byte array
uh so if the source then we're going to
say that the
current source equals whoops
a d
of the
which just basically says let's take
this and we've seen this before you can
go look in the
python stuff but
basically we're going to make cursors
equal to a decode of that
otherwise
source is just the string
and we'll just make it a string so let's
do string of
source column index
and this is just so we can do some
string compares and things like that and
we're going to do the same thing with
the destination
so this needs to be our destination
column
and destination column
and current destination
if not
i'm going to do here
and it's going to destination come so
what we've got is we've figured out
regardless what we have is we're going
to
is that right yeah then we've set these
two up
and what we'll see is we'll see here
that these things are going to sometimes
be different so if we come in here
and we do uh current source
i do it the same order so current source
current destination
now we've got uh some specialty type
things so if index is one
then we're going to have to do something
here um
let's see we're going to go ahead and
we don't need to mess with that so
if it's one then if the
uh
what do we call the current
source curve source dot upper starts
with that or
cursed source
that upper starts with that
then
we have to split it up a little bit
and this is what we've seen before
because of the whoops src
um we've seen this because of situations
where we have those like ants that have
a
a size and
or in some kinds they don't sometimes
just straight up sometimes it's an int
and it has a default size which can be a
problem so we're going to fix that in
both of these
just to make sure that they are
we're doing an apple to apple comparison
and so we have to do a little bit of
conversion for those
and then we don't worry about that
and then um
[Music]
we're just going to go ahead and now
compare the two
and uh let's see
do we want this uh
yeah so we're gonna say
here
oh and this is just
oh actually we can do this so we're
going to say
let's get
let's get rid of this
let's take this uh so we're gonna do
that otherwise we're gonna say if these
things are true
um if those two are equal if source then
uh oh if they're not equal then we're
gonna have to update it and they're
gonna walk through our indexes
so then what we're gonna do is say okay
so now
what do we do if we have to update it
so now what we wanna do is we're gonna
come down here
and we're going to say
uh this is not going to call row uh so
the name
is going to be
uh source
oh
call source
i'm sorry current source id uh zero
so that's our name
uh the type
is again we can just do um
let's see did we we decoded those each
time right so we have
we're doing the decodes here
oh so this is for each one
so
really what we want to do
oh this is if update okay i'm sorry if
update so uh name equals that
um
so this is going to be
uh
let's go ahead and do it this way
source column
apologies i'm sort of thinking on the
fly here a little bit as i'm going
through this so name is going to be here
the
column type
that's going to be source
column
[Music]
1. and we probably want to do
actually let's do it this way
student name equals this
type equals this
what else do we need
we need default
uh
so he's gonna be that
okay so we can do default up here
we need to enable name type column type
whoops
where'd you go
name type
default and
so table is good so i just need column
let's call them column type instead
and then what i'm going to do
is
here
i'm going to do this real quick so i'm
going to say
if
well if it's equal to 1
then i know that the name is equal to
oops i'm sorry that's not going to be it
let's not be that that's going to be
here
and so when i walk out of this if the
index is 1 then the column type
whoops is equal to the current source
if
[Music]
i guess i can do an else here
else out let's do lift
idx equals zero
if a0
then the name
is equal to the current source
whoops
and so we're going to do is we're going
to build out our little string if we
need to
which we don't yes maybe you could say
it's just it's a bit of a pain but it
also saves us walking through some crap
later so uh zero one
uh next is the default so four
then default equals current source
and
let's see column type name and default
those are the three we need okay
so we really need that so those are our
three
um
do we need
yeah because right now we're not going
to worry about uh some of the other
values like a primary care anything like
that so we should be good with those
three
so then that means uh if we do an update
so did we already get that we don't have
the default okay so then we need to do
the default thing
then we need to do if those are in there
then we're going to change up our
default
and
then we do an offer table we're going to
print and we're going to update so let's
see what happens
so if we run that um
let's do
yeah we'll just sync rows we're gonna
leave that at 10 right now and let's go
see what this runs out to be
okay so we've got an issue somewhere in
here which is in the none okay so
uh alter t oh so we need to deal with
that none before we had that
where did we run into that here we go
uh
oh
so
let's update
so if
let's just do this because i think this
is just for the default maybe so if
default
equals none
then default
oh let's do this
this way
else
default equals that we've got to blank
it back out so if he's in there or he's
in there that's good oh
let's do this
see so if default is not equal to none
and
fault
not equal to that
okay there we go
let's see if that works okay so now it
doesn't uh blah blah blah oh we're still
getting default none
is that right
oh
it's probably here let's try that
let's just say if default
because otherwise it's not let's see
what happens here nope that's still not
going to give it to us
okay if he's not equal to null
then
default should be equal to that so let's
try this again let's see what happens
so we're still getting
this i'm getting all these nuns in here
okay
alter table modify oh we're not getting
the name so we didn't get the name right
uh
name name name name name name
ah we didn't get name
so
we're gonna say
um which we know right right up here
name is never going to be that problem
so name is just going to be equal to
destination column so you're actually
going to be source columns just to be
consistent
whoops
and now we're going to get that there we
go and now we're getting missing columns
is not defined
oh we've got that somewhere in that's in
sync columns which we don't need
oh i probably moved that from the wrong
place
where is where is it looking for missing
columns
no here we go nope that's right
all right missing columns
there we go
i've got that there
[Music]
oh here we go
sync columns does not mean
uh
that
right
yep
so if we do it there we go so we get all
our inserts we fly through and we're
gonna see some stuff here for example
where there's a varchar2
but here it was a uh a line it shouldn't
be
and we're gonna go through here
and we're gonna see we're gonna see some
interesting changes here so we're gonna
go through next time around and track
these down uh try to see what's going on
we're gonna try to get a little bit
better of a debug because i don't think
it's getting us
exactly like it's supposed to uh for
example like lookup type default
undefined
uh we probably need to like we probably
need to deal with that so we've got a
couple special things to deal with we're
gonna track that down next time though
so go out there and have yourself a
great day a great week and we will talk
to you
next time
[Music]
you
Transcript Segments
1.35

[Music]

27.279

well hello and welcome back we are

29.279

continuing our season our series where

31.519

we're looking at

33.76

uh if i can get back here we're looking

35.52

at python and at sql server and sql in

39.6

general i guess and we are building a

41.36

synchronization

43.84

and we've started with tables

46

we've added columns we've added data so

49.6

in each case this is

52.16

adding things that don't exist from a

53.92

source to a destination as first tables

56.96

are concerned um really there's not much

59.44

there it's either it's there or it's not

62.399

and since this is a

64.4

essentially a one-way synchronization

66.72

our goal is

68.479

just to find tables that exist in the

70.159

source and

72.72

go ahead and add them into the

75.04

destination

76.479

we could go vice versa or i guess say

80.32

anything that doesn't exist in the

82

source we want to drop that table

84.159

in the destination but i think we'll

86.159

keep it now that we probably will swing

88.479

back around to that later as a

90.96

maybe a setting or something like that

93.439

but for now i want to take that next

94.799

step

95.759

which is with the table

98.64

what do we do if a because we've we've

100.72

got columns uh first cases column exists

103.6

in source

105.04

doesn't exist in destination

107.84

another situation is

109.52

it exists in both but it's different

112.72

and that's what we're going to start

113.84

into uh today

116

so before we had this syncrose where

118.64

we're just going to synchronize the rows

120

from one to the other

121.439

and what we want to do this time is

122.56

we're going to actually do the same

123.68

thing essentially but we're going to do

126.479

and actually that should have been adros

128.56

but we're going to do it with the

129.759

columns

131.28

and so the way we need to come through

133.04

here

133.92

where is this at

135.2

so with this one what we're going to do

136.319

is we're going to start with our we've

137.44

got our source our destination databases

140.16

and then a list of tables and that

142.16

tables list

143.76

which is right here source tables

147.04

is the list of tables from the source

150.239

table uh source database so we're just

152.4

going to go through that source results

154.239

at all our tables uh missing columns

157.36

sync columns here we go

160

and we're going to need a couple cursors

161.599

so we come in we're going to send those

163.12

in

163.92

and we're going to have a cursor for the

165.92

source database

167.44

and then we're going to have two

169.12

different cursors for the

171.2

destination database

173.2

because we're going to be

174.56

spinning through that a couple different

175.92

ways

176.8

and we have this missing columns which

178.319

we really don't need i don't know why

179.92

it's because i copy and paste error so

182

we're going to do is for each table in

184.239

the list

185.28

we're going to walk through

186.72

and the first thing we do is going to

187.92

get and this is the source

190

we're going to walk through and we're

191.28

going to get a description we're going

192.48

to get the details of the table in the

194.72

source database

196.879

and actually what we're going to do is

198.08

we're going to get it in both so if you

199.599

can see here

201.2

get the list of columns in the first in

203.92

the source list of columns in the

205.28

destination

206.56

and then we come through and we

208.879

load those into source columns and

210.879

destination columns

212.879

and then what we're going to do is for

214.56

each column in source columns so for

217.92

source column in there

219.68

we're going to then for each one of

220.959

those we're going to walk through the

222.319

destination columns

224.56

and we're basically going to say that

226.959

uh if destination column name

231.04

uh let's see is that right yeah i'm

232.799

sorry

234.08

that's your destination

235.68

column

238.799

uh for each one of those if

241.36

the destination column is equal to

244.56

the source column

246.72

which is the name then we're going to do

248.48

something otherwise

250.56

we don't do anything at all so basically

252.159

first we're going to do is we're going

252.879

to say hey if the name is the same so

254.799

we're not going to allow for name

256.239

changes because we don't really have a

258.239

way to track that that's the same column

261.6

so what we're going to do is go ahead

262.8

and just assume that we're good

265.759

uh other than if the chain if we have

267.52

the same name so if the name is a chain

269.52

same

270.72

they're going to walk through we're

271.759

going to assume that we don't need to

273.68

update it

275.44

uh and you'll sort of see as we go into

277.68

it

278.479

but otherwise we're going to do is we're

279.919

going to walk through

282.32

let's see so we're going through

287.919

we're going to go through the length of

291.12

source column

295.68

uh remember these are

297.36

arrays essentially so

299.52

we're going to get the length of source

301.039

column and then we're going to basically

302.479

walk through each of them and actually i

303.759

guess we could start it at 1.

307.6

and right now we're going to print each

309.36

of those we're going to print that

310.96

should be

312.479

source column

315.039

and then destination column so we're

316.24

going to print what is the

319.28

details of each column and it may be

320.88

things like you know we skip the name

322.479

but it could be type

326.8

we've got functions we've got whether

328.56

it's primary key or

330.24

key or not some things like that

333.44

now first thing we need to do is

335.919

we have some instances where in either

338.08

of those

339.12

we're going to have a um and let's

341.84

actually just do it like this we're

343.039

going to do

344

a current destination

347.44

equals something let's do that and

350.88

current source

352.8

equals

353.919

because what we need to do is we need to

355.44

figure out if the type of the row uh

358.8

let's see source column because we get

360.8

these a little bit differently from time

362.8

to time

364.96

sometimes they come back in strings

366.16

sometimes sometimes they come back as a

367.759

byte array

368.88

uh so if the source then we're going to

371.28

say that the

373.68

current source equals whoops

378.4

a d

379.68

of the

381.039

which just basically says let's take

382.639

this and we've seen this before you can

383.919

go look in the

385.36

python stuff but

387.28

basically we're going to make cursors

388.8

equal to a decode of that

391.52

otherwise

394.96

source is just the string

397.68

and we'll just make it a string so let's

399.12

do string of

401.44

source column index

404

and this is just so we can do some

405.28

string compares and things like that and

407.84

we're going to do the same thing with

409.12

the destination

412.479

so this needs to be our destination

415.12

column

417.759

and destination column

421.199

and current destination

424.479

if not

425.68

i'm going to do here

428.16

and it's going to destination come so

429.52

what we've got is we've figured out

431.199

regardless what we have is we're going

432.319

to

433.12

is that right yeah then we've set these

435.68

two up

437.039

and what we'll see is we'll see here

438.639

that these things are going to sometimes

440

be different so if we come in here

442.4

and we do uh current source

445.919

i do it the same order so current source

449.84

current destination

456.56

now we've got uh some specialty type

459.12

things so if index is one

462.08

then we're going to have to do something

463.919

here um

465.599

let's see we're going to go ahead and

468.08

we don't need to mess with that so

470.96

if it's one then if the

474.24

uh

475.199

what do we call the current

476.84

source curve source dot upper starts

479.919

with that or

484.96

cursed source

487.28

that upper starts with that

489.759

then

491.36

we have to split it up a little bit

494.8

and this is what we've seen before

496.16

because of the whoops src

499.28

um we've seen this because of situations

502.319

where we have those like ants that have

504.08

a

505.52

a size and

507.28

or in some kinds they don't sometimes

509.52

just straight up sometimes it's an int

511.52

and it has a default size which can be a

513.68

problem so we're going to fix that in

515.519

both of these

517.279

just to make sure that they are

520.88

we're doing an apple to apple comparison

527

and so we have to do a little bit of

529.04

conversion for those

534.399

and then we don't worry about that

537.279

and then um

538.56

[Music]

540.56

we're just going to go ahead and now

542.08

compare the two

552.8

and uh let's see

557.44

do we want this uh

559.44

yeah so we're gonna say

560.959

here

565.44

oh and this is just

567.519

oh actually we can do this so we're

569.12

going to say

570.16

let's get

572.56

let's get rid of this

574.08

let's take this uh so we're gonna do

577.04

that otherwise we're gonna say if these

579.76

things are true

584.399

um if those two are equal if source then

587.76

uh oh if they're not equal then we're

589.44

gonna have to update it and they're

590.56

gonna walk through our indexes

593.6

so then what we're gonna do is say okay

595.04

so now

596.08

what do we do if we have to update it

598.64

so now what we wanna do is we're gonna

600.399

come down here

602.32

and we're going to say

604.8

uh this is not going to call row uh so

607.04

the name

609.12

is going to be

613.519

uh source

616.64

oh

619.2

call source

621.92

i'm sorry current source id uh zero

631.12

so that's our name

632.72

uh the type

636.48

is again we can just do um

639.68

let's see did we we decoded those each

642.16

time right so we have

646

we're doing the decodes here

652.16

oh so this is for each one

654.959

so

659.519

really what we want to do

662.8

oh this is if update okay i'm sorry if

664.959

update so uh name equals that

668.399

um

673.76

so this is going to be

680.959

uh

681.92

let's go ahead and do it this way

683.839

source column

685.839

apologies i'm sort of thinking on the

687.2

fly here a little bit as i'm going

688.64

through this so name is going to be here

691.839

the

692.8

column type

695.36

that's going to be source

698.079

column

698.78

[Music]

700.839

1. and we probably want to do

707.68

actually let's do it this way

711.279

student name equals this

714

type equals this

718

what else do we need

721.2

we need default

728

uh

731.519

so he's gonna be that

734.959

okay so we can do default up here

740.56

we need to enable name type column type

742.56

whoops

744.32

where'd you go

745.6

name type

747.68

default and

753.519

so table is good so i just need column

755.519

let's call them column type instead

759.92

and then what i'm going to do

762.56

is

764.16

here

768.24

i'm going to do this real quick so i'm

769.44

going to say

770.399

if

771.68

well if it's equal to 1

774.8

then i know that the name is equal to

784.56

oops i'm sorry that's not going to be it

787.44

let's not be that that's going to be

789.04

here

790.72

and so when i walk out of this if the

792.56

index is 1 then the column type

798.839

whoops is equal to the current source

804.639

if

805.16

[Music]

807.12

i guess i can do an else here

810.079

else out let's do lift

814.24

idx equals zero

823.279

if a0

825.199

then the name

827.6

is equal to the current source

831.36

whoops

833.12

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

834.079

to build out our little string if we

835.519

need to

837.04

which we don't yes maybe you could say

839.04

it's just it's a bit of a pain but it

840.8

also saves us walking through some crap

842.399

later so uh zero one

844.399

uh next is the default so four

849.519

then default equals current source

853.68

and

854.8

let's see column type name and default

857.92

those are the three we need okay

861.36

so we really need that so those are our

863.279

three

865.92

um

867.199

do we need

870

yeah because right now we're not going

871.199

to worry about uh some of the other

873.519

values like a primary care anything like

875.44

that so we should be good with those

876.56

three

877.519

so then that means uh if we do an update

886.48

so did we already get that we don't have

888.079

the default okay so then we need to do

890.32

the default thing

894

then we need to do if those are in there

895.36

then we're going to change up our

896.399

default

903.44

and

904.88

then we do an offer table we're going to

906.16

print and we're going to update so let's

907.6

see what happens

909.839

so if we run that um

914.079

let's do

915.6

yeah we'll just sync rows we're gonna

916.88

leave that at 10 right now and let's go

918.72

see what this runs out to be

922.32

okay so we've got an issue somewhere in

924.16

here which is in the none okay so

927.12

uh alter t oh so we need to deal with

928.88

that none before we had that

933.36

where did we run into that here we go

942.16

uh

943.279

oh

944.639

so

954.639

let's update

961.519

so if

963.04

let's just do this because i think this

964.48

is just for the default maybe so if

966.88

default

969.68

equals none

973.199

then default

979.92

oh let's do this

983.68

this way

986.959

else

988.88

default equals that we've got to blank

991.36

it back out so if he's in there or he's

993.6

in there that's good oh

997.92

let's do this

1005.68

see so if default is not equal to none

1011.759

and

1013.44

fault

1018.8

not equal to that

1021.12

okay there we go

1024.799

let's see if that works okay so now it

1026.64

doesn't uh blah blah blah oh we're still

1028.88

getting default none

1030.959

is that right

1038.24

oh

1039.52

it's probably here let's try that

1045.28

let's just say if default

1048.96

because otherwise it's not let's see

1050.08

what happens here nope that's still not

1052.16

going to give it to us

1064.96

okay if he's not equal to null

1068.48

then

1072.4

default should be equal to that so let's

1073.919

try this again let's see what happens

1077.44

so we're still getting

1078.84

this i'm getting all these nuns in here

1083.44

okay

1085.6

alter table modify oh we're not getting

1088.16

the name so we didn't get the name right

1091.12

uh

1092.16

name name name name name name

1099.44

ah we didn't get name

1101.28

so

1102.08

we're gonna say

1103.52

um which we know right right up here

1106.64

name is never going to be that problem

1108

so name is just going to be equal to

1112.24

destination column so you're actually

1114.08

going to be source columns just to be

1115.6

consistent

1117.76

whoops

1119.12

and now we're going to get that there we

1120.559

go and now we're getting missing columns

1122.72

is not defined

1126.48

oh we've got that somewhere in that's in

1128.96

sync columns which we don't need

1139.28

oh i probably moved that from the wrong

1141.36

place

1145.52

where is where is it looking for missing

1148.96

columns

1157.52

no here we go nope that's right

1164.799

all right missing columns

1167.6

there we go

1168.72

i've got that there

1169.6

[Music]

1172

oh here we go

1176

sync columns does not mean

1179.039

uh

1182.799

that

1184

right

1184.88

yep

1187.28

so if we do it there we go so we get all

1189.52

our inserts we fly through and we're

1191.039

gonna see some stuff here for example

1192.64

where there's a varchar2

1195.36

but here it was a uh a line it shouldn't

1198.16

be

1200

and we're gonna go through here

1204.799

and we're gonna see we're gonna see some

1206.96

interesting changes here so we're gonna

1208.48

go through next time around and track

1210.48

these down uh try to see what's going on

1212.72

we're gonna try to get a little bit

1214.08

better of a debug because i don't think

1215.919

it's getting us

1218.159

exactly like it's supposed to uh for

1220.559

example like lookup type default

1223.6

undefined

1226

uh we probably need to like we probably

1227.84

need to deal with that so we've got a

1229.2

couple special things to deal with we're

1230.96

gonna track that down next time though

1232.4

so go out there and have yourself a

1234

great day a great week and we will talk

1236.4

to you

1237.76

next time

1239.5

[Music]

1255.039

you