Merge pull request #126 from fourdim/tenant

Add table prefix and suffix for multi-tenant environment support
This commit is contained in:
go-jet 2022-03-18 11:42:32 +01:00 committed by GitHub
commit 47a554d771
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 213 additions and 5 deletions

23
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View file

@ -0,0 +1,23 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: bug
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**Environment (please complete the following information):**
- OS: [e.g. linux, windows, macosx]
- Database: [e.g. postgres, mysql, sqlite]
- Database driver: [e.g. pq, pgx]
- Jet version [e.g. 2.6.0 or branch name]
**Code snippet**
Query statement and model files of interest.
**Expected behavior**
A clear and concise description of what you expected to happen.

View file

@ -0,0 +1,14 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: missing feature
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.

View file

@ -42,6 +42,16 @@ func (a ActorTable) FromSchema(schemaName string) *ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorTable with assigned table prefix
func (a ActorTable) WithPrefix(prefix string) *ActorTable {
return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorTable with assigned table suffix
func (a ActorTable) WithSuffix(suffix string) *ActorTable {
return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorTable(schemaName, tableName, alias string) *ActorTable {
return &ActorTable{
actorTable: newActorTableImpl(schemaName, tableName, alias),

View file

@ -41,6 +41,16 @@ func (a CategoryTable) FromSchema(schemaName string) *CategoryTable {
return newCategoryTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new CategoryTable with assigned table prefix
func (a CategoryTable) WithPrefix(prefix string) *CategoryTable {
return newCategoryTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new CategoryTable with assigned table suffix
func (a CategoryTable) WithSuffix(suffix string) *CategoryTable {
return newCategoryTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newCategoryTable(schemaName, tableName, alias string) *CategoryTable {
return &CategoryTable{
categoryTable: newCategoryTableImpl(schemaName, tableName, alias),

View file

@ -51,6 +51,16 @@ func (a FilmTable) FromSchema(schemaName string) *FilmTable {
return newFilmTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new FilmTable with assigned table prefix
func (a FilmTable) WithPrefix(prefix string) *FilmTable {
return newFilmTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new FilmTable with assigned table suffix
func (a FilmTable) WithSuffix(suffix string) *FilmTable {
return newFilmTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newFilmTable(schemaName, tableName, alias string) *FilmTable {
return &FilmTable{
filmTable: newFilmTableImpl(schemaName, tableName, alias),

View file

@ -41,6 +41,16 @@ func (a FilmActorTable) FromSchema(schemaName string) *FilmActorTable {
return newFilmActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new FilmActorTable with assigned table prefix
func (a FilmActorTable) WithPrefix(prefix string) *FilmActorTable {
return newFilmActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new FilmActorTable with assigned table suffix
func (a FilmActorTable) WithSuffix(suffix string) *FilmActorTable {
return newFilmActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newFilmActorTable(schemaName, tableName, alias string) *FilmActorTable {
return &FilmActorTable{
filmActorTable: newFilmActorTableImpl(schemaName, tableName, alias),

View file

@ -41,6 +41,16 @@ func (a FilmCategoryTable) FromSchema(schemaName string) *FilmCategoryTable {
return newFilmCategoryTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new FilmCategoryTable with assigned table prefix
func (a FilmCategoryTable) WithPrefix(prefix string) *FilmCategoryTable {
return newFilmCategoryTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new FilmCategoryTable with assigned table suffix
func (a FilmCategoryTable) WithSuffix(suffix string) *FilmCategoryTable {
return newFilmCategoryTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newFilmCategoryTable(schemaName, tableName, alias string) *FilmCategoryTable {
return &FilmCategoryTable{
filmCategoryTable: newFilmCategoryTableImpl(schemaName, tableName, alias),

View file

@ -41,6 +41,16 @@ func (a LanguageTable) FromSchema(schemaName string) *LanguageTable {
return newLanguageTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new LanguageTable with assigned table prefix
func (a LanguageTable) WithPrefix(prefix string) *LanguageTable {
return newLanguageTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new LanguageTable with assigned table suffix
func (a LanguageTable) WithSuffix(suffix string) *LanguageTable {
return newLanguageTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newLanguageTable(schemaName, tableName, alias string) *LanguageTable {
return &LanguageTable{
languageTable: newLanguageTableImpl(schemaName, tableName, alias),

View file

@ -42,6 +42,16 @@ func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable {
return newActorInfoTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorInfoTable with assigned table prefix
func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorInfoTable with assigned table suffix
func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable {
return &ActorInfoTable{
actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias),

View file

@ -47,6 +47,16 @@ func (a CustomerListTable) FromSchema(schemaName string) *CustomerListTable {
return newCustomerListTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new CustomerListTable with assigned table prefix
func (a CustomerListTable) WithPrefix(prefix string) *CustomerListTable {
return newCustomerListTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new CustomerListTable with assigned table suffix
func (a CustomerListTable) WithSuffix(suffix string) *CustomerListTable {
return newCustomerListTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newCustomerListTable(schemaName, tableName, alias string) *CustomerListTable {
return &CustomerListTable{
customerListTable: newCustomerListTableImpl(schemaName, tableName, alias),

View file

@ -49,6 +49,16 @@ func (a {{tableTemplate.TypeName}}) FromSchema(schemaName string) {{tableTemplat
return new{{tableTemplate.TypeName}}(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new {{tableTemplate.TypeName}} with assigned table prefix
func (a {{tableTemplate.TypeName}}) WithPrefix(prefix string) {{tableTemplate.TypeName}} {
return new{{tableTemplate.TypeName}}(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new {{tableTemplate.TypeName}} with assigned table suffix
func (a {{tableTemplate.TypeName}}) WithSuffix(suffix string) {{tableTemplate.TypeName}} {
return new{{tableTemplate.TypeName}}(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func new{{tableTemplate.TypeName}}(schemaName, tableName, alias string) {{tableTemplate.TypeName}} {
var (
{{- range $i, $c := .Columns}}
@ -119,6 +129,16 @@ func (a {{tableTemplate.TypeName}}) FromSchema(schemaName string) *{{tableTempla
return new{{tableTemplate.TypeName}}(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new {{tableTemplate.TypeName}} with assigned table prefix
func (a {{tableTemplate.TypeName}}) WithPrefix(prefix string) *{{tableTemplate.TypeName}} {
return new{{tableTemplate.TypeName}}(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new {{tableTemplate.TypeName}} with assigned table suffix
func (a {{tableTemplate.TypeName}}) WithSuffix(suffix string) *{{tableTemplate.TypeName}} {
return new{{tableTemplate.TypeName}}(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func new{{tableTemplate.TypeName}}(schemaName, tableName, alias string) *{{tableTemplate.TypeName}} {
return &{{tableTemplate.TypeName}}{
{{structImplName}}: new{{tableTemplate.TypeName}}Impl(schemaName, tableName, alias),

View file

@ -236,6 +236,16 @@ func (a ActorTable) FromSchema(schemaName string) ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorTable with assigned table prefix
func (a ActorTable) WithPrefix(prefix string) ActorTable {
return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorTable with assigned table suffix
func (a ActorTable) WithSuffix(suffix string) ActorTable {
return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorTable(schemaName, tableName, alias string) ActorTable {
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")
@ -322,6 +332,16 @@ func (a ActorInfoTable) FromSchema(schemaName string) ActorInfoTable {
return newActorInfoTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorInfoTable with assigned table prefix
func (a ActorInfoTable) WithPrefix(prefix string) ActorInfoTable {
return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorInfoTable with assigned table suffix
func (a ActorInfoTable) WithSuffix(suffix string) ActorInfoTable {
return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorInfoTable(schemaName, tableName, alias string) ActorInfoTable {
var (
ActorIDColumn = mysql.IntegerColumn("actor_id")

View file

@ -313,6 +313,16 @@ func (a ActorTable) FromSchema(schemaName string) *ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorTable with assigned table prefix
func (a ActorTable) WithPrefix(prefix string) *ActorTable {
return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorTable with assigned table suffix
func (a ActorTable) WithSuffix(suffix string) *ActorTable {
return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorTable(schemaName, tableName, alias string) *ActorTable {
return &ActorTable{
actorTable: newActorTableImpl(schemaName, tableName, alias),
@ -412,6 +422,16 @@ func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable {
return newActorInfoTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorInfoTable with assigned table prefix
func (a ActorInfoTable) WithPrefix(prefix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorInfoTable with assigned table suffix
func (a ActorInfoTable) WithSuffix(suffix string) *ActorInfoTable {
return newActorInfoTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable {
return &ActorInfoTable{
actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias),
@ -705,6 +725,16 @@ func (a AllTypesTable) FromSchema(schemaName string) *AllTypesTable {
return newAllTypesTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new AllTypesTable with assigned table prefix
func (a AllTypesTable) WithPrefix(prefix string) *AllTypesTable {
return newAllTypesTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new AllTypesTable with assigned table suffix
func (a AllTypesTable) WithSuffix(suffix string) *AllTypesTable {
return newAllTypesTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newAllTypesTable(schemaName, tableName, alias string) *AllTypesTable {
return &AllTypesTable{
allTypesTable: newAllTypesTableImpl(schemaName, tableName, alias),

View file

@ -1,16 +1,17 @@
package sqlite
import (
"github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/tests/.gentestdata/sqlite/sakila/model"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"os/exec"
"reflect"
"testing"
"github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/internal/testutils"
"github.com/go-jet/jet/v2/tests/.gentestdata/sqlite/sakila/model"
"github.com/go-jet/jet/v2/tests/internal/utils/repo"
"github.com/stretchr/testify/require"
)
func TestGeneratedModel(t *testing.T) {
@ -183,6 +184,16 @@ func (a ActorTable) FromSchema(schemaName string) *ActorTable {
return newActorTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new ActorTable with assigned table prefix
func (a ActorTable) WithPrefix(prefix string) *ActorTable {
return newActorTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new ActorTable with assigned table suffix
func (a ActorTable) WithSuffix(suffix string) *ActorTable {
return newActorTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newActorTable(schemaName, tableName, alias string) *ActorTable {
return &ActorTable{
actorTable: newActorTableImpl(schemaName, tableName, alias),
@ -264,6 +275,16 @@ func (a FilmListTable) FromSchema(schemaName string) *FilmListTable {
return newFilmListTable(schemaName, a.TableName(), a.Alias())
}
// WithPrefix creates new FilmListTable with assigned table prefix
func (a FilmListTable) WithPrefix(prefix string) *FilmListTable {
return newFilmListTable(a.SchemaName(), prefix+a.TableName(), a.TableName())
}
// WithSuffix creates new FilmListTable with assigned table suffix
func (a FilmListTable) WithSuffix(suffix string) *FilmListTable {
return newFilmListTable(a.SchemaName(), a.TableName()+suffix, a.TableName())
}
func newFilmListTable(schemaName, tableName, alias string) *FilmListTable {
return &FilmListTable{
filmListTable: newFilmListTableImpl(schemaName, tableName, alias),