📺 Develpreneur YouTube Episode

Video + transcript

SQL Sync Part 29

2022-11-22 •Youtube

Detailed Notes

Focus for this episode: This episode continues the exploration of pulling only needed and related data into the target tables. It is tedious, but a step towards the final product.

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 tutorial SQL python sort
of combo thing and last time around we
were getting into some of the uh the
complicated Parts basically of the data
and this episode we're going to continue
through that we were we're basically
getting into our
uh let me see where I'm at here
uh we're getting into the point
we're actually walking through and
copying in uh some of our data moving it
based on relational issues and we hit a
bug and so that was where we were last
at so if we look at uh
DB sync 518 so in here what's coming
what it's doing is it's coming through
and saying that hey we've got something
that's not quite right
and how I did this
and let's see so what are we gonna do so
we have this local variable insert base
before silent so we are referring to it
and the base is
uh let's see
I don't think we need
values
oh this is where okay so what we're
going to do is we're going to come in
we're going to load the data for the
table
but then what we're going to need to do
is going to have to actually execute
that which we get
so that's what we got is we got our
destination rows and let's uh let's do
this real quick we're going to do a quit
and we're just going to do a print
whoop
destination rows
whoop that's not what I want I want to
save it I'm going to run it
and then it's going to come through oh
so there aren't any
so let's look at
it's equal and see if we built that
right and if I type that right
so if we look at our SQL
so let's see what did we break here
select star from giganator.member where
member ID and select distinct member
from giganator.performer
interesting
so here
oh
let's do that and
where was that from that was from the
source
so let's go somewhere down here
we can execute there we go so he works
fine
but for here
he works fine too so why
let's see
he's gonna from the source which is
right he's gonna execute
given me a
no oh I know why
um
let's see I want to do a fetch haul so
it's actually
this way whoops uh let's do this little
copy here sorry
so I come in here
that's going to be destination rows
and I think I can do it like that
so now if we print it
there we go so here's all our rows
and what we're going to need to do
is we're going to go through we're going
to create that base
which we didn't before so let's go see
if we can steal that code
and that's here
so we can do that first
uh
uh that's not the one I just did
jump down to here we go so here's one I
just did
so I'm going to create that but here
okay so here I'm gonna do first row
first right because blah blah blah
foreign
so what I really want to do is for item
in
uh so I need to figure out
the results
which is going to be interesting because
I gotta go figure that out first
um so wait let's go back to here we're
going to have to do this a little
differently because what we did
uh here we go let's describe
and if we do all of that
it's going to look something like
that probably so
um
actually let's see
and then we're going to do that whole
thing well let's see uh it's here
and
here's our insert base
here
I think is that
let's see if that works
let's see what do we got here
okay A little table for the here we go
so
destination rows which we're not using
right now we did our description we get
our description results we create out
our base
we don't need this so that's our data
query
and
insert into that
why did we do that before that's an
interesting one I don't think we need
that because now we're going to go
through destination rows gives us all of
our rows
and then we're going to go through for
each of those that's in the description
results
uh that's that now we do our values okay
which is going to be up here
I could actually walk through all of
this but this seems a little
here we go
so he does that data query select blank
from blank description limit blah blah
blah blah that's the data query so he
does sorceress okay
where it's actually going to be
destination rows this time
um
uh this one
so for each one of these so we're going
to come through we're going to do select
blah blah blah
I hate it a little differently last time
so let's see see which one we're going
to do that yeah and then we're going to
do our values and then this is going to
be in destination rows
and then I'm going to come through I
don't need Source rows anymore because I
use that destination rows I guess I
don't need first row
[Music]
um
why do I use first row
good question
surveys
if first
don't think I need that
so I probably don't even need first row
and that's just to do the comma okay
and then
I'm going to come through
and then I'm gonna do an execute so
instead
I want to
do that I'm going to print my insert
statement
and let's see what this looks like
and let's see I should have I think
about what 10 different little insert
statements so
I'm going to so let's look at these and
let's take a couple of these and just
sort of see if those look right
because if we come in here
so it's going to say insert into
performer which is okay
connected.performer 4 ID blah blah blah
blah blah which is
uh each of these values
oh it's taking
oh this is a different
so maybe I'm doing the wrong thing uh
insert into
table
so this is going to be select referenced
so my table name is here
so that's where I found each of those
so that's the table but I actually need
to and so okay
so contract has certain ones
and then for example
he has some performer stuff which is
let's go ahead and print that out
just because
just to make sure we got the right one
because I think I'm looking at two
different records
uh see here we go
so here we go Okay so
uh the reference table is member and
it's referenced by performer
and so oh okay so I'm grabbing the wrong
I'm describing from the wrong table so
what I need to do here
when I do the describe where did I do
that describe describe is not table now
it's actually
um
I don't know why it's popping that up
it's actually going to be Source row
uh this needs to be the reference table
name so it'd probably be one
see what that looks like
and let's see if this looks a little
different
so if I take those two
so that's the one okay so if I take that
that's my I should be dealing with a
member
is in for is inserting performers which
should be
that performer
which uh oh because it's not getting the
uh which I probably need that so I
probably need
uh this uh I need to do it like this
there we go
so now if I do it's gonna look a little
different
and well it's the same description but
now I need to go to
uh insert into
member uh see yeah I use the table name
wrong again
so now
yeah remember remember ID okay so now if
I try that
see so I want to do that locally
just because if I go to my local
database
so there we go so if I go to local
uh where did I just put that uh that's
performer that's not what I want
remember here we go
so if I do that
so it's going to give me a duplicate
entry because I always have that record
in there so
I am now able to go through and actually
create these records now the problem
I've got is that I have not deleted all
of the records at this point
in the uh the destination so what I'm
going to need to do
which becomes its own little challenge
because what we can do is we can say
let's only add records
so what we could do is we could just
basically see hey if it doesn't exist
then we'll just skip it
I think we can do like an insert ignore
I think it's that simple
yep
and so it's going to tell me it's
success even though it's going to give
me that little warning so I could do
that I could do it that simply we're
here I could just say okay if it already
exists cool we're going to add it in in
which case
uh let's see let's do this let's do our
execute and let's see what this does
and oh so it is giving me that
um
so let's see am I in the right place
so wait let's do that
so maybe it doesn't like that
it may not do it in this uh whoops
insert statement it may be this driver
is not going to give us out
let's see
I bet that's it so if I take this
and run it yeah so here but now it's
going to give me that it gives me an
error here so I can't just get away from
can't do it as simple as insert ignore
but I really wanted to do anyways is get
rid of that data
beforehand
so we can either go back to our
truncation of tables which I think I
just dropped that out of what it was
doing
if I go to main
quit smart empty sink
compare yeah so one of the things I'm
not doing is I'm not actually going in I
think I already dropped that out of here
so I can go in and I can actually clean
up the data delete it and then reinsert
it
or we can look at ways to add it and so
I think what we're going to do first is
we're going to go back to our removing
of data
which will be if I go to smart empty
sink
I think
it's as simple as
uh let's see select reference table blah
blah blah oh I have somewhere here
I want to do not referential load I want
to do nope
load table data I think calls
uh
somewhere
is it sync ropes
well
drop data there we go
so drop data
I don't really need that guy
which means I don't need oh
did I not clean that up I didn't clean
that up
so I need to take a cursor destination
Clues
so and so drop data here is going to
just like crank through this stuff
uh except for it probably needs
oh I think because I set that up front
uh yeah I've got my databases set up so
I should be able to yeah
so see I have this D in feature flag so
if I don't have it then I'm not going to
drop it
and
so I need to actually do do I sync rows
somewhere yeah here we go
so now I'm going to go back and actually
fix a few things
so I start with the missing tables I
deal with the feature Flags oh I'm sorry
with the missing columns
so what I can do is then come into here
uh rows
and that's we're going to start next
time we're going to run this sucker
through or see how it looks and we will
uh carry on actually the next thing
we'll probably look at is the ability to
only bring in additional records so
we're going to have to do a little bit
of extra check to take a little longer
but we're going to basically do the same
insert except for now we're going to
check beforehand we're gonna find a way
to be able to avoid that that error and
just say hey if it's not there then we
don't have to worry about it which is
going to allow us at some point to do
some things like
uh potentially working off time stamps
and some things like that so still got
some other ways you want to play around
with data but I think that'll wrap it up
for this one we're getting there we're
starting to see that we are building
this stuff out we can execute it we can
cleanly add some rows in and we'll start
playing around with that that being said
go out there and have yourself a great
day a great week and we will talk to you
next time
thank you
foreign
Transcript Segments
10.7

thank you

18.89

[Music]

27.119

well hello and welcome back we're

28.859

continuing our tutorial SQL python sort

31.74

of combo thing and last time around we

34.68

were getting into some of the uh the

38.399

complicated Parts basically of the data

41.28

and this episode we're going to continue

43.44

through that we were we're basically

46.5

getting into our

48.66

uh let me see where I'm at here

51.719

uh we're getting into the point

54.12

we're actually walking through and

55.92

copying in uh some of our data moving it

59.52

based on relational issues and we hit a

62.399

bug and so that was where we were last

64.5

at so if we look at uh

68.159

DB sync 518 so in here what's coming

72.18

what it's doing is it's coming through

73.979

and saying that hey we've got something

75.6

that's not quite right

77.7

and how I did this

79.82

and let's see so what are we gonna do so

82.979

we have this local variable insert base

84.72

before silent so we are referring to it

89.34

and the base is

96.14

uh let's see

100.02

I don't think we need

103.2

values

109.86

oh this is where okay so what we're

111.899

going to do is we're going to come in

112.68

we're going to load the data for the

113.759

table

115.02

but then what we're going to need to do

116.82

is going to have to actually execute

119.64

that which we get

121.439

so that's what we got is we got our

123

destination rows and let's uh let's do

125.939

this real quick we're going to do a quit

