-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·1597 lines (1328 loc) · 62.5 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import click
import mysql.connector
from mysql.connector import Error
import json
import os
from datetime import datetime
import shutil
from typing import List
from tabulate import tabulate
import csv
from history_manager import HistoryManager
import pandas as pd
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
class MigrationManager:
def __init__(self, config_path='.mydb/migrations.json'):
self.config_path = config_path
self.migrations = self._load_migrations()
def _load_migrations(self):
if not os.path.exists(self.config_path):
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
return {}
with open(self.config_path, 'r') as f:
return json.load(f)
def _save_migrations(self):
with open(self.config_path, 'w') as f:
json.dump(self.migrations, f, indent=4)
def create_migration(self, name, description, branch):
migration_number = self._get_next_migration_number(branch)
timestamp = datetime.now().isoformat()
migration = {
"number": migration_number,
"name": name,
"description": description,
"branch": branch,
"status": "pending",
"created_at": timestamp,
"applied_at": None
}
if branch not in self.migrations:
self.migrations[branch] = []
self.migrations[branch].append(migration)
self._save_migrations()
return migration_number
def _get_next_migration_number(self, branch):
if branch not in self.migrations or not self.migrations[branch]:
return 1
return max(m["number"] for m in self.migrations[branch]) + 1
def apply_migration(self, migration_number, branch):
for migration in self.migrations.get(branch, []):
if migration["number"] == migration_number:
migration["status"] = "applied"
migration["applied_at"] = datetime.now().isoformat()
self._save_migrations()
return True
return False
def rollback_migration(self, migration_number, branch):
for migration in self.migrations.get(branch, []):
if migration["number"] == migration_number:
migration["status"] = "rolled_back"
migration["applied_at"] = None
self._save_migrations()
return True
return False
def get_migration_status(self, branch):
return self.migrations.get(branch, [])
def get_current_migration(self, branch):
applied_migrations = [m for m in self.migrations.get(branch, []) if m["status"] == "applied"]
if not applied_migrations:
return -1
return max(m["number"] for m in applied_migrations)
class DatabaseManager:
def __init__(self, config_path='.mydb/config.json', config=None):
self.config_path = config_path
self.config = config or self._load_config()
self.connection = None
self.history_manager = HistoryManager()
self.migration_manager = MigrationManager()
def _load_config(self):
"""Load configuration from JSON file"""
if not os.path.exists(self.config_path):
return self._create_default_config()
with open(self.config_path, 'r') as f:
config = json.load(f)
if not config or 'connection' not in config:
return self._create_default_config()
# Ensure auth_plugin is set in existing configs
if 'auth_plugin' not in config['connection']:
config['connection']['auth_plugin'] = 'mysql_native_password'
with open(self.config_path, 'w') as f:
json.dump(config, f, indent=4)
return config
def _create_default_config(self):
"""Create and save a default configuration"""
default_config = {
'current_branch': 'main',
'branches': {
'main': {
'created_at': datetime.now().isoformat(),
'last_accessed': datetime.now().isoformat()
}
},
'connection': {
'user': 'root',
'password': 'B#@w@+123',
'host': 'localhost',
'port': 3306,
'database': 'mydb',
'auth_plugin': 'mysql_native_password'
}
}
os.makedirs(os.path.dirname(self.config_path), exist_ok=True)
with open(self.config_path, 'w') as f:
json.dump(default_config, f, indent=4)
return default_config
def _save_config(self):
"""Save current configuration to JSON file"""
with open(self.config_path, 'w') as f:
json.dump(self.config, f, indent=4)
def connect(self):
"""Create database connection"""
try:
self.connection = mysql.connector.connect(**self.config['connection'])
return self.connection.is_connected()
except Error as e:
click.echo(f"Error connecting to database: {e}")
return False
def see_databases(self):
"""List all configured databases."""
databases = self.config.get('databases', {})
if not databases:
click.echo("No databases configured.")
return
headers = ['Name', 'Host', 'Database']
data = [[name, db['host'], db['database']] for name, db in databases.items()]
click.echo(tabulate(data, headers=headers, tablefmt='grid'))
def connect_database(self, name, host, user, password, database):
"""Connect to a new database."""
new_config = {
'connection': {
'host': host,
'user': user,
'password': password,
'database': database,
'auth_plugin': 'mysql_native_password'
}
}
# Test the connection
temp_manager = DatabaseManager(config=new_config)
if temp_manager.connect():
# If connection successful, save the new database configuration
if 'databases' not in self.config:
self.config['databases'] = {}
self.config['databases'][name] = new_config['connection']
self._save_config()
click.echo(f"Successfully connected and saved database '{name}'.")
else:
click.echo("Failed to connect to the database. Configuration not saved.")
def reach_home(self):
"""Connect back to the default mydb database."""
home_config = self.config['databases'].get('mydb')
if home_config:
self.config['connection'] = home_config
self._save_config()
if self.connect():
click.echo("Successfully connected back to mydb database.")
else:
click.echo("Failed to connect to mydb database.")
else:
click.echo("Default mydb database not configured.")
def create_branch(self, branch_name):
"""Create a new branch from current branch"""
# add history code snippet 1
if branch_name in self.config['branches']:
self.history_manager.add_entry(
command='create_branch',
details=f"Failed to create branch '{branch_name}' - branch already exists",
status='failed'
)
click.echo(f"Branch '{branch_name}' already exists!")
return False
try:
# Connect to database
if not self.connect():
return False
cursor = self.connection.cursor()
current_branch = self.config['current_branch']
# Create new database for branch
new_db_name = f"{self.config['connection']['database']}_{branch_name}"
cursor.execute(f"CREATE DATABASE {new_db_name}")
# If this is not the main branch, copy data from current branch
if current_branch == 'main' and branch_name != 'main':
# For main branch, just create the database
pass
else:
# Copy from current branch
current_db_name = f"{self.config['connection']['database']}_{current_branch}"
cursor.execute(f"SHOW TABLES FROM {current_db_name}")
tables = cursor.fetchall()
# Copy schema and data
for (table_name,) in tables:
if isinstance(table_name, bytearray):
table_name = table_name.decode('utf-8')
cursor.execute(f"CREATE TABLE {new_db_name}.{table_name} LIKE {current_db_name}.{table_name}")
cursor.execute(f"INSERT INTO {new_db_name}.{table_name} SELECT * FROM {current_db_name}.{table_name}")
# Update config
self.config['branches'][branch_name] = {
'created_at': datetime.now().isoformat(),
'last_accessed': datetime.now().isoformat(),
'created_from': current_branch
}
self._save_config()
# self.create_schema_migrations_table(branch_name)
# click.echo(f"Initialized schema_migrations table in branch '{branch_name}'")
click.echo(f"Successfully created branch '{branch_name}' from '{current_branch}'")
# Record successful branch creation
# add history code snippet 2
self.history_manager.add_entry(
command='create_branch',
details=f"Successfully created branch '{branch_name}' from '{current_branch}'",
status='success'
)
return True
except Error as e:
# Record failed branch creation
# add history code snippet 3
self.history_manager.add_entry(
command='create_branch',
details=f"Failed to create branch '{branch_name}' - {str(e)}",
status='failed'
)
click.echo(f"Error creating branch: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def switch_branch(self, branch_name):
"""Switch to a different branch"""
if branch_name not in self.config['branches']:
self.history_manager.add_entry(
command='switch_branch',
details=f"Failed to switch branch '{branch_name}' - branch does not exist",
status='failed'
)
click.echo(f"Branch '{branch_name}' does not exist!")
return False
try:
# Update current branch
self.config['current_branch'] = branch_name
self.config['branches'][branch_name]['last_accessed'] = datetime.now().isoformat()
self._save_config()
click.echo(f"Switched to branch '{branch_name}'")
self.history_manager.add_entry(
command='switch_branch',
details=f"Successfully switched to branch '{branch_name}'",
status='success'
)
return True
except Exception as e:
self.history_manager.add_entry(
command='switch_branch',
details=f"Failed to switch branch '{branch_name}' - {str(e)}",
status='failed'
)
click.echo(f"Error switching branch: {e}")
return False
def delete_branch(self, branch_name):
"""Delete a branch"""
if branch_name not in self.config['branches']:
click.echo(f"Branch '{branch_name}' does not exist!")
return False
if branch_name == 'main':
click.echo("Cannot delete 'main' branch!")
return False
if branch_name == self.config['current_branch']:
click.echo("Cannot delete current branch!")
return False
try:
if not self.connect():
return False
cursor = self.connection.cursor()
# Drop the branch database
db_name = f"{self.config['connection']['database']}_{branch_name}"
cursor.execute(f"DROP DATABASE IF EXISTS {db_name}")
# Remove branch from config
del self.config['branches'][branch_name]
self._save_config()
click.echo(f"Successfully deleted branch '{branch_name}'")
return True
except Error as e:
click.echo(f"Error deleting branch: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def list_branches(self):
"""List all branches"""
click.echo("\nAvailable branches:")
click.echo("================")
for branch_name, branch_info in self.config['branches'].items():
current = "*" if branch_name == self.config['current_branch'] else " "
created_at = datetime.fromisoformat(branch_info['created_at']).strftime("%Y-%m-%d %H:%M:%S")
click.echo(f"{current} {branch_name} (created: {created_at})")
def create_table(self, table_name: str, columns: List[str]):
"""Create a new table in the current branch"""
try:
if not self.connect():
return False
cursor = self.connection.cursor()
current_branch = self.config['current_branch']
# Determine the appropriate database name
if current_branch == 'main':
current_db = self.config['connection']['database'] # Use 'mydb'
else:
current_db = f"{self.config['connection']['database']}_{current_branch}" # e.g., 'mydb_nimish'
# Use the appropriate database
cursor.execute(f"USE {current_db}")
# Create the table with the provided columns
create_table_sql = f"CREATE TABLE {table_name} ({', '.join(columns)})"
cursor.execute(create_table_sql)
click.echo(f"Successfully created table '{table_name}' in branch '{current_branch}'")
return True
except Error as e:
click.echo(f"Error creating table: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def list_tables(self):
"""List all tables in the current branch"""
try:
if not self.connect():
return False
cursor = self.connection.cursor()
current_branch = self.config['current_branch']
if current_branch == 'main':
current_db = self.config['connection']['database'] # Use 'mydb'
else:
current_db = f"{self.config['connection']['database']}_{current_branch}" # e.g., 'mydb_nimish'
cursor.execute(f"USE {current_db}")
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
if not tables:
click.echo(f"No tables found in branch '{current_branch}'")
return True
# Get table information
table_info = []
for table_row in tables:
# Convert byte string to regular string if necessary
table_name = table_row[0]
if isinstance(table_name, bytes):
table_name = table_name.decode('utf-8')
elif isinstance(table_name, bytearray):
table_name = table_name.decode('utf-8')
# Use proper quoting for table names
cursor.execute(f"SHOW CREATE TABLE `{table_name}`")
create_stmt = cursor.fetchone()[1]
cursor.execute(f"SELECT COUNT(*) FROM `{table_name}`")
row_count = cursor.fetchone()[0]
table_info.append([table_name, row_count])
# Display tables in a nice format
click.echo(f"\nTables in branch '{current_branch}':")
click.echo(tabulate(table_info, headers=['Table Name', 'Row Count'], tablefmt='grid'))
return True
except Error as e:
click.echo(f"Error listing tables: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def describe_table(self, table_name):
"""Show detailed information about a specific table"""
try:
if not self.connect():
return None
cursor = self.connection.cursor()
current_branch = self.config['current_branch']
# Get correct database name
db_name = self.config['connection']['database']
if current_branch != 'main':
db_name = f"{db_name}_{current_branch}"
# Switch to current database
cursor.execute(f"USE `{db_name}`")
# Get table description
cursor.execute(f"DESCRIBE `{table_name}`")
columns = cursor.fetchall()
if not columns:
return None
# Format the results
column_info = []
for col in columns:
column_info.append({
'Field': col[0],
'Type': col[1],
'Null': col[2],
'Key': col[3],
'Default': col[4],
'Extra': col[5]
})
return pd.DataFrame(column_info)
except Error as e:
print(f"Error describing table: {e}")
return None
finally:
if 'cursor' in locals() and cursor:
cursor.close()
if self.connection and self.connection.is_connected():
self.connection.close()
def drop_table(self, table_name: str):
"""Drop a table from the current branch"""
try:
if not self.connect():
return False
cursor = self.connection.cursor()
current_branch = self.config['current_branch']
if current_branch == 'main':
current_db = self.config['connection']['database'] # Use 'mydb'
else:
current_db = f"{self.config['connection']['database']}_{current_branch}" # e.g., 'mydb_nimish'
# Switch to current database
cursor.execute(f"USE {current_db}")
# Drop the table
cursor.execute(f"DROP TABLE {table_name}")
click.echo(f"Successfully dropped table '{table_name}' from branch '{current_branch}'")
return True
except Error as e:
click.echo(f"Error dropping table: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def _init_branch_migrations(self, branch_name):
"""Initialize migrations directory structure for a branch"""
branch_migrations_dir = f"migrations/{branch_name}"
os.makedirs(branch_migrations_dir, exist_ok=True)
return branch_migrations_dir
def create_schema_migrations_table(self, branch_name=None):
"""Create branch-specific schema_migrations table with proper structure."""
try:
if not self.connect():
return False
cursor = self.connection.cursor()
branch = branch_name or self.config['current_branch']
# Use branch-specific database
db_name = self.config['connection']['database']
if branch != 'main':
db_name = f"{db_name}_{branch}"
cursor.execute(f"USE {db_name}")
# Drop existing table if it exists to ensure correct structure
cursor.execute("DROP TABLE IF EXISTS schema_migrations")
# Create schema_migrations table with corrected structure
cursor.execute("""
CREATE TABLE schema_migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
migration_number INT NOT NULL,
migration_name VARCHAR(255),
description TEXT,
branch_name VARCHAR(255) NOT NULL,
parent_branch VARCHAR(255),
parent_migration_number INT,
status ENUM('pending', 'applied', 'failed', 'rolled_back') DEFAULT 'pending',
applied_at TIMESTAMP NULL,
error_message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY unique_migration (migration_number, branch_name)
)
""")
self.connection.commit()
click.echo(f"Successfully created schema_migrations table in database '{db_name}'")
return True
except Error as e:
click.echo(f"Error creating schema_migrations table: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def get_next_migration_number(self, branch_name=None):
"""Get the next migration number for a specific branch."""
branch = branch_name or self.config['current_branch']
# Set branch-specific migrations directory
branch_migrations_dir = f"migrations/{branch}"
# Ensure the directory exists
if not os.path.exists(branch_migrations_dir):
os.makedirs(branch_migrations_dir, exist_ok=True)
return 0 # Start at 0000 if no migrations exist for the branch
# List all migration folders and find the highest number
existing_migrations = [
d for d in os.listdir(branch_migrations_dir) if os.path.isdir(os.path.join(branch_migrations_dir, d))
]
if not existing_migrations:
return 0 # Start at 0000 if no migrations exist
# Extract migration numbers from folder names and find the highest one
migration_numbers = [
int(m.split('_')[0]) for m in existing_migrations if m.split('_')[0].isdigit()
]
next_number = max(migration_numbers) + 1 if migration_numbers else 0
return next_number
def create_migration(self, name, description=None):
"""Create a new migration for the current branch."""
try:
branch = self.config['current_branch']
migration_number = self.get_next_migration_number(branch)
if migration_number is None:
return False
# Get branch-specific migration directory
branch_dir = self._init_branch_migrations(branch)
migration_dir = f"{branch_dir}/{migration_number:04d}_{name}"
os.makedirs(migration_dir, exist_ok=True)
# Create migration files
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
# Create up.sql and down.sql files
for file_name in ['up.sql', 'down.sql']:
with open(f"{migration_dir}/{file_name}", "w") as f:
f.write(f"-- Migration: {name}\n")
f.write(f"-- Created at: {datetime.now().isoformat()}\n")
f.write(f"-- Branch: {branch}\n")
f.write(f"-- {file_name.split('.')[0].capitalize()} Migration\n\n")
# Get parent branch information
parent_branch = self.config['branches'][branch].get('created_from')
parent_migration_number = self._get_parent_branch_migration_number(branch)
# Create metadata for JSON
migration_data = {
"migration_number": migration_number,
"name": name,
"description": description,
"branch": branch,
"created_at": timestamp,
"parent_branch": parent_branch,
"parent_migration_number": parent_migration_number,
"status": "pending",
"applied_at": None
}
# Save metadata to JSON file
if branch not in self.migration_manager.migrations:
self.migration_manager.migrations[branch] = []
self.migration_manager.migrations[branch].append(migration_data)
self.migration_manager._save_migrations()
# Create metadata.json in migration directory (for backwards compatibility)
with open(f"{migration_dir}/metadata.json", "w") as f:
json.dump(migration_data, f, indent=4)
click.echo(f"Created migration {migration_number:04d}_{name} in branch '{branch}'")
return True
except Exception as e:
click.echo(f"Error creating migration: {e}")
return False
def apply_migration(self, migration_number=None, branch_name=None):
try:
if not self.connect():
return False, []
cursor = self.connection.cursor()
branch = branch_name or self.config['current_branch']
db_name = self.config['connection']['database']
if branch != 'main':
db_name = f"{db_name}_{branch}"
if not self.connection.is_connected():
click.echo("Reconnecting to the database...")
self.connection.reconnect(attempts=3, delay=2)
cursor = self.connection.cursor()
cursor.execute(f"USE {db_name}")
if migration_number is None:
pending_migrations = [m for m in self.migration_manager.migrations.get(branch, [])
if m['status'] == 'pending']
if not pending_migrations:
click.echo(f"No pending migrations for branch '{branch}'")
return True, []
migration = pending_migrations[0]
migration_number = migration['migration_number']
migration_name = migration['name']
click.echo(f"Applying migration {migration_number:04d}_{migration_name}")
migration_files = self._get_migration_files(branch, migration_number)
if not migration_files:
return False, []
cursor.execute("START TRANSACTION")
executed_queries = []
with open(migration_files['up'], 'r') as f:
sql = f.read()
if sql.strip():
for statement in sql.split(';'):
if statement.strip():
cursor.execute(statement)
executed_queries.append(statement.strip())
for migration in self.migration_manager.migrations.get(branch, []):
if migration['migration_number'] == migration_number:
migration['status'] = 'applied'
migration['applied_at'] = datetime.now().isoformat()
break
self.migration_manager._save_migrations()
cursor.execute("COMMIT")
click.echo(f"Successfully applied migration {migration_number:04d} in branch '{branch}'")
return True, executed_queries
except Error as e:
cursor.execute("ROLLBACK")
for migration in self.migration_manager.migrations.get(branch, []):
if migration['migration_number'] == migration_number:
migration['status'] = 'failed'
migration['error_message'] = str(e)
break
self.migration_manager._save_migrations()
click.echo(f"Error applying migration {migration_number:04d}: {e}")
return False, []
finally:
if cursor:
cursor.close()
if self.connection and self.connection.is_connected():
self.connection.close()
def _get_migration_files(self, branch, migration_number):
"""Get migration files for a specific branch and migration number."""
branch_dir = f"migrations/{branch}"
if not os.path.exists(branch_dir):
click.echo(f"No migrations directory for branch '{branch}'")
return None
# Find migration directory (accounts for named migrations)
migration_dirs = [d for d in os.listdir(branch_dir)
if d.startswith(f"{migration_number:04d}_")]
if not migration_dirs:
click.echo(f"Migration {migration_number:04d} not found in branch '{branch}'")
return None
migration_dir = f"{branch_dir}/{migration_dirs[0]}"
return {
'up': f"{migration_dir}/up.sql",
'down': f"{migration_dir}/down.sql",
'metadata': f"{migration_dir}/metadata.json"
}
def migration_status(self, branch_name=None):
"""Get detailed migration status for a branch, including current state."""
try:
if not self.connect():
return False, "Failed to connect to the database.", None
branch = branch_name or self.config['current_branch']
migrations = self.migration_manager.migrations.get(branch, [])
if not migrations:
return True, f"No migrations found for branch '{branch}'", None
# Get the current migration state
current_migration = max(
(m for m in migrations if m['status'] == 'applied'),
key=lambda x: x['migration_number'],
default=None
)
# Prepare migration data for tabulation
headers = ['Number', 'Name', 'Status', 'Applied At', 'Error']
table_data = [[
f"{m['migration_number']:04d}",
m['name'] or 'unnamed',
m['status'],
m['applied_at'] or 'N/A',
(m.get('error_message', '')[:50] + '...') if m.get('error_message') and len(m.get('error_message')) > 50 else (m.get('error_message') or 'N/A')
] for m in migrations]
return True, table_data, current_migration
except Exception as e:
return False, f"Error getting migration status: {str(e)}", None
def _get_parent_branch_migration_number(self, branch):
"""Get the last migration number from parent branch when this branch was created."""
parent_branch = self.config['branches'][branch].get('created_from')
if not parent_branch:
return 0
branch_created_at = datetime.fromisoformat(self.config['branches'][branch]['created_at'])
parent_migrations = self.migration_manager.migrations.get(parent_branch, [])
applied_migrations = [
m for m in parent_migrations
if m['status'] == 'applied' and datetime.fromisoformat(m['applied_at']) <= branch_created_at
]
if not applied_migrations:
return 0
return max(m['migration_number'] for m in applied_migrations)
def get_current_migration(self):
"""Get the number of the last applied migration in current branch."""
branch = self.config['current_branch']
migrations = self.migration_manager.migrations.get(branch, [])
applied_migrations = [m for m in migrations if m['status'] == 'applied']
if not applied_migrations:
return -1
return max(m['migration_number'] for m in applied_migrations)
def rollback_migration(self, migration_number):
"""Rollback a specific migration."""
try:
branch = self.config['current_branch']
# Check if migration exists and is applied
migration = next((m for m in self.migration_manager.migrations.get(branch, [])
if m['migration_number'] == migration_number), None)
if not migration or migration['status'] != 'applied':
click.echo(f"Migration {migration_number:04d} is not applied or doesn't exist")
return False
# Get migration files
migration_files = self._get_migration_files(branch, migration_number)
if not migration_files:
return False
# Connect to the database
if not self.connect():
return False
cursor = self.connection.cursor()
# Use branch-specific database
db_name = self.config['connection']['database']
if branch != 'main':
db_name = f"{db_name}_{branch}"
cursor.execute(f"USE {db_name}")
cursor.execute("START TRANSACTION")
try:
executed_queries = []
# Apply down migration
with open(migration_files['down'], 'r') as f:
sql = f.read()
if sql.strip():
for statement in sql.split(';'):
if statement.strip():
cursor.execute(statement)
executed_queries.append(statement.strip())
# Update migration status in JSON
migration['status'] = 'rolled_back'
migration['applied_at'] = None
self.migration_manager._save_migrations()
cursor.execute("COMMIT")
click.echo(f"Successfully rolled back migration {migration_number:04d}")
return True, executed_queries
except Error as e:
cursor.execute("ROLLBACK")
click.echo(f"Error rolling back migration: {e}")
return False, []
except Exception as e:
click.echo(f"Error during rollback process: {e}")
return False
finally:
if self.connection and self.connection.is_connected():
cursor.close()
self.connection.close()
def migrate_up(self):
current_migration = self.get_current_migration()
next_migration = current_migration + 1
return self.apply_migration(next_migration)
def migrate_down(self):
current_migration = self.get_current_migration()
if current_migration > 0:
# We need to implement a rollback_migration method
return self.rollback_migration(current_migration)
else:
return False, []
def _get_parent_branch_pending_migrations(self, branch):
"""
Get pending migrations from source branch that need to be applied to current branch.
This considers the branch ancestry and ensures proper migration ordering.
Args:
branch (str): Source branch name to check migrations from
Returns:
list: List of tuples (migration_number, migration_name) that need to be applied
"""
try:
# Get branch creation timestamps and parent information
source_created_at = datetime.fromisoformat(self.config['branches'][branch]['created_at'])
target_branch = self.config['current_branch']
target_created_at = datetime.fromisoformat(self.config['branches'][target_branch]['created_at'])
# Determine common ancestor
source_lineage = self._get_branch_lineage(branch)
target_lineage = self._get_branch_lineage(target_branch)
common_ancestor = next((ancestor for ancestor in source_lineage if ancestor in target_lineage), None)
if not common_ancestor:
click.echo("No common ancestor found between branches")
return []
# Get migrations from source branch after common ancestor
source_migrations = self.migration_manager.migrations.get(branch, [])
common_ancestor_migrations = self.migration_manager.migrations.get(common_ancestor, [])
# Find the last applied migration in common ancestor before source branch creation
last_common_migration = max(
(m for m in common_ancestor_migrations if m['status'] == 'applied' and
datetime.fromisoformat(m['applied_at']) <= source_created_at),
key=lambda x: x['migration_number'],
default=None
)
last_common_migration_number = last_common_migration['migration_number'] if last_common_migration else -1
# Get all applied migrations from source branch after the common point
source_applied_migrations = [
(m['migration_number'], m['name']) for m in source_migrations
if m['status'] == 'applied' and m['migration_number'] > last_common_migration_number
]
# Get already applied migrations in target branch
target_migrations = self.migration_manager.migrations.get(target_branch, [])
applied_in_target = {m['migration_number'] for m in target_migrations if m['status'] == 'applied'}
# Filter out migrations that are already applied in target
pending_migrations = [
(num, name) for num, name in source_applied_migrations
if num not in applied_in_target
]
return pending_migrations
except Exception as e:
click.echo(f"Error checking source branch migrations: {e}")
return []
def _get_branch_lineage(self, branch_name):
"""
Get the complete lineage of a branch back to main.
Args:
branch_name (str): Name of the branch to trace
Returns:
list: List of branch names representing the lineage back to main
"""
lineage = [branch_name]
current = branch_name
while current in self.config['branches']:
parent = self.config['branches'][current].get('created_from')
if not parent or parent == current:
break
lineage.append(parent)
current = parent
return lineage
def merge_branch(self, source_branch, target_branch):
"""
Merge complete contents (schema + data) from source_branch into target_branch.
Args:
source_branch (str): Name of the branch to merge from
target_branch (str): Name of the branch to merge into
Returns: