Skip to content

Commit 7b003c4

Browse files
sl/ydb: fixed database path handling and table creation
1 parent 971b6a0 commit 7b003c4

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed

sql/ydb/driver.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ func opener(ctx context.Context, dsn *url.URL) (*sqlclient.Client, error) {
7474
conn, err := ydbSdk.Connector(
7575
nativeDriver,
7676
ydbSdk.WithAutoDeclare(),
77-
ydbSdk.WithTablePathPrefix(nativeDriver.Name()),
7877
ydbSdk.WithQueryService(true),
7978
)
8079
if err != nil {

sql/ydb/inspect.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"slices"
14+
"strings"
1415

1516
"ariga.io/atlas/sql/internal/sqlx"
1617
"ariga.io/atlas/sql/schema"
@@ -138,7 +139,11 @@ func (i *inspect) inspectTables(
138139
return err
139140
}
140141
for _, table := range schema.Tables {
141-
tableDesc, err := i.tableClient.DescribeTable(ctx, table.Name)
142+
// table.Name is a relative path (e.g., "users" or "dir1/users"),
143+
// but DescribeTable needs the full path (e.g., "/local/users").
144+
// schema.Name contains the database path (e.g., "/local").
145+
fullPath := schema.Name + "/" + table.Name
146+
tableDesc, err := i.tableClient.DescribeTable(ctx, fullPath)
142147
if err != nil {
143148
return fmt.Errorf("ydb: failed describe table: %v", err)
144149
}
@@ -182,12 +187,15 @@ func (i *inspect) tables(ctx context.Context, s *schema.Schema, opts *schema.Ins
182187

183188
switch currEntry.Type {
184189
case scheme.EntryTable:
190+
relativePath := strings.TrimPrefix(currEntry.fullPath, rootPath+"/")
191+
185192
shouldAdd := opts == nil ||
186193
len(opts.Tables) == 0 ||
194+
slices.Contains(opts.Tables, relativePath) ||
187195
slices.Contains(opts.Tables, currEntry.fullPath)
188196

189197
if shouldAdd {
190-
t := schema.NewTable(currEntry.fullPath)
198+
t := schema.NewTable(relativePath)
191199
s.AddTables(t)
192200
}
193201

sql/ydb/inspect_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestInspect_InspectSchema_Simple(t *testing.T) {
144144
require.NoError(t, err)
145145
require.Equal(t, "/local", s.Name)
146146
require.Len(t, s.Tables, 1)
147-
require.Equal(t, "/local/users", s.Tables[0].Name)
147+
require.Equal(t, "users", s.Tables[0].Name)
148148
require.Len(t, s.Tables[0].Columns, 2)
149149
require.NotNil(t, s.Tables[0].PrimaryKey)
150150
require.Equal(t, "PRIMARY", s.Tables[0].PrimaryKey.Name)
@@ -337,8 +337,8 @@ func TestInspect_InspectSchema_NestedDirectories(t *testing.T) {
337337
require.Len(t, s.Tables, 2)
338338

339339
tableNames := []string{s.Tables[0].Name, s.Tables[1].Name}
340-
require.Contains(t, tableNames, "/local/users")
341-
require.Contains(t, tableNames, "/local/app/settings")
340+
require.Contains(t, tableNames, "users")
341+
require.Contains(t, tableNames, "app/settings")
342342
}
343343

344344
func TestInspect_InspectRealm(t *testing.T) {
@@ -564,15 +564,15 @@ func TestInspect_TableFilter(t *testing.T) {
564564
ctx := context.Background()
565565

566566
s, err := insp.InspectSchema(ctx, "/local", &schema.InspectOptions{
567-
Tables: []string{"/local/users", "/local/orders"},
567+
Tables: []string{"users", "orders"},
568568
})
569569
require.NoError(t, err)
570570
require.Len(t, s.Tables, 2)
571571

572572
tableNames := []string{s.Tables[0].Name, s.Tables[1].Name}
573-
require.Contains(t, tableNames, "/local/users")
574-
require.Contains(t, tableNames, "/local/orders")
575-
require.NotContains(t, tableNames, "/local/products")
573+
require.Contains(t, tableNames, "users")
574+
require.Contains(t, tableNames, "orders")
575+
require.NotContains(t, tableNames, "products")
576576
}
577577

578578
func TestInspect_EmptySchema(t *testing.T) {
@@ -695,7 +695,7 @@ func TestInspect_DeeplyNestedDirectories(t *testing.T) {
695695
s, err := insp.InspectSchema(ctx, "/local", nil)
696696
require.NoError(t, err)
697697
require.Len(t, s.Tables, 1)
698-
require.Equal(t, "/local/level1/level2/deep_table", s.Tables[0].Name)
698+
require.Equal(t, "level1/level2/deep_table", s.Tables[0].Name)
699699
}
700700

701701
func TestInspect_MixedEntryTypes(t *testing.T) {
@@ -742,8 +742,8 @@ func TestInspect_MixedEntryTypes(t *testing.T) {
742742
require.Len(t, s.Tables, 2)
743743

744744
tableNames := []string{s.Tables[0].Name, s.Tables[1].Name}
745-
require.Contains(t, tableNames, "/local/users")
746-
require.Contains(t, tableNames, "/local/subdir/orders")
745+
require.Contains(t, tableNames, "users")
746+
require.Contains(t, tableNames, "subdir/orders")
747747
}
748748

749749
func TestInspect_ColumnTypeRawValue(t *testing.T) {

sql/ydb/migrate.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,24 @@ func (s *state) plan(changes []schema.Change) error {
9797
return nil
9898
}
9999

100+
// tablePath returns the full YDB path for a table.
101+
func (s *state) tablePath(t *schema.Table) string {
102+
if s.database == "" {
103+
return t.Name
104+
}
105+
return s.database + "/" + t.Name
106+
}
107+
100108
// addTable builds and executes the query for creating a table in a schema.
101109
func (s *state) addTable(addTable *schema.AddTable) error {
102110
var errs []string
103111
builder := s.Build("CREATE TABLE")
104112

105-
builder.Table(addTable.T)
113+
if sqlx.Has(addTable.Extra, &schema.IfNotExists{}) {
114+
builder.P("IF NOT EXISTS")
115+
}
116+
117+
builder.Ident(s.tablePath(addTable.T))
106118
builder.WrapIndent(func(b *sqlx.Builder) {
107119
b.MapIndent(addTable.T.Columns, func(i int, b *sqlx.Builder) {
108120
if err := s.column(b, addTable.T.Columns[i]); err != nil {
@@ -127,7 +139,7 @@ func (s *state) addTable(addTable *schema.AddTable) error {
127139
}
128140

129141
reverse := s.Build("DROP TABLE").
130-
Table(addTable.T).
142+
Ident(s.tablePath(addTable.T)).
131143
String()
132144

133145
s.append(&migrate.Change{
@@ -154,7 +166,7 @@ func (s *state) dropTable(drop *schema.DropTable) error {
154166
if sqlx.Has(drop.Extra, &schema.IfExists{}) {
155167
builder.P("IF EXISTS")
156168
}
157-
builder.Table(drop.T)
169+
builder.Ident(s.tablePath(drop.T))
158170

159171
// The reverse of 'DROP TABLE' might be a multi-statement operation
160172
reverse := func() any {
@@ -235,7 +247,7 @@ func (s *state) alterTable(t *schema.Table, changes []schema.Change) error {
235247
var reverse []schema.Change
236248

237249
buildFunc := func(changes []schema.Change) (string, error) {
238-
b := s.Build("ALTER TABLE").Table(t)
250+
b := s.Build("ALTER TABLE").Ident(s.tablePath(t))
239251

240252
err := b.MapCommaErr(changes, func(i int, builder *sqlx.Builder) error {
241253
switch change := changes[i].(type) {
@@ -291,7 +303,7 @@ func (s *state) addIndexes(src schema.Change, t *schema.Table, indexes ...*schem
291303
hasAttrs := sqlx.Has(index.Attrs, &indexAttrs)
292304

293305
builder := s.Build("ALTER TABLE").
294-
Table(t).
306+
Ident(s.tablePath(t)).
295307
P("ADD INDEX").
296308
Ident(index.Name).
297309
P("GLOBAL")
@@ -316,7 +328,7 @@ func (s *state) addIndexes(src schema.Change, t *schema.Table, indexes ...*schem
316328
}
317329

318330
reverseOp := s.Build("ALTER TABLE").
319-
Table(t).
331+
Ident(s.tablePath(t)).
320332
P("DROP INDEX").
321333
Ident(index.Name).
322334
String()
@@ -359,8 +371,8 @@ func (s *state) renameTable(c *schema.RenameTable) {
359371
s.append(&migrate.Change{
360372
Source: c,
361373
Comment: fmt.Sprintf("rename a table from %q to %q", c.From.Name, c.To.Name),
362-
Cmd: s.Build("ALTER TABLE").Table(c.From).P("RENAME TO").Table(c.To).String(),
363-
Reverse: s.Build("ALTER TABLE").Table(c.To).P("RENAME TO").Table(c.From).String(),
374+
Cmd: s.Build("ALTER TABLE").Ident(s.tablePath(c.From)).P("RENAME TO").Ident(s.tablePath(c.To)).String(),
375+
Reverse: s.Build("ALTER TABLE").Ident(s.tablePath(c.To)).P("RENAME TO").Ident(s.tablePath(c.From)).String(),
364376
})
365377
}
366378

@@ -369,8 +381,8 @@ func (s *state) renameIndex(modify *schema.ModifyTable, c *schema.RenameIndex) {
369381
s.append(&migrate.Change{
370382
Source: c,
371383
Comment: fmt.Sprintf("rename an index from %q to %q", c.From.Name, c.To.Name),
372-
Cmd: s.Build("ALTER TABLE").Table(modify.T).P("RENAME INDEX").Ident(c.From.Name).P("TO").Ident(c.To.Name).String(),
373-
Reverse: s.Build("ALTER TABLE").Table(modify.T).P("RENAME INDEX").Ident(c.To.Name).P("TO").Ident(c.From.Name).String(),
384+
Cmd: s.Build("ALTER TABLE").Ident(s.tablePath(modify.T)).P("RENAME INDEX").Ident(c.From.Name).P("TO").Ident(c.To.Name).String(),
385+
Reverse: s.Build("ALTER TABLE").Ident(s.tablePath(modify.T)).P("RENAME INDEX").Ident(c.To.Name).P("TO").Ident(c.From.Name).String(),
374386
})
375387
}
376388

sql/ydb/migrate_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,32 @@ func TestPlanChanges_AddTable(t *testing.T) {
140140
},
141141
wantErr: true,
142142
},
143+
{
144+
name: "create table if not exists",
145+
changes: []schema.Change{
146+
&schema.AddTable{
147+
T: schema.NewTable("users").
148+
AddColumns(
149+
schema.NewColumn("id").SetType(&schema.IntegerType{T: TypeInt64}),
150+
schema.NewColumn("name").SetType(&schema.StringType{T: TypeUtf8}),
151+
).
152+
SetPrimaryKey(schema.NewPrimaryKey(
153+
schema.NewColumn("id").SetType(&schema.IntegerType{T: TypeInt64}),
154+
)),
155+
Extra: []schema.Clause{&schema.IfNotExists{}},
156+
},
157+
},
158+
wantPlan: &migrate.Plan{
159+
Transactional: false,
160+
Changes: []*migrate.Change{
161+
{
162+
Cmd: "CREATE TABLE IF NOT EXISTS `users` (`id` int64 NOT NULL, `name` utf8 NOT NULL, PRIMARY KEY (`id`))",
163+
Reverse: "DROP TABLE `users`",
164+
Comment: `create "users" table`,
165+
},
166+
},
167+
},
168+
},
143169
{
144170
name: "table with various types",
145171
changes: []schema.Change{

0 commit comments

Comments
 (0)