velocious 1.0.46 → 1.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/package.json +1 -1
  2. package/spec/cli/commands/db/migrate-spec.js +52 -8
  3. package/spec/dummy/src/database/migrations/20250915085450-change-authentication-tokens-user-id-nullable.js +11 -0
  4. package/spec/dummy/src/database/migrations/20250916111330-rename_authentication_tokens_token_to_user_token.js +11 -0
  5. package/src/database/drivers/base-column.js +28 -0
  6. package/src/database/drivers/base-foreign-key.js +31 -0
  7. package/src/database/drivers/base-table.js +29 -0
  8. package/src/database/drivers/base.js +19 -1
  9. package/src/database/drivers/mssql/column.js +22 -0
  10. package/src/database/drivers/mssql/columns-index.js +1 -1
  11. package/src/database/drivers/mssql/foreign-key.js +2 -5
  12. package/src/database/drivers/mssql/index.js +16 -6
  13. package/src/database/drivers/mssql/sql/alter-table.js +4 -0
  14. package/src/database/drivers/mssql/table.js +37 -1
  15. package/src/database/drivers/mysql/column.js +38 -4
  16. package/src/database/drivers/mysql/foreign-key.js +2 -5
  17. package/src/database/drivers/mysql/index.js +9 -1
  18. package/src/database/drivers/mysql/sql/alter-table.js +4 -0
  19. package/src/database/drivers/mysql/table.js +41 -0
  20. package/src/database/drivers/pgsql/column.js +22 -0
  21. package/src/database/drivers/pgsql/foreign-key.js +2 -5
  22. package/src/database/drivers/pgsql/index.js +10 -2
  23. package/src/database/drivers/pgsql/sql/alter-table.js +4 -0
  24. package/src/database/drivers/pgsql/table.js +32 -2
  25. package/src/database/drivers/sqlite/base.js +5 -5
  26. package/src/database/drivers/sqlite/column.js +28 -36
  27. package/src/database/drivers/sqlite/columns-index.js +13 -1
  28. package/src/database/drivers/sqlite/foreign-key.js +8 -7
  29. package/src/database/drivers/sqlite/index.js +1 -1
  30. package/src/database/drivers/sqlite/index.web.js +3 -1
  31. package/src/database/drivers/sqlite/query.js +1 -2
  32. package/src/database/drivers/sqlite/sql/alter-table.js +143 -1
  33. package/src/database/drivers/sqlite/table.js +38 -0
  34. package/src/database/migration/index.js +31 -15
  35. package/src/database/query/alter-table-base.js +32 -7
  36. package/src/database/query/create-index-base.js +28 -4
  37. package/src/database/query/create-table-base.js +21 -82
  38. package/src/database/table-data/index.js +36 -70
  39. package/src/database/table-data/table-column.js +145 -0
  40. package/src/database/table-data/table-foreign-key.js +21 -0
  41. package/src/database/table-data/table-index.js +18 -0
  42. package/src/database/table-data/table-reference.js +6 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.46",
6
+ "version": "1.0.48",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "test": "VELOCIOUS_TEST_DIR=../ cd spec/dummy && npx velocious test",
@@ -40,15 +40,43 @@ describe("Cli - Commands - db:migrate", () => {
40
40
  }
41
41
  }
42
42
 
43
- // It creates unique indexes
43
+ // It creates the correct index
44
44
  const authenticationTokensTable = await dbs.default.getTableByName("authentication_tokens")
45
- const tokenColumn = await authenticationTokensTable.getColumnByName("token")
46
- const tokenIndex = await tokenColumn.getIndexByName("index_on_token")
45
+ const indexes = await authenticationTokensTable.getIndexes()
46
+ const indexesNames = indexes
47
+ .map((index) => index.getName())
48
+ .filter((indexName) => indexName != "authentication_tokens_pkey" && indexName != "PRIMARY" && !indexName.startsWith("PK__"))
49
+ .sort()
50
+
51
+ if (defaultDatabaseType == "mysql") {
52
+ expect(indexesNames).toEqual(["index_on_token","user_id"])
53
+ } else if (defaultDatabaseType == "sqlite") {
54
+ expect(indexesNames).toEqual(["index_on_authentication_tokens_token", "index_on_authentication_tokens_user_id"])
55
+ } else {
56
+ expect(indexesNames).toEqual(["index_on_token", "index_on_user_id"])
57
+ }
58
+
59
+ // It creates unique indexes
60
+ let tokenIndexName
61
+
62
+ if (defaultDatabaseType == "sqlite") {
63
+ tokenIndexName = "index_on_authentication_tokens_token"
64
+ } else {
65
+ tokenIndexName = "index_on_token"
66
+ }
47
67
 
