Skip to content
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

Respect 'SchemaDumper.ignore_tables' in databases structure dump #29077

Merged
merged 6 commits into from
May 16, 2017

Conversation

guilleiguaran
Copy link
Member

This includes changes from #29060, fix a broken test and adds support for MySQL and SQLite3 as well.

@guilleiguaran
Copy link
Member Author

Will squash some commits and add CHANGELOG to this after of getting review 🙌

ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
if ignore_tables.any?
tables = `sqlite3 #{dbfile} .tables`.split - ignore_tables
condition = tables.map { |table| "tbl_name = '#{table}'" }.join(" OR ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like it might be more consistent, if nothing else, to use a NOT IN (..) here, instead of doing the subtraction ourselves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also use the proper string-quoting method.

tables = `sqlite3 #{dbfile} .tables`.split - ignore_tables
condition = tables.map { |table| "tbl_name = '#{table}'" }.join(" OR ")
statement = "SELECT sql FROM sqlite_master WHERE #{condition} ORDER BY tbl_name, type DESC, name"
`sqlite3 #{flags} #{dbfile} "#{statement}" > #{filename}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be anyway, but this level of control over the contents of the string makes it even more important for this to be using the array form, instead of the string-construction form, of command invocation.

@@ -64,6 +66,18 @@ def configuration
def root
@root
end

def run_cmd(cmd, args, out)
#fail run_cmd_error(cmd, args) unless Kernel.system(cmd, *args, out: out)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I'm doing something wrong but this fails:

Kernel.system(cmd, *args, out: out)

while this works correctly:

Kernel.system("#{cmd} #{args.join(' ')}", out: out)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess would be the quoting on statement, which isn't required for the splat form.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, that worked 👍

@guilleiguaran
Copy link
Member Author

@matthewd I've done all the suggested changes in SQLite rake task 🙌

Rusty Geldmacher and others added 5 commits May 15, 2017 18:19
When using `sql` as the schema format, or even just doing `rake
db:structure:dump`, it would be good to respect the list of ignored
tables that has been configured.
* Use NOT IN in SQL query
* Quote table names propertly
* Use array form of command invocation
@guilleiguaran guilleiguaran merged commit d7a0d28 into master May 16, 2017
@guilleiguaran guilleiguaran deleted the ignore-tables-in-sql-dump branch May 16, 2017 00:39
kamipo added a commit to kamipo/rails that referenced this pull request Jun 1, 2017
Follow up of rails#29077.

Before:

```sql
SELECT sql FROM sqlite_master WHERE tbl_name NOT IN ("foo") ORDER BY tbl_name, type DESC, name
```

After:

```sql
SELECT sql FROM sqlite_master WHERE tbl_name NOT IN ('foo') ORDER BY tbl_name, type DESC, name
```

> If a keyword in double quotes (ex: "key" or "glob") is used in a
context where it cannot be resolved to an identifier but where a string
literal is allowed, then the token is understood to be a string literal
instead of an identifier.

http://www.sqlite.org/lang_keywords.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants