Project

General

Profile

« Previous | Next » 

Revision 2890

csv2db: Support reinstalling just the errors table using new errors_table_only option

View differences:

csv2db
28 28
    
29 29
    # Parse args
30 30
    input_cmd = sys.argv[1:]
31
    if input_cmd == []: usage_err()
32 31
    
33 32
    # Get config from env vars
34 33
    table = opts.get_env_var('table', None, env_names)
35 34
    schema = opts.get_env_var('schema', 'public', env_names)
36 35
    db_config = opts.get_env_vars(sql.db_config_names, None, env_names)
36
    errors_table_only = opts.env_flag('errors_table_only', False, env_names)
37 37
    verbosity = util.cast(float, opts.get_env_var('verbosity', 1, env_names))
38
    if not (table != None and 'engine' in db_config): usage_err()
39 38
    
39
    if not (input_cmd != [] and table != None and 'engine' in db_config):
40
        usage_err()
41
    
40 42
    # Connect to DB
41 43
    def log(msg, level=1):
42 44
        '''Higher level -> more verbose'''
43 45
        if level <= verbosity: sys.stderr.write(msg.rstrip('\n')+'\n')
44 46
    db = sql.connect(db_config, log_debug=log)
45 47
    
48
    def mk_errors_table():
49
        log('Creating errors table')
50
        errors_table = sql.errors_table(db, table, if_exists=False)
51
        sql.drop_table(db, errors_table)
52
        typed_cols = [
53
            sql_gen.TypedCol('column', 'text', nullable=False),
54
            sql_gen.TypedCol('value', 'text'),
55
            sql_gen.TypedCol('error_code', 'character varying(5)',
56
                nullable=False),
57
            sql_gen.TypedCol('error', 'text', nullable=False),
58
            ]
59
        sql.create_table(db, errors_table, typed_cols, has_pkey=False)
60
        index_cols = ['column', 'value', 'error_code', 'error']
61
        sql.add_index(db, index_cols, errors_table, unique=True)
62
        db.db.commit()
63
    
46 64
    use_copy_from = [True]
47 65
    
48 66
    # Loads data into the table using the currently-selected approach.
......
121 139
        db.db.rollback()
122 140
        sql.vacuum(db, table)
123 141
        
124
        log('Creating errors table')
125
        errors_table = sql.errors_table(db, table, if_exists=False)
126
        typed_cols = [
127
            sql_gen.TypedCol('column', 'text', nullable=False),
128
            sql_gen.TypedCol('value', 'text'),
129
            sql_gen.TypedCol('error_code', 'character varying(5)',
130
                nullable=False),
131
            sql_gen.TypedCol('error', 'text', nullable=False),
132
            ]
133
        sql.create_table(db, errors_table, typed_cols, has_pkey=False)
134
        index_cols = ['column', 'value', 'error_code', 'error']
135
        sql.add_index(db, index_cols, errors_table, unique=True)
136
        db.db.commit()
142
        mk_errors_table()
137 143
    
138
    try: load()
139
    except sql.DatabaseErrors, e:
140
        if use_copy_from[0]: # first try
141
            exc.print_ex(e, plain=True)
142
            use_copy_from[0] = False
143
            load() # try again with different approach
144
        else: raise
144
    if errors_table_only: mk_errors_table()
145
    else:
146
        try: load()
147
        except sql.DatabaseErrors, e:
148
            if use_copy_from[0]: # first try
149
                exc.print_ex(e, plain=True)
150
                use_copy_from[0] = False
151
                load() # try again with different approach
152
            else: raise
145 153

  
146 154
main()

Also available in: Unified diff