48
- expect(tokenIndex.getName()).toEqual("index_on_token")
68
+ const tokenColumn = await authenticationTokensTable.getColumnByName("user_token")
69
+ const tokenIndex = await tokenColumn.getIndexByName(tokenIndexName)
70
+
71
+ expect(tokenIndex.getName()).toEqual(tokenIndexName)
49
72
  expect(tokenIndex.isPrimaryKey()).toBeFalse()
50
73
  expect(tokenIndex.isUnique()).toBeTrue()
51
74
 
75
+ // It creates foreign keys
76
+ const authTokensTableForeignKeys = await authenticationTokensTable.getForeignKeys()
77
+
78
+ expect(authTokensTableForeignKeys.length).toEqual(1)
79
+
52
80
  for (const db of Object.values(dbs)) {
53
81
  const schemaMigrations = await db.query("SELECT * FROM schema_migrations ORDER BY version")
54
82
 
@@ -64,8 +92,6 @@ describe("Cli - Commands - db:migrate", () => {
64
92
  }
65
93
  })
66
94
 
67
-
68
-
69
95
  expect(projectForeignKey.getTableName()).toEqual("tasks")
70
96
  expect(projectForeignKey.getColumnName()).toEqual("project_id")
71
97
  expect(projectForeignKey.getReferencedTableName()).toEqual("projects")
@@ -84,7 +110,16 @@ describe("Cli - Commands - db:migrate", () => {
84
110
  ]
85
111
  )
86
112
 
87
- expect(uniqunize(defaultSchemaMigrations.sort())).toEqual(["20230728075328", "20230728075329", "20250605133926", "20250903112845", "20250912183605", "20250912183606"])
113
+ expect(uniqunize(defaultSchemaMigrations.sort())).toEqual([
114
+ "20230728075328",
115
+ "20230728075329",
116
+ "20250605133926",
117
+ "20250903112845",
118
+ "20250912183605",
119
+ "20250912183606",
120
+ "20250915085450",
121
+ "20250916111330"
122
+ ])
88
123
  } else {
89
124
  expect(tablesResult.sort()).toEqual(
90
125
  [
@@ -99,7 +134,16 @@ describe("Cli - Commands - db:migrate", () => {
99
134
  ]
100
135
  )
101
136
 
102
- expect(defaultSchemaMigrations.sort()).toEqual(["20230728075328", "20230728075329", "20250605133926", "20250903112845", "20250912183605", "20250912183606"])
137
+ expect(defaultSchemaMigrations.sort()).toEqual([
138
+ "20230728075328",
139
+ "20230728075329",
140
+ "20250605133926",
141
+ "20250903112845",
142
+ "20250912183605",
143
+ "20250912183606",
144
+ "20250915085450",
145
+ "20250916111330"
146
+ ])
103
147
  }
104
148
  })
105
149
  })
@@ -0,0 +1,11 @@
1
+ import Migration from "../../../../../src/database/migration/index.js"
2
+
3
+ export default class ChangeAuthenticationTokensUserIdNullable extends Migration {
4
+ async up() {
5
+ await this.changeColumnNull("authentication_tokens", "user_id", true)
6
+ }
7
+
8
+ async down() {
9
+ await this.changeColumnNull("authentication_tokens", "user_id", false)
10
+ }
11
+ }
@@ -0,0 +1,11 @@
1
+ import Migration from "../../../../../src/database/migration/index.js"
2
+
3
+ export default class RenameAuthenticationTokensTokenToUserToken extends Migration {
4
+ async up() {
5
+ await this.renameColumn("authentication_tokens", "token", "user_token")
6
+ }
7
+
8
+ async down() {
9
+ await this.renameColumn("authentication_tokens", "user_token", "token")
10
+ }
11
+ }
@@ -1,3 +1,6 @@
1
+ import TableColumn from "../table-data/table-column.js"
2
+ import TableData from "../table-data/index.js"
3
+
1
4
  export default class VelociousDatabaseDriversBaseColumn {
2
5
  async getIndexByName(indexName) {
3
6
  const indexes = await this.getIndexes()
@@ -6,6 +9,21 @@ export default class VelociousDatabaseDriversBaseColumn {
6
9
  return index
7
10
  }
8
11
 
12
+ async changeNullable(nullable) {
13
+ const tableData = new TableData(this.getTable().getName())
14
+ const column = this.getTableDataColumn()
15
+
16
+ column.setNull(nullable)
17
+
18
+ tableData.addColumn(column)
19
+
20
+ const sqls = await this.getDriver().alterTableSql(tableData)
21
+
22
+ for (const sql of sqls) {
23
+ await this.getDriver().query(sql)
24
+ }
25
+ }
26
+
9
27
  getDriver() {
10
28
  return this.getTable().getDriver()
11
29
  }
@@ -19,4 +37,14 @@ export default class VelociousDatabaseDriversBaseColumn {
19
37
 
20
38
  return this.table
21
39
  }
40
+
41
+ getTableDataColumn() {
42
+ return new TableColumn(this.getName(), {
43
+ default: this.getDefault(),
44
+ isNewColumn: false,
45
+ maxLength: this.getMaxLength(),
46
+ null: this.getNull(),
47
+ type: this.getType()
48
+ })
49
+ }
22
50
  }
@@ -0,0 +1,31 @@
1
+ import TableForeignKey from "../table-data/table-foreign-key.js"
2
+
3
+ export default class VelociousDatabaseDriversBaseForeignKey {
4
+ constructor(data) {
5
+ this.data = data
6
+ }
7
+
8
+ getDriver() {
9
+ return this.getTable().getDriver()
10
+ }
11
+
12
+ getOptions() {
13
+ return this.getDriver().options()
14
+ }
15
+
16
+ getTable() {
17
+ if (!this.table) throw new Error("No table set on column")
18
+
19
+ return this.table
20
+ }
21
+
22
+ getTableDataForeignKey() {
23
+ return new TableForeignKey({
24
+ columnName: this.getColumnName(),
25
+ name: this.getName(),
26
+ tableName: this.getTableName(),
27
+ referencedColumnName: this.getReferencedColumnName(),
28
+ referencedTableName: this.getReferencedTableName()
29
+ })
30
+ }
31
+ }
@@ -1,4 +1,5 @@
1
1
  import {digg} from "diggerize"
2
+ import TableData from "../table-data/index.js"
2
3
 
3
4
  export default class VelociousDatabaseDriversBaseTable {
4
5
  async getColumnByName(columnName) {
@@ -18,6 +19,34 @@ export default class VelociousDatabaseDriversBaseTable {
18
19
  return this.getDriver().options()
19
20
  }
20
21
 
22
+ async getTableData() {
23
+ const tableData = new TableData(this.getName())
24
+ const tableDataColumns = []
25
+
26
+ for (const column of await this.getColumns()) {
27
+ const tableDataColumn = column.getTableDataColumn()
28
+
29
+ tableData.addColumn(tableDataColumn)
30
+ tableDataColumns.push(tableDataColumn)
31
+ }
32
+
33
+ for (const foreignKey of await this.getForeignKeys()) {
34
+ tableData.addForeignKey(foreignKey.getTableDataForeignKey())
35
+
36
+ const tableDataColumn = tableDataColumns.find((tableDataColumn) => tableDataColumn.getName() == foreignKey.getColumnName())
37
+
38
+ if (!tableDataColumn) throw new Error(`Couldn't find table data column for foreign key: ${foreignKey.getColumnName()}`)
39
+
40
+ tableDataColumn.setForeignKey(foreignKey)
41
+ }
42
+
43
+ for (const index of await this.getIndexes()) {
44
+ tableData.addIndex(index.getTableDataIndex())
45
+ }
46
+
47
+ return tableData
48
+ }
49
+
21
50
  async rowsCount() {
22
51
  const result = await this.getDriver().query(`SELECT COUNT(*) AS count FROM ${this.getOptions().quoteTableName(this.getName())}`)
23
52
 
@@ -3,6 +3,8 @@ import Query from "../query/index.js"
3
3
  import Handler from "../handler.js"
4
4
  import strftime from "strftime"
5
5
  import UUID from "pure-uuid"
6
+ import TableData from "../table-data/index.js"
7
+ import TableColumn from "../table-data/table-column.js"
6
8
 
7
9
  export default class VelociousDatabaseDriversBase {
8
10
  constructor(config, configuration) {
@@ -42,7 +44,7 @@ export default class VelociousDatabaseDriversBase {
42
44
  if (!this.configuration) throw new Error("No configuration set")
43
45
 
44
46
  return this.configuration
45
- }
47
+ }
46
48
 
47
49
  getIdSeq() {
48
50
  return this.idSeq
@@ -203,6 +205,22 @@ export default class VelociousDatabaseDriversBase {
203
205
  await this.query(`SAVEPOINT ${savePointName}`)
204
206
  }
205
207
 
208
+ async renameColumn(tableName, oldColumnName, newColumnName) {
209
+ const tableColumn = new TableColumn(oldColumnName)
210
+
211
+ tableColumn.setNewName(newColumnName)
212
+
213
+ const tableData = new TableData(tableName)
214
+
215
+ tableData.addColumn(tableColumn)
216
+
217
+ const alterTableSQLs = await this.alterTableSql(tableData)
218
+
219
+ for (const alterTableSQL of alterTableSQLs) {
220
+ await this.query(alterTableSQL)
221
+ }
222
+ }
223
+
206
224
  async releaseSavePoint(savePointName) {
207
225
  await this.query(`RELEASE SAVEPOINT ${savePointName}`)
208
226
  }
@@ -45,7 +45,29 @@ export default class VelociousDatabaseDriversMssqlColumn extends BaseColumn {
45
45
  return indexes
46
46
  }
47
47
 
48
+ getDefault() {
49
+ return digg(this, "data", "COLUMN_DEFAULT")
50
+ }
51
+
52
+ getMaxLength() {
53
+ return digg(this, "data", "CHARACTER_MAXIMUM_LENGTH")
54
+ }
55
+
48
56
  getName() {
49
57
  return digg(this, "data", "COLUMN_NAME")
50
58
  }
59
+
60
+ getNull() {
61
+ const nullValue = digg(this, "data", "IS_NULLABLE")
62
+
63
+ if (nullValue == "NO") {
64
+ return false
65
+ } else {
66
+ return true
67
+ }
68
+ }
69
+
70
+ getType() {
71
+ return digg(this, "data", "DATA_TYPE")
72
+ }
51
73
  }
@@ -1,6 +1,6 @@
1
1
  import BaseColumnsIndex from "../base-columns-index.js"
2
2
 
3
- export default class VelociousDatabaseDriversMssqlColumn extends BaseColumnsIndex {
3
+ export default class VelociousDatabaseDriversMssqlColumnsIndex extends BaseColumnsIndex {
4
4
  constructor(table, data) {
5
5
  super()
6
6
  this.data = data
@@ -1,10 +1,7 @@
1
+ import BaseForeignKey from "../base-foreign-key.js"
1
2
  import {digg} from "diggerize"
2
3
 
3
- export default class VelociousDatabaseDriversMssqlForeignKey {
4
- constructor(data) {
5
- this.data = data
6
- }
7
-
4
+ export default class VelociousDatabaseDriversMssqlForeignKey extends BaseForeignKey {
8
5
  getColumnName = () => digg(this, "data", "ParentColumn")
9
6
  getName = () => digg(this, "data", "CONSTRAINT_NAME")
10
7
  getTableName = () => digg(this, "data", "TableName")
@@ -1,3 +1,4 @@
1
+ import AlterTable from "./sql/alter-table.js"
1
2
  import Base from "../base.js"
2
3
  import CreateDatabase from "./sql/create-database.js"
3
4
  import CreateIndex from "./sql/create-index.js"
@@ -32,6 +33,13 @@ export default class VelociousDatabaseDriversMssql extends Base{
32
33
  this.connection = undefined
33
34
  }
34
35
 
36
+ async alterTableSql(tableData) {
37
+ const alterArgs = {tableData, driver: this}
38
+ const alterTable = new AlterTable(alterArgs)
39
+
40
+ return await alterTable.toSqls()
41
+ }
42
+
35
43
  createDatabaseSql(databaseName, args) {
36
44
  const createArgs = Object.assign({databaseName, driver: this}, args)
37
45
  const createDatabase = new CreateDatabase(createArgs)
@@ -47,7 +55,7 @@ export default class VelociousDatabaseDriversMssql extends Base{
47
55
  }
48
56
 
49
57
  createTableSql(tableData) {
50
- const createArgs = Object.assign({tableData, driver: this, indexInCreateTable: false})
58
+ const createArgs = {tableData, driver: this, indexInCreateTable: false}
51
59
  const createTable = new CreateTable(createArgs)
52
60
 
53
61
  return createTable.toSql()
@@ -111,9 +119,7 @@ export default class VelociousDatabaseDriversMssql extends Base{
111
119
  return new QueryParser({query}).toSql()
112
120
  }
113
121
 
114
- shouldSetAutoIncrementWhenPrimaryKey = () => true
115
-
116
-
122
+ shouldSetAutoIncrementWhenPrimaryKey() { return true }
117
123
 
118
124
  escape(value) {
119
125
  value = this._convertValue(value)
@@ -139,8 +145,12 @@ export default class VelociousDatabaseDriversMssql extends Base{
139
145
  return escapeString(value)
140
146
  }
141
147
 
142
- quoteColumn = (string) => this.options().quoteColumnName(string)
143
- quoteTable = (string) => this.options().quoteTableName(string)
148
+ quoteColumn(string) { return this.options().quoteColumnName(string) }
149
+ quoteTable(string) { return this.options().quoteTableName(string) }
150
+
151
+ async renameColumn(tableName, oldColumnName, newColumnName) {
152
+ await this.query(`EXEC sp_rename ${this.quote(`${tableName}.${oldColumnName}`)}, ${this.quote(newColumnName)}, 'COLUMN'`)
153
+ }
144
154
 
145
155
  deleteSql({tableName, conditions}) {
146
156
  const deleteInstruction = new Delete({conditions, driver: this, tableName})
@@ -0,0 +1,4 @@
1
+ import AlterTableBase from "../../../query/alter-table-base.js"
2
+
3
+ export default class VelociousDatabaseConnectionDriversMssqlSqlAlterTable extends AlterTableBase {
4
+ }
@@ -1,5 +1,6 @@
1
1
  import BaseTable from "../base-table.js"
2
2
  import Column from "./column.js"
3
+ import ColumnsIndex from "./columns-index.js"
3
4
  import {digg} from "diggerize"
4
5
  import ForeignKey from "./foreign-key.js"
5
6
 
@@ -11,7 +12,7 @@ export default class VelociousDatabaseDriversMssqlTable extends BaseTable {
11
12
  }
12
13
 
13
14
  async getColumns() {
14
- const result = await this.driver.query(`SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE [TABLE_NAME] = ${this.driver.quote(this.getName())}`)
15
+ const result = await this.driver.query(`SELECT * FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE [TABLE_NAME] = ${this.driver.quote(this.getName())}`)
15
16
  const columns = []
16
17
 
17
18
  for (const data of result) {
@@ -61,6 +62,41 @@ export default class VelociousDatabaseDriversMssqlTable extends BaseTable {
61
62
  return foreignKeys
62
63
  }
63
64
 
65
+ async getIndexes() {
66
+ const options = this.getOptions()
67
+ const sql = `
68
+ SELECT
69
+ sys.tables.name AS TableName,
70
+ sys.columns.name AS ColumnName,
71
+ sys.indexes.name AS index_name,
72
+ sys.indexes.type_desc AS IndexType,
73
+ sys.index_columns.is_included_column AS IsIncludedColumn,
74
+ sys.indexes.is_unique,
75
+ sys.indexes.is_primary_key,
76
+ sys.indexes.is_unique_constraint
77
+ FROM sys.indexes
78
+ INNER JOIN sys.index_columns ON sys.indexes.object_id = sys.index_columns.object_id AND sys.indexes.index_id = sys.index_columns.index_id
79
+ INNER JOIN sys.columns ON sys.index_columns.object_id = sys.columns.object_id AND sys.index_columns.column_id = sys.columns.column_id
80
+ INNER JOIN sys.tables ON sys.indexes.object_id = sys.tables.object_id
81
+ WHERE
82
+ sys.tables.name = ${options.quote(this.getName())}
83
+ ORDER BY
84
+ sys.indexes.name,
85
+ sys.index_columns.key_ordinal
86
+ `
87
+
88
+ const rows = await this.getDriver().query(sql)
89
+ const indexes = []
90
+
91
+ for (const row of rows) {
92
+ const index = new ColumnsIndex(this, row)
93
+
94
+ indexes.push(index)
95
+ }
96
+
97
+ return indexes
98
+ }
99
+
64
100
  getName() {
65
101
  return digg(this.data, "TABLE_NAME")
66
102
  }
@@ -21,9 +21,10 @@ export default class VelociousDatabaseDriversMysqlColumn extends BaseColumn {
21
21
  NON_UNIQUE,
22
22
  INDEX_TYPE
23
23
  FROM INFORMATION_SCHEMA.STATISTICS
24
- WHERE TABLE_SCHEMA = DATABASE()
25
- AND TABLE_NAME = ${options.quote(this.table.getName())}
26
- AND COLUMN_NAME = ${options.quote(this.getName())}
24
+ WHERE
25
+ TABLE_SCHEMA = DATABASE() AND
26
+ TABLE_NAME = ${options.quote(this.table.getName())} AND
27
+ COLUMN_NAME = ${options.quote(this.getName())}
27
28
  `
28
29
  const indexesRows = await this.getDriver().query(sql)
29
30
  const indexes = []
@@ -49,5 +50,38 @@ export default class VelociousDatabaseDriversMysqlColumn extends BaseColumn {
49
50
  return indexes
50
51
  }
51
52
 
52
- getName = () => digg(this, "data", "Field")
53
+ getDefault() { return digg(this, "data", "Default") }
54
+
55
+ getMaxLength() {
56
+ const type = digg(this, "data", "Type")
57
+ const match = type.match(/\((\d+)\)$/)
58
+
59
+ if (match) {
60
+ const maxLength = parseInt(match[1])
61
+
62
+ return maxLength
63
+ }
64
+ }
65
+
66
+ getName() { return digg(this, "data", "Field") }
67
+
68
+ getNull() {
69
+ const nullValue = digg(this, "data", "Null")
70
+
71
+ if (nullValue == "NO") {
72
+ return false
73
+ } else if (nullValue == "YES") {
74
+ return true
75
+ } else {
76
+ throw new Error(`Unknown null value: ${nullValue}`)
77
+ }
78
+ }
79
+
80
+ getType() {
81
+ const type = digg(this, "data", "Type")
82
+ const match = type.match(/^(.+)\((\d+)\)$/)
83
+ const columnType = match[1]
84
+
85
+ return columnType
86
+ }
53
87
  }
@@ -1,10 +1,7 @@
1
+ import BaseForeignKey from "../base-foreign-key.js"
1
2
  import {digg} from "diggerize"
2
3
 
3
- export default class VelociousDatabaseDriversMysqlForeignKey {
4
- constructor(data) {
5
- this.data = data
6
- }
7
-
4
+ export default class VelociousDatabaseDriversMysqlForeignKey extends BaseForeignKey {
8
5
  getColumnName = () => digg(this, "data", "COLUMN_NAME")
9
6
  getName = () => digg(this, "data", "CONSTRAINT_NAME")
10
7
  getTableName = () => digg(this, "data", "TABLE_NAME")
@@ -1,3 +1,4 @@
1
+ import AlterTable from "./sql/alter-table.js"
1
2
  import Base from "../base.js"
2
3
  import connectConnection from "./connect-connection.js"
3
4
  import CreateDatabase from "./sql/create-database.js"
@@ -41,6 +42,13 @@ export default class VelociousDatabaseDriversMysql extends Base{
41
42
  return connectArgs
42
43
  }
43
44
 
45
+ async alterTableSql(tableData) {
46
+ const alterArgs = {tableData, driver: this}
47
+ const alterTable = new AlterTable(alterArgs)
48
+
49
+ return await alterTable.toSqls()
50
+ }
51
+
44
52
  createDatabaseSql(databaseName, args) {
45
53
  const createArgs = Object.assign({databaseName, driver: this}, args)
46
54
  const createDatabase = new CreateDatabase(createArgs)
@@ -56,7 +64,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
56
64
  }
57
65
 
58
66
  createTableSql(tableData) {
59
- const createArgs = Object.assign({tableData, driver: this})
67
+ const createArgs = {tableData, driver: this}
60
68
  const createTable = new CreateTable(createArgs)
61
69
 
62
70
  return createTable.toSql()
@@ -0,0 +1,4 @@
1
+ import AlterTableBase from "../../../query/alter-table-base.js"
2
+
3
+ export default class VelociousDatabaseConnectionDriversMysqlSqlAlterTable extends AlterTableBase {
4
+ }
@@ -1,5 +1,6 @@
1
1
  import BaseTable from "../base-table.js"
2
2
  import Column from "./column.js"
3
+ import ColumnsIndex from "./columns-index.js"
3
4
  import ForeignKey from "./foreign-key.js"
4
5
 
5
6
  export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
@@ -43,6 +44,46 @@ export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
43
44
  return foreignKeys
44
45
  }
45
46
 
47
+ async getIndexes() {
48
+ const options = this.getOptions()
49
+ const sql = `
50
+ SELECT
51
+ TABLE_SCHEMA,
52
+ TABLE_NAME,
53
+ INDEX_NAME AS index_name,
54
+ COLUMN_NAME,
55
+ SEQ_IN_INDEX,
56
+ NON_UNIQUE,
57
+ INDEX_TYPE
58
+ FROM INFORMATION_SCHEMA.STATISTICS
59
+ WHERE
60
+ TABLE_SCHEMA = DATABASE() AND
61
+ TABLE_NAME = ${options.quote(this.getName())}
62
+ `
63
+ const indexesRows = await this.getDriver().query(sql)
64
+ const indexes = []
65
+
66
+ for (const indexRow of indexesRows) {
67
+ if (indexRow.NON_UNIQUE == 1) {
68
+ indexRow.is_unique = false
69
+ } else {
70
+ indexRow.is_unique = true
71
+ }
72
+
73
+ if (indexRow.index_name == "PRIMARY") {
74
+ indexRow.is_primary_key = true
75
+ } else {
76
+ indexRow.is_primary_key = false
77
+ }
78
+
79
+ const index = new ColumnsIndex(this, indexRow)
80
+
81
+ indexes.push(index)
82
+ }
83
+
84
+ return indexes
85
+ }
86
+
46
87
  getName() {
47
88
  return Object.values(this.data)[0]
48
89
  }
@@ -39,7 +39,29 @@ export default class VelociousDatabaseDriversPgsqlColumn extends BaseColumn {
39
39
  return indexes
40
40
  }
41
41
 
42
+ getDefault() {
43
+ return digg(this, "data", "column_default")
44
+ }
45
+
46
+ getMaxLength() {
47
+ return digg(this, "data", "character_maximum_length")
48
+ }
49
+
42
50
  getName() {
43
51
  return digg(this, "data", "column_name")
44
52
  }
53
+
54
+ getNull() {
55
+ const nullValue = digg(this, "data", "is_nullable")
56
+
57
+ if (nullValue == "NO") {
58
+ return false
59
+ } else {
60
+ return true
61
+ }
62
+ }
63
+
64
+ getType() {
65
+ return digg(this, "data", "data_type")
66
+ }
45
67
  }
@@ -1,10 +1,7 @@
1
+ import BaseForeignKey from "../base-foreign-key.js"
1
2
  import {digg} from "diggerize"
2
3
 
3
- export default class VelociousDatabaseDriversPgsqlForeignKey {
4
- constructor(data) {
5
- this.data = data
6
- }
7
-
4
+ export default class VelociousDatabaseDriversPgsqlForeignKey extends BaseForeignKey {
8
5
  getColumnName = () => digg(this, "data", "column_name")
9
6
  getName = () => digg(this, "data", "constraint_name")
10
7
  getTableName = () => digg(this, "data", "table_name")