Conditions | 4 |
Total Lines | 66 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | #models tests |
||
14 | def create_tables(): |
||
15 | """create tables in postgresql database""" |
||
16 | commands = ( |
||
17 | """ |
||
18 | DROP TABLE IF EXISTS tb_users; |
||
19 | CREATE TABLE tb_users( |
||
20 | user_id SERIAL PRIMARY KEY, |
||
21 | username VARCHAR(100) UNIQUE NOT NULL, |
||
22 | firstname VARCHAR(50) NOT NULL, |
||
23 | lastname VARCHAR(50) NOT NULL, |
||
24 | password VARCHAR(255) NOT NULL, |
||
25 | created_on TIMESTAMP NOT NULL, |
||
26 | last_login TIMESTAMP |
||
27 | ) |
||
28 | """ |
||
29 | , |
||
30 | """ |
||
31 | DROP TABLE IF EXISTS tb_request; |
||
32 | CREATE TABLE tb_request( |
||
33 | request_id SERIAL PRIMARY KEY, |
||
34 | requestor VARCHAR(255) NOT NULL, |
||
35 | email VARCHAR(100) NOT NULL, |
||
36 | type VARCHAR(50) NOT NULL, |
||
37 | status VARCHAR(50) NOT NULL, |
||
38 | description TEXT, |
||
39 | created_on TIMESTAMP NOT NULL |
||
40 | ) |
||
41 | """ |
||
42 | , |
||
43 | """ |
||
44 | DROP TABLE IF EXISTS tb_admin; |
||
45 | CREATE TABLE tb_admin( |
||
46 | admin_id SERIAL PRIMARY KEY, |
||
47 | username VARCHAR(100) UNIQUE NOT NULL, |
||
48 | fullname VARCHAR(100) NOT NULL, |
||
49 | password VARCHAR(255) NOT NULL, |
||
50 | created_on TIMESTAMP NOT NULL, |
||
51 | last_login TIMESTAMP |
||
52 | ) |
||
53 | """ |
||
54 | ) |
||
55 | |||
56 | conn = None |
||
57 | try: |
||
58 | |||
59 | conn = psycopg2.connect(dbname='test_db',user="antonio", password="pass.123") |
||
60 | |||
61 | #create cursor |
||
62 | cur = conn.cursor() |
||
63 | |||
64 | #execute statement |
||
65 | for command in commands: |
||
66 | cur.execute(command) |
||
67 | |||
68 | cur.close() |
||
69 | |||
70 | #commit the changes |
||
71 | conn.commit() |
||
72 | |||
73 | except (Exception, psycopg2.DatabaseError) as error: |
||
74 | print(error) |
||
75 | finally: |
||
76 | if conn is not None: |
||
77 | conn.close() |
||
78 | |||
79 | return "Tables user, request and admin created succesfully" |
||
80 | |||
169 | pass |