1+ // Copyright 2025 The Kubeflow Authors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // https://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ // This file is a minimal proof-of-concept (PoC) to verify that GORM v2's basic APIs
16+ // (Open, AutoMigrate, Create, Find, Update, Delete) work correctly with MySQL.
17+ // The goal is to ensure functional compatibility before integrating GORM v2 into the main codebase.
18+
19+ package main
20+
21+ import (
22+ "fmt"
23+
24+ "gorm.io/driver/mysql"
25+ "gorm.io/gorm"
26+ )
27+
28+ // Define model
29+ type User struct {
30+ ID uint `gorm:"primaryKey"`
31+ Name string
32+ Email string
33+ }
34+
35+ func main () {
36+ // Initialize GORM v2 with MySQL
37+ dsn := "testuser:testpw@tcp(127.0.0.1:3306)/testdb?charset=utf8mb4&parseTime=True&loc=Local"
38+ db , err := gorm .Open (mysql .Open (dsn ), & gorm.Config {})
39+ if err != nil {
40+ panic ("failed to connect database" )
41+ }
42+
43+ // Auto-migrate table schema
44+ if err := db .AutoMigrate (& User {}); err != nil {
45+ panic ("failed to migrate" )
46+ }
47+
48+ // Insert data
49+ db .
Create (
& User {
Name :
"Alice" ,
Email :
"[email protected] " })
50+
51+ // Query data
52+ var user User
53+ db .First (& user , "name = ?" , "Alice" )
54+ fmt .Printf ("User found: %+v\n " , user )
55+
56+ // Find all users
57+ var users []User
58+ db .Find (& users )
59+ fmt .Printf ("All users: %+v\n " , users )
60+
61+ // Update user email
62+ db .
Model (
& user ).
Update (
"Email" ,
"[email protected] " )
63+ fmt .Printf ("Updated user: %+v\n " , user )
64+
65+ // Delete user
66+ db .Delete (& user )
67+ fmt .Println ("User deleted." )
68+
69+ // Verify deletion
70+ var count int64
71+ db .Model (& User {}).Count (& count )
72+ fmt .Printf ("Remaining users count: %d\n " , count )
73+
74+ // Raw SQL: Show tables in MySQL
75+ var tableNames []string
76+ db .Raw ("SHOW TABLES" ).Scan (& tableNames )
77+ fmt .Printf ("Tables in DB (via Raw): %v\n " , tableNames )
78+ }
0 commit comments