Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge all sql files into one #4

Merged
merged 3 commits into from
Dec 11, 2023
Merged

Merge all sql files into one #4

merged 3 commits into from
Dec 11, 2023

Conversation

Alva8756
Copy link
Contributor

@Alva8756 Alva8756 commented Dec 8, 2023

CREATE TABLE is generated from server service[1].

make test-database can generate same tables on crdb-v23.1.11 successfully[2].

Tested with add-server from sandbox:

  • create server
  • create credentials
  • create attributes

Made some manual changes:

  1. Add INSERT INTO server_credential_types(...) to match existing INSERT. Otherwise we will get sql: no rows in result set when create credentials.
  2. Delete -- Validate foreign key constraints sections from [1]. It reports errors but it doesn't affect generated tables. We may want to add it back in the future.

[1]

root@:26257/serverservice_test> show tables;
  schema_name |         table_name         | type  | owner | estimated_row_count | locality
--------------+----------------------------+-------+-------+---------------------+-----------
  public      | aoc_mac_address            | table | root  |                   0 | NULL
  public      | attributes                 | table | root  |                   0 | NULL
  public      | attributes_firmware_set    | table | root  |                   0 | NULL
  public      | bmc_mac_address            | table | root  |                   0 | NULL
  public      | bom_info                   | table | root  |                   0 | NULL
  public      | component_firmware_set     | table | root  |                   0 | NULL
  public      | component_firmware_set_map | table | root  |                   0 | NULL
  public      | component_firmware_version | table | root  |                   0 | NULL
  public      | goose_db_version           | table | root  |                  22 | NULL
  public      | server_component_types     | table | root  |                   0 | NULL
  public      | server_components          | table | root  |                   0 | NULL
  public      | server_credential_types    | table | root  |                   1 | NULL
  public      | server_credentials         | table | root  |                   0 | NULL
  public      | servers                    | table | root  |                   0 | NULL
  public      | versioned_attributes       | table | root  |                   0 | NULL
(15 rows)

root@:26257/serverservice_test> SHOW CREATE ALL TABLES;
                                                                                              create_statement
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  CREATE TABLE public.goose_db_version (
      id INT8 NOT NULL DEFAULT unique_rowid(),
      version_id INT8 NOT NULL,
      is_applied BOOL NOT NULL,
      tstamp TIMESTAMP NULL DEFAULT now():::TIMESTAMP,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      FAMILY "primary" (id, version_id, is_applied, tstamp)
  );
  CREATE TABLE public.servers (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      name STRING NULL,
      facility_code STRING NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      deleted_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      INDEX idx_facility (facility_code ASC),
      FAMILY "primary" (id, name, facility_code, created_at, updated_at, deleted_at)
  );
  CREATE TABLE public.server_component_types (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      name STRING NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      slug STRING NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX idx_name (name ASC),
      UNIQUE INDEX server_component_types_slug_key (slug ASC),
      FAMILY "primary" (id, name, created_at, updated_at, slug)
  );
  CREATE TABLE public.server_components (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      name STRING NULL,
      vendor STRING NULL,
      model STRING NULL,
      serial STRING NULL,
      server_component_type_id UUID NOT NULL,
      server_id UUID NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      INDEX idx_server_component_type_id (server_component_type_id ASC),
      INDEX idx_server_id (server_id ASC),
      UNIQUE INDEX idx_server_components (server_id ASC, serial ASC, server_component_type_id ASC) WHERE ((server_id IS NOT NULL) AND (serial IS NOT NULL)) AND (server_component_type_id IS NOT NULL),
      FAMILY "primary" (id, name, vendor, model, serial, server_component_type_id, server_id, created_at, updated_at)
  );
  CREATE TABLE public.versioned_attributes (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      server_id UUID NULL,
      namespace STRING NOT NULL,
      data JSONB NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      server_component_id UUID NULL,
      tally INT8 NOT NULL DEFAULT 0:::INT8,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      INDEX idx_server_id (server_id ASC) WHERE server_id IS NOT NULL,
      INDEX idx_server_namespace (server_id ASC, namespace ASC, created_at ASC) WHERE server_id IS NOT NULL,
      INVERTED INDEX idx_server_data (server_id, namespace, data) WHERE server_id IS NOT NULL,
      INDEX idx_server_component_id (server_component_id ASC) WHERE server_component_id IS NOT NULL,
      INDEX idx_server_component_namespace (server_component_id ASC, namespace ASC, created_at ASC) WHERE server_component_id IS NOT NULL,
      INVERTED INDEX idx_server_component_data (server_component_id, namespace, data) WHERE server_component_id IS NOT NULL,
      FAMILY "primary" (id, server_id, namespace, data, created_at, updated_at, server_component_id, tally)
  );
  CREATE TABLE public.attributes (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      server_id UUID NULL,
      server_component_id UUID NULL,
      namespace STRING NOT NULL,
      data JSONB NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      INDEX idx_server_id (server_id ASC) WHERE server_id IS NOT NULL,
      UNIQUE INDEX idx_server_namespace (server_id ASC, namespace ASC) WHERE server_id IS NOT NULL,
      INVERTED INDEX idx_server_data (server_id, namespace, data) WHERE server_id IS NOT NULL,
      INDEX idx_server_component_id (server_component_id ASC) WHERE server_component_id IS NOT NULL,
      UNIQUE INDEX idx_server_component_namespace (server_component_id ASC, namespace ASC) WHERE server_component_id IS NOT NULL,
      INVERTED INDEX idx_server_component_data (server_component_id, namespace, data) WHERE server_component_id IS NOT NULL,
      FAMILY "primary" (id, server_id, server_component_id, namespace, data, created_at, updated_at)
  );
  CREATE TABLE public.component_firmware_version (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      component STRING NOT NULL,
      vendor STRING NOT NULL,
      model STRING[] NOT NULL,
      filename STRING NOT NULL,
      version STRING NOT NULL,
      checksum STRING NOT NULL,
      upstream_url STRING NOT NULL,
      repository_url STRING NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX vendor_component_version_filename_unique (vendor ASC, component ASC, version ASC, filename ASC),
      FAMILY "primary" (id, component, vendor, model, filename, version, checksum, upstream_url, repository_url, created_at, updated_at)
  );
  CREATE TABLE public.server_credential_types (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      name STRING NOT NULL,
      slug STRING NOT NULL,
      builtin BOOL NOT NULL DEFAULT false,
      created_at TIMESTAMPTZ NOT NULL,
      updated_at TIMESTAMPTZ NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX server_secret_types_slug_key (slug ASC),
      FAMILY "primary" (id, name, slug, builtin, created_at, updated_at)
  );
  CREATE TABLE public.server_credentials (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      server_id UUID NOT NULL,
      server_credential_type_id UUID NOT NULL,
      password STRING NOT NULL,
      created_at TIMESTAMPTZ NOT NULL,
      updated_at TIMESTAMPTZ NOT NULL,
      username STRING NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX idx_server_secrets_by_type (server_id ASC, server_credential_type_id ASC),
      FAMILY "primary" (id, server_id, server_credential_type_id, password, created_at, updated_at, username)
  );
  CREATE TABLE public.component_firmware_set (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      name STRING NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX idx_name (name ASC),
      FAMILY "primary" (id, name, created_at, updated_at)
  );
  CREATE TABLE public.component_firmware_set_map (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      firmware_set_id UUID NOT NULL,
      firmware_id UUID NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      UNIQUE INDEX component_firmware_set_map_firmware_set_id_firmware_id_key (firmware_set_id ASC, firmware_id ASC),
      FAMILY "primary" (id, firmware_set_id, firmware_id)
  );
  CREATE TABLE public.attributes_firmware_set (
      id UUID NOT NULL DEFAULT gen_random_uuid(),
      firmware_set_id UUID NULL,
      namespace STRING NOT NULL,
      data JSONB NOT NULL,
      created_at TIMESTAMPTZ NULL,
      updated_at TIMESTAMPTZ NULL,
      CONSTRAINT "primary" PRIMARY KEY (id ASC),
      INDEX idx_firmware_set_id (firmware_set_id ASC) WHERE firmware_set_id IS NOT NULL,
      INVERTED INDEX idx_firmware_set_data (firmware_set_id, namespace, data) WHERE firmware_set_id IS NOT NULL,
      UNIQUE INDEX idx_firmware_set_namespace (firmware_set_id ASC, namespace ASC) WHERE firmware_set_id IS NOT NULL,
      FAMILY "primary" (id, firmware_set_id, namespace, data, created_at, updated_at)
  );
  CREATE TABLE public.bom_info (
      serial_num STRING NOT NULL,
      aoc_mac_address STRING NULL,
      bmc_mac_address STRING NULL,
      num_defi_pmi STRING NULL,
      num_def_pwd STRING NULL,
      metro STRING NULL,
      CONSTRAINT "primary" PRIMARY KEY (serial_num ASC),
      FAMILY "primary" (serial_num, aoc_mac_address, bmc_mac_address, num_defi_pmi, num_def_pwd, metro)
  );
  CREATE TABLE public.aoc_mac_address (
      aoc_mac_address STRING NOT NULL,
      serial_num STRING NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (aoc_mac_address ASC),
      FAMILY "primary" (aoc_mac_address, serial_num)
  );
  CREATE TABLE public.bmc_mac_address (
      bmc_mac_address STRING NOT NULL,
      serial_num STRING NOT NULL,
      CONSTRAINT "primary" PRIMARY KEY (bmc_mac_address ASC),
      FAMILY "primary" (bmc_mac_address, serial_num)
  );
  ALTER TABLE public.server_components ADD CONSTRAINT fk_server_component_type_id_ref_server_component_types FOREIGN KEY (server_component_type_id) REFERENCES public.server_component_types(id);
  ALTER TABLE public.server_components ADD CONSTRAINT fk_server_id_ref_servers FOREIGN KEY (server_id) REFERENCES public.servers(id) ON DELETE CASCADE;
  ALTER TABLE public.versioned_attributes ADD CONSTRAINT fk_server_id_ref_servers FOREIGN KEY (server_id) REFERENCES public.servers(id) ON DELETE CASCADE;
  ALTER TABLE public.versioned_attributes ADD CONSTRAINT fk_server_component_id_ref_server_components FOREIGN KEY (server_component_id) REFERENCES public.server_components(id) ON DELETE CASCADE;
  ALTER TABLE public.attributes ADD CONSTRAINT fk_server_id_ref_servers FOREIGN KEY (server_id) REFERENCES public.servers(id) ON DELETE CASCADE;
  ALTER TABLE public.attributes ADD CONSTRAINT fk_server_component_id_ref_server_components FOREIGN KEY (server_component_id) REFERENCES public.server_components(id) ON DELETE CASCADE;
  ALTER TABLE public.server_credentials ADD CONSTRAINT fk_server_id_ref_servers FOREIGN KEY (server_id) REFERENCES public.servers(id) ON DELETE CASCADE;
  ALTER TABLE public.server_credentials ADD CONSTRAINT fk_server_secret_type_id_ref_server_secret_types FOREIGN KEY (server_credential_type_id) REFERENCES public.server_credential_types(id);
  ALTER TABLE public.component_firmware_set_map ADD CONSTRAINT fk_firmware_set_id_ref_component_firmware_set FOREIGN KEY (firmware_set_id) REFERENCES public.component_firmware_set(id) ON DELETE CASCADE;
  ALTER TABLE public.component_firmware_set_map ADD CONSTRAINT fk_firmware_id_ref_component_firmware_version FOREIGN KEY (firmware_id) REFERENCES public.component_firmware_version(id) ON DELETE RESTRICT;
  ALTER TABLE public.attributes_firmware_set ADD CONSTRAINT fk_firmware_set_id_ref_component_firmware_set FOREIGN KEY (firmware_set_id) REFERENCES public.component_firmware_set(id) ON DELETE CASCADE;
  ALTER TABLE public.aoc_mac_address ADD CONSTRAINT fk_serial_num_ref_bom_info FOREIGN KEY (serial_num) REFERENCES public.bom_info(serial_num) ON DELETE CASCADE;
  ALTER TABLE public.bmc_mac_address ADD CONSTRAINT fk_serial_num_ref_bom_info FOREIGN KEY (serial_num) REFERENCES public.bom_info(serial_num) ON DELETE CASCADE;
  -- Validate foreign key constraints. These can fail if there was unvalidated data during the SHOW CREATE ALL TABLES
  ALTER TABLE public.server_components VALIDATE CONSTRAINT fk_server_component_type_id_ref_server_component_types;
  ALTER TABLE public.server_components VALIDATE CONSTRAINT fk_server_id_ref_servers;
  ALTER TABLE public.versioned_attributes VALIDATE CONSTRAINT fk_server_id_ref_servers;
  ALTER TABLE public.versioned_attributes VALIDATE CONSTRAINT fk_server_component_id_ref_server_components;
  ALTER TABLE public.attributes VALIDATE CONSTRAINT fk_server_id_ref_servers;
  ALTER TABLE public.attributes VALIDATE CONSTRAINT fk_server_component_id_ref_server_components;
  ALTER TABLE public.server_credentials VALIDATE CONSTRAINT fk_server_id_ref_servers;
  ALTER TABLE public.server_credentials VALIDATE CONSTRAINT fk_server_secret_type_id_ref_server_secret_types;
  ALTER TABLE public.component_firmware_set_map VALIDATE CONSTRAINT fk_firmware_set_id_ref_component_firmware_set;
  ALTER TABLE public.component_firmware_set_map VALIDATE CONSTRAINT fk_firmware_id_ref_component_firmware_version;
  ALTER TABLE public.attributes_firmware_set VALIDATE CONSTRAINT fk_firmware_set_id_ref_component_firmware_set;
  ALTER TABLE public.aoc_mac_address VALIDATE CONSTRAINT fk_serial_num_ref_bom_info;
  ALTER TABLE public.bmc_mac_address VALIDATE CONSTRAINT fk_serial_num_ref_bom_info;