127.68

and we're just going to do a print

130.739

whoop

134.16

destination rows

137.76

whoop that's not what I want I want to

140.52

save it I'm going to run it

142.8

and then it's going to come through oh

144.48

so there aren't any

146.099

so let's look at

152.7

it's equal and see if we built that

154.5

right and if I type that right

161.04

so if we look at our SQL

167.879

so let's see what did we break here

176.34

select star from giganator.member where

179.459

member ID and select distinct member

181.8

from giganator.performer

184.14

interesting

185.58

so here

187.44

oh

188.64

let's do that and

191.22

where was that from that was from the

193.62

source

196.86

so let's go somewhere down here

201.78

we can execute there we go so he works

204.54

fine

206.819

but for here

210.84

he works fine too so why

215.159

let's see

218.519

he's gonna from the source which is

220.739

right he's gonna execute

227.7

given me a

229.86

no oh I know why

235.379

um

239.4

let's see I want to do a fetch haul so

242.28

it's actually

248.459

this way whoops uh let's do this little

250.86

copy here sorry

253.08

so I come in here

256.739

that's going to be destination rows

267.9

and I think I can do it like that

271.62

so now if we print it

273.24

there we go so here's all our rows

277.08

and what we're going to need to do

279.24

is we're going to go through we're going

280.74

to create that base

282.66

which we didn't before so let's go see

285.78

if we can steal that code

291.24

and that's here

294.54

so we can do that first

298.04

uh

300

uh that's not the one I just did

304.8

jump down to here we go so here's one I

307.32

just did

310.919

so I'm going to create that but here

317.52

okay so here I'm gonna do first row

319.56

first right because blah blah blah

322.68

foreign

329.82

so what I really want to do is for item

332.46

in

338.3

uh so I need to figure out

342.72

the results

348.24

which is going to be interesting because

350.34

I gotta go figure that out first

354.12

um so wait let's go back to here we're

355.74

going to have to do this a little

357.6

differently because what we did

359.88

uh here we go let's describe

364.139

and if we do all of that

368.52

it's going to look something like

372.18

that probably so

377.039

um

379.08

actually let's see

381.539

and then we're going to do that whole

383.819

thing well let's see uh it's here

391.28

and

392.94

here's our insert base

399.96

here

403.02

I think is that

413.28

let's see if that works

417.12

let's see what do we got here

423.12

okay A little table for the here we go

425.22

so

427.5

destination rows which we're not using

429.3

right now we did our description we get

432.06

our description results we create out

433.919

our base

441.3

we don't need this so that's our data

443.52

query

451.74

and

455.72

insert into that

463.38

why did we do that before that's an

465.12

interesting one I don't think we need

466.259

that because now we're going to go

467.639

through destination rows gives us all of

