/
update.go
4729 lines (4230 loc) · 157 KB
/
update.go
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
package cluster
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"github.com/canonical/lxd/lxd/db/query"
"github.com/canonical/lxd/lxd/db/schema"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/osarch"
)
// Schema for the cluster database.
func Schema() *schema.Schema {
schema := schema.NewFromMap(updates)
schema.Fresh(freshSchema)
return schema
}
// FreshSchema returns the fresh schema definition of the global database.
func FreshSchema() string {
return freshSchema
}
// SchemaDotGo refreshes the schema.go file in this package, using the updates
// defined here.
func SchemaDotGo() error {
return schema.DotGo(updates, "schema")
}
// SchemaVersion is the current version of the cluster database schema.
var SchemaVersion = len(updates)
var updates = map[int]schema.Update{
1: updateFromV0,
2: updateFromV1,
3: updateFromV2,
4: updateFromV3,
5: updateFromV4,
6: updateFromV5,
7: updateFromV6,
8: updateFromV7,
9: updateFromV8,
10: updateFromV9,
11: updateFromV10,
12: updateFromV11,
13: updateFromV12,
14: updateFromV13,
15: updateFromV14,
16: updateFromV15,
17: updateFromV16,
18: updateFromV17,
19: updateFromV18,
20: updateFromV19,
21: updateFromV20,
22: updateFromV21,
23: updateFromV22,
24: updateFromV23,
25: updateFromV24,
26: updateFromV25,
27: updateFromV26,
28: updateFromV27,
29: updateFromV28,
30: updateFromV29,
31: updateFromV30,
32: updateFromV31,
33: updateFromV32,
34: updateFromV33,
35: updateFromV34,
36: updateFromV35,
37: updateFromV36,
38: updateFromV37,
39: updateFromV38,
40: updateFromV39,
41: updateFromV40,
42: updateFromV41,
43: updateFromV42,
44: updateFromV43,
45: updateFromV44,
46: updateFromV45,
47: updateFromV46,
48: updateFromV47,
49: updateFromV48,
50: updateFromV49,
51: updateFromV50,
52: updateFromV51,
53: updateFromV52,
54: updateFromV53,
55: updateFromV54,
56: updateFromV55,
57: updateFromV56,
58: updateFromV57,
59: updateFromV58,
60: updateFromV59,
61: updateFromV60,
62: updateFromV61,
63: updateFromV62,
64: updateFromV63,
65: updateFromV64,
66: updateFromV65,
67: updateFromV66,
68: updateFromV67,
69: updateFromV68,
70: updateFromV69,
71: updateFromV70,
72: updateFromV71,
73: updateFromV72,
}
func updateFromV72(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
DROP TABLE permissions;
DROP TABLE auth_groups_permissions;
CREATE TABLE auth_groups_permissions (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
auth_group_id INTEGER NOT NULL,
entity_type INTEGER NOT NULL,
entity_id INTEGER NOT NULL,
entitlement TEXT NOT NULL,
FOREIGN KEY (auth_group_id) REFERENCES auth_groups (id) ON DELETE CASCADE,
UNIQUE (auth_group_id, entity_type, entitlement, entity_id)
);
`)
if err != nil {
return err
}
return nil
}
func updateFromV71(ctx context.Context, tx *sql.Tx) error {
_, err := tx.ExecContext(ctx, `
ALTER TABLE identities ADD COLUMN first_seen_date DATETIME NOT NULL DEFAULT "0001-01-01T00:00:00Z";
ALTER TABLE identities ADD COLUMN last_seen_date DATETIME NOT NULL DEFAULT "0001-01-01T00:00:00Z";
ALTER TABLE identities ADD COLUMN updated_date DATETIME NOT NULL DEFAULT "0001-01-01T00:00:00Z";
`)
if err != nil {
return err
}
return nil
}
func updateFromV70(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE auth_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
UNIQUE (name)
);
CREATE TABLE identities_auth_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
identity_id INTEGER NOT NULL,
auth_group_id INTEGER NOT NULL,
FOREIGN KEY (identity_id) REFERENCES identities (id) ON DELETE CASCADE,
FOREIGN KEY (auth_group_id) REFERENCES auth_groups (id) ON DELETE CASCADE,
UNIQUE (identity_id, auth_group_id)
);
CREATE TABLE identity_provider_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
UNIQUE (name)
);
CREATE TABLE auth_groups_identity_provider_groups (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
auth_group_id INTEGER NOT NULL,
identity_provider_group_id INTEGER NOT NULL,
FOREIGN KEY (auth_group_id) REFERENCES auth_groups (id) ON DELETE CASCADE,
FOREIGN KEY (identity_provider_group_id) REFERENCES identity_provider_groups (id) ON DELETE CASCADE,
UNIQUE (auth_group_id, identity_provider_group_id)
);
CREATE TABLE permissions (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
entitlement TEXT NOT NULL,
entity_type TEXT NOT NULL,
entity_id INTEGER NOT NULL,
UNIQUE (entitlement, entity_type, entity_id)
);
CREATE TABLE auth_groups_permissions (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
auth_group_id INTEGER NOT NULL,
permission_id INTEGER NOT NULL,
FOREIGN KEY (auth_group_id) REFERENCES auth_groups (id) ON DELETE CASCADE,
FOREIGN KEY (permission_id) REFERENCES permissions (id) ON DELETE CASCADE,
UNIQUE (auth_group_id, permission_id)
);
`)
if err != nil {
return err
}
return nil
}
func updateFromV69(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE identities (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
auth_method INTEGER NOT NULL,
type INTEGER NOT NULL,
identifier TEXT NOT NULL,
name TEXT NOT NULL,
metadata TEXT NOT NULL,
UNIQUE (auth_method, identifier),
UNIQUE (type, identifier)
);
CREATE TABLE identities_projects (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
identity_id INTEGER NOT NULL,
project_id INTEGER NOT NULL,
FOREIGN KEY (identity_id) REFERENCES identities (id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects (id) ON DELETE CASCADE,
UNIQUE (identity_id, project_id)
);
INSERT INTO identities (id, auth_method, type, identifier, name, metadata) SELECT id, 1, 1, fingerprint, name, json_object('cert', certificate) FROM certificates WHERE type = 1 AND restricted = 1;
INSERT INTO identities (id, auth_method, type, identifier, name, metadata) SELECT id, 1, 2, fingerprint, name, json_object('cert', certificate) FROM certificates WHERE type = 1 AND restricted = 0;
INSERT INTO identities (id, auth_method, type, identifier, name, metadata) SELECT id, 1, 3, fingerprint, name, json_object('cert', certificate) FROM certificates WHERE type = 2;
INSERT INTO identities (id, auth_method, type, identifier, name, metadata) SELECT id, 1, 4, fingerprint, name, json_object('cert', certificate) FROM certificates WHERE type = 3 AND restricted = 1;
INSERT INTO identities (id, auth_method, type, identifier, name, metadata) SELECT id, 1, 6, fingerprint, name, json_object('cert', certificate) FROM certificates WHERE type = 3 AND restricted = 0;
INSERT INTO identities_projects (identity_id, project_id) SELECT certificate_id, project_id FROM certificates_projects;
DROP TABLE certificates;
DROP TABLE certificates_projects;
`)
if err != nil {
return err
}
return nil
}
// updateFromV68 fixes unique index for record name to make it zone specific.
func updateFromV68(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE networks_zones_records_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_id INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
entries TEXT NOT NULL,
UNIQUE (network_zone_id, name),
FOREIGN KEY (network_zone_id) REFERENCES networks_zones (id) ON DELETE CASCADE
);
CREATE TABLE networks_zones_records_config_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_record_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_zone_record_id, key),
FOREIGN KEY (network_zone_record_id) REFERENCES networks_zones_records_new (id) ON DELETE CASCADE
);
INSERT INTO "networks_zones_records_new" SELECT * FROM "networks_zones_records";
INSERT INTO "networks_zones_records_config_new" SELECT * FROM "networks_zones_records_config";
DROP TABLE "networks_zones_records";
ALTER TABLE "networks_zones_records_new" RENAME TO "networks_zones_records";
DROP TABLE "networks_zones_records_config";
ALTER TABLE "networks_zones_records_config_new" RENAME TO "networks_zones_records_config";
`)
if err != nil {
return fmt.Errorf("Failed altering network_zones_records schema: %w", err)
}
return nil
}
// updateFromV67 adds features.networks.zones=true to any project that has features.networks=true.
func updateFromV67(ctx context.Context, tx *sql.Tx) error {
// Find projects that have features.networks=true.
rows, err := tx.QueryContext(ctx, `
SELECT
projects.id
FROM projects
JOIN
projects_config ON projects_config.project_id = projects.id
AND projects_config.key = "features.networks"
AND projects_config.value = "true"
`)
if err != nil {
return fmt.Errorf("Failed finding projects with features.networks=true: %w", err)
}
defer func() { _ = rows.Close() }()
var projectIDs []int64
for rows.Next() {
var projectID int64
err := rows.Scan(&projectID)
if err != nil {
return fmt.Errorf("Failed scanning project ID row: %w", err)
}
projectIDs = append(projectIDs, projectID)
}
_ = rows.Close()
// Add features.networks.zones=true to any project that has features.networks=true.
for _, projectID := range projectIDs {
_, err = tx.Exec(`INSERT OR REPLACE INTO projects_config (project_id,key,value) VALUES(?,?,?);`, projectID, "features.networks.zones", "true")
if err != nil {
return fmt.Errorf("Failed adding features.networks.zones=true to project ID %q: %w", projectID, err)
}
logger.Info("Added features.networks.zones=true on project with features.networks=true", logger.Ctx{"projectID": projectID})
}
return nil
}
// updateFromV66 adds creation_date column to storage_volumes and storage_volumes_snapshots tables.
func updateFromV66(ctx context.Context, tx *sql.Tx) error {
q := `
ALTER TABLE storage_volumes ADD COLUMN creation_date DATETIME NOT NULL DEFAULT "0001-01-01T00:00:00Z";
ALTER TABLE storage_volumes_snapshots ADD COLUMN creation_date DATETIME NOT NULL DEFAULT "0001-01-01T00:00:00Z";
DROP VIEW storage_volumes_all;
CREATE VIEW storage_volumes_all (
id,
name,
storage_pool_id,
node_id,
type,
description,
project_id,
content_type,
creation_date) AS
SELECT id,
name,
storage_pool_id,
node_id,
type,
description,
project_id,
content_type,
creation_date
FROM storage_volumes UNION
SELECT storage_volumes_snapshots.id,
printf('%s/%s', storage_volumes.name, storage_volumes_snapshots.name),
storage_volumes.storage_pool_id,
storage_volumes.node_id,
storage_volumes.type,
storage_volumes_snapshots.description,
storage_volumes.project_id,
storage_volumes.content_type,
storage_volumes_snapshots.creation_date
FROM storage_volumes
JOIN storage_volumes_snapshots ON storage_volumes.id = storage_volumes_snapshots.storage_volume_id;
`
_, err := tx.Exec(q)
if err != nil {
return fmt.Errorf("Failed adding creation_date column to storage volumes: %w", err)
}
return nil
}
// updateFromV65 fixes typo in cephobject.radosgw.endpoint* settings.
func updateFromV65(ctx context.Context, tx *sql.Tx) error {
q := `
UPDATE storage_pools_config
SET key = REPLACE(key, "cephobject.radosgsw.endpoint", "cephobject.radosgw.endpoint")
WHERE key IN ("cephobject.radosgsw.endpoint", "cephobject.radosgsw.endpoint_cert_file")
`
_, err := tx.Exec(q)
if err != nil {
return fmt.Errorf("Failed replacing storage pool config cephobject.radosgsw.endpoint* with cephobject.radosgw.endpoint*: %w", err)
}
return nil
}
// updatefromV64 updates nodes_cluster_groups to include an ID field so that it works well with lxd-generate.
func updateFromV64(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE "nodes_cluster_groups_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
node_id INTEGER NOT NULL,
group_id INTEGER NOT NULL,
FOREIGN KEY (node_id) REFERENCES nodes (id) ON DELETE CASCADE,
FOREIGN KEY (group_id) REFERENCES cluster_groups (id) ON DELETE CASCADE,
UNIQUE (node_id, group_id)
);
INSERT INTO nodes_cluster_groups_new (node_id, group_id)
SELECT node_id, group_id FROM nodes_cluster_groups;
DROP TABLE nodes_cluster_groups;
ALTER TABLE nodes_cluster_groups_new RENAME TO nodes_cluster_groups;
`)
if err != nil {
return fmt.Errorf("Failed altering nodes_cluster_groups table: %w", err)
}
return nil
}
// updateFromV63 creates the storage buckets tables and adds features.storage.buckets=true to all projects that
// have features.storage.volumes=true.
func updateFromV63(ctx context.Context, tx *sql.Tx) error {
// Find all projects that have features.storage.volumes=true and add features.storage.buckets=true.
rows, err := tx.QueryContext(ctx, `SELECT project_id FROM projects_config WHERE key = "features.storage.volumes" AND value = "true"`)
if err != nil {
return fmt.Errorf("Failed getting projects with features.storage.volumes=true: %w", err)
}
defer func() { _ = rows.Close() }()
var projectIDs []int64
for rows.Next() {
var projectID int64
err = rows.Scan(&projectID)
if err != nil {
return fmt.Errorf("Failed scanning project ID row: %w", err)
}
projectIDs = append(projectIDs, projectID)
}
err = rows.Err()
if err != nil {
return fmt.Errorf("Got a row error getting projects with features.storage.volumes=true: %w", err)
}
for _, projectID := range projectIDs {
_, err = tx.Exec(`INSERT OR REPLACE INTO projects_config (project_id,key,value) VALUES(?,?,?);`, projectID, "features.storage.buckets", "true")
if err != nil {
return fmt.Errorf("Failed adding features.storage.buckets=true to projects: %w", err)
}
}
// Create storage buckets tables.
_, err = tx.Exec(`
CREATE TABLE IF NOT EXISTS "storage_buckets" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
storage_pool_id INTEGER NOT NULL,
node_id INTEGER,
description TEXT NOT NULL,
project_id INTEGER NOT NULL,
UNIQUE (node_id, name),
FOREIGN KEY (storage_pool_id) REFERENCES "storage_pools" (id) ON DELETE CASCADE,
FOREIGN KEY (node_id) REFERENCES "nodes" (id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES "projects" (id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX storage_buckets_unique_storage_pool_id_node_id_name ON "storage_buckets" (storage_pool_id, IFNULL(node_id, -1), name);
CREATE TABLE "storage_buckets_config" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_bucket_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (storage_bucket_id, key),
FOREIGN KEY (storage_bucket_id) REFERENCES "storage_buckets" (id) ON DELETE CASCADE
);
CREATE TABLE "storage_buckets_keys" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_bucket_id INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
access_key TEXT NOT NULL,
secret_key TEXT NOT NULL,
role TEXT NOT NULL,
UNIQUE (storage_bucket_id, name),
FOREIGN KEY (storage_bucket_id) REFERENCES "storage_buckets" (id) ON DELETE CASCADE
);
`)
if err != nil {
return fmt.Errorf("Failed adding storage bucket tables: %w", err)
}
return nil
}
// updateFromV62 adds unique index to storage_volumes that prevents duplicate volumes when using remote storage
// pool where the node_id column is NULL.
// Also ensures that the default project has features.networks set to true.
func updateFromV62(ctx context.Context, tx *sql.Tx) error {
// Find the default project ID, and what it has features.networks config key set to (if at all).
rows := tx.QueryRowContext(ctx, `
SELECT
projects.id,
IFNULL(projects_config.key, "") as key,
IFNULL(projects_config.value, "") as value
FROM projects
LEFT JOIN
projects_config ON projects_config.project_id = projects.id
AND projects_config.key = "features.networks"
WHERE projects.name = "default"
`)
var defaultProjectID int64
var featureKey, featureValue string
err := rows.Scan(&defaultProjectID, &featureKey, &featureValue)
if err != nil {
return fmt.Errorf("Failed scanning default project row: %w", err)
}
// If the features.networks key is missing or not set to true, insert/replace the correct row.
if featureKey == "" || featureValue != "true" {
_, err = tx.Exec(`INSERT OR REPLACE INTO projects_config (project_id,key,value) VALUES(?,?,?);`, defaultProjectID, "features.networks", "true")
if err != nil {
return fmt.Errorf("Failed adding features.networks=true to default project: %w", err)
}
}
// Create unique index on storage_volumes that protects against duplicate volumes when using remote
// storage pool where the node_id field is NULL (which the current unique index doesn't protect against).
_, err = tx.Exec(`CREATE UNIQUE INDEX storage_volumes_unique_storage_pool_id_node_id_project_id_name_type ON "storage_volumes" (storage_pool_id, IFNULL(node_id, -1), project_id, name, type);`)
if err != nil {
return fmt.Errorf("Failed adding storage volumes unique index: %w", err)
}
return nil
}
// updateFromV61 converts config value fields to NOT NULL and config key fields to TEXT (from VARCHAR).
func updateFromV61(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE "instances_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
instance_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (instance_id) REFERENCES "instances" (id) ON DELETE CASCADE,
UNIQUE (instance_id, key)
);
INSERT INTO "instances_config_new" SELECT * FROM "instances_config";
DROP TABLE "instances_config";
ALTER TABLE "instances_config_new" RENAME TO "instances_config";
CREATE TABLE "instances_devices_config_new" (
id INTEGER primary key AUTOINCREMENT NOT NULL,
instance_device_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (instance_device_id) REFERENCES "instances_devices" (id) ON DELETE CASCADE,
UNIQUE (instance_device_id, key)
);
INSERT INTO "instances_devices_config_new" SELECT * FROM "instances_devices_config";
DROP TABLE "instances_devices_config";
ALTER TABLE "instances_devices_config_new" RENAME TO "instances_devices_config";
CREATE TABLE "instances_snapshots_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
instance_snapshot_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (instance_snapshot_id) REFERENCES "instances_snapshots" (id) ON DELETE CASCADE,
UNIQUE (instance_snapshot_id, key)
);
INSERT INTO "instances_snapshots_config_new" SELECT * FROM "instances_snapshots_config";
DROP TABLE "instances_snapshots_config";
ALTER TABLE "instances_snapshots_config_new" RENAME TO "instances_snapshots_config";
CREATE TABLE "instances_snapshots_devices_config_new" (
id INTEGER primary key AUTOINCREMENT NOT NULL,
instance_snapshot_device_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (instance_snapshot_device_id) REFERENCES "instances_snapshots_devices" (id) ON DELETE CASCADE,
UNIQUE (instance_snapshot_device_id, key)
);
INSERT INTO "instances_snapshots_devices_config_new" SELECT * FROM "instances_snapshots_devices_config";
DROP TABLE "instances_snapshots_devices_config";
ALTER TABLE "instances_snapshots_devices_config_new" RENAME TO "instances_snapshots_devices_config";
CREATE TABLE "networks_acls_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_acl_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_acl_id, key),
FOREIGN KEY (network_acl_id) REFERENCES "networks_acls" (id) ON DELETE CASCADE
);
INSERT INTO "networks_acls_config_new" SELECT * FROM "networks_acls_config";
DROP TABLE "networks_acls_config";
ALTER TABLE "networks_acls_config_new" RENAME TO "networks_acls_config";
CREATE TABLE "networks_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_id INTEGER NOT NULL,
node_id INTEGER,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_id, node_id, key),
FOREIGN KEY (network_id) REFERENCES "networks" (id) ON DELETE CASCADE,
FOREIGN KEY (node_id) REFERENCES "nodes" (id) ON DELETE CASCADE
);
INSERT INTO "networks_config_new" SELECT * FROM "networks_config";
DROP TABLE "networks_config";
ALTER TABLE "networks_config_new" RENAME TO "networks_config";
CREATE UNIQUE INDEX networks_unique_network_id_node_id_key ON "networks_config" (network_id, IFNULL(node_id, -1), key);
CREATE TABLE "networks_forwards_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_forward_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_forward_id, key),
FOREIGN KEY (network_forward_id) REFERENCES "networks_forwards" (id) ON DELETE CASCADE
);
INSERT INTO "networks_forwards_config_new" SELECT * FROM "networks_forwards_config";
DROP TABLE "networks_forwards_config";
ALTER TABLE "networks_forwards_config_new" RENAME TO "networks_forwards_config";
CREATE TABLE "networks_peers_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_peer_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_peer_id, key),
FOREIGN KEY (network_peer_id) REFERENCES "networks_peers" (id) ON DELETE CASCADE
);
INSERT INTO "networks_peers_config_new" SELECT * FROM "networks_peers_config";
DROP TABLE "networks_peers_config";
ALTER TABLE "networks_peers_config_new" RENAME TO "networks_peers_config";
CREATE TABLE "networks_zones_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_zone_id, key),
FOREIGN KEY (network_zone_id) REFERENCES "networks_zones" (id) ON DELETE CASCADE
);
INSERT INTO "networks_zones_config_new" SELECT * FROM "networks_zones_config";
DROP TABLE "networks_zones_config";
ALTER TABLE "networks_zones_config_new" RENAME TO "networks_zones_config";
CREATE TABLE networks_zones_records_config_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_record_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_zone_record_id, key),
FOREIGN KEY (network_zone_record_id) REFERENCES networks_zones_records (id) ON DELETE CASCADE
);
INSERT INTO "networks_zones_records_config_new" SELECT * FROM "networks_zones_records_config";
DROP TABLE "networks_zones_records_config";
ALTER TABLE "networks_zones_records_config_new" RENAME TO "networks_zones_records_config";
CREATE TABLE "nodes_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
node_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (node_id) REFERENCES "nodes" (id) ON DELETE CASCADE,
UNIQUE (node_id, key)
);
INSERT INTO "nodes_config_new" SELECT * FROM "nodes_config";
DROP TABLE "nodes_config";
ALTER TABLE "nodes_config_new" RENAME TO "nodes_config";
CREATE TABLE "profiles_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (profile_id, key),
FOREIGN KEY (profile_id) REFERENCES "profiles"(id) ON DELETE CASCADE
);
INSERT INTO "profiles_config_new" SELECT * FROM "profiles_config";
DROP TABLE "profiles_config";
ALTER TABLE "profiles_config_new" RENAME TO "profiles_config";
CREATE TABLE "profiles_devices_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
profile_device_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (profile_device_id, key),
FOREIGN KEY (profile_device_id) REFERENCES "profiles_devices" (id) ON DELETE CASCADE
);
INSERT INTO "profiles_devices_config_new" SELECT * FROM "profiles_devices_config";
DROP TABLE "profiles_devices_config";
ALTER TABLE "profiles_devices_config_new" RENAME TO "profiles_devices_config";
CREATE TABLE "projects_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
project_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (project_id) REFERENCES "projects" (id) ON DELETE CASCADE,
UNIQUE (project_id, key)
);
INSERT INTO "projects_config_new" SELECT * FROM "projects_config";
DROP TABLE "projects_config";
ALTER TABLE "projects_config_new" RENAME TO "projects_config";
CREATE TABLE "storage_pools_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_pool_id INTEGER NOT NULL,
node_id INTEGER,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (storage_pool_id, node_id, key),
FOREIGN KEY (storage_pool_id) REFERENCES "storage_pools" (id) ON DELETE CASCADE,
FOREIGN KEY (node_id) REFERENCES "nodes" (id) ON DELETE CASCADE
);
INSERT INTO "storage_pools_config_new" SELECT * FROM "storage_pools_config";
DROP TABLE "storage_pools_config";
ALTER TABLE "storage_pools_config_new" RENAME TO "storage_pools_config";
CREATE UNIQUE INDEX storage_pools_unique_storage_pool_id_node_id_key ON storage_pools_config (storage_pool_id, IFNULL(node_id, -1), key);
CREATE TABLE "storage_volumes_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_volume_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (storage_volume_id, key),
FOREIGN KEY (storage_volume_id) REFERENCES "storage_volumes" (id) ON DELETE CASCADE
);
INSERT INTO "storage_volumes_config_new" SELECT * FROM "storage_volumes_config";
DROP TABLE "storage_volumes_config";
ALTER TABLE "storage_volumes_config_new" RENAME TO "storage_volumes_config";
CREATE TABLE "storage_volumes_snapshots_config_new" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
storage_volume_snapshot_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
FOREIGN KEY (storage_volume_snapshot_id) REFERENCES "storage_volumes_snapshots" (id) ON DELETE CASCADE,
UNIQUE (storage_volume_snapshot_id, key)
);
INSERT INTO "storage_volumes_snapshots_config_new" SELECT * FROM "storage_volumes_snapshots_config";
DROP TABLE "storage_volumes_snapshots_config";
ALTER TABLE "storage_volumes_snapshots_config_new" RENAME TO "storage_volumes_snapshots_config";
`)
if err != nil {
return fmt.Errorf("Failed altering config tables schema: %w", err)
}
return nil
}
// updateFromV60 creates the networks_load_balancers and networks_load_balancers_config tables.
func updateFromV60(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE "networks_load_balancers" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_id INTEGER NOT NULL,
node_id INTEGER,
listen_address TEXT NOT NULL,
description TEXT NOT NULL,
backends TEXT NOT NULL,
ports TEXT NOT NULL,
UNIQUE (network_id, node_id, listen_address),
FOREIGN KEY (network_id) REFERENCES "networks" (id) ON DELETE CASCADE,
FOREIGN KEY (node_id) REFERENCES "nodes" (id) ON DELETE CASCADE
);
CREATE TABLE "networks_load_balancers_config" (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_load_balancer_id INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
UNIQUE (network_load_balancer_id, key),
FOREIGN KEY (network_load_balancer_id) REFERENCES "networks_load_balancers" (id) ON DELETE CASCADE
);
`)
if err != nil {
return fmt.Errorf("Failed creating network load balancers tables: %w", err)
}
return nil
}
func updateFromV59(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE networks_zones_records (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_id INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
entries TEXT NOT NULL,
UNIQUE (name),
FOREIGN KEY (network_zone_id) REFERENCES networks_zones (id) ON DELETE CASCADE
);
CREATE TABLE networks_zones_records_config (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
network_zone_record_id INTEGER NOT NULL,
key VARCHAR(255) NOT NULL,
value TEXT,
UNIQUE (network_zone_record_id, key),
FOREIGN KEY (network_zone_record_id) REFERENCES networks_zones_records (id) ON DELETE CASCADE
);
`)
if err != nil {
return fmt.Errorf("Failed creating network zone records tables: %w", err)
}
return nil
}
func updateFromV58(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
UPDATE sqlite_sequence SET seq = (
SELECT max(
(SELECT coalesce(max(storage_volumes.id), 0) FROM storage_volumes),
(SELECT coalesce(max(storage_volumes_snapshots.id), 0)
FROM storage_volumes_snapshots)))
WHERE name='storage_volumes';
`)
return err
}
func updateFromV57(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
UPDATE sqlite_sequence SET seq = (
SELECT coalesce(max(max(coalesce(storage_volumes.id, 0)), max(coalesce(storage_volumes_snapshots.id, 0))), 0)
FROM storage_volumes, storage_volumes_snapshots)
WHERE name='storage_volumes';
`)
return err
}
func updateFromV56(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
UPDATE sqlite_sequence SET seq = (
SELECT max(max(coalesce(storage_volumes.id, 0)), max(coalesce(storage_volumes_snapshots.id, 0)))
FROM storage_volumes, storage_volumes_snapshots)
WHERE name='storage_volumes';
`)
return err
}
func updateFromV55(ctx context.Context, tx *sql.Tx) error {
_, err := tx.Exec(`
DROP VIEW storage_volumes_all;
CREATE TABLE projects_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
UNIQUE (name)
);
INSERT INTO projects_new (id, name, description) SELECT id, name, IFNULL(description, '') FROM projects;
CREATE TABLE certificates_projects_new (
certificate_id INTEGER NOT NULL,
project_id INTEGER NOT NULL,
FOREIGN KEY (certificate_id) REFERENCES certificates (id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects_new (id) ON DELETE CASCADE,
UNIQUE (certificate_id, project_id)
);
INSERT INTO certificates_projects_new (certificate_id, project_id) SELECT certificate_id, project_id FROM certificates_projects;
CREATE TABLE images_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
fingerprint TEXT NOT NULL,
filename TEXT NOT NULL,
size INTEGER NOT NULL,
public INTEGER NOT NULL DEFAULT 0,
architecture INTEGER NOT NULL,
creation_date DATETIME,
expiry_date DATETIME,
upload_date DATETIME NOT NULL,
cached INTEGER NOT NULL DEFAULT 0,
last_use_date DATETIME,
auto_update INTEGER NOT NULL DEFAULT 0,
project_id INTEGER NOT NULL,
type INTEGER NOT NULL DEFAULT 0,
UNIQUE (project_id, fingerprint),
FOREIGN KEY (project_id) REFERENCES projects_new (id) ON DELETE CASCADE
);
INSERT INTO images_new (id, fingerprint, filename, size, public, architecture, creation_date, expiry_date, upload_date, cached, last_use_date, auto_update, project_id, type)
SELECT id, fingerprint, filename, size, public, architecture, creation_date, expiry_date, upload_date, cached, last_use_date, auto_update, project_id, type FROM images;
CREATE TABLE images_aliases_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
image_id INTEGER NOT NULL,
description TEXT NOT NULL,
project_id INTEGER NOT NULL,
UNIQUE (project_id, name),
FOREIGN KEY (image_id) REFERENCES images_new (id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects_new (id) ON DELETE CASCADE
);
INSERT INTO images_aliases_new (id, name, image_id, description, project_id)
SELECT id, name, image_id, IFNULL(description, ''), project_id FROM images_aliases;
CREATE TABLE nodes_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
address TEXT NOT NULL,
schema INTEGER NOT NULL,
api_extensions INTEGER NOT NULL,
heartbeat DATETIME DEFAULT CURRENT_TIMESTAMP,
state INTEGER NOT NULL DEFAULT 0,
arch INTEGER NOT NULL DEFAULT 0 CHECK (arch > 0),
failure_domain_id INTEGER DEFAULT NULL REFERENCES nodes_failure_domains (id) ON DELETE SET NULL,
UNIQUE (name),
UNIQUE (address)
);
INSERT INTO nodes_new (id, name, description, address, schema, api_extensions, heartbeat, state, arch, failure_domain_id)
SELECT id, name, IFNULL(description, ''), address, schema, api_extensions, heartbeat, state, arch, failure_domain_id FROM nodes;
CREATE TABLE images_nodes_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_id INTEGER NOT NULL,
node_id INTEGER NOT NULL,
UNIQUE (image_id, node_id),
FOREIGN KEY (image_id) REFERENCES images_new (id) ON DELETE CASCADE,
FOREIGN KEY (node_id) REFERENCES nodes_new (id) ON DELETE CASCADE
);
INSERT INTO images_nodes_new (id, image_id, node_id)
SELECT id, image_id, node_id FROM images_nodes;
CREATE TABLE profiles_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name TEXT NOT NULL,
description TEXT NOT NULL,
project_id INTEGER NOT NULL,
UNIQUE (project_id, name),
FOREIGN KEY (project_id) REFERENCES projects_new (id) ON DELETE CASCADE
);
INSERT INTO profiles_new (id, name, description, project_id)
SELECT id, name, IFNULL(description, ''), project_id FROM profiles;
CREATE TABLE images_profiles_new (
image_id INTEGER NOT NULL,
profile_id INTEGER NOT NULL,
FOREIGN KEY (image_id) REFERENCES images_new (id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles_new (id) ON DELETE CASCADE,
UNIQUE (image_id, profile_id)
);
INSERT INTO images_profiles_new (image_id, profile_id)
SELECT image_id, profile_id FROM images_profiles;
CREATE TABLE images_properties_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_id INTEGER NOT NULL,
type INTEGER NOT NULL,
key TEXT NOT NULL,
value TEXT,
FOREIGN KEY (image_id) REFERENCES images_new (id) ON DELETE CASCADE
);
INSERT INTO images_properties_new (id, image_id, type, key, value)
SELECT id, image_id, type, key, value FROM images_properties;
CREATE TABLE images_source_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
image_id INTEGER NOT NULL,
server TEXT NOT NULL,
protocol INTEGER NOT NULL,
certificate TEXT NOT NULL,
alias TEXT NOT NULL,
FOREIGN KEY (image_id) REFERENCES images_new (id) ON DELETE CASCADE
);
INSERT INTO images_source_new (id, image_id, server, protocol, certificate, alias)
SELECT id, image_id, server, protocol, certificate, alias FROM images_source;
CREATE TABLE instances_new (
id INTEGER primary key AUTOINCREMENT NOT NULL,
node_id INTEGER NOT NULL,
name TEXT NOT NULL,
architecture INTEGER NOT NULL,
type INTEGER NOT NULL,
ephemeral INTEGER NOT NULL DEFAULT 0,
creation_date DATETIME NOT NULL DEFAULT 0,
stateful INTEGER NOT NULL DEFAULT 0,
last_use_date DATETIME,
description TEXT NOT NULL,
project_id INTEGER NOT NULL,
expiry_date DATETIME,
UNIQUE (project_id, name),
FOREIGN KEY (node_id) REFERENCES nodes_new (id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects_new (id) ON DELETE CASCADE
);
INSERT INTO instances_new (id, node_id, name, architecture, type, ephemeral, creation_date, stateful, last_use_date, description, project_id, expiry_date)
SELECT id, node_id, name, architecture, type, ephemeral, creation_date, stateful, last_use_date, IFNULL(description, ''), project_id, expiry_date FROM instances;
CREATE TABLE instances_backups_new (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
instance_id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
creation_date DATETIME,
expiry_date DATETIME,
container_only INTEGER NOT NULL default 0,