Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to SQLAlchemy and Alembic

Introduction to SQLAlchemy and Alembic

In this talk, we'll examine how to use SQLAlchemy ORM and Core in both simple queries and query builder type applications. Next, we'll explore Alembic database migrations and how we can use them to handle database changes.

Jason Myers

July 27, 2013
Tweet

More Decks by Jason Myers

Other Decks in Technology

Transcript

  1. f r o m s q l a l c

    h e m y i m p o r t c r e a t e _ e n g i n e e n g i n e = c r e a t e _ e n g i n e ( ‘ d i a l e c t + d r i v e r : / / U S E R : P A S S @ H O S T : P O R T / D B ’ )
  2. f r o m s q l a l c

    h e m y . e x t . d e c l a r a t i v e i m p o r t ( d e c l a r a t i v e _ b a s e ) B a s e = d e c l a r a t i v e _ b a s e ( )
  3. f r o m s q l a l c

    h e m y i m p o r t ( C o l u m n , 
 I n t e g e r , S t r i n g , F l o a t ) f r o m s q l a l c h e m y i m p o r t F o r e i g n K e y f r o m s q l a l c h e m y . o r m i m p o r t ( r e l a t i o n s h i p , b a c k r e f )
  4. c l a s s U s e r (

    B a s e ) : _ _ t a b l e n a m e _ _ = ' u s e r s ' i d = C o l u m n ( I n t e g e r , p r i m a r y _ k e y = T r u e ) n a m e = C o l u m n ( S t r i n g ) f u l l n a m e = C o l u m n ( S t r i n g ) b a l a n c e = C o l u m n ( F l o a t ) g r o u p = C o l u m n ( S t r i n g ) a d d r e s s e s = r e l a t i o n s h i p ( " A d d r e s s " , o r d e r _ b y = " A d d r e s s . i d " , b a c k r e f = " u s e r " )
  5. d e f _ _ i n i t _

    _ ( s e l f , n a m e , f u l l n a m e , b a l a n c e , g r o u p ) : s e l f . n a m e = n a m e s e l f . f u l l n a m e = f u l l n a m e s e l f . b a l a n c e = b a l a n c e s e l f . g r o u p = g r o u p
  6. B a s e . m e t a d

    a t a . c r e a t e _ a l l ( e n g i n e )
  7. f r o m s q l a l c

    h e m y . o r m i m p o r t s e s s i o n m a k e r S e s s i o n = s e s s i o n m a k e r ( b i n d = e n g i n e ) d b = S e s s i o n ( )
  8. u s e r 1 = U s e r

    ( ' B o b ' , ' B i g B o b ' , 1 0 0 0 0 0 0 . 0 0 , ' M o b ' ) u s e r 2 = U s e r ( ' L i n d a ' , ' L i n d a L u ' , 1 0 0 . 5 0 , ' D i n e r ' ) u s e r 3 = U s e r ( ' L i l B o b ' , ' B o b b y J r ' , 1 0 0 5 0 0 . 0 0 , ' M o b ' ) u s e r 4 = U s e r ( ' R a c h a e l ' , ' R a c h a e l R a c h ' , 1 2 5 . 5 0 , ' P e r s o n a l ' )
  9. d b . a d d ( u s e

    r 1 ) d b . c o m m i t ( ) d b . d e l e t e ( u s e r 1 )
  10. d b . e x p u n g e

    ( u s e r 1 ) d b . r e f r e s h ( u s e r 1 ) d b . e x p i r e ( u s e r 1 ) d b . r o l l b a c k ( )
  11. f o r u s e r i n d

    b . q u e r y ( U s e r ) . a l l ( ) : p r i n t u s e r . n a m e , u s e r . b a l a n c e O u t [ 1 ] : B o b 1 0 0 0 0 0 0 . 0
  12. e n t r i e s = d b

    . s e s s i o n . q u e r y ( E n t r i e s ) . f i l t e r _ b y ( u s e r _ i d = u s e r . i d ) \ . f i l t e r ( E n t r i e s . e n t r y _ t i m e > ( d a t e t i m e . d a t e t i m e . u t c n o w ( ) - d a t e t i m e . t i m e d e l t a ( 3 0 ) ) ) . o r d e r _ b y ( ' I D D E S C ' ) . a l l ( )
  13. S E L E C T e n t r

    i e s . i d A S e n t r i e s _ i d , e n t r i e s . u s e r _ i d A S e n t r i e s _ u s e r _ i d , e n t r i e s . p h o n e A S e n t r i e s _ p h o n e , e n t r i e s . m e a s u r e m e n t A S e n t r i e s _ m e a s u r e m e n t , e n t r i e s . i n s u l i n A S e n t r i e s _ i n s u l i n , e n t r i e s . i n s u l i n _ t y p e A S e n t r i e s _ i n s u l i n _ t y p e , e n t r i e s . c a r b s A S e n t r i e s _ c a r b s , e n t r i e s . t a g A S e n t r i e s _ t a g , e n t r i e s . t h r e e _ s i x t y _ i d A S e n t r i e s _ t h r e e _ s i x t y _ i d , e n t r i e s . e n t r y _ t i m e A S e n t r i e s _ e n t r y _ t i m e , e n t r i e s . c r e a t e d _ t i m e A S e n t r i e s _ c r e a t e d _ t i m e F R O M e n t r i e s W H E R E e n t r i e s . u s e r _ i d = : u s e r _ i d _ 1 A N D e n t r i e s . e n t r y _ t i m e > : e n t r y _ t i m e _ 1 O R D E R B Y I D D E S C
  14. f r o m s q l a l c

    h e m y i m p o r t f u n c f r o m s q l a l c h e m y . s q l i m p o r t l a b e l r e s u l t s = d b . q u e r y ( U s e r . g r o u p , l a b e l ( ' m e m b e r s ' , f u n c . c o u n t ( U s e r . i d ) ) , l a b e l ( ' t o t a l _ b a l a n c e ' , f u n c . s u m ( U s e r . b a l a n c e ) ) ) . g r o u p _ b y ( U s e r . g r o u p ) . a l l ( ) f o r r e s u l t i n r e s u l t s : p r i n t r e s u l t . g r o u p , r e s u l t . m e m b e r s , r e s u l t . t o t a l _ b a l a n c e
  15. b o b . a d d r e s

    s e s . a p p e n d ( h o m e _ a d d r e s s ) b o b . a d d r e s s e s b o b . a d d r e s s e s . f i l t e r ( A d d r e s s . t y p e = ' H ' ) . o n e ( )
  16. q u e r y = d b . q

    u e r y ( U s e r , A d d r e s s ) . f i l t e r ( U s e r . i d = = A d d r e s s . u s e r _ i d ) q u e r y = q u e r y . f i l t e r ( A d d r e s s . e m a i l _ a d d r e s s = = ' j a c k @ g o o g . c o m ' ) . a l l ( )
  17. @ h y b r i d _ p r

    o p e r t y d e f g r a n d _ t o t a l ( s e l f ) : r o l l u p _ f i e l d s = [ ' m e r c h a n d i s e _ c o s t ' , ' t a x ' , ' s h i p p i n g ' , ] t o t a l = s u m ( [ s e l f . _ _ g e t a t t r i b u t e _ _ ( x ) f o r x i n r o l l u p _ f i e l d s ] ) r e t u r n r o u n d ( t o t a l , 2 )
  18. f r o m s q l a l c

    h e m y i m p o r t c r e a t e _ e n g i n e e n g i n e = c r e a t e _ e n g i n e ( ‘ d i a l e c t + d r i v e r : / / U S E R : P A S S @ H O S T : P O R T / D B ’ )
  19. f r o m s q l a l c

    h e m y i m p o r t ( T a b l e , C o l u m n , I n t e g e r , S t r i n g , M e t a D a t a , F o r e i g n K e y ) m e t a d a t a = M e t a D a t a ( ) u s e r s = T a b l e ( ' u s e r s ' , m e t a d a t a , C o l u m n ( ' i d ' , I n t e g e r , p r i m a r y _ k e y = T r u e ) , C o l u m n ( ' n a m e ' , S t r i n g ) , C o l u m n ( ' f u l l n a m e ' , S t r i n g ) , )
  20. B a s e . m e t a d

    a t a . c r e a t e _ a l l ( e n g i n e ) c o n n = e n g i n e . c o n n e c t ( )
  21. i n s = u s e r s .

    i n s e r t ( ) . v a l u e s ( n a m e = ' j a c k ' , f u l l n a m e = ' J a c k B e l l ' ) r e s u l t = c o n n . e x e c u t e ( i n s ) i n s = u s e r s . i n s e r t ( ) c o n n . e x e c u t e ( i n s , i d = 2 , n a m e = ' w e n d y ' , f u l l n a m e = ' W e n d y M c D o n a l d s ' )
  22. c o n n . e x e c u

    t e ( a d d r e s s e s . i n s e r t ( ) , [ { ' u s e r _ i d ' : 1 , ' e m a i l _ a d d r e s s ' : ' j @ y . c o m ' } , { ' u s e r _ i d ' : 1 , ' e m a i l _ a d d r e s s ' : ' j @ m . c o m ' } , ] )
  23. d e f b u i l d _ t

    a b l e ( t a b l e _ n a m e ) : r e t u r n T a b l e ( t a b l e _ n a m e , m e t a d a t a , a u t o l o a d = T r u e , a u t o l o a d _ w i t h = e n g i n e )
  24. b u i l d _ t a b l

    e ( ' c e n s u s ' ) u n a v a i l a b l e _ f i e l d s = [ c . n a m e f o r c i n t . c i f i s i n s t a n c e ( c . t y p e , N u l l T y p e ) ]
  25. c l a s s U n l o a

    d F r o m S e l e c t ( E x e c u t a b l e , C l a u s e E l e m e n t ) : d e f _ _ i n i t _ _ ( s e l f , s e l e c t , b u c k e t , a c c e s s _ k e y , s e c r e t _ k e y ) : s e l f . s e l e c t = s e l e c t s e l f . b u c k e t = b u c k e t s e l f . a c c e s s _ k e y = a c c e s s _ k e y s e l f . s e c r e t _ k e y = s e c r e t _ k e y @ c o m p i l e s ( U n l o a d F r o m S e l e c t ) d e f v i s i t _ u n l o a d _ f r o m _ s e l e c t ( e l e m e n t , c o m p i l e r , * * k w ) : r e t u r n " u n l o a d ( ' % ( q u e r y ) s ' ) t o ' % ( b u c k e t ) s ' c r e d e n t i a l s ' a w s _ a c c e s s _ k e y _ i d = % ( a c c e s s _ k e y ) s ; a w s _ s e c r e t _ a c c e s s _ k e y = % ( s e c r e t _ k e y ) s ' d e l i m i t e r ' , ' a d d q u o t e s a l l o w o v e r w r i t e " % { ' q u e r y ' : c o m p i l e r . p r o c e s s ( e l e m e n t . s e l e c t , u n l o a d _ s e l e c t = T r u e , l i t e r a l _ b i n d s = T r u e ) , ' b u c k e t ' : e l e m e n t . b u c k e t , ' a c c e s s _ k e y ' : e l e m e n t . a c c e s s _ k e y , ' s e c r e t _ k e y ' : e l e m e n t . s e c r e t _ k e y , }
  26. u n l o a d = U n l

    o a d F r o m S e l e c t ( s e l e c t ( [ f i e l d s ] ) , ' / ' . j o i n ( [ ' s 3 : / ' , B U C K E T , f i l e n a m e ] ) , A C C E S S _ K E Y , S E C R E T _ K E Y )
  27. u n l o a d ( ' s e

    l e c t * f r o m v e n u e w h e r e v e n u e i d i n ( s e l e c t v e n u e i d f r o m v e n u e o r d e r b y v e n u e i d d e s c l i m i t 1 0 ) ' ) t o ' s 3 : / / m y b u c k e t / v e n u e _ p i p e _ ' c r e d e n t i a l s ' a w s _ a c c e s s _ k e y _ i d = A C C E S S _ K E Y ; a w s _ s e c r e t _ a c c e s s _ k e y = S E C R E T _ K E Y ' ;
  28. s = s e l e c t ( [

    t . c . r a c e , t . c . f a c t o r , f u n c . s u m ( g . t . c . v a l u e ) . l a b e l ( ' s u m m e d ' ) ] , t . c . r a c e > 0 ) . w h e r e ( a n d _ ( t . c . t y p e = = ' P O V E R T Y ' , t . c . v a l u e ! = 0 ) ) . g r o u p _ b y ( t . c . r a c e , t . c . f a c t o r ) . o r d e r _ b y ( t . c . r a c e , t . c . f a c t o r )
  29. s = s e l e c t ( [

    t a b l e . c . d i s c h a r g e _ y e a r , f u n c . c o u n t ( 1 ) . l a b e l ( ' p a t i e n t _ d i s c h a r g e s ' ) , t a b l e . c . z i p _ c o d e , ] , t a b l e . c . d i s c h a r g e _ y e a r . i n _ ( y e a r s ) ) . g r o u p _ b y ( t a b l e . c . d i s c h a r g e _ y e a r ) s = s . w h e r e ( t a b l e . c . h o s p i t a l _ n a m e = = p r o v i d e r ) i f ' t o t a l _ c h a r g e s ' n o t i n u n a v a i l a b l e _ f i e l d s : s = s . c o l u m n ( f u n c . s u m ( t a b l e . c . t o t a l _ c h a r g e s ) . l a b e l ( ' p a t i e n t _ c h a r g e s ' ) ) s = s . g r o u p _ b y ( t a b l e . c . z i p _ c o d e ) s = s . o r d e r _ b y ( ' d i s c h a r g e s D E S C ' ) c a s e s = c o n n . e x e c u t e ( s ) . f e t c h a l l ( )
  30. # A g e n e r i c ,

    s i n g l e d a t a b a s e c o n f i g u r a t i o n . [ a l e m b i c ] # p a t h t o m i g r a t i o n s c r i p t s s c r i p t _ l o c a t i o n = a l e m b i c # t e m p l a t e u s e d t o g e n e r a t e m i g r a t i o n f i l e s # f i l e _ t e m p l a t e = % % ( r e v ) s _ % % ( s l u g ) s # s e t t o ' t r u e ' t o r u n t h e e n v i r o n m e n t d u r i n g # t h e ' r e v i s i o n ' c o m m a n d , r e g a r d l e s s o f a u t o g e n e r a t e # r e v i s i o n _ e n v i r o n m e n t = f a l s e s q l a l c h e m y . u r l = d r i v e r : / / u s e r : p a s s @ l o c a l h o s t / d b n a m e
  31. f r o m g l u i m p

    o r t d b t a r g e t _ m e t a d a t a = d b . m e t a d a t a d e f r u n _ m i g r a t i o n s _ o n l i n e ( ) : a l e m b i c _ c o n f i g = c o n f i g . g e t _ s e c t i o n ( c o n f i g . c o n f i g _ i n i _ s e c t i o n ) f r o m c o n f i g i m p o r t S Q L A L C H E M Y _ D A T A B A S E _ U R I a l e m b i c _ c o n f i g [ ' s q l a l c h e m y . u r l ' ] = S Q L A L C H E M Y _ D A T A B A S E _ U R I e n g i n e = e n g i n e _ f r o m _ c o n f i g ( a l e m b i c _ c o n f i g , p r e f i x = ' s q l a l c h e m y . ' , p o o l c l a s s = p o o l . N u l l P o o l )
  32. d e f u p g r a d e

    ( ) : o p . c r e a t e _ t a b l e ( ' u s e r s _ t o _ u s e r s ' , s a . C o l u m n ( ' p a t i e n t _ u s e r _ i d ' , s a . I n t e g e r ( ) , n u l l a b l e = F a l s e ) , s a . C o l u m n ( ' p r o v i d e r _ u s e r _ i d ' , s a . I n t e g e r ( ) , n u l l a b l e = F a l s e ) , s a . F o r e i g n K e y C o n s t r a i n t ( [ ' p a t i e n t _ u s e r _ i d ' ] , [ ' u s e r s . i d ' ] , ) , s a . F o r e i g n K e y C o n s t r a i n t ( [ ' p r o v i d e r _ u s e r _ i d ' ] , [ ' u s e r s . i d ' ] , ) , s a . P r i m a r y K e y C o n s t r a i n t ( ' p a t i e n t _ u s e r _ i d ' , ' p r o v i d e r _ u s e r _ i d ' ) ) o p . a l t e r _ c o l u m n ( u ' r e m i n d e r s ' , u ' u s e r _ c r e a t e d ' , n u l l a b l e = T r u e )
  33. d e f d o w n g r a

    d e ( ) : o p . a l t e r _ c o l u m n ( u ' r e m i n d e r s ' , u ' u s e r _ c r e a t e d ' , e x i s t i n g _ t y p e = m y s q l . T I N Y I N T ( d i s p l a y _ w i d t h = 1 ) , n u l l a b l e = F a l s e ) o p . d r o p _ t a b l e ( ' u s e r s _ t o _ u s e r s ' )
  34. alembic current alembic upgrade +2 alembic downgrade -1 alembic upgrade

    ae1 alembic upgrade 1 --sql > file.sql alembic history
  35. 2 8 0 6 7 6 1 d f 1

    3 9 - > 1 e 9 8 3 1 c 8 f a 7 d ( h e a d ) , A d d i n g t a g s t o . . . 2 8 0 6 7 6 1 d f 1 3 9 - > 4 6 a 1 d 4 d e 6 e 0 4 ( h e a d ) , A d d e d t i m e z o n e . . . 4 f 7 1 1 9 8 5 5 d a f - > 2 8 0 6 7 6 1 d f 1 3 9 ( b r a n c h p o i n t ) , A d d e d P r . . . 3 7 7 a d d f 2 3 e d b - > 4 f 7 1 1 9 8 5 5 d a f , A d d e d u s e r _ c r e a t e d t o . . . 4 8 3 d 9 a 6 3 f b f 5 - > 3 7 7 a d d f 2 3 e d b , A d d i n g c l a i m e d t o u s e r . . . 4 6 4 b a 4 1 d 7 a d 8 - > 4 8 3 d 9 a 6 3 f b f 5 , A d d i n g u s e r n a m e / p a s s w o . . . 2 c f d 9 d c 8 9 2 6 7 - > 4 6 4 b a 4 1 d 7 a d 8 , A d d i n g I n t e r c o m . i o d 4 7 7 4 a 3 c e 8 - > 2 c f d 9 d c 8 9 2 6 7 , S e p e r a t i n g R o l e s a n d U s e . . . N o n e - > d 4 7 7 4 a 3 c e 8 , B a s e