Skip to content

Adding support for Phinx #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"esversion": 6,
"browser": false,
"strict": "global"
}
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# What is this
# TableDump Plugin for TablePlus

This is a TablePlus's Plugin, by install `tabledump` you will have a menu `Copy Script As` in context menu.

This fork includes support for Phinx migrations, and will also export any indexes and foreign keys associated with the chosen table.

![tabledump](https://github.com/TablePlus/tabledump/blob/master/images/export-laravel.gif "tabledump")

# Support
## Support

TablePlus build 200 and above.

# Install
## Install

### From release

Install via Plugin Manager: `command + L`

### Build from source

```
```console
git clone [email protected]:TablePlus/tabledump.git
cd tabledump/tabledump.tableplusplugin
npm install
npm run build
open .
```

# How to use
## How to use

1. Open a connection.
3. Right on a item in left panel.
4. Select `Copy Script As` in menu.
2. Right on a item in left panel.
3. Select `Copy Script As` in menu.

# License
## License

tabledump is released under the MIT license. See [LICENSE](https://github.com/TablePlus/DummiesData/blob/master/LICENSE) for details.
200 changes: 5 additions & 195 deletions tabledump.tableplusplugin/library/helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";
/* globals SystemService */
'use strict';

function dumpTableAsDefinition(context, item) {
context.itemDefinition(item, function(creation) {
context.itemDefinition(item, function (creation) {
SystemService.insertToClipboard(creation);
SystemService.notify(
"Copy creation",
Expand All @@ -12,201 +13,10 @@ function dumpTableAsDefinition(context, item) {

function camelize(str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w|_\w)/g, function(letter, index) {
.replace(/(?:^\w|[A-Z]|\b\w|_\w)/g, function (letter, index) {
return letter.toUpperCase();
})
.replace(/\s+|-|_/g, "");
}

function getColumnMigrate(columnName, dataType, isNullable, defaultVal, extra, columnComment) {
var typeArr = dataType.split("(");
var typeOnly = typeArr[0];
var typeLength = "";
if (typeArr.length > 1) {
typeLength = typeArr[1];
}
var migration = "";
switch (typeOnly) {
case "varchar":
if (typeLength.length > 0) {
migration = "$table->string('" + columnName + "', " + typeLength + "";
} else {
migration = "$table->string('" + columnName + "')";
}
break;
case "float":
case "double":
case "decimal":
if (typeLength.length > 0) {
// Pretty length format: 8,2) => 8, 2)
typeLength = typeLength.replace(",", ", ");
migration =
`$table->${typeOnly}('` + columnName + "', " + typeLength + "";
} else {
migration = `$table->${typeOnly}('` + columnName + "')";
}
break;
case "float4":
case "float8":
migration = "$table->float('" + columnName + "')";
break;
case "char":
migration = "$table->char('" + columnName + "', " + typeLength + "";
break;
case "enum":
typeLength = typeLength.substring(0, typeLength.length - 1);
migration = "$table->enum('" + columnName + "', [" + typeLength + "])";
break;
case "int8":
case "bigint":
migration = "$table->bigInteger('" + columnName + "')";
break;
case "int":
case "int4":
migration = "$table->integer('" + columnName + "')";
break;
case "int3":
case "mediumint":
migration = "$table->mediumInteger('" + columnName + "')";
break;
case "int2":
case "smallint":
migration = "$table->smallInteger('" + columnName + "')";
break;
case "int1":
case "tinyint":
migration = "$table->tinyInteger('" + columnName + "')";
break;
default:
migration = `$table->${typeOnly}('` + columnName + "')";
break;
}
if (dataType.includes("unsigned")) {
migration += "->unsigned()";
}

if (isNullable.toLowerCase().startsWith("y")) {
migration += "->nullable()";
}

if (defaultVal) {
migration += "->default(" + defaultVal + ")";
}

if (extra) {
switch (extra) {
case "auto_increment":
migration += "->autoIncrement()";
break;
}
}

if (typeof columnComment != 'undefined' && columnComment) {
migration += "->comment('" + columnComment.trim() + "')";
}

return migration + ";";
}

function dumpTableAsLaravel(context, item) {
var nameCamelcase = camelize(item.name());
var header = `<?php

use Illuminate\\Support\\Facades\\Schema;
use Illuminate\\Database\\Schema\\Blueprint;
use Illuminate\\Database\\Migrations\\Migration;

/**
* Migration auto-generated by TablePlus ${Application.appVersion()}(${Application.appBuild()})
* @author https://tableplus.com
* @source https://github.com/TablePlus/tabledump
*/
class Create${nameCamelcase}Table extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('${item.name()}', function (Blueprint $table) {
`;
var columnNames = [];
var columnTypes = [];
var isNullables = [];
var defaultVals = [];
var columnComments = [];
var extras = [];
var query;
var driver = context.driver();
switch (driver) {
case "MySQL":
case "MariaDB":
query = `SELECT ordinal_position as ordinal_position,column_name as column_name,column_type AS data_type,is_nullable as is_nullable,column_default as column_default,extra as extra,column_name AS foreign_key,column_comment AS comment FROM information_schema.columns WHERE table_schema='${item.schema()}'AND table_name='${item.name()}';`;
break;
case "PostgreSQL":
query = `SELECT ordinal_position,column_name,udt_name AS data_type,numeric_precision,datetime_precision,numeric_scale,character_maximum_length AS data_length,is_nullable,column_name as check,column_name as check_constraint,column_default,column_name AS foreign_key,pg_catalog.col_description(16402,ordinal_position) as comment FROM information_schema.columns WHERE table_name='${item.name()}'AND table_schema='${item.schema()}';`;
break;
default:
context.alert("Error", driver + " is not supported");
return;
}
context.execute(query, res => {
res.rows.sort((l, r) => {
return (
parseInt(l.raw("ordinal_position")) >
parseInt(r.raw("ordinal_position"))
);
});

res.rows.forEach(row => {
let columnName = row.raw("column_name");
let columnType = row.raw("data_type");
let isNullable = row.raw("is_nullable");
let defaultVal = row.raw("column_default");
let extra = row.raw("extra");
columnNames.push(columnName);
columnTypes.push(columnType);
isNullables.push(isNullable);
defaultVals.push(defaultVal);
extras.push(extra);
columnComments.push(row.raw("comment"));
});

var result = header;

for (let i = 0; i < columnNames.length; i++) {
var columnMigrate = getColumnMigrate(
columnNames[i],
columnTypes[i],
isNullables[i],
defaultVals[i],
extras[i],
columnComments[i]
);
result += ` ${columnMigrate}\n`;
}
result += ` });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('${item.name()}');
}
}
`;
SystemService.insertToClipboard(result);
SystemService.notify(
"Laravel export",
item.type() + " " + item.name() + " export statement is copied!"
);
});
}

export { dumpTableAsDefinition, dumpTableAsLaravel };
export { dumpTableAsDefinition, camelize };
Loading