let () =
  let dbh = PGOCaml.connect () in
  (* Define DDL statements for each table *)
  let ddl_statements = [
    "CREATE TABLE IF NOT EXISTS Databases (
      id SERIAL PRIMARY KEY,
      name VARCHAR(255) NOT NULL,
      description TEXT,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW()
    )";

    "CREATE TABLE IF NOT EXISTS Schemas (
      id SERIAL PRIMARY KEY,
      database_id INT NOT NULL,
      name VARCHAR(255) NOT NULL,
      description TEXT,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW(),
      FOREIGN KEY (database_id) REFERENCES Databases (id) ON DELETE CASCADE
    )";

    "CREATE TABLE IF NOT EXISTS Tables (
      id SERIAL PRIMARY KEY,
      schema_id INT NOT NULL,
      name VARCHAR(255) NOT NULL,
      description TEXT,
      is_partitioned BOOLEAN DEFAULT FALSE,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW(),
      FOREIGN KEY (schema_id) REFERENCES Schemas (id) ON DELETE CASCADE
    )";

    "CREATE TABLE IF NOT EXISTS Columns (
      id SERIAL PRIMARY KEY,
      table_id INT NOT NULL,
      name VARCHAR(255) NOT NULL,
      data_type VARCHAR(50) NOT NULL,
      is_nullable BOOLEAN DEFAULT TRUE,
      description TEXT,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW(),
      FOREIGN KEY (table_id) REFERENCES Tables (id) ON DELETE CASCADE
    )";

    "CREATE TABLE IF NOT EXISTS Partitions (
      id SERIAL PRIMARY KEY,
      table_id INT NOT NULL,
      partition_key VARCHAR(255) NOT NULL,
      partition_value VARCHAR(255) NOT NULL,
      created_at TIMESTAMP DEFAULT NOW(),
      FOREIGN KEY (table_id) REFERENCES Tables (id) ON DELETE CASCADE
    )";

    "CREATE TABLE IF NOT EXISTS Users (
      id SERIAL PRIMARY KEY,
      username VARCHAR(255) UNIQUE NOT NULL,
      email VARCHAR(255) UNIQUE NOT NULL,
      role VARCHAR(50) DEFAULT 'user',
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW()
    )";

    "CREATE TABLE IF NOT EXISTS Permissions (
      id SERIAL PRIMARY KEY,
      user_id INT NOT NULL,
      resource_type VARCHAR(50) NOT NULL,
      resource_id INT NOT NULL,
      access_type VARCHAR(50) NOT NULL,
      granted_at TIMESTAMP DEFAULT NOW(),
      FOREIGN KEY (user_id) REFERENCES Users (id) ON DELETE CASCADE
    )";
  ] in

  (* Execute each DDL statement *)
  let execute_ddl dbh ddl =
    [%pgsql dbh "execute" "CREATE TABLE IF NOT EXISTS Databases (
      id SERIAL PRIMARY KEY,
      name VARCHAR(255) NOT NULL,
      description TEXT,
      created_at TIMESTAMP DEFAULT NOW(),
      updated_at TIMESTAMP DEFAULT NOW()
    )"]
  in

  let () =
    try
      List.iter (execute_ddl dbh) ddl_statements;
      Printf.printf "Database initialized successfully.\n"
    with
    | PGOCaml.Error err ->
        Printf.eprintf "Error initializing database: %s\n" (PGOCaml.string_of_error err)
    | exn ->
        Printf.eprintf "Unexpected error: %s\n" (Printexc.to_string exn)
  in

  (* Close the connection *)
  PGOCaml.close dbh

