Skip to content

Commit

Permalink
updated files for DB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
rimashah25 committed Aug 30, 2023
1 parent e8acd59 commit db9b61b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
5 changes: 2 additions & 3 deletions traffic_ops/app/db/SQUASH.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ under the License.

# Squashing database migrations

For convenience, [`squash_migrations.sh`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/squash_migrations.sh) script squashes the migrations, but whoever PRs the result is responsible for verifying that the migrations are squashed and `LastSquashedMigrationVersion` in [`db/admin.go`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/admin.go) is updated correctly, regardless of the result of having run the script.
For convenience, [`squash_migrations.sh`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/squash_migrations.sh) script squashes the migrations, but whoever PRs the result is responsible for verifying that the migrations are squashed, regardless of the result of having run the script.

--------

Expand All @@ -43,8 +43,7 @@ And suppose the ATC [`master`](https://github.com/apache/trafficcontrol/commits/

1. In order to prepare database migrations for the next major release, in this case, ATC 148.0.0, migrations `1` and `3` should be collapsed into `create_tables.sql` and migrations `4` and `9` should remain in [`traffic_ops/app/db/migrations/`](https://github.com/apache/trafficcontrol/tree/master/traffic_ops/app/db/migrations/).

2. * Note that `3` is the migration timestamp of the last up/down migration set. Find the definition for `LastSquashedMigrationTimestamp` in [`traffic_ops/app/db/admin.go`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/admin.go) and change it to `3`.
* After migrations from ATC 147.5.8 have been collapsed, the first migration version will be `4`. Find the definition for `FirstMigrationTimestamp` in [`traffic_ops/app/db/admin.go`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/admin.go) and change it to `4`.
2. * After migrations from ATC 147.5.8 have been collapsed, the first migration version will be `4`. Find the definition for `FirstMigrationTimestamp` in [`traffic_ops/app/db/admin.go`](https://github.com/apache/trafficcontrol/blob/master/traffic_ops/app/db/admin.go) and change it to `4`.

Past PRs that have collapsed the DB migrations:
- https://github.com/apache/trafficcontrol/pull/6065
Expand Down
64 changes: 64 additions & 0 deletions traffic_ops/app/db/seeds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,47 @@
-- cdns
INSERT INTO public.cdn ("name", dnssec_enabled, domain_name) VALUES ('ALL', FALSE, '-') ON CONFLICT ("name") DO NOTHING;

-- cdni_limit
INSERT INTO public.cdni_limits (limit_id,
scope_type,
scope_value,
limit_type,
maximum_hard,
maximum_soft,
telemetry_id,
telemetry_metric,
capability_id)
SELECT CONCAT('host_limit_', chl.limit_type, '_', chl.telemetry_metric),
'published-host',
ARRAY[chl.host],
chl.limit_type,
chl.maximum_hard,
chl.maximum_soft,
chl.telemetry_id,
chl.telemetry_metric,
chl.capability_id
FROM public.cdni_host_limits as chl;

INSERT INTO public.cdni_limits (limit_id,
scope_type,
scope_value,
limit_type,
maximum_hard,
maximum_soft,
telemetry_id,
telemetry_metric,
capability_id)
SELECT CONCAT('total_limit_', thl.limit_type, '_', thl.telemetry_metric),
NULL,
NULL,
thl.limit_type,
thl.maximum_hard,
thl.maximum_soft,
thl.telemetry_id,
thl.telemetry_metric,
thl.capability_id
FROM public.cdni_total_limits as thl;

-- parameters
-- Moved into postinstall global parameters
INSERT INTO public.profile ("name", "description", "type", cdn) VALUES ('GLOBAL', 'Global Traffic Ops profile, DO NOT DELETE', 'UNK_PROFILE', (SELECT id FROM cdn WHERE "name"='ALL')) ON CONFLICT ("name") DO NOTHING;
Expand Down Expand Up @@ -61,6 +102,12 @@ INSERT INTO public.profile ("name", "description", "type", cdn) VALUES ('TRAFFIC
INSERT INTO public.profile ("name", "description", "type", cdn) VALUES ('INFLUXDB', 'InfluxDb profile', 'INFLUXDB_PROFILE', (SELECT id FROM cdn WHERE "name"='ALL')) ON CONFLICT ("name") DO NOTHING;
INSERT INTO public.profile ("name", "description", "type", cdn) VALUES ('RIAK_ALL', 'Riak profile for all CDNs', 'RIAK_PROFILE', (SELECT id FROM cdn WHERE "name"='ALL')) ON CONFLICT ("name") DO NOTHING;

-- server_profile
INSERT into public.server_profile(server, profile_name, priority)
SELECT s.id, p.name, 0
FROM public.server AS s
JOIN public.profile p ON p.id=s.profile;

-- statuses
INSERT INTO public.status ("name", "description") VALUES ('OFFLINE', 'Server is Offline. Not active in any configuration.') ON CONFLICT ("name") DO NOTHING;
INSERT INTO public.status ("name", "description") VALUES ('ONLINE', 'Server is online.') ON CONFLICT ("name") DO NOTHING;
Expand Down Expand Up @@ -97,6 +144,23 @@ FROM public.role
WHERE "name" in ('operations', 'read-only', 'portal', 'federation', 'steering')
ON CONFLICT DO NOTHING;

INSERT INTO public.role_capability
SELECT id, perm FROM public.role
CROSS JOIN (
VALUES ('DNS-SEC:READ'), ('DNS-SEC:DELETE')
) AS perms(perm)
WHERE priv_level >= 30
ON CONFLICT DO NOTHING;

INSERT INTO public.role_capability (role_id, cap_name)
SELECT id, perm
FROM public.role
CROSS JOIN (
VALUES ('DELIVERY-SERVICE-SAFE:UPDATE')
) AS perms(perm)
WHERE "priv_level" < 20 AND "priv_level" > 0
ON CONFLICT DO NOTHING;

-- Using role 'read-only'
INSERT INTO public.role_capability
SELECT id, perm
Expand Down
8 changes: 4 additions & 4 deletions traffic_ops/app/db/squash_migrations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ last_squashed_migration="$(<<<"$migrations_to_squash" tail -n1)"
last_squashed_migration_timestamp="$(<<<"$last_squashed_migration" sed -E 's|migrations/([0-9]+).*|\1|')"
first_migration="$(ls migrations/*.sql | grep -A1 "/${last_squashed_migration_timestamp}_" | tail -n1)"
first_migration_timestamp="$(<<<"$first_migration" sed -E 's|migrations/([0-9]+).*|\1|')"
sed -i '/^--/,$d' create_tables.sql # keeps the Apache License 2.0 header
sed -Ei "s|(LastSquashedMigrationTimestamp\s+uint\s+= ).*|\1${last_squashed_migration_timestamp} // ${last_squashed_migration}|" admin.go
sed -Ei "s|(FirstMigrationTimestamp\s+uint\s+= ).*|\1${first_migration_timestamp} // ${first_migration}|" admin.go
sed -i.bak '/^--/,$d' create_tables.sql # keeps the Apache License 2.0 header
sed -Ei.bak "s|(LastSquashedMigrationTimestamp\s+uint\s+= ).*|\1${last_squashed_migration_timestamp} // ${last_squashed_migration}|" admin.go
sed -Ei.bak "s|(FirstMigrationTimestamp\s+uint\s+= ).*|\1${first_migration_timestamp} // ${first_migration}|" admin.go

dump_db_with_migrations() {
trap 'echo "Error on line ${LINENO} of dump_db_with_migrations" >/dev/stderr; exit 1' ERR
Expand Down Expand Up @@ -55,6 +55,6 @@ echo "$migrations_to_squash" | xargs git rm
git commit -m "Remove migrations that existed at ${last_release}"

git add -p admin.go
git commit -m 'Update LastSquashedMigrationTimestamp and FirstMigrationTimestamp'
#git commit -m 'Update LastSquashedMigrationTimestamp and FirstMigrationTimestamp'

echo 'Migrations squashed successfully!'

0 comments on commit db9b61b

Please sign in to comment.