orm: fix unique fields (#11045)

pull/11079/head
Louis Schmieder 2021-08-06 05:21:59 +02:00 committed by GitHub
parent 490dec222f
commit a4358a6801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 2 deletions

View File

@ -870,7 +870,7 @@ jobs:
git clone --depth 1 https://github.com/vlang/gitly git clone --depth 1 https://github.com/vlang/gitly
cd gitly cd gitly
../v . ../v .
# ./gitly -ci_run #TODO ./gitly -ci_run
../v -autofree . ../v -autofree .
cd .. cd ..

View File

@ -355,7 +355,7 @@ pub fn orm_table_gen(table string, para string, defaults bool, def_unique_len in
stmt += ' NOT NULL' stmt += ' NOT NULL'
} }
if is_unique { if is_unique {
mut f := 'UNIQUE KEY($para$field.name$para' mut f := 'UNIQUE($para$field.name$para'
if ctyp == 'TEXT' && def_unique_len > 0 { if ctyp == 'TEXT' && def_unique_len > 0 {
if unique_len > 0 { if unique_len > 0 {
f += '($unique_len)' f += '($unique_len)'

View File

@ -174,6 +174,85 @@ fn test_orm_table_gen() {
}, },
], sql_type_from_v, true) or { panic(err) } ], sql_type_from_v, true) or { panic(err) }
assert alt_query == "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='test_table' and xtype='U') CREATE TABLE 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));" assert alt_query == "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='test_table' and xtype='U') CREATE TABLE 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'));"
unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: 7
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: 18
attrs: [
StructAttribute{
name: 'unique'
},
]
},
orm.TableField{
name: 'abc'
typ: 8
default_val: '6754'
},
], sql_type_from_v, false) or { panic(err) }
assert unique_query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, PRIMARY KEY('id'), UNIQUE('test'));"
mult_unique_query := orm.orm_table_gen('test_table', "'", true, 0, [
orm.TableField{
name: 'id'
typ: 7
default_val: '10'
attrs: [
StructAttribute{
name: 'primary'
},
StructAttribute{
name: 'sql'
has_arg: true
arg: 'serial'
kind: .plain
},
]
},
orm.TableField{
name: 'test'
typ: 18
attrs: [
StructAttribute{
name: 'unique'
has_arg: true
arg: 'test'
kind: .string
},
]
},
orm.TableField{
name: 'abc'
typ: 8
default_val: '6754'
attrs: [
StructAttribute{
name: 'unique'
has_arg: true
arg: 'test'
kind: .string
},
]
},
], sql_type_from_v, false) or { panic(err) }
assert mult_unique_query == "CREATE TABLE IF NOT EXISTS 'test_table' ('id' SERIAL DEFAULT 10, 'test' TEXT, 'abc' INT64 DEFAULT 6754, /* test */UNIQUE('test', 'abc'), PRIMARY KEY('id'));"
} }
fn sql_type_from_v(typ int) ?string { fn sql_type_from_v(typ int) ?string {