Conditions | 13 |
Total Lines | 70 |
Code Lines | 46 |
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:
Complex classes like db_sync_tool.database.process.import_database_dump() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/env python3 |
||
71 | def import_database_dump(): |
||
72 | """ |
||
73 | Importing the selected database dump file |
||
74 | :return: |
||
75 | """ |
||
76 | if not system.config['is_same_client'] and not mode.is_import(): |
||
77 | prepare_target_database_dump() |
||
78 | |||
79 | if system.config['clear_database']: |
||
80 | output.message( |
||
81 | output.Subject.TARGET, |
||
82 | 'Clearing database before import', |
||
83 | True |
||
84 | ) |
||
85 | clear_database(mode.Client.TARGET) |
||
86 | |||
87 | database_utility.truncate_tables() |
||
88 | |||
89 | if not system.config['keep_dump'] and not mode.is_dump(): |
||
90 | |||
91 | database_utility.get_database_version(mode.Client.TARGET) |
||
92 | |||
93 | output.message( |
||
94 | output.Subject.TARGET, |
||
95 | 'Importing database dump', |
||
96 | True |
||
97 | ) |
||
98 | |||
99 | if not mode.is_import(): |
||
100 | _dump_path = helper.get_dump_dir( |
||
101 | mode.Client.TARGET) + database_utility.database_dump_file_name |
||
102 | else: |
||
103 | _dump_path = system.config['import'] |
||
104 | |||
105 | if not system.config['yes']: |
||
106 | _host_name = helper.get_ssh_host_name(mode.Client.TARGET, True) if mode.is_remote( |
||
107 | mode.Client.TARGET) else 'local' |
||
108 | |||
109 | _input = helper.confirm( |
||
110 | output.message( |
||
111 | output.Subject.TARGET, |
||
112 | f'Are you sure, you want to import the dump file into {_host_name} database?', |
||
113 | False |
||
114 | ), |
||
115 | True |
||
116 | ) |
||
117 | |||
118 | if not _input: return |
||
119 | |||
120 | database_utility.check_database_dump(mode.Client.TARGET, _dump_path) |
||
121 | |||
122 | import_database_dump_file(mode.Client.TARGET, _dump_path) |
||
123 | |||
124 | if 'after_dump' in system.config['target']: |
||
125 | _after_dump = system.config['target']['after_dump'] |
||
126 | output.message( |
||
127 | output.Subject.TARGET, |
||
128 | f'Importing after_dump file {output.CliFormat.BLACK}{_after_dump}{output.CliFormat.ENDC}', |
||
129 | True |
||
130 | ) |
||
131 | import_database_dump_file(mode.Client.TARGET, _after_dump) |
||
132 | |||
133 | if 'post_sql' in system.config['target']: |
||
134 | output.message( |
||
135 | output.Subject.TARGET, |
||
136 | f'Running addition post sql commands', |
||
137 | True |
||
138 | ) |
||
139 | for _sql_command in system.config['target']['post_sql']: |
||
140 | database_utility.run_database_command(mode.Client.TARGET, _sql_command, True) |
||
141 | |||
223 |
This check looks for lines that are too long. You can specify the maximum line length.