dialect/sql/schema: universl id allocation support

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/9

Reviewed By: alexsn

Differential Revision: D16252229

fbshipit-source-id: 795b6556d322e5c1ff5fb826c3b06ba5421ac857
This commit is contained in:
Ariel Mashraki
2019-07-15 02:26:54 -07:00
committed by Facebook Github Bot
parent ad051e6d72
commit b5cdb810b8
57 changed files with 4164 additions and 964 deletions

View File

@@ -16,6 +16,7 @@ func TestSQLite_Create(t *testing.T) {
tests := []struct {
name string
tables []*Table
options []MigrateOption
before func(sqlmock.Sqlmock)
wantErr bool
}{
@@ -125,14 +126,112 @@ func TestSQLite_Create(t *testing.T) {
mock.ExpectCommit()
},
},
{
name: "universal id for all tables",
tables: []*Table{
NewTable("users").AddPrimary(&Column{Name: "id", Type: field.TypeInt, Increment: true}),
NewTable("groups").AddPrimary(&Column{Name: "id", Type: field.TypeInt, Increment: true}),
},
options: []MigrateOption{WithGlobalUniqueID(true)},
before: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectQuery("PRAGMA foreign_keys").
WillReturnRows(sqlmock.NewRows([]string{"foreign_keys"}).AddRow(1))
// creating ent_types table.
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "ent_types").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("CREATE TABLE `ent_types`(`id` integer PRIMARY KEY AUTOINCREMENT, `type` varchar(255) UNIQUE)")).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "users").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("CREATE TABLE `users`(`id` integer PRIMARY KEY AUTOINCREMENT)")).
WillReturnResult(sqlmock.NewResult(0, 1))
// set users id range.
mock.ExpectExec(escape("INSERT INTO `ent_types` (`type`) VALUES (?)")).
WithArgs("users").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_sequence` WHERE `name` = ?")).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("INSERT INTO `sqlite_sequence` (`name`, `seq`) VALUES (?, ?)")).
WithArgs("users", 0).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "groups").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("CREATE TABLE `groups`(`id` integer PRIMARY KEY AUTOINCREMENT)")).
WillReturnResult(sqlmock.NewResult(0, 1))
// set groups id range.
mock.ExpectExec(escape("INSERT INTO `ent_types` (`type`) VALUES (?)")).
WithArgs("groups").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_sequence` WHERE `name` = ?")).
WithArgs("groups").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("INSERT INTO `sqlite_sequence` (`name`, `seq`) VALUES (?, ?)")).
WithArgs("groups", 1<<32).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},
},
{
name: "universal id for restored tables",
tables: []*Table{
NewTable("users").AddPrimary(&Column{Name: "id", Type: field.TypeInt, Increment: true}),
NewTable("groups").AddPrimary(&Column{Name: "id", Type: field.TypeInt, Increment: true}),
},
options: []MigrateOption{WithGlobalUniqueID(true)},
before: func(mock sqlmock.Sqlmock) {
mock.ExpectBegin()
mock.ExpectQuery("PRAGMA foreign_keys").
WillReturnRows(sqlmock.NewRows([]string{"foreign_keys"}).AddRow(1))
// query ent_types table.
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "ent_types").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery(escape("SELECT `type` FROM `ent_types` ORDER BY `id` ASC")).
WillReturnRows(sqlmock.NewRows([]string{"type"}).AddRow("users"))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "users").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("CREATE TABLE `users`(`id` integer PRIMARY KEY AUTOINCREMENT)")).
WillReturnResult(sqlmock.NewResult(0, 1))
// set users id range (without inserting to ent_types).
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_sequence` WHERE `name` = ?")).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectExec(escape("UPDATE `sqlite_sequence` SET `seq` = ? WHERE `name` = ?")).
WithArgs(0, "users").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_master` WHERE `type` = ? AND `name` = ?")).
WithArgs("table", "groups").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("CREATE TABLE `groups`(`id` integer PRIMARY KEY AUTOINCREMENT)")).
WillReturnResult(sqlmock.NewResult(0, 1))
// set groups id range.
mock.ExpectExec(escape("INSERT INTO `ent_types` (`type`) VALUES (?)")).
WithArgs("groups").
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectQuery(escape("SELECT COUNT(*) FROM `sqlite_sequence` WHERE `name` = ?")).
WithArgs("groups").
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(escape("INSERT INTO `sqlite_sequence` (`name`, `seq`) VALUES (?, ?)")).
WithArgs("groups", 1<<32).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.ExpectCommit()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
tt.before(mock)
sqlite := &SQLite{sql.OpenDB("sqlite", db)}
err = sqlite.Create(context.Background(), tt.tables...)
migrate, err := NewMigrate(sql.OpenDB("sqlite3", db), tt.options...)
require.NoError(t, err)
err = migrate.Create(context.Background(), tt.tables...)
require.Equal(t, tt.wantErr, err != nil, err)
})
}