469.5

our rows

471.419

and then we're going to go through for

472.8

each of those that's in the description

474.599

results

478.8

uh that's that now we do our values okay

483.66

which is going to be up here

489.539

I could actually walk through all of

491.099

this but this seems a little

494.22

here we go

496.139

so he does that data query select blank

498.419

from blank description limit blah blah

500.099

blah blah that's the data query so he

502.379

does sorceress okay

504.06

where it's actually going to be

505.56

destination rows this time

511.74

um

514.5

uh this one

522.779

so for each one of these so we're going

525.48

to come through we're going to do select

527.6

blah blah blah

534.899

I hate it a little differently last time

537.06

so let's see see which one we're going

538.26

to do that yeah and then we're going to

539.339

do our values and then this is going to

541.32

be in destination rows

545.7

and then I'm going to come through I

547.74

don't need Source rows anymore because I

549.54

use that destination rows I guess I

551.16

don't need first row

555.29

[Music]

555.899

um

559.339

why do I use first row

564.98

good question

573.959

surveys

585.18

if first

595.74

don't think I need that

597.779

so I probably don't even need first row

604.2

and that's just to do the comma okay

612.54

and then

614.64

I'm going to come through

616.92

and then I'm gonna do an execute so

618.899

instead

620.36

I want to

622.56

do that I'm going to print my insert

624.36

statement

628.92

and let's see what this looks like

632.7

and let's see I should have I think

634.56

about what 10 different little insert

636.36

statements so

639.42

I'm going to so let's look at these and

641.76

let's take a couple of these and just

643.019

sort of see if those look right

645.779

because if we come in here

649.14

so it's going to say insert into

651.66

performer which is okay

654.019

connected.performer 4 ID blah blah blah

656.7

blah blah which is

658.26

uh each of these values

661.14

oh it's taking

670.5

oh this is a different

682.2

so maybe I'm doing the wrong thing uh

684.42

insert into

690.18

table

697.5

so this is going to be select referenced

703.2

so my table name is here

708.06

so that's where I found each of those

711.839

so that's the table but I actually need

713.76

to and so okay

715.92

so contract has certain ones

718.079

and then for example

722.339

he has some performer stuff which is

728.459

let's go ahead and print that out

730.74

just because

734.94

just to make sure we got the right one

737.76

because I think I'm looking at two

739.2

different records

742.86

uh see here we go

751.2

so here we go Okay so

755.339

uh the reference table is member and

757.62

it's referenced by performer

764.459

and so oh okay so I'm grabbing the wrong

766.86

I'm describing from the wrong table so

768.54

what I need to do here

770.579

when I do the describe where did I do

773.16

that describe describe is not table now

776.399

it's actually

781.079

um

783

I don't know why it's popping that up

784.98

it's actually going to be Source row

790.32

uh this needs to be the reference table

792.899

name so it'd probably be one

795.6

see what that looks like

801.54

and let's see if this looks a little

803.519

different

806.88

so if I take those two

809.339

so that's the one okay so if I take that

811.62

that's my I should be dealing with a

815.16

member

818.279

is in for is inserting performers which

821.16

should be

823.62

that performer

825.48

which uh oh because it's not getting the

831.06

uh which I probably need that so I

833.04

probably need

835.519

uh this uh I need to do it like this

843.839

there we go

848.639

so now if I do it's gonna look a little

850.68

different

855.74

and well it's the same description but

858.48

now I need to go to

860.3

uh insert into

867

member uh see yeah I use the table name

870.66

wrong again

874.019

so now

877.8

yeah remember remember ID okay so now if

880.86

I try that

887.279

see so I want to do that locally

890.82

just because if I go to my local

893.339

database

896.1

so there we go so if I go to local

900.019

uh where did I just put that uh that's

902.94

performer that's not what I want

904.86

remember here we go

907.56

so if I do that

911.399

so it's going to give me a duplicate

912.72

entry because I always have that record

913.74

in there so

915.839

I am now able to go through and actually

919.98

create these records now the problem

923.1

I've got is that I have not deleted all

926.82

of the records at this point

929.16

in the uh the destination so what I'm

932.339

going to need to do

936.839

which becomes its own little challenge

938.339

because what we can do is we can say

941.42

let's only add records

946.079

so what we could do is we could just

947.22

basically see hey if it doesn't exist

948.72

then we'll just skip it

950.399

I think we can do like an insert ignore

954.12

I think it's that simple

958.8

yep

959.88

and so it's going to tell me it's

961.199

success even though it's going to give

963.18

me that little warning so I could do

965.16

that I could do it that simply we're

968.1

here I could just say okay if it already

970.62

exists cool we're going to add it in in

973.5

which case

975.3

uh let's see let's do this let's do our

977.82

execute and let's see what this does

982.38

and oh so it is giving me that

986.04

um

991.68

so let's see am I in the right place

1001.48

so wait let's do that

1004.579

so maybe it doesn't like that

1008.18

it may not do it in this uh whoops

1010.579

insert statement it may be this driver

1014.3

is not going to give us out

1020

let's see

1022.1

I bet that's it so if I take this

1027.319

and run it yeah so here but now it's

1029.36

going to give me that it gives me an

1031.04

error here so I can't just get away from

1033.98

can't do it as simple as insert ignore

1036.02

but I really wanted to do anyways is get

1039.38

rid of that data

1041.179

beforehand

1042.919

so we can either go back to our

1046.64

truncation of tables which I think I

1048.559

just dropped that out of what it was

1050.6

doing

1052.1

if I go to main

1055.88

quit smart empty sink

1059.44

compare yeah so one of the things I'm

1061.76

not doing is I'm not actually going in I

1063.559

think I already dropped that out of here

1064.7

so I can go in and I can actually clean

1066.559

up the data delete it and then reinsert

1068.66

it

1069.74

or we can look at ways to add it and so

1074.179

I think what we're going to do first is

1075.559

we're going to go back to our removing

1078.32

of data

1079.82

which will be if I go to smart empty

1082.58

sink

1084.02

I think

1086.539

it's as simple as

1089.72

uh let's see select reference table blah

1092.96

blah blah oh I have somewhere here

1097.52

I want to do not referential load I want

1101.299

to do nope

1103.1

load table data I think calls

1110

uh

1113.48

somewhere

1116.24

is it sync ropes

1118.94

well

1119.96

drop data there we go

1121.7

so drop data

1125.12

I don't really need that guy

1127.28

which means I don't need oh

1130.88

did I not clean that up I didn't clean

1132.38

that up

1134

so I need to take a cursor destination

1138.14

Clues

1146.919

so and so drop data here is going to

1150.26

just like crank through this stuff

1153.02

uh except for it probably needs

1156.44

oh I think because I set that up front

1160

uh yeah I've got my databases set up so

1162.799

I should be able to yeah

1165.14

so see I have this D in feature flag so

1167.6

if I don't have it then I'm not going to

1168.919

drop it

1172.16

and

1175.7

so I need to actually do do I sync rows

1178.46

somewhere yeah here we go

1181.94

so now I'm going to go back and actually

1183.5

fix a few things

1185.24

so I start with the missing tables I

1187.34

deal with the feature Flags oh I'm sorry

1190.34

with the missing columns

1192.86

so what I can do is then come into here

1195.74

uh rows

1201.74

and that's we're going to start next

1203.059

time we're going to run this sucker

1204.14

through or see how it looks and we will

1207.14

uh carry on actually the next thing

1208.64

we'll probably look at is the ability to

1211.1

only bring in additional records so

1213.919

we're going to have to do a little bit

1214.94

of extra check to take a little longer

1216.26

but we're going to basically do the same

1218.96

insert except for now we're going to

1221.24

check beforehand we're gonna find a way

1222.86

to be able to avoid that that error and

1226.76

just say hey if it's not there then we

1228.5

don't have to worry about it which is

1230

going to allow us at some point to do

1231.559

some things like

1233.419

uh potentially working off time stamps

1235.46

and some things like that so still got

1237.08

some other ways you want to play around

1238.1

with data but I think that'll wrap it up

1240.02

for this one we're getting there we're

1241.94

starting to see that we are building

1243.5

this stuff out we can execute it we can

1245.539

cleanly add some rows in and we'll start

1248.179

playing around with that that being said

1249.919

go out there and have yourself a great

1251.24

day a great week and we will talk to you

1254.9

next time

1265.96

thank you

1269.6

foreign