fleet/infrastructure/dogfood/terraform/gcp/mysql.tf
Roberto Dip 305392e7bb
enforce an uniform collation for all tables (#10515)
related to #10441, inspired by the prior work done in
https://github.com/kolide/fleet/pull/1360, this PR:

1. Adds a migration to use `utf8mb4_general_ci` as the default collation
for the database and all the tables. From [MySQL's documentation][1]:

> To change the table default character set and all character columns
    > (CHAR, VARCHAR, TEXT) to a new character set, use a statement like
    > this:
    >
    > ```
    > ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
    > ```
> The statement also changes the collation of all character columns. If
> you specify no COLLATE clause to indicate which collation to use, the
    > statement uses default collation for the character set.

2. Changes the connection settings to use `utf8mb4_general_ci` as the
default collation, from the [driver docs][2]:

   > Sets the collation used for client-server interaction on
connection. In contrast to charset, collation does not issue additional
queries. If the specified collation is unavailable on the target server,
the connection will fail.

[1]: https://dev.mysql.com/doc/refman/5.7/en/alter-table.html
[2]: https://github.com/go-sql-driver/mysql


**TODO:** discuss how we can enforce this, is setting the database
default collation enough? should we add some kind of custom lint rule to
all migrations?

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2023-03-16 15:49:24 -03:00

48 lines
1.2 KiB
HCL

resource "random_password" "fleet-db-user-pw" {
length = 12
}
module "fleet-mysql" {
source = "GoogleCloudPlatform/sql-db/google//modules/mysql"
version = "9.0.0"
name = "${var.prefix}-mysql"
random_instance_name = true
project_id = var.project_id
deletion_protection = false
additional_users = [
{
name = var.db_user
password = random_password.fleet-db-user-pw.result
host = "% (any host)"
type = "BUILT_IN"
}
]
ip_configuration = {
ipv4_enabled = false
# We never set authorized networks, we need all connections via the
# public IP to be mediated by Cloud SQL.
authorized_networks = []
require_ssl = false
private_network = module.vpc.network_self_link
}
database_version = var.db_version
region = var.region
zone = var.db_zone
tier = var.db_tier
additional_databases = [
{
name = var.db_name
charset = "utf8mb4"
collation = "utf8mb4_unicode_ci"
}
]
// Optional: used to enforce ordering in the creation of resources.
module_depends_on = [module.private-service-access.peering_completed]
}