📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 28

2022-11-17 •Youtube

Detailed Notes

Focus for this episode: We start into building out the custom SQL to pull limited, but needed source data into our destination system.

This tutorial walks through the creation of a database synchronization tool in Python. It continues our Python and SQL Tutorials with a useful application that provides a deeper look at SQL structures and commands.

Repository For Code: git clone [email protected]:rbroadhead/dbsync.git

Transcript Text
thank you
[Music]
well hello and welcome back we're
continuing our SQL python series of
tutorials sort of this combo as we're
building a database synchronization app
we are deep into this at this point and
uh this episode will probably be a
little more uh python focused because
what we're going to do
is uh what we've done is we've we're
figuring out this sort of uh hierarchy
of tables where some tables have
referential have references to others
and we need to make sure that we take
the reference data as opposed to just
saying Hey I want you know 10 rows out
of this
now for the ones that we call our root
tables where it's basically hey this
table doesn't have any references or it
doesn't reference anything so we're just
going to take X number of rows that's
pretty easy
but now we get into the non-root and
what we did is we started sort of coding
this thing out but what I want to do
this time is I'm going to go in
and I'm going to extract this out
because this is going to end up being
um
it's going to end up being a procedure
and it's one that's going to be let's
see we'll do
uh we'll call it a referee
referential load
so what we're going to do with this is
this is going to and it's going to be
something where it may actually call
itself
uh actually probably will call itself
so we want to make sure that we can do
that and we're just going to go ahead
and do it right here uh where did I do
that
so non-root tables
I'm going to take that
and what I'm probably going to do right
here is just do a straight up
let's probably do it here is I'm going
to do referential
um actually I'll start self Dot
referential load
and then I'm going to give it the table
and that may be
oh and I'm going to give it imported
tables
because this is where I'm gonna have to
figure out has my table been loaded and
then I'm good or has it not as far as
like all of the um
the parents
so I'm going to come up here
and this is going to be a table
and it is going to be what I call that
loaded
I'm sorry imported rows
and it's uh see it I call it that here
I'm sorry it's imported tables
and then I probably want to return
because I'm going to be updating this as
I go
um
here we go
imported tables equals
so if I'm done
then
I'll be adding it to the
imported tables there we go I'll be
adding it to my list
so now what I want to do
is in here so I'm going to see if this
is referenced in a foreign key so I'm
going to try to load this
if it is
let's see so here
if it's referenced
then I'm going to have to deal with the
parent so here if the parent needs to be
loaded then all I have to do is do
reference uh
self Dot referential load
a table comma oh it's actually going to
be a parent comma
imported tables
and then imported tables
equals let me get that right
so there we go
so I could see if it's
there and he's not in
oop this is imported tables
right
uh
yes okay so the parents not then I've
got to load it otherwise
um
oh so that's for each of those parents
and then all I need to do is I'm going
to go through once I've got that done
uh let's see
so I'm going to load all my parents and
then all I have to do is load me so I'll
actually bring this back up
oh wait nope uh uh nope nope nope nope
so what I need to do here is for the
parents I'm going to have to build this
little complicated thing out that's
going to get sort of
interesting
oh no it's not so let's see so what I'm
going to do is
for each of these
and this is where I get this select star
and actually I can probably
now this is going to be interesting
because if I have multiple parents I may
have to load for multiple rows
uh
which is where did I do that
I think I had it
uh
oh so it was something like this
oops
so I'm going to do is I'm going to get a
list of IDs
and what I probably want to do
uh
let's see
and so here's sort of what I want to do
is I really want to take all of these
and Union them
I think what I want here
and there so what I'm seeing here is
this is going to be I'm going to build
these things out into a union because
what I want to do is say here's all the
IDS that I need to cover all of my
parents
so what I need to do here is I need to
say let's see
this is going to go it's going to get a
little more complicated because
um yeah because I'm gonna have to build
this out a little bit so what I need to
do here is I'm going to do uh smart load
SQL equals empty
and then here
I'm going to say if
smart load sequel
equals empty
then all I have to do is do that
else
I'm going to do
equals
whoops
smart load
SQL
Plus
and here's going to be Union dot value
or that big query type thing
and so when I get done
let's just get to here and see if I can
create this smart load SQL for anybody
so let's see what this looks like
I don't know if this is going to run at
all so let's find out where we at so if
we come through here
there we go so and so I've got something
here that's not off oh I've got a tuple
index out of range okay
uh which I probably didn't get all these
so I've got constraint stema
huh
what is my Tuple
I wonder if it doesn't like this
that's probably it this has probably got
something a little messed up so let's go
like this
yep that's it
that's going to be the problem
there we go so now let's try it
so we're going to come through do a
little data clean blah blah blah blah
blah
and now we're still okay so we still
have a tuple that's out of range
and it is on 513.
let's see did we miss something here
so the row is zero one two three four
let's just do print row
and see what it does for me
see if I missed something foolishly
simple in it
here we go up yep I did not get it so
Source sequels or SQL
Source row
well that's my problem
um
that's what I want I want Source row
and this needs to be
Source row ah
I figured it was something stupid like
that so let's go in here
and we're going to do this
these things out
and now let's take a look at it
and what we should see is something
that's I don't know if it's gonna have a
union or not let's see what our first
one is that we get uh nothing okay can't
concatenate
uh so I've got zero one two three four
five so I've got that
uh let's see what is
oh it's gonna be
so fetchall but I only need
zero
so let's look at it that way
yeah it's a problem because it was a
tuple and it's not there we go okay
so
my insert is going to look like this
let's just pick one here
uh giganator dot member
[Music]
hmm
I wonder if
that's my issue
yep oops
I need to move my quotes
like that or
do this
yep there we go okay so let's fix my
quotes that's what I figured I have here
I need to do that for sorcerer zero
source for zero
Source row zero
and Source row zero
so now
what I'm going to be doing is I'm going
to be basically insert into
the destination
what I am selecting from the other
uh and I wanted to do so how did we do
that before
is we took it
and that's missing tables
whoops smart empty referential load
I don't need that I need
uh where's my insert statement
um
foreign
let's do this let's go find it
okay
so load table data
is basically going to be
[Music]
oh here we go so for Row in
and I need uh okay so I need my insert
base which is here
so let's do that
steal that again
uh referential load okay so here
what I need to do
is now I'm going to go
um
cursor source
execute
that and then I'm going to be
destination rows equals
and let me do a quit here just because
and so now what I'm going to do is I
don't need that
can I call it Source rows I may be able
to call it Source rows
um
nope okay so for for
new row in
destination rows
there's my insert statement uh
I need
to do this
[Music]
where did I use row I didn't even use it
oh for value and there we go I'm sorry
for Value in new row
current value equals that uh
and so instead of executing in the
destination
I'm going to do a print
insert statement cool okay so now let's
go look at it and let's see what it's
going to build
and we should see a series of inserts
uh we did not
but
I think we'll end here and we're going
to debug this but we're getting pretty
close next time around we will come in
and hopefully we'll have this thing
knocked out and be able to actually give
it a shot
that being said go out there and have
yourself a great day a great week and we
will talk to you next time
foreign
Transcript Segments
10.7

thank you

18.89

[Music]

27.3

well hello and welcome back we're

29.099

continuing our SQL python series of

32.279

tutorials sort of this combo as we're

34.86

building a database synchronization app

37.559

we are deep into this at this point and

41.28

uh this episode will probably be a

42.54

little more uh python focused because

46.26

what we're going to do

47.94

is uh what we've done is we've we're

50.1

figuring out this sort of uh hierarchy

52.62

of tables where some tables have

54.899

referential have references to others

58.32

and we need to make sure that we take

60.3

the reference data as opposed to just

63.66

saying Hey I want you know 10 rows out

65.88

of this

66.84

now for the ones that we call our root

69.06

tables where it's basically hey this

71.34

table doesn't have any references or it

74.7

doesn't reference anything so we're just

76.5

going to take X number of rows that's

79.08

pretty easy

80.7

but now we get into the non-root and

83.58

what we did is we started sort of coding

85.259

this thing out but what I want to do

87.18

this time is I'm going to go in

90.479

and I'm going to extract this out

94.439

because this is going to end up being

97.5

um

98.1

it's going to end up being a procedure

102.479

and it's one that's going to be let's

105.72

see we'll do

107.42

uh we'll call it a referee

110.24

referential load

115.14

so what we're going to do with this is

116.579

this is going to and it's going to be

117.78

something where it may actually call

120.24

itself

122.04

uh actually probably will call itself

124.68

so we want to make sure that we can do

127.619

that and we're just going to go ahead

129.06

and do it right here uh where did I do

131.64

that

132.66

so non-root tables

136.44

I'm going to take that

139.62

and what I'm probably going to do right

141.18

here is just do a straight up

146.34

let's probably do it here is I'm going

148.08

to do referential

150.06

um actually I'll start self Dot

152.4

referential load

154.379

and then I'm going to give it the table

156.06

and that may be

159.06

oh and I'm going to give it imported

161.76

tables

166.86

because this is where I'm gonna have to

168.18

figure out has my table been loaded and

170.7

then I'm good or has it not as far as

173.819

like all of the um

175.86

the parents

177.9

so I'm going to come up here

180.9

and this is going to be a table

185.04

and it is going to be what I call that

187.44

loaded

189.9

I'm sorry imported rows

198.72

and it's uh see it I call it that here

201.959

I'm sorry it's imported tables

209.94

and then I probably want to return

211.92

because I'm going to be updating this as

213.9

I go

215.28

um

216.959

here we go

219.78

imported tables equals

224.4

so if I'm done

230.64

then

232.92

I'll be adding it to the

237.239

imported tables there we go I'll be

239.28

adding it to my list

242.28

so now what I want to do

244.5

is in here so I'm going to see if this

247.019

is referenced in a foreign key so I'm

249

going to try to load this

250.86

if it is

253.14

let's see so here

255.659

if it's referenced

260.94

then I'm going to have to deal with the

262.68

parent so here if the parent needs to be

265.979

loaded then all I have to do is do

267.66

reference uh

270.419

self Dot referential load

274.639

a table comma oh it's actually going to

278.28

be a parent comma

282.259

imported tables

285.54

and then imported tables

289.5

equals let me get that right

295.44

so there we go

297.96

so I could see if it's

301.62

there and he's not in

305.46

oop this is imported tables

309.78

right

311.28

uh

314.46

yes okay so the parents not then I've

317.94

got to load it otherwise

321.419

um

324.84

oh so that's for each of those parents

327.96

and then all I need to do is I'm going

330.6

to go through once I've got that done

333.66

uh let's see

336.96

so I'm going to load all my parents and

338.4

then all I have to do is load me so I'll

340.5

actually bring this back up

349.5

oh wait nope uh uh nope nope nope nope

356.58

so what I need to do here is for the

358.979

parents I'm going to have to build this

361.139

little complicated thing out that's

363.24

going to get sort of

366.6

interesting

369.479

oh no it's not so let's see so what I'm

372.479

going to do is

377.94

for each of these

380.94

and this is where I get this select star

385.44

and actually I can probably

389.4

now this is going to be interesting

390.539

because if I have multiple parents I may

392.58

have to load for multiple rows

396.479

uh

398.759

which is where did I do that

401.759

I think I had it

404.16

uh

408.24

oh so it was something like this

410.58

oops

414

so I'm going to do is I'm going to get a

416.28

list of IDs

420.06

and what I probably want to do

424.22

uh

425.94

let's see

429.72

and so here's sort of what I want to do

431.46

is I really want to take all of these

433.56

and Union them

437.22

I think what I want here

440.94

and there so what I'm seeing here is

443.28

this is going to be I'm going to build

444.78

these things out into a union because

446.16

what I want to do is say here's all the

448.08

IDS that I need to cover all of my

451.5

parents

453.18

so what I need to do here is I need to

455.58

say let's see

457.919

this is going to go it's going to get a

459.66

little more complicated because

463.8

um yeah because I'm gonna have to build

465.3

this out a little bit so what I need to

466.919

do here is I'm going to do uh smart load

471.66

SQL equals empty

477.479

and then here

479.4

I'm going to say if

482.94

smart load sequel

485.3

equals empty

487.919

then all I have to do is do that

492.72

else

498.18

I'm going to do

501

equals

503.819

whoops

505.74

smart load

509.639

SQL

511.259

Plus

513.899

and here's going to be Union dot value

516.659

or that big query type thing

520.979

and so when I get done

523.86

let's just get to here and see if I can

525.959

create this smart load SQL for anybody

532.38

so let's see what this looks like

534.3

I don't know if this is going to run at

536.88

all so let's find out where we at so if

539.399

we come through here

543.66

there we go so and so I've got something

546.839

here that's not off oh I've got a tuple

548.58

index out of range okay

550.399

uh which I probably didn't get all these

552.54

so I've got constraint stema

556.92

huh

558.6

what is my Tuple

563.519

I wonder if it doesn't like this

567.54

that's probably it this has probably got

569.64

something a little messed up so let's go

573.18

like this

576.06

yep that's it

579

that's going to be the problem

583.38

there we go so now let's try it

588.6

so we're going to come through do a

590.519

little data clean blah blah blah blah

592.26

blah

594.48

and now we're still okay so we still

596.459

have a tuple that's out of range

600.12

and it is on 513.

607.2

let's see did we miss something here

614.339

so the row is zero one two three four

623.58

let's just do print row

626.519

and see what it does for me

632.88

see if I missed something foolishly

635.16

simple in it

637.26

here we go up yep I did not get it so

641.88

Source sequels or SQL

644.82

Source row

646.92

well that's my problem

651.48

um

654.3

that's what I want I want Source row

658.14

and this needs to be

660.959

Source row ah

663.36

I figured it was something stupid like

664.86

that so let's go in here

667.74

and we're going to do this

675.72

these things out

681.18

and now let's take a look at it

687.18

and what we should see is something

689.459

that's I don't know if it's gonna have a

690.66

union or not let's see what our first

691.68

one is that we get uh nothing okay can't

693.959

concatenate

696.24

uh so I've got zero one two three four

698.1

five so I've got that

700.56

uh let's see what is

711.6

oh it's gonna be

717.959

so fetchall but I only need

722.459

zero

725.7

so let's look at it that way

727.86

yeah it's a problem because it was a

729.18

tuple and it's not there we go okay

733.44

so

736.56

my insert is going to look like this

741.42

let's just pick one here

746.339

uh giganator dot member

749.06

[Music]

753.66

hmm

756.36

I wonder if

759.079

that's my issue

766.019

yep oops

770.04

I need to move my quotes

775.56

like that or

778.74

do this

786.12

yep there we go okay so let's fix my

788.16

quotes that's what I figured I have here

792.72

I need to do that for sorcerer zero

796.62

source for zero

799.68

Source row zero

802.86

and Source row zero

807.48

so now

809.519

what I'm going to be doing is I'm going

811.26

to be basically insert into

814.8

the destination

817.86

what I am selecting from the other

821.88

uh and I wanted to do so how did we do

824.76

that before

827.04

is we took it

832.079

and that's missing tables

835.56

whoops smart empty referential load

839.339

I don't need that I need

849.3

uh where's my insert statement

852.66

um

855.72

foreign

869.82

let's do this let's go find it

872.82

okay

874.8

so load table data

881.82

is basically going to be

885.62

[Music]

888.959

oh here we go so for Row in

896.1

and I need uh okay so I need my insert

899.76

base which is here

902.22

so let's do that

904.079

steal that again

907.68

uh referential load okay so here

911.399

what I need to do

913.68

is now I'm going to go

916.38

um

918.26

cursor source

921.839

execute

924.3

that and then I'm going to be

926.839

destination rows equals

932.1

and let me do a quit here just because

938.1

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

941.279

don't need that

947.22

can I call it Source rows I may be able

949.56

to call it Source rows

951.42

um

952.199

nope okay so for for

958.079

new row in

962.579

destination rows

965.82

there's my insert statement uh

970.92

I need

976.62

to do this

984.8

[Music]

986.399

where did I use row I didn't even use it

992.459

oh for value and there we go I'm sorry

994.38

for Value in new row

997.74

current value equals that uh

1003.139

and so instead of executing in the

1006.5

destination

1009.32

I'm going to do a print

1015.92

insert statement cool okay so now let's

1019.279

go look at it and let's see what it's

1020.42

going to build

1024.799

and we should see a series of inserts

1028.04

uh we did not

1031.04

but

1032.54

I think we'll end here and we're going

1034.22

to debug this but we're getting pretty

1035.48

close next time around we will come in

1037.4

and hopefully we'll have this thing

1039.74

knocked out and be able to actually give

1042.02

it a shot

1043.16

that being said go out there and have

1045.02

yourself a great day a great week and we

1047.78

will talk to you next time

1063.98

foreign