(42 rows)

[2]

serverservice % docker exec -it cockroachdb ./cockroach sql --insecure -d serverservice_test
#
# Welcome to the CockroachDB SQL shell.
# All statements must be terminated by a semicolon.
# To exit, type: \q.
#
# Server version: CockroachDB CCL v23.1.11 (aarch64-unknown-linux-gnu, built 2023/09/27 02:36:23, go1.19.10) (same version as client)
# Cluster ID: 95807384-80ce-49e6-a275-9bc8f0025c68
#
# Enter \? for a brief introduction.
#
root@localhost:26257/serverservice_test> show tables;
  schema_name |         table_name         | type  | owner | estimated_row_count | locality
--------------+----------------------------+-------+-------+---------------------+-----------
  public      | aoc_mac_address            | table | root  |                   0 | NULL
  public      | attributes                 | table | root  |                   0 | NULL
  public      | attributes_firmware_set    | table | root  |                   0 | NULL
  public      | bmc_mac_address            | table | root  |                   0 | NULL
  public      | bom_info                   | table | root  |                   0 | NULL
  public      | component_firmware_set     | table | root  |                   0 | NULL
  public      | component_firmware_set_map | table | root  |                   0 | NULL
  public      | component_firmware_version | table | root  |                   0 | NULL
  public      | goose_db_version           | table | root  |                   0 | NULL
  public      | server_component_types     | table | root  |                   0 | NULL
  public      | server_components          | table | root  |                   0 | NULL
  public      | server_credential_types    | table | root  |                   0 | NULL
  public      | server_credentials         | table | root  |                   0 | NULL
  public      | servers                    | table | root  |                   0 | NULL
  public      | versioned_attributes       | table | root  |                   0 | NULL
(15 rows)

@Alva8756 Alva8756 changed the title merge all sql files into one [WIP]merge all sql files into one Dec 8, 2023
@Alva8756 Alva8756 changed the title [WIP]merge all sql files into one Merge all sql files into one Dec 11, 2023
Copy link

codecov bot commented Dec 11, 2023

Welcome to Codecov 🎉

Once merged to your default branch, Codecov will compare your coverage reports and display the results in this comment.

Thanks for integrating Codecov - We've got you covered ☂️

@DoctorVin
Copy link
Contributor

This looks really good. Am I reading this correctly when you say you can use this as a drop-in replacement for server-service in the sandbox? If that's correct, that's awesome. If not it's the logical next step so we can stop using server-service in the sandbox.

@Alva8756 Alva8756 merged commit 2144f38 into main Dec 11, 2023
3 checks passed
@Alva8756
Copy link
Contributor Author

To answer: https://github.com/metal-toolbox/fleetdb/pull/4/commits#issuecomment-1850889369
@DoctorVin
I tested with integration tests and add-server in sandbox.
I will create a new fleetdb pod, once that works,
we can delete the server